OrderStatisticServices.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. $area = [];
  30. if (!empty($where['order_province'])) $area['order_province'] = $where['order_province'];
  31. if (!empty($where['order_city'])) $area['order_city'] = $where['order_city'];
  32. if (!empty($where['order_district'])) $area['order_district'] = $where['order_district'];
  33. $data['pay_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'time' => $where['time'], 'area' => $area], 'pay_price', true);
  34. $data['ali_pay_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'time' => $where['time'], 'pay_type' => 'alipay', 'area' => $area], 'pay_price', true);
  35. $data['wechat_pay_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'time' => $where['time'], 'pay_type' => 'weixin', 'area' => $area], 'pay_price', true);
  36. $data['yue_pay_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'time' => $where['time'], 'pay_type' => 'yue', 'area' => $area], 'pay_price', true);
  37. $data['integral_pay_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'time' => $where['time'], 'area' => $area], 'use_integral', true);
  38. $data['offline_pay_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'time' => $where['time'], 'pay_type' => 'offline', 'area' => $area], 'pay_price', true);
  39. $data['award_price'] = $orderService->sum(['paid' => 1, 'pid' => [0, -1], 'time' => $where['time'], 'area' => $area], 'award_price', true);
  40. $data['pay_count'] = $orderService->count(['paid' => 1, 'pid' => 0, 'time' => $where['time'], 'area' => $area]);
  41. $data['refund_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'is_refund' => 1, 'time' => $where['time'], 'area' => $area], 'pay_price', true);
  42. $data['refund_count'] = $orderService->count(['paid' => 1, 'pid' => 0, 'is_refund' => 1, 'time' => $where['time'], 'area' => $area]);
  43. $data['coupon_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'time' => $where['time'], 'area' => $area], 'coupon_price', true);
  44. $data['coupon_count'] = $orderService->search(['paid' => 1, 'pid' => 0, 'time' => $where['time'], 'is_coupon' => 1, 'area' => $area])->count();
  45. return $data;
  46. }
  47. /**
  48. * 订单趋势
  49. * @param $where
  50. * @return array
  51. */
  52. public function getTrend($where)
  53. {
  54. $area = [];
  55. if (!empty($where['order_province'])) $area['order_province'] = $where['order_province'];
  56. if (!empty($where['order_city'])) $area['order_city'] = $where['order_city'];
  57. if (!empty($where['order_district'])) $area['order_district'] = $where['order_district'];
  58. $time = explode('-', $where['time']);
  59. if (count($time) != 2) throw new AdminException('参数错误');
  60. $dayCount = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  61. $data = [];
  62. if ($dayCount == 1) {
  63. $data = $this->trend($time, $area, 0);
  64. } elseif ($dayCount > 1 && $dayCount <= 31) {
  65. $data = $this->trend($time, $area, 1);
  66. } elseif ($dayCount > 31 && $dayCount <= 92) {
  67. $data = $this->trend($time, $area, 3);
  68. } elseif ($dayCount > 92) {
  69. $data = $this->trend($time, $area, 30);
  70. }
  71. return $data;
  72. }
  73. /**
  74. * 订单趋势
  75. * @param $time
  76. * @param $num
  77. * @param false $excel
  78. * @return array
  79. */
  80. public function trend($time, $area, $num, $excel = false)
  81. {
  82. /** @var StoreOrderServices $storeOrder */
  83. $storeOrder = app()->make(StoreOrderServices::class);
  84. if ($num == 0) {
  85. $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'];
  86. $timeType = '%H';
  87. } elseif ($num != 0) {
  88. $dt_start = strtotime($time[0]);
  89. $dt_end = strtotime($time[1]);
  90. while ($dt_start <= $dt_end) {
  91. if ($num == 30) {
  92. $xAxis[] = date('Y-m', $dt_start);
  93. $dt_start = strtotime("+1 month", $dt_start);
  94. $timeType = '%Y-%m';
  95. } else {
  96. $xAxis[] = date('Y-m-d', $dt_start);
  97. $dt_start = strtotime("+$num day", $dt_start);
  98. $timeType = '%Y-%m-%d';
  99. }
  100. }
  101. }
  102. $pay_price = array_column($storeOrder->getProductTrend($time, $timeType, 'add_time', 'sum(pay_price)', 'pay', $area), 'num', 'days');
  103. $pay_count = array_column($storeOrder->getProductTrend($time, $timeType, 'add_time', 'count(id)', 'pay', $area), 'num', 'days');
  104. $refund_price = array_column($storeOrder->getProductTrend($time, $timeType, 'add_time', 'sum(pay_price)', 'refund', $area), 'num', 'days');
  105. $refund_count = array_column($storeOrder->getProductTrend($time, $timeType, 'add_time', 'count(id)', 'refund', $area), 'num', 'days');
  106. $coupon_price = array_column($storeOrder->getProductTrend($time, $timeType, 'add_time', 'sum(coupon_price)', 'coupon', $area), 'num', 'days');
  107. $coupon_count = array_column($storeOrder->getProductTrend($time, $timeType, 'add_time', 'count(id)', 'coupon', $area), 'num', 'days');
  108. $data = $series = [];
  109. foreach ($xAxis as $item) {
  110. $data['订单金额'][] = isset($pay_price[$item]) ? floatval($pay_price[$item]) : 0;
  111. $data['订单量'][] = isset($pay_count[$item]) ? floatval($pay_count[$item]) : 0;
  112. $data['退款金额'][] = isset($refund_price[$item]) ? floatval($refund_price[$item]) : 0;
  113. $data['退款订单量'][] = isset($refund_count[$item]) ? floatval($refund_count[$item]) : 0;
  114. $data['用券金额'][] = isset($coupon_price[$item]) ? floatval($coupon_price[$item]) : 0;
  115. $data['用券数量'][] = isset($coupon_count[$item]) ? floatval($coupon_count[$item]) : 0;
  116. }
  117. foreach ($data as $key => $item) {
  118. $series[] = [
  119. 'name' => $key,
  120. 'data' => $item,
  121. 'type' => 'line',
  122. ];
  123. }
  124. return compact('xAxis', 'series');
  125. }
  126. /**
  127. * 订单来源
  128. * @param $where
  129. * @return array
  130. */
  131. public function getChannel($where)
  132. {
  133. $area = [];
  134. if (!empty($where['order_province'])) $area['order_province'] = $where['order_province'];
  135. if (!empty($where['order_city'])) $area['order_city'] = $where['order_city'];
  136. if (!empty($where['order_district'])) $area['order_district'] = $where['order_district'];
  137. /** @var StoreOrderServices $orderService */
  138. $orderService = app()->make(StoreOrderServices::class);
  139. $bing_xdata = ['公众号', '小程序', 'H5', 'PC', 'APP'];
  140. $color = ['#64a1f4', '#3edeb5', '#70869f', '#ffc653', '#fc7d6a'];
  141. $bing_data = [];
  142. foreach ($bing_xdata as $key => $item) {
  143. $bing_data[] = [
  144. 'name' => $item,
  145. 'value' => $orderService->count(['paid' => 1, 'pid' => 0, 'is_channel' => $key, 'time' => $where['time'], 'area' => $area]),
  146. 'itemStyle' => ['color' => $color[$key]]
  147. ];
  148. }
  149. $list = [];
  150. $count = array_sum(array_column($bing_data, 'value'));
  151. foreach ($bing_data as $key => $item) {
  152. $list[] = [
  153. 'name' => $item['name'],
  154. 'value' => $item['value'],
  155. 'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 2) : 0,
  156. ];
  157. }
  158. array_multisort(array_column($list, 'value'), SORT_DESC, $list);
  159. return compact('bing_xdata', 'bing_data', 'list');
  160. }
  161. /**
  162. * 订单类型
  163. * @param $where
  164. * @return array
  165. */
  166. public function getType($where)
  167. {
  168. /** @var StoreOrderServices $orderService */
  169. $orderService = app()->make(StoreOrderServices::class);
  170. $area = [];
  171. if (!empty($where['order_province'])) $area['order_province'] = $where['order_province'];
  172. if (!empty($where['order_city'])) $area['order_city'] = $where['order_city'];
  173. if (!empty($where['order_district'])) $area['order_district'] = $where['order_district'];
  174. $bing_xdata = ['普通订单', '秒杀订单', '砍价订单', '拼团订单', '套餐订单', '预售订单', '新人订单', '抽奖订单', '拼单订单', '桌码订单'];
  175. $color = ['#64a1f4', '#3edeb5', '#70869f', '#ffc653', '#fc7d6a', '#b37feb', '#ff85c0', '#6dd230', '#e8ea48', '#ef3737'];
  176. $bing_data = [];
  177. foreach ($bing_xdata as $key => $item) {
  178. $keys = $key;
  179. if ($key > 4) $keys = $key + 3;
  180. $bing_data[] = [
  181. 'name' => $item,
  182. 'value' => $orderService->together(['paid' => 1, 'pid' => 0, 'type' => $keys, 'time' => $where['time'], 'area' => $area], 'pay_price', 'sum'),
  183. 'itemStyle' => ['color' => $color[$key]]
  184. ];
  185. }
  186. $list = [];
  187. $count = array_sum(array_column($bing_data, 'value'));
  188. foreach ($bing_data as $key => $item) {
  189. $list[] = [
  190. 'name' => $item['name'],
  191. 'value' => $item['value'],
  192. 'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 2) : 0,
  193. ];
  194. }
  195. array_multisort(array_column($list, 'value'), SORT_DESC, $list);
  196. return compact('bing_xdata', 'bing_data', 'list');
  197. }
  198. }