<?php

namespace app\controller;

use app\extra\basic\Base;
use app\middleware\AuthMiddleware;
use app\model\saas\SaasOrder;
use app\model\saas\SaasOrderLife;
use app\model\saas\SaasStore;
use LinFly\Annotation\Route\Controller;
use LinFly\Annotation\Route\Middleware;
use LinFly\Annotation\Route\Route;
use support\Request;
use support\Response;


#[Controller(prefix: "/api/dashboard"),Middleware(AuthMiddleware::class)]
class Dashboard extends Base
{


    /**
     *
     * @return Response
     */
    #[Route(path: "manage",methods: "get")]
    public function getManageData(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "money.default" => 7,
                "hide.default"  => 0,
                "order.default"  => 7
            ],$request->method());
            $moneyDay = $param['money'];
            $data = $moneys = $orders = [];
            $orderDay = $param['order'];
            $mode = (new SaasOrder);
            if ($param['hide'] == 1) {
                $data = [
                    "userNum"   => $mode->whereDay("create_at")->where('status','in',[1,3])->sum("money"), // 今日营业额
                    "rechargeNum"   => $mode->whereDay("create_at")->where('status','in',[1,3])->count(), // 今日订单数
                    "rechargeMoney"   => $mode->whereDay("create_at")->where('status',1)->count(), // 手机自助打印待处理
                ];
                $fields = ['ROUND(sum(money)/100,2)' => 'total','substr(create_at,1,10)' => 'mday'];
                $orderMoney = $mode->field($fields)->where('status','in',[1,3])->whereTime('create_at', '-'.$moneyDay.' days')->group('mday')->select()->column(null, 'mday');
                for ($i = $moneyDay; $i >= 0; $i--) {
                    $date = date('Y-m-d', strtotime("-{$i}days"));
                    $moneys[] = [
                        '当天日期' => date('Y-m-d', strtotime("-{$i}days")),
                        '订单金额' => ($orderMoney[$date] ?? [])['total'] ?? 0,
                    ];
                }
                $field = ['count(1)' => 'count', 'substr(create_at,1,10)' => 'mday'];
                $orderNum = $mode->field($field)->where('status','in',[1,3])->whereTime('create_at', '-'.$orderDay.' days')->group('mday')->select()->column(null, 'mday');
                for ($i = $orderDay; $i >= 0; $i--) {
                    $date = date('Y-m-d', strtotime("-{$i}days"));
                    $orders[] = [
                        '当天日期' => date('Y-m-d', strtotime("-{$i}days")),
                        '订单数量' => ($orderNum[$date] ?? [])['count'] ?? 0,
                    ];
                }
            } else {
                for ($i = $moneyDay; $i >= 0; $i--) {
                    $date = date('Y-m-d', strtotime("-{$i}days"));
                    $moneys[] = [
                        '当天日期' => date('Y-m-d', strtotime("-{$i}days")),
                        '订单金额' => 888,
                    ];
                }
                for ($i = $orderDay; $i >= 0; $i--) {
                    $date = date('Y-m-d', strtotime("-{$i}days"));
                    $orders[] = [
                        '当天日期' => date('Y-m-d', strtotime("-{$i}days")),
                        '订单数量' => 888,
                    ];
                }
            }
            return success("ok",compact("data","orders","moneys"));
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    /**
     * @param Request $request
     * @return Response
     */
    #[Route(path: "store",methods: "get")]
    public function getStoreData(Request $request): Response
    {
        try {
            $map = [ "agent_id" => $request->user['agent_id'] ];
            $total = (new SaasOrderLife)->where($map)->where("status","in",[1,2,3,6])->whereDay("create_at")->field("ROUND(sum(pay_amount)/100,2) as money,count(id) as num")->find();
            $today = [
                "order_money"   => $total['money']??0, // 营业额
                "order_num"     => $total['num']??0, // 订单数
                "order_done"    => "", // 已核销
                "order_send"    => "",  // 待发货
                "store_num"     => (new SaasStore)->where($map)->count(), // 总店铺数量
            ];
            $moneys = $orders = [];
            $fields = ['ROUND(sum(pay_amount)/100,2)' => 'total','substr(create_at,1,10)' => 'mday'];
            $orderMoney = (new SaasOrderLife)->field($fields)->where($map)->where("status","in",[1,3,4,5])->whereTime('create_at', '-15 days')->group('mday')->select()->column(null, 'mday');
            for ($i = 15; $i >= 0; $i--) {
                $date = date('Y-m-d', strtotime("-{$i}days"));
                $moneys[] = [
                    '当天日期' => date('Y-m-d', strtotime("-{$i}days")),
                    '订单金额' => ($orderMoney[$date] ?? [])['total'] ?? 0,
                ];
            }
            $field = ['count(1)' => 'count', 'substr(create_at,1,10)' => 'mday'];
            $orderNum = (new SaasOrderLife)->field($field)->where($map)->where("status","in",[1,2,3,6])->whereTime('create_at', '-15 days')->group('mday')->select()->column(null, 'mday');
            for ($i = 15; $i >= 0; $i--) {
                $date = date('Y-m-d', strtotime("-{$i}days"));
                $orders[] = [
                    '当天日期' => date('Y-m-d', strtotime("-{$i}days")),
                    '订单数量' => ($orderNum[$date] ?? [])['count'] ?? 0,
                ];
            }
            return success("ok",compact("today","moneys","orders"));
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }


    /**
     * @param Request $request
     * @return Response
     */
    #[Route(path: "shop",methods: "get")]
    public function getShopData(Request $request): Response
    {
        try {
            $map = [ "store_id" => $request->user['store_id'],"agent_id" => $request->user['agent_id'] ];
            $total = (new SaasOrderLife)->where($map)->where("status","in",[1,3,4,5])->whereDay("create_at")->field("ROUND(sum(pay_amount)/100,2) as money,count(id) as num")->find();
            $today = [
                "order_money"   => $total['money']??0, // 营业额
                "order_num"     => $total['num']??0, // 订单数
                "order_done"    => "", // 已核销
                "order_send"    => "",  // 待发货
            ];
            $moneys = $orders = [];
            $fields = ['ROUND(sum(pay_amount)/100,2)' => 'total','substr(create_at,1,10)' => 'mday'];
            $orderMoney = (new SaasOrderLife)->field($fields)->where($map)->where("status","in",[1,3,4,5])->whereTime('create_at', '-15 days')->group('mday')->select()->column(null, 'mday');
            for ($i = 15; $i >= 0; $i--) {
                $date = date('Y-m-d', strtotime("-{$i}days"));
                $moneys[] = [
                    '当天日期' => date('Y-m-d', strtotime("-{$i}days")),
                    '订单金额' => ($orderMoney[$date] ?? [])['total'] ?? 0,
                ];
            }
            $field = ['count(1)' => 'count', 'substr(create_at,1,10)' => 'mday'];
            $orderNum = (new SaasOrderLife)->field($field)->where($map)->where("status","in",[1,3,4,5])->whereTime('create_at', '-15 days')->group('mday')->select()->column(null, 'mday');
            for ($i = 15; $i >= 0; $i--) {
                $date = date('Y-m-d', strtotime("-{$i}days"));
                $orders[] = [
                    '当天日期' => date('Y-m-d', strtotime("-{$i}days")),
                    '订单数量' => ($orderNum[$date] ?? [])['count'] ?? 0,
                ];
            }
            return success("ok",compact("today","moneys","orders"));
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

}