OrderStatService.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace addons\TinyShop\services\order;
  3. use Yii;
  4. use common\enums\PayStatusEnum;
  5. use common\helpers\ArrayHelper;
  6. use common\helpers\EchantsHelper;
  7. use common\components\Service;
  8. use common\enums\StatusEnum;
  9. use common\helpers\BcHelper;
  10. use addons\TinyShop\common\models\order\Order;
  11. use addons\TinyShop\common\enums\AccessTokenGroupEnum;
  12. use addons\TinyShop\common\enums\OrderTypeEnum;
  13. /**
  14. * Class OrderStatService
  15. * @package addons\TinyShop\services\order
  16. * @author jianyan74 <751393839@qq.com>
  17. */
  18. class OrderStatService extends Service
  19. {
  20. /**
  21. * 获取每天订单数量、总金额、产品数量
  22. *
  23. * @return array|\yii\db\ActiveRecord|null
  24. */
  25. public function getDayStatByTime($time)
  26. {
  27. return Order::find()
  28. ->select([
  29. 'sum(product_count) as product_count',
  30. 'count(id) as count',
  31. 'sum(pay_money) as pay_money',
  32. "from_unixtime(created_at, '%Y-%c-%d') as day"
  33. ])
  34. ->where(['pay_status' => PayStatusEnum::YES])
  35. ->andWhere(['>', 'pay_time', $time])
  36. ->groupBy(['day'])
  37. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  38. ->andFilterWhere(['store_id' => Yii::$app->params['store_id']])
  39. ->asArray()
  40. ->all();
  41. }
  42. /**
  43. * 获取订单数量、总金额、产品数量
  44. *
  45. * @return array|\yii\db\ActiveRecord|null
  46. */
  47. public function getStatByTime($time, $select = [])
  48. {
  49. $select = ArrayHelper::merge([
  50. 'sum(product_count) as product_count',
  51. 'count(id) as count',
  52. 'sum(pay_money) as pay_money'
  53. ], $select);
  54. return Order::find()
  55. ->select($select)
  56. ->where(['pay_status' => PayStatusEnum::YES])
  57. ->andWhere(['>', 'pay_time', $time])
  58. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  59. ->andFilterWhere(['store_id' => Yii::$app->params['store_id']])
  60. ->asArray()
  61. ->one();
  62. }
  63. /**
  64. * @param string $type
  65. * @param string $count_sql
  66. * @return array
  67. */
  68. public function getBetweenProductMoneyAndCountStatToEchant($type)
  69. {
  70. $fields = [
  71. 'pay_money' => '下单金额',
  72. ];
  73. // 获取时间和格式化
  74. list($time, $format) = EchantsHelper::getFormatTime($type);
  75. // 获取数据
  76. return EchantsHelper::lineOrBarInTime(function ($start_time, $end_time, $formatting) {
  77. return Order::find()
  78. ->select([
  79. 'sum(pay_money) as pay_money',
  80. "from_unixtime(created_at, '$formatting') as time"
  81. ])
  82. ->where(['pay_status' => PayStatusEnum::YES])
  83. ->andWhere(['between', 'pay_time', $start_time, $end_time])
  84. ->groupBy(['time'])
  85. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  86. ->andFilterWhere(['store_id' => Yii::$app->params['store_id']])
  87. ->asArray()
  88. ->all();
  89. }, $fields, $time, $format);
  90. }
  91. /**
  92. * @param string $type
  93. * @param string $count_sql
  94. * @return array
  95. */
  96. public function getBetweenCountStatToEchant($type)
  97. {
  98. $fields = [
  99. 'count' => '订单笔数',
  100. 'product_count' => '订单量',
  101. ];
  102. // 获取时间和格式化
  103. list($time, $format) = EchantsHelper::getFormatTime($type);
  104. // 获取数据
  105. return EchantsHelper::lineOrBarInTime(function ($start_time, $end_time, $formatting) {
  106. return Order::find()
  107. ->select([
  108. 'count(id) as count',
  109. 'sum(product_count) as product_count',
  110. "from_unixtime(created_at, '$formatting') as time"
  111. ])
  112. ->where(['pay_status' => PayStatusEnum::YES])
  113. ->andWhere(['between', 'pay_time', $start_time, $end_time])
  114. ->groupBy(['time'])
  115. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  116. ->andFilterWhere(['store_id' => Yii::$app->params['store_id']])
  117. ->asArray()
  118. ->all();
  119. }, $fields, $time, $format);
  120. }
  121. /**
  122. * @param string $type
  123. * @param string $count_sql
  124. * @return array
  125. */
  126. public function getBetweenProductCountAndCountStatToEchant($type)
  127. {
  128. $fields = [
  129. 'product_count' => '商品售出数',
  130. ];
  131. // 获取时间和格式化
  132. list($time, $format) = EchantsHelper::getFormatTime($type);
  133. // 获取数据
  134. return EchantsHelper::lineOrBarInTime(function ($start_time, $end_time, $formatting) {
  135. return Order::find()
  136. ->select([
  137. 'sum(product_count) as product_count',
  138. "from_unixtime(created_at, '$formatting') as time"
  139. ])
  140. ->where(['pay_status' => PayStatusEnum::YES])
  141. ->andWhere(['between', 'pay_time', $start_time, $end_time])
  142. ->groupBy(['time'])
  143. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  144. ->asArray()
  145. ->all();
  146. }, $fields, $time, $format);
  147. }
  148. /**
  149. * @param string $type
  150. * @param string $count_sql
  151. * @return array
  152. */
  153. public function getOrderCreateCountStat($type)
  154. {
  155. $fields = [
  156. [
  157. 'name' => '下单数量',
  158. 'type' => 'bar',
  159. 'field' => 'count',
  160. ],
  161. [
  162. 'name' => '支付数量',
  163. 'type' => 'bar',
  164. 'field' => 'pay_count',
  165. ],
  166. [
  167. 'name' => '下单支付转化率',
  168. 'type' => 'line',
  169. 'field' => 'pay_rate',
  170. ],
  171. ];
  172. // 获取时间和格式化
  173. list($time, $format) = EchantsHelper::getFormatTime($type);
  174. // 获取数据
  175. return EchantsHelper::lineOrBarInTime(function ($start_time, $end_time, $formatting) {
  176. $data = Order::find()
  177. ->select([
  178. 'count(id) as count',
  179. 'sum(pay_status) as pay_count',
  180. "from_unixtime(created_at, '$formatting') as time"
  181. ])
  182. ->andWhere(['between', 'created_at', $start_time, $end_time])
  183. ->groupBy(['time'])
  184. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  185. ->asArray()
  186. ->all();
  187. foreach ($data as &$datum) {
  188. $datum['pay_rate'] = BcHelper::mul(BcHelper::div($datum['pay_count'], $datum['count']), 100);
  189. }
  190. return $data;
  191. }, $fields, $time, $format);
  192. }
  193. /**
  194. * 订单来源统计
  195. *
  196. * @return array
  197. */
  198. public function getFormStat($type)
  199. {
  200. $fields = array_values(AccessTokenGroupEnum::getMap());
  201. // 获取时间和格式化
  202. list($time, $format) = EchantsHelper::getFormatTime($type);
  203. // 获取数据
  204. return EchantsHelper::pie(function ($start_time, $end_time) use ($fields) {
  205. $data = Order::find()
  206. ->select(['count(id) as value', 'order_from'])
  207. ->where(['status' => StatusEnum::ENABLED])
  208. ->andFilterWhere(['between', 'created_at', $start_time, $end_time])
  209. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  210. ->groupBy(['order_from'])
  211. ->asArray()
  212. ->all();
  213. foreach ($data as &$datum) {
  214. $datum['name'] = AccessTokenGroupEnum::getValue($datum['order_from']);
  215. }
  216. return [$data, $fields];
  217. }, $time);
  218. }
  219. /**
  220. * 订单类型统计
  221. *
  222. * @return array
  223. */
  224. public function getOrderTypeStat($type)
  225. {
  226. $fields = array_values(OrderTypeEnum::getMap());
  227. // 获取时间和格式化
  228. list($time, $format) = EchantsHelper::getFormatTime($type);
  229. // 获取数据
  230. return EchantsHelper::pie(function ($start_time, $end_time) use ($fields) {
  231. $data = Order::find()
  232. ->select(['count(id) as value', 'order_type'])
  233. ->where(['status' => StatusEnum::ENABLED])
  234. ->andFilterWhere(['between', 'created_at', $start_time, $end_time])
  235. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  236. ->groupBy(['order_type'])
  237. ->asArray()
  238. ->all();
  239. foreach ($data as &$datum) {
  240. $datum['name'] = OrderTypeEnum::getValue($datum['order_type']);
  241. }
  242. return [$data, $fields];
  243. }, $time);
  244. }
  245. }
粤ICP备19079148号