| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- namespace addons\TinyShop\services\order;
- use Yii;
- use common\enums\PayStatusEnum;
- use common\helpers\ArrayHelper;
- use common\helpers\EchantsHelper;
- use common\components\Service;
- use common\enums\StatusEnum;
- use common\helpers\BcHelper;
- use addons\TinyShop\common\models\order\Order;
- use addons\TinyShop\common\enums\AccessTokenGroupEnum;
- use addons\TinyShop\common\enums\OrderTypeEnum;
- /**
- * Class OrderStatService
- * @package addons\TinyShop\services\order
- * @author jianyan74 <751393839@qq.com>
- */
- class OrderStatService extends Service
- {
- /**
- * 获取每天订单数量、总金额、产品数量
- *
- * @return array|\yii\db\ActiveRecord|null
- */
- public function getDayStatByTime($time)
- {
- return Order::find()
- ->select([
- 'sum(product_count) as product_count',
- 'count(id) as count',
- 'sum(pay_money) as pay_money',
- "from_unixtime(created_at, '%Y-%c-%d') as day"
- ])
- ->where(['pay_status' => PayStatusEnum::YES])
- ->andWhere(['>', 'pay_time', $time])
- ->groupBy(['day'])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->andFilterWhere(['store_id' => Yii::$app->params['store_id']])
- ->asArray()
- ->all();
- }
- /**
- * 获取订单数量、总金额、产品数量
- *
- * @return array|\yii\db\ActiveRecord|null
- */
- public function getStatByTime($time, $select = [])
- {
- $select = ArrayHelper::merge([
- 'sum(product_count) as product_count',
- 'count(id) as count',
- 'sum(pay_money) as pay_money'
- ], $select);
- return Order::find()
- ->select($select)
- ->where(['pay_status' => PayStatusEnum::YES])
- ->andWhere(['>', 'pay_time', $time])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->andFilterWhere(['store_id' => Yii::$app->params['store_id']])
- ->asArray()
- ->one();
- }
- /**
- * @param string $type
- * @param string $count_sql
- * @return array
- */
- public function getBetweenProductMoneyAndCountStatToEchant($type)
- {
- $fields = [
- 'pay_money' => '下单金额',
- ];
- // 获取时间和格式化
- list($time, $format) = EchantsHelper::getFormatTime($type);
- // 获取数据
- return EchantsHelper::lineOrBarInTime(function ($start_time, $end_time, $formatting) {
- return Order::find()
- ->select([
- 'sum(pay_money) as pay_money',
- "from_unixtime(created_at, '$formatting') as time"
- ])
- ->where(['pay_status' => PayStatusEnum::YES])
- ->andWhere(['between', 'pay_time', $start_time, $end_time])
- ->groupBy(['time'])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->andFilterWhere(['store_id' => Yii::$app->params['store_id']])
- ->asArray()
- ->all();
- }, $fields, $time, $format);
- }
- /**
- * @param string $type
- * @param string $count_sql
- * @return array
- */
- public function getBetweenCountStatToEchant($type)
- {
- $fields = [
- 'count' => '订单笔数',
- 'product_count' => '订单量',
- ];
- // 获取时间和格式化
- list($time, $format) = EchantsHelper::getFormatTime($type);
- // 获取数据
- return EchantsHelper::lineOrBarInTime(function ($start_time, $end_time, $formatting) {
- return Order::find()
- ->select([
- 'count(id) as count',
- 'sum(product_count) as product_count',
- "from_unixtime(created_at, '$formatting') as time"
- ])
- ->where(['pay_status' => PayStatusEnum::YES])
- ->andWhere(['between', 'pay_time', $start_time, $end_time])
- ->groupBy(['time'])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->andFilterWhere(['store_id' => Yii::$app->params['store_id']])
- ->asArray()
- ->all();
- }, $fields, $time, $format);
- }
- /**
- * @param string $type
- * @param string $count_sql
- * @return array
- */
- public function getBetweenProductCountAndCountStatToEchant($type)
- {
- $fields = [
- 'product_count' => '商品售出数',
- ];
- // 获取时间和格式化
- list($time, $format) = EchantsHelper::getFormatTime($type);
- // 获取数据
- return EchantsHelper::lineOrBarInTime(function ($start_time, $end_time, $formatting) {
- return Order::find()
- ->select([
- 'sum(product_count) as product_count',
- "from_unixtime(created_at, '$formatting') as time"
- ])
- ->where(['pay_status' => PayStatusEnum::YES])
- ->andWhere(['between', 'pay_time', $start_time, $end_time])
- ->groupBy(['time'])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->asArray()
- ->all();
- }, $fields, $time, $format);
- }
- /**
- * @param string $type
- * @param string $count_sql
- * @return array
- */
- public function getOrderCreateCountStat($type)
- {
- $fields = [
- [
- 'name' => '下单数量',
- 'type' => 'bar',
- 'field' => 'count',
- ],
- [
- 'name' => '支付数量',
- 'type' => 'bar',
- 'field' => 'pay_count',
- ],
- [
- 'name' => '下单支付转化率',
- 'type' => 'line',
- 'field' => 'pay_rate',
- ],
- ];
- // 获取时间和格式化
- list($time, $format) = EchantsHelper::getFormatTime($type);
- // 获取数据
- return EchantsHelper::lineOrBarInTime(function ($start_time, $end_time, $formatting) {
- $data = Order::find()
- ->select([
- 'count(id) as count',
- 'sum(pay_status) as pay_count',
- "from_unixtime(created_at, '$formatting') as time"
- ])
- ->andWhere(['between', 'created_at', $start_time, $end_time])
- ->groupBy(['time'])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->asArray()
- ->all();
- foreach ($data as &$datum) {
- $datum['pay_rate'] = BcHelper::mul(BcHelper::div($datum['pay_count'], $datum['count']), 100);
- }
- return $data;
- }, $fields, $time, $format);
- }
- /**
- * 订单来源统计
- *
- * @return array
- */
- public function getFormStat($type)
- {
- $fields = array_values(AccessTokenGroupEnum::getMap());
- // 获取时间和格式化
- list($time, $format) = EchantsHelper::getFormatTime($type);
- // 获取数据
- return EchantsHelper::pie(function ($start_time, $end_time) use ($fields) {
- $data = Order::find()
- ->select(['count(id) as value', 'order_from'])
- ->where(['status' => StatusEnum::ENABLED])
- ->andFilterWhere(['between', 'created_at', $start_time, $end_time])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->groupBy(['order_from'])
- ->asArray()
- ->all();
- foreach ($data as &$datum) {
- $datum['name'] = AccessTokenGroupEnum::getValue($datum['order_from']);
- }
- return [$data, $fields];
- }, $time);
- }
- /**
- * 订单类型统计
- *
- * @return array
- */
- public function getOrderTypeStat($type)
- {
- $fields = array_values(OrderTypeEnum::getMap());
- // 获取时间和格式化
- list($time, $format) = EchantsHelper::getFormatTime($type);
- // 获取数据
- return EchantsHelper::pie(function ($start_time, $end_time) use ($fields) {
- $data = Order::find()
- ->select(['count(id) as value', 'order_type'])
- ->where(['status' => StatusEnum::ENABLED])
- ->andFilterWhere(['between', 'created_at', $start_time, $end_time])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->groupBy(['order_type'])
- ->asArray()
- ->all();
- foreach ($data as &$datum) {
- $datum['name'] = OrderTypeEnum::getValue($datum['order_type']);
- }
- return [$data, $fields];
- }, $time);
- }
- }
|