Dashboard.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace app\admin\controller\shopro;
  3. use app\admin\model\shopro\order\Order;
  4. use app\admin\model\shopro\order\OrderItem;
  5. use app\admin\model\shopro\goods\Goods;
  6. use app\admin\model\shopro\user\User;
  7. use app\admin\model\shopro\Share;
  8. use addons\shopro\service\SearchHistory;
  9. class Dashboard extends Common
  10. {
  11. public function index()
  12. {
  13. $cardList = [
  14. [
  15. 'name' => 'total',
  16. 'card' => 'shopro/dashboard/total',
  17. 'status' => $this->auth->check('shopro/dashboard/total')
  18. ],
  19. [
  20. 'name' => 'chart',
  21. 'card' => 'shopro/dashboard/chart',
  22. 'status' => $this->auth->check('shopro/dashboard/chart')
  23. ],
  24. [
  25. 'name' => 'ranking',
  26. 'card' => 'shopro/dashboard/ranking',
  27. 'status' => $this->auth->check('shopro/dashboard/ranking')
  28. ],
  29. ];
  30. $this->assignconfig('cardList', $cardList);
  31. return $this->view->fetch();
  32. }
  33. public function total()
  34. {
  35. // 用户数据
  36. $userData = [
  37. 'total' => User::count(),
  38. 'today' => User::whereTime('createtime', 'today')->count(),
  39. 'week' => User::whereTime('createtime', 'week')->count(),
  40. 'array' => collection(User::field('id,createtime')->whereTime('createtime', 'today')->select())->each(function ($user) {
  41. $user->counter = 1;
  42. $user->createtime_unix = $user->getData('createtime') * 1000;
  43. })
  44. ];
  45. // -- commission code start --
  46. $agentData = [
  47. 'total' => \app\admin\model\shopro\commission\Agent::count(),
  48. 'today' => \app\admin\model\shopro\commission\Agent::whereTime('createtime', 'today')->count(),
  49. 'week' => \app\admin\model\shopro\commission\Agent::whereTime('createtime', 'week')->count(),
  50. 'array' => collection(\app\admin\model\shopro\commission\Agent::field('user_id,createtime')->whereTime('createtime', 'today')->select())->each(function ($agent) {
  51. $agent->counter = 1;
  52. $agent->createtime_unix = $agent->getData('createtime') * 1000;
  53. })
  54. ];
  55. // -- commission code end --
  56. // 分享数据
  57. $shareData = [
  58. 'total' => Share::count(),
  59. 'today' => Share::whereTime('createtime', 'today')->count(),
  60. 'week' => Share::whereTime('createtime', 'week')->count(),
  61. 'array' => collection(Share::field('id,createtime')->whereTime('createtime', 'today')->select())->each(function ($share) {
  62. $share->counter = 1;
  63. $share->createtime_unix = $share->getData('createtime') * 1000;
  64. })
  65. ];
  66. $this->success('获取成功', null, [
  67. 'user_data' => $userData,
  68. 'agent_data' => $agentData ?? null,
  69. 'share_data' => $shareData
  70. ]);
  71. }
  72. public function chart()
  73. {
  74. $date = $this->request->param('date', '');
  75. $date = array_values(array_filter(explode(' - ', $date)));
  76. $orders = Order::with(['items'])->whereTime('createtime', 'between', $date)
  77. ->order('id', 'asc')->select();
  78. // 订单数
  79. $data['orderNum'] = count($orders);
  80. $data['orderArr'] = [];
  81. // 支付订单(包含退款的订单, 不包含货到付款还未收货(未支付)订单)
  82. $data['payOrderNum'] = 0;
  83. $data['payOrderArr'] = [];
  84. //支付金额(包含退款的订单, 不包含货到付款还未收货(未支付)订单)
  85. $data['payAmountNum'] = 0;
  86. $data['payAmountArr'] = [];
  87. // 支付用户(一个人不管下多少单,都算一个,包含退款的订单, 不包含货到付款还未收货(未支付)订单)
  88. $userIds = [];
  89. $data['payUserNum'] = 0;
  90. $data['payUserArr'] = [];
  91. // 代发货(包含货到付款)
  92. $data['noSendNum'] = 0;
  93. $data['noSendArr'] = [];
  94. //售后维权
  95. $data['aftersaleNum'] = 0;
  96. $data['aftersaleArr'] = [];
  97. //退款订单
  98. $data['refundNum'] = 0;
  99. $data['refundArr'] = [];
  100. foreach ($orders as $key => $order) {
  101. $data['orderArr'][] = [
  102. 'counter' => 1,
  103. 'createtime' => $order->getData('createtime') * 1000,
  104. 'user_id' => $order->user_id
  105. ];
  106. // 已支付的,不包含,货到付款未支付的
  107. if (in_array($order->status, [Order::STATUS_PAID, Order::STATUS_COMPLETED])) {
  108. // 支付订单数
  109. $data['payOrderNum']++;
  110. $data['payOrderArr'][] = [
  111. 'counter' => 1,
  112. 'createtime' => $order->getData('createtime') * 1000,
  113. 'user_id' => $order->user_id
  114. ];
  115. // 支付金额
  116. $data['payAmountNum'] = bcadd((string)$data['payAmountNum'], $order->pay_fee, 2);
  117. $data['payAmountArr'][] = [
  118. 'counter' => $order->pay_fee,
  119. 'createtime' => $order->getData('createtime') * 1000,
  120. ];
  121. // 下单用户
  122. if (!in_array($order->user_id, $userIds)) {
  123. $data['payUserNum']++;
  124. $data['payUserArr'][] = [
  125. 'counter' => 1,
  126. 'createtime' => $order->getData('createtime') * 1000,
  127. 'user_id' => $order->user_id
  128. ];
  129. }
  130. }
  131. // 已支付的,和 货到付款未支付的
  132. if (in_array($order->status, [Order::STATUS_PAID, Order::STATUS_COMPLETED]) || $order->isOffline($order)) {
  133. $flagnoSend = false;
  134. $flagaftersale = false;
  135. $flagrefund = false;
  136. $aftersaleIng = false;
  137. foreach ($order->items as $k => $item) {
  138. if (
  139. !$flagnoSend
  140. && $item->dispatch_status == OrderItem::DISPATCH_STATUS_NOSEND
  141. && $item->refund_status == OrderItem::REFUND_STATUS_NOREFUND
  142. && in_array($order->apply_refund_status, [
  143. Order::APPLY_REFUND_STATUS_NOAPPLY,
  144. Order::APPLY_REFUND_STATUS_REFUSE
  145. ])
  146. ) {
  147. $flagnoSend = true;
  148. }
  149. if (
  150. $item->aftersale_status == OrderItem::AFTERSALE_STATUS_ING
  151. && $item->dispatch_status == OrderItem::DISPATCH_STATUS_NOSEND
  152. && $item->refund_status == OrderItem::REFUND_STATUS_NOREFUND
  153. ) {
  154. $aftersaleIng = true;
  155. }
  156. if (!$flagaftersale && $item->aftersale_status != OrderItem::AFTERSALE_STATUS_NOAFTER) {
  157. $data['aftersaleNum']++;
  158. // $data['aftersaleArr'][] = [
  159. // 'counter' => 1,
  160. // 'createtime' => $order->getData('createtime') * 1000,
  161. // ];
  162. $flagaftersale = true;
  163. }
  164. if (!$flagrefund && $item->refund_status > OrderItem::REFUND_STATUS_NOREFUND) {
  165. $data['refundNum']++;
  166. // $data['refundArr'][] = [
  167. // 'counter' => 1,
  168. // 'createtime' => $order->getData('createtime') * 1000,
  169. // ];
  170. $flagrefund = true;
  171. }
  172. }
  173. if (!$aftersaleIng && $flagnoSend) {
  174. // 存在正在售后中的订单,不算待发货(和订单列表保持一直)
  175. $data['noSendNum']++;
  176. // $data['noSendArr'][] = [
  177. // 'counter' => 1,
  178. // 'createtime' => $order->getData('createtime') * 1000,
  179. // ];
  180. }
  181. }
  182. }
  183. $this->success('获取成功', null, $data);
  184. }
  185. public function ranking()
  186. {
  187. $goods = Goods::limit(5)->order('sales', 'desc')->select();
  188. foreach ($goods as $key => $gd) {
  189. $gd->append(['real_sales']);
  190. $result = OrderItem::field('sum(goods_num * goods_price) as sale_total_money')->where('goods_id', $gd['id'])
  191. ->whereExists(function ($query) use ($gd) {
  192. $order_table_name = (new Order())->getQuery()->getTable();
  193. $table_name = (new OrderItem())->getQuery()->getTable();
  194. $query->table($order_table_name)->where($table_name . '.order_id=' . $order_table_name . '.id')
  195. ->whereIn('status', [Order::STATUS_PAID, Order::STATUS_COMPLETED]); // 已支付的订单
  196. })->find();
  197. $gd['sale_total_money'] = $result['sale_total_money'] ?: 0;
  198. }
  199. $searchHistory = new SearchHistory();
  200. $this->success('获取成功', null, [
  201. 'goods' => $goods,
  202. 'hot_search' => $searchHistory->hotSearch()
  203. ]);
  204. }
  205. }