Dashboard.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace app\admin\controller\shopro;
  3. use app\common\controller\Backend;
  4. use think\Config;
  5. /**
  6. * 控制台
  7. *
  8. * @icon fa fa-dashboard
  9. * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
  10. */
  11. class Dashboard extends Backend
  12. {
  13. /**
  14. * 查看
  15. */
  16. public function index()
  17. {
  18. if ($this->request->isAjax()) {
  19. //如果发送的来源是Selectpage,则转发到Selectpage
  20. $datetimerange = explode(' - ', $this->request->request('datetimerange'));
  21. $startTime = strtotime($datetimerange[0]);
  22. $endTime = strtotime($datetimerange[1]);
  23. $this->model = new \app\admin\model\shopro\order\Order;
  24. $where = [
  25. 'createtime' => ['between', [$startTime, $endTime]]
  26. ];
  27. $list = $this->model
  28. ->where($where)
  29. ->with('item')
  30. ->order('id')
  31. ->select();
  32. $data = $this->getTotalData($list);
  33. // 商品列表
  34. $goodsList = \addons\shopro\model\Goods::limit(5)->order('sales', 'desc')->select();
  35. foreach ($goodsList as $key => $goods) {
  36. $result = \app\admin\model\shopro\order\OrderItem::field('sum(goods_num * goods_price) as sale_total_money')->where('goods_id', $goods['id'])
  37. ->whereExists(function ($query) use ($goods) {
  38. $order_table_name = $this->model->getQuery()->getTable();
  39. $table_name = (new \app\admin\model\shopro\order\OrderItem())->getQuery()->getTable();
  40. $query->table($order_table_name)->where('order_id=' . $order_table_name . '.id')
  41. ->where('status', '>', \app\admin\model\shopro\order\Order::STATUS_NOPAY); // 已支付的订单
  42. })->find();
  43. $goods['sale_total_money'] = $result['sale_total_money'] ? : 0;
  44. }
  45. $data['goodsList'] = $goodsList;
  46. extract($this->orderScale($list));
  47. $data['orderFinish'] = $orderFinish;
  48. $data['payedFinish'] = $payedFinish;
  49. $this->success('数据中心', '', $data);
  50. }
  51. return $this->view->fetch();
  52. }
  53. private function orderScale ($list) {
  54. $total = count($list);
  55. $total_money = array_sum(array_column($list, 'total_fee'));
  56. $data['orderFinish'] = [
  57. 'order_scale' => 0,
  58. 'order_user' => 0
  59. ];
  60. $data['payedFinish'] = [
  61. 'payed_scale' => 0,
  62. 'payed_money' => 0
  63. ];
  64. // 支付单数
  65. $payed_num = 0;
  66. // 支付金额
  67. $payed_money = 0;
  68. // 支付的用户 id
  69. $payed_user_ids = [];
  70. foreach ($list as $key => $order) {
  71. if ($order['status'] > 0) {
  72. $payed_num++;
  73. $payed_money = bcadd($payed_money, $order['total_fee'], 2);
  74. $payed_user_ids[] = $order['user_id'];
  75. }
  76. }
  77. $orderFinish = [
  78. 'order_scale' => $total ? round(($payed_num / $total), 2) : 0,
  79. 'order_payed' => $payed_num,
  80. ];
  81. $payedFinish = [
  82. 'payed_scale' => $total_money ? round(($payed_money / $total_money), 2) : 0,
  83. 'payed_money' => round($payed_money, 2)
  84. ];
  85. return compact("orderFinish", "payedFinish");
  86. }
  87. private function getTotalData($list) {
  88. // 支付订单
  89. $data['payOrderNum'] = 0;
  90. $data['payOrderArr'] = [];
  91. //支付金额
  92. $data['payAmountNum'] = 0;
  93. $data['payAmountArr'] = [];
  94. // 代发货
  95. $data['noSentNum'] = 0;
  96. $data['noSentArr'] = [];
  97. //支付人数
  98. $data['orderNum'] = count($list);
  99. $data['orderArr'] = [];
  100. //售后维权
  101. $data['aftersaleNum'] = 0;
  102. $data['aftersaleArr'] = [];
  103. //退款订单
  104. $data['refundNum'] = 0;
  105. $data['refundArr'] = [];
  106. //所有下单金额
  107. $data['totalAmount'] = 0;
  108. $data['tranPeople'] = [];
  109. $data['wechatPay'] = 0;
  110. $data['alipayPay'] = 0;
  111. $data['walletPay'] = 0;
  112. $data['allTypePay'] = 0;
  113. foreach ($list as $key => $order) {
  114. $data['orderArr'][] = [
  115. 'counter' => 1,
  116. 'createtime' => $order['createtime'] * 1000,
  117. 'user_id' => $order['user_id']
  118. ];
  119. $data['totalAmount'] = bcadd($data['totalAmount'], $order['pay_fee'], 2); // 这里可能要使用 total_fee
  120. if ($order['status'] > 0) {
  121. $data['payOrderNum']++;
  122. $data['payOrderArr'][] = [
  123. 'counter' => 1,
  124. 'createtime' => $order['createtime'] * 1000,
  125. 'user_id' => $order['user_id']
  126. ];
  127. $data['payAmountNum'] = bcadd($data['payAmountNum'], $order['pay_fee'], 2);
  128. $data['payAmountArr'][] = [
  129. 'counter' => $order['pay_fee'],
  130. 'createtime' => $order['createtime'] * 1000,
  131. ];
  132. $data['tranPeople']++;
  133. $flagnoSent = false;
  134. $flagaftersale = false;
  135. $flagrefund = false;
  136. foreach ($order['item'] as $k => $item) {
  137. if (!$flagnoSent && $item['dispatch_status'] == 0 && $item['refund_status'] == 0) {
  138. $data['noSentNum']++;
  139. $data['noSentArr'][] = [
  140. 'counter' => 1,
  141. 'createtime' => $order['createtime'] * 1000,
  142. ];
  143. $flagnoSent = true;
  144. }
  145. if (!$flagaftersale && $item['aftersale_status'] > 0) {
  146. $data['aftersaleNum']++;
  147. $data['aftersaleArr'][] = [
  148. 'counter' => 1,
  149. 'createtime' => $order['createtime'] * 1000,
  150. ];
  151. $flagaftersale = true;
  152. }
  153. if (!$flagrefund && $item['refund_status'] > 0) {
  154. $data['refundNum']++;
  155. $data['refundArr'][] = [
  156. 'counter' => 1,
  157. 'createtime' => $order['createtime'] * 1000,
  158. ];
  159. $flagrefund = true;
  160. }
  161. }
  162. $data['allTypePay']++;
  163. if ($order['pay_type'] == 'wechat') {
  164. $data['wechatPay']++;
  165. }
  166. if ($order['pay_type'] == 'alipay') {
  167. $data['alipayPay']++;
  168. }
  169. if ($order['pay_type'] == 'wallet') {
  170. $data['walletPay']++;
  171. }
  172. }
  173. }
  174. return $data;
  175. }
  176. }