Dash.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace app\controller\admin;
  3. use app\model\hx\HxGoods;
  4. use app\model\hx\HxOrderLife;
  5. use app\model\hx\HxStorePoi;
  6. use support\Request;
  7. use support\Response;
  8. class Dash
  9. {
  10. public function getDashData(Request $request): Response
  11. {
  12. try {
  13. $param = [];
  14. if (!empty($request->user['agent_id'])) {
  15. $param['agent_id'] = $request->user['agent_id'];
  16. }
  17. $total = ['t0' => [
  18. 'num' => 0,
  19. 'money' => 0,
  20. ], 't1' => [
  21. 'num' => 0,
  22. 'money' => 0,
  23. ], 't2' => [
  24. 'num' => 0,
  25. 'money' => 0,
  26. ], 't3' => [
  27. 'num' => 0,
  28. 'money' => 0,
  29. ], 't4' => [
  30. 'num' => 0,
  31. 'money' => 0,
  32. ], 'ta' => 0];
  33. foreach ((new HxOrderLife)->whereDay("create_at")->field('status,count(1) total,sum(pay_amount) as money')->group('status')->cursor() as $item) {
  34. [$total['t'.$item['status']] = [
  35. 'num' => $item['total'],
  36. 'money' => $item['money']
  37. ],$total['ta'] += $item['total']];
  38. }
  39. // 今日核销金额
  40. // 今日退款金额
  41. // 商品总数量
  42. $goods = (new HxGoods)->where($param)->count();
  43. // POI数量
  44. $poi = (new HxStorePoi)->where($param)->count();
  45. // 累计付款金额 (元)
  46. // 核销金额 (元)
  47. // 待核销金额 (元)
  48. // 核销订单数
  49. $moneys = $orders = [];
  50. $fields = ['ROUND(sum(pay_amount)/100,2)' => 'total','substr(create_at,1,10)' => 'mday'];
  51. $orderMoney = (new HxOrderLife)->field($fields)->where($param)->where("status","in",[1,4])->whereTime('create_at', '-15 days')->group('mday')->select()->column(null, 'mday');
  52. for ($i = 15; $i >= 0; $i--) {
  53. $date = date('Y-m-d', strtotime("-{$i}days"));
  54. $moneys[] = [
  55. '当天日期' => date('Y-m-d', strtotime("-{$i}days")),
  56. '订单金额' => ($orderMoney[$date] ?? [])['total'] ?? 0,
  57. ];
  58. }
  59. $field = ['count(1)' => 'count', 'substr(create_at,1,10)' => 'mday'];
  60. $orderNum = (new HxOrderLife)->field($field)->where($param)->where("status","in",[1,4])->whereTime('create_at', '-15 days')->group('mday')->select()->column(null, 'mday');
  61. for ($i = 15; $i >= 0; $i--) {
  62. $date = date('Y-m-d', strtotime("-{$i}days"));
  63. $orders[] = [
  64. '当天日期' => date('Y-m-d', strtotime("-{$i}days")),
  65. '订单数量' => ($orderNum[$date] ?? [])['count'] ?? 0,
  66. ];
  67. }
  68. $info = [
  69. "order" => ''
  70. ];
  71. return success("ok",compact("total","moneys","orders","info","poi","goods"));
  72. } catch (\Throwable $throwable) {
  73. return error($throwable->getMessage());
  74. }
  75. }
  76. }