OrderStatisticServices.php 8.2 KB

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