Dashboard.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace app\controller;
  3. use app\extra\basic\Base;
  4. use app\middleware\AuthMiddleware;
  5. use app\model\saas\SaasOrderLife;
  6. use app\model\saas\SaasStore;
  7. use LinFly\Annotation\Route\Controller;
  8. use LinFly\Annotation\Route\Middleware;
  9. use LinFly\Annotation\Route\Route;
  10. use support\Response;
  11. #[Controller(prefix: "/api/dashboard"),Middleware(AuthMiddleware::class)]
  12. class Dashboard extends Base
  13. {
  14. /**
  15. *
  16. * @return Response
  17. */
  18. #[Route(path: "manage",methods: "get")]
  19. public function getManageData(): Response
  20. {
  21. try {
  22. $total = (new SaasOrderLife)->where("status","in",[1,2,3,6])->whereDay("create_at")->field("ROUND(sum(pay_amount)/100,2) as money,count(id) as num")->find();
  23. $today = [
  24. "order_money" => $total['money']??0, // 营业额
  25. "order_num" => $total['num']??0, // 订单数
  26. "order_done" => "", // 已核销
  27. "order_send" => "", // 待发货
  28. "store_num" => (new SaasStore)->count(), // 总店铺数量
  29. ];
  30. $moneys = $orders = [];
  31. $fields = ['ROUND(sum(pay_amount)/100,2)' => 'total','substr(create_at,1,10)' => 'mday'];
  32. $orderMoney = (new SaasOrderLife)->field($fields)->where("status","in",[1,3,4,5])->whereTime('create_at', '-15 days')->group('mday')->select()->column(null, 'mday');
  33. for ($i = 15; $i >= 0; $i--) {
  34. $date = date('Y-m-d', strtotime("-{$i}days"));
  35. $moneys[] = [
  36. '当天日期' => date('Y-m-d', strtotime("-{$i}days")),
  37. '订单金额' => ($orderMoney[$date] ?? [])['total'] ?? 0,
  38. ];
  39. }
  40. $field = ['count(1)' => 'count', 'substr(create_at,1,10)' => 'mday'];
  41. $orderNum = (new SaasOrderLife)->field($field)->where("status","in",[1,3,4,5])->whereTime('create_at', '-15 days')->group('mday')->select()->column(null, 'mday');
  42. for ($i = 15; $i >= 0; $i--) {
  43. $date = date('Y-m-d', strtotime("-{$i}days"));
  44. $orders[] = [
  45. '当天日期' => date('Y-m-d', strtotime("-{$i}days")),
  46. '订单数量' => ($orderNum[$date] ?? [])['count'] ?? 0,
  47. ];
  48. }
  49. return success("ok",compact("today","moneys","orders"));
  50. } catch (\Throwable $throwable) {
  51. return error($throwable->getMessage());
  52. }
  53. }
  54. }