OrderStatisticServices.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\statistic;
  12. use app\services\BaseServices;
  13. use app\services\order\StoreCartServices;
  14. use app\services\order\StoreOrderServices;
  15. use app\services\product\product\StoreVisitServices;
  16. use app\services\user\UserBillServices;
  17. use crmeb\exceptions\AdminException;
  18. class OrderStatisticServices extends BaseServices
  19. {
  20. /**
  21. * 订单统计基础
  22. * @param $where
  23. * @return array
  24. */
  25. public function getBasic($where)
  26. {
  27. /** @var StoreOrderServices $orderService */
  28. $orderService = app()->make(StoreOrderServices::class);
  29. $data['pay_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'time' => $where['time']], 'pay_price', true);
  30. $data['ali_pay_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'time' => $where['time'], 'pay_type' => 'alipay'], 'pay_price', true);
  31. $data['wechat_pay_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'time' => $where['time'], 'pay_type' => 'weixin'], 'pay_price', true);
  32. $data['yue_pay_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'time' => $where['time'], 'pay_type' => 'yue'], 'pay_price', true);
  33. $data['integral_pay_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'time' => $where['time']], 'use_integral', true);
  34. $data['offline_pay_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'time' => $where['time'], 'pay_type' => 'offline'], 'pay_price', true);
  35. $data['award_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'time' => $where['time']], 'award_price', true);
  36. $data['pay_count'] = $orderService->count(['paid' => 1, 'pid' => 0, 'time' => $where['time']]);
  37. $data['refund_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'is_refund' => 1, 'time' => $where['time']], 'pay_price', true);
  38. $data['refund_count'] = $orderService->count(['paid' => 1, 'pid' => 0, 'is_refund' => 1, 'time' => $where['time']]);
  39. $data['coupon_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'time' => $where['time']], 'coupon_price', true);
  40. $data['coupon_count'] = $orderService->search(['paid' => 1, 'pid' => 0, 'time' => $where['time'], 'is_coupon' => 1])->count();
  41. return $data;
  42. }
  43. /**
  44. * 订单趋势
  45. * @param $where
  46. * @return array
  47. */
  48. public function getTrend($where)
  49. {
  50. $time = explode('-', $where['time']);
  51. if (count($time) != 2) throw new AdminException('参数错误');
  52. $dayCount = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  53. $data = [];
  54. if ($dayCount == 1) {
  55. $data = $this->trend($time, 0);
  56. } elseif ($dayCount > 1 && $dayCount <= 31) {
  57. $data = $this->trend($time, 1);
  58. } elseif ($dayCount > 31 && $dayCount <= 92) {
  59. $data = $this->trend($time, 3);
  60. } elseif ($dayCount > 92) {
  61. $data = $this->trend($time, 30);
  62. }
  63. return $data;
  64. }
  65. /**
  66. * 订单趋势
  67. * @param $time
  68. * @param $num
  69. * @param false $excel
  70. * @return array
  71. */
  72. public function trend($time, $num, $excel = false)
  73. {
  74. /** @var StoreOrderServices $storeOrder */
  75. $storeOrder = app()->make(StoreOrderServices::class);
  76. if ($num == 0) {
  77. $xAxis = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23'];
  78. $timeType = '%H';
  79. } elseif ($num != 0) {
  80. $dt_start = strtotime($time[0]);
  81. $dt_end = strtotime($time[1]);
  82. while ($dt_start <= $dt_end) {
  83. if ($num == 30) {
  84. $xAxis[] = date('Y-m', $dt_start);
  85. $dt_start = strtotime("+1 month", $dt_start);
  86. $timeType = '%Y-%m';
  87. } else {
  88. $xAxis[] = date('Y-m-d', $dt_start);
  89. $dt_start = strtotime("+$num day", $dt_start);
  90. $timeType = '%Y-%m-%d';
  91. }
  92. }
  93. }
  94. $pay_price = array_column($storeOrder->getProductTrend($time, $timeType, 'add_time', 'sum(pay_price)', 'pay'), 'num', 'days');
  95. $pay_count = array_column($storeOrder->getProductTrend($time, $timeType, 'add_time', 'count(id)', 'pay'), 'num', 'days');
  96. $refund_price = array_column($storeOrder->getProductTrend($time, $timeType, 'add_time', 'sum(pay_price)', 'refund'), 'num', 'days');
  97. $refund_count = array_column($storeOrder->getProductTrend($time, $timeType, 'add_time', 'count(id)', 'refund'), 'num', 'days');
  98. $coupon_price = array_column($storeOrder->getProductTrend($time, $timeType, 'add_time', 'sum(coupon_price)', 'coupon'), 'num', 'days');
  99. $coupon_count = array_column($storeOrder->getProductTrend($time, $timeType, 'add_time', 'count(id)', 'coupon'), 'num', 'days');
  100. $data = $series = [];
  101. foreach ($xAxis as $item) {
  102. $data['订单金额'][] = isset($pay_price[$item]) ? floatval($pay_price[$item]) : 0;
  103. $data['订单量'][] = isset($pay_count[$item]) ? floatval($pay_count[$item]) : 0;
  104. $data['退款金额'][] = isset($refund_price[$item]) ? floatval($refund_price[$item]) : 0;
  105. $data['退款订单量'][] = isset($refund_count[$item]) ? floatval($refund_count[$item]) : 0;
  106. $data['用券金额'][] = isset($coupon_price[$item]) ? floatval($coupon_price[$item]) : 0;
  107. $data['用券数量'][] = isset($coupon_count[$item]) ? floatval($coupon_count[$item]) : 0;
  108. }
  109. foreach ($data as $key => $item) {
  110. $series[] = [
  111. 'name' => $key,
  112. 'data' => $item,
  113. 'type' => 'line',
  114. ];
  115. }
  116. return compact('xAxis', 'series');
  117. }
  118. /**
  119. * 订单来源
  120. * @param $where
  121. * @return array
  122. */
  123. public function getChannel($where)
  124. {
  125. /** @var StoreOrderServices $orderService */
  126. $orderService = app()->make(StoreOrderServices::class);
  127. $bing_xdata = ['公众号', '小程序', 'H5', 'PC', 'APP'];
  128. $color = ['#64a1f4', '#3edeb5', '#70869f', '#ffc653', '#fc7d6a'];
  129. $bing_data = [];
  130. foreach ($bing_xdata as $key => $item) {
  131. $bing_data[] = [
  132. 'name' => $item,
  133. 'value' => $orderService->count(['paid' => 1, 'pid' => 0, 'is_channel' => $key, 'time' => $where['time']]),
  134. 'itemStyle' => ['color' => $color[$key]]
  135. ];
  136. }
  137. $list = [];
  138. $count = array_sum(array_column($bing_data, 'value'));
  139. foreach ($bing_data as $key => $item) {
  140. $list[] = [
  141. 'name' => $item['name'],
  142. 'value' => $item['value'],
  143. 'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 2) : 0,
  144. ];
  145. }
  146. array_multisort(array_column($list, 'value'), SORT_DESC, $list);
  147. return compact('bing_xdata', 'bing_data', 'list');
  148. }
  149. /**
  150. * 订单类型
  151. * @param $where
  152. * @return array
  153. */
  154. public function getType($where)
  155. {
  156. /** @var StoreOrderServices $orderService */
  157. $orderService = app()->make(StoreOrderServices::class);
  158. $bing_xdata = ['普通订单', '秒杀订单', '砍价订单', '拼团订单', '套餐订单', '预售订单', '新人订单', '抽奖订单', '拼单订单', '桌码订单'];
  159. $color = ['#64a1f4', '#3edeb5', '#70869f', '#ffc653', '#fc7d6a', '#b37feb', '#ff85c0', '#6dd230', '#e8ea48', '#ef3737'];
  160. $bing_data = [];
  161. foreach ($bing_xdata as $key => $item) {
  162. $keys = $key;
  163. if ($key > 4) $keys = $key + 3;
  164. $bing_data[] = [
  165. 'name' => $item,
  166. 'value' => $orderService->together(['paid' => 1, 'pid' => 0, 'type' => $keys, 'time' => $where['time']], 'pay_price', 'sum'),
  167. 'itemStyle' => ['color' => $color[$key]]
  168. ];
  169. }
  170. $list = [];
  171. $count = array_sum(array_column($bing_data, 'value'));
  172. foreach ($bing_data as $key => $item) {
  173. $list[] = [
  174. 'name' => $item['name'],
  175. 'value' => $item['value'],
  176. 'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 2) : 0,
  177. ];
  178. }
  179. array_multisort(array_column($list, 'value'), SORT_DESC, $list);
  180. return compact('bing_xdata', 'bing_data', 'list');
  181. }
  182. }