OrderStatisticServices.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. return $data;
  34. }
  35. /**
  36. * 订单趋势
  37. * @param $where
  38. * @return array
  39. */
  40. public function getTrend($where)
  41. {
  42. $time = explode('-', $where['time']);
  43. if (count($time) != 2) throw new AdminException(100100);
  44. $dayCount = (strtotime($time[1]) - strtotime($time[0])) / 86400 + 1;
  45. $data = [];
  46. if ($dayCount == 1) {
  47. $data = $this->trend($time, 0);
  48. } elseif ($dayCount > 1 && $dayCount <= 31) {
  49. $data = $this->trend($time, 1);
  50. } elseif ($dayCount > 31 && $dayCount <= 92) {
  51. $data = $this->trend($time, 3);
  52. } elseif ($dayCount > 92) {
  53. $data = $this->trend($time, 30);
  54. }
  55. return $data;
  56. }
  57. /**
  58. * 订单趋势
  59. * @param $time
  60. * @param $num
  61. * @param false $excel
  62. * @return array
  63. */
  64. public function trend($time, $num, $excel = false)
  65. {
  66. /** @var StoreVisitServices $storeVisit */
  67. $storeVisit = app()->make(StoreVisitServices::class);
  68. /** @var StoreOrderServices $storeOrder */
  69. $storeOrder = app()->make(StoreOrderServices::class);
  70. /** @var StoreCartServices $storeCart */
  71. $storeCart = app()->make(StoreCartServices::class);
  72. /** @var UserBillServices $userBillServices */
  73. $userBillServices = app()->make(UserBillServices::class);
  74. if ($num == 0) {
  75. $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'];
  76. $timeType = '%H';
  77. } elseif ($num != 0) {
  78. $dt_start = strtotime($time[0]);
  79. $dt_end = strtotime($time[1]);
  80. while ($dt_start <= $dt_end) {
  81. if ($num == 30) {
  82. $xAxis[] = date('Y-m', $dt_start);
  83. $dt_start = strtotime("+1 month", $dt_start);
  84. $timeType = '%Y-%m';
  85. } else {
  86. $xAxis[] = date('m-d', $dt_start);
  87. $dt_start = strtotime("+$num day", $dt_start);
  88. $timeType = '%m-%d';
  89. }
  90. }
  91. }
  92. $pay_price = array_column($storeOrder->getProductTrend($time, $timeType, 'add_time', 'sum(pay_price)', 'pay'), 'num', 'days');
  93. $pay_count = array_column($storeOrder->getProductTrend($time, $timeType, 'add_time', 'count(id)', 'pay'), 'num', 'days');
  94. $refund_price = array_column($storeOrder->getProductTrend($time, $timeType, 'add_time', 'sum(pay_price)', 'refund'), 'num', 'days');
  95. $refund_count = array_column($storeOrder->getProductTrend($time, $timeType, 'add_time', 'count(id)', 'refund'), 'num', 'days');
  96. $data = $series = [];
  97. foreach ($xAxis as $item) {
  98. $data['订单金额'][] = isset($pay_price[$item]) ? floatval($pay_price[$item]) : 0;
  99. $data['订单量'][] = isset($pay_count[$item]) ? floatval($pay_count[$item]) : 0;
  100. $data['退款金额'][] = isset($refund_price[$item]) ? floatval($refund_price[$item]) : 0;
  101. $data['退款订单量'][] = isset($refund_count[$item]) ? floatval($refund_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'];
  154. $bing_data = [];
  155. foreach ($bing_xdata as $key => $item) {
  156. $bing_data[] = [
  157. 'name' => $item,
  158. 'value' => $orderService->together(['paid' => 1, 'pid' => 0, 'activity_type' => $key, 'time' => $where['time']], 'pay_price', 'sum'),
  159. 'itemStyle' => ['color' => $color[$key]]
  160. ];
  161. }
  162. $list = [];
  163. $count = array_sum(array_column($bing_data, 'value'));
  164. foreach ($bing_data as $key => $item) {
  165. $list[] = [
  166. 'name' => $item['name'],
  167. 'value' => $item['value'],
  168. 'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 2) : 0,
  169. ];
  170. }
  171. array_multisort(array_column($list, 'value'), SORT_DESC, $list);
  172. return compact('bing_xdata', 'bing_data', 'list');
  173. }
  174. }