OrderStatisticServices.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. $time = explode('-', $where['time']);
  28. if (count($time) != 2) throw new AdminException('请选择时间');
  29. /** @var StoreOrderServices $orderService */
  30. $orderService = app()->make(StoreOrderServices::class);
  31. $data['pay_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'time' => $where['time']], 'pay_price', true);
  32. $data['pay_count'] = $orderService->count(['paid' => 1, 'pid' => 0, 'time' => $where['time']]);
  33. $data['refund_price'] = $orderService->sum(['paid' => 1, 'pid' => 0, 'is_refund' => 1, 'time' => $where['time']], 'pay_price', true);
  34. $data['refund_count'] = $orderService->count(['paid' => 1, 'pid' => 0, 'is_refund' => 1, 'time' => $where['time']]);
  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 = bcadd(bcdiv(bcsub(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)
  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('m-d', $dt_start);
  83. $dt_start = strtotime("+$num day", $dt_start);
  84. $timeType = '%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. $data = $series = [];
  93. foreach ($xAxis as $item) {
  94. $data['订单金额'][] = isset($pay_price[$item]) ? floatval($pay_price[$item]) : 0;
  95. $data['订单量'][] = isset($pay_count[$item]) ? floatval($pay_count[$item]) : 0;
  96. $data['退款金额'][] = isset($refund_price[$item]) ? floatval($refund_price[$item]) : 0;
  97. $data['退款订单量'][] = isset($refund_count[$item]) ? floatval($refund_count[$item]) : 0;
  98. }
  99. foreach ($data as $key => $item) {
  100. $series[] = [
  101. 'name' => $key,
  102. 'data' => $item,
  103. 'type' => 'line',
  104. ];
  105. }
  106. return compact('xAxis', 'series');
  107. }
  108. /**
  109. * 订单来源
  110. * @param $where
  111. * @return array
  112. */
  113. public function getChannel($where)
  114. {
  115. /** @var StoreOrderServices $orderService */
  116. $orderService = app()->make(StoreOrderServices::class);
  117. $bing_xdata = ['公众号', '小程序', 'H5', 'PC', 'APP'];
  118. $color = ['#64a1f4', '#3edeb5', '#70869f', '#ffc653', '#fc7d6a'];
  119. $bing_data = [];
  120. foreach ($bing_xdata as $key => $item) {
  121. $bing_data[] = [
  122. 'name' => $item,
  123. 'value' => $orderService->count(['paid' => 1, 'pid' => 0, 'is_channel' => $key, 'time' => $where['time']]),
  124. 'itemStyle' => ['color' => $color[$key]]
  125. ];
  126. }
  127. $list = [];
  128. $count = array_sum(array_column($bing_data, 'value'));
  129. foreach ($bing_data as $key => $item) {
  130. $list[] = [
  131. 'name' => $item['name'],
  132. 'value' => $item['value'],
  133. 'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 2) : 0,
  134. ];
  135. }
  136. array_multisort(array_column($list, 'value'), SORT_DESC, $list);
  137. return compact('bing_xdata', 'bing_data', 'list');
  138. }
  139. /**
  140. * 订单类型
  141. * @param $where
  142. * @return array
  143. */
  144. public function getType($where)
  145. {
  146. /** @var StoreOrderServices $orderService */
  147. $orderService = app()->make(StoreOrderServices::class);
  148. $bing_xdata = ['普通订单', '秒杀订单', '砍价订单', '拼团订单', '预售订单'];
  149. $color = ['#64a1f4', '#3edeb5', '#70869f', '#ffc653', '#fc7d6a'];
  150. $bing_data = [];
  151. foreach ($bing_xdata as $key => $item) {
  152. $bing_data[] = [
  153. 'name' => $item,
  154. 'value' => $orderService->together(['paid' => 1, 'pid' => 0, 'activity_type' => $key, 'time' => $where['time']], 'pay_price', 'sum'),
  155. 'itemStyle' => ['color' => $color[$key]]
  156. ];
  157. }
  158. $list = [];
  159. $count = array_sum(array_column($bing_data, 'value'));
  160. foreach ($bing_data as $key => $item) {
  161. $list[] = [
  162. 'name' => $item['name'],
  163. 'value' => $item['value'],
  164. 'percent' => $count != 0 ? bcmul((string)bcdiv((string)$item['value'], (string)$count, 4), '100', 2) : 0,
  165. ];
  166. }
  167. array_multisort(array_column($list, 'value'), SORT_DESC, $list);
  168. return compact('bing_xdata', 'bing_data', 'list');
  169. }
  170. }