package com.ruoyi.web.dashboard;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.ruoyi.bst.order.constants.OrderDailyKeys;
import com.ruoyi.bst.order.domain.OrderQuery;
import com.ruoyi.bst.order.domain.enums.OrderStatus;
import com.ruoyi.bst.order.domain.query.OrderRefundQuery;
import com.ruoyi.bst.order.domain.query.OrderStatQuery;
import com.ruoyi.bst.order.domain.vo.OrderDailyRefundStatVO;
import com.ruoyi.bst.order.domain.vo.OrderDailyStatVO;
import com.ruoyi.bst.order.domain.vo.OrderRankVO;
import com.ruoyi.bst.order.service.OrderDashboard;
import com.ruoyi.bst.refund.domain.enums.RefundStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.collection.CollectionUtils;

@RestController
@RequestMapping("/dashboard/order")
public class DashboardOrderController extends BaseController {

    @Autowired
    private OrderDashboard orderDashboard;

    // 查询每日订单金额
    @PreAuthorize("@ss.hasPermi('dashboard:order:dailyAmount')")
    @GetMapping("/dailyAmount")
    public AjaxResult getDailyAmount(OrderQuery query) {
        query.setStatusList(OrderStatus.valid());
        query.setScope(true);
        List<String> keys = Arrays.asList(OrderDailyKeys.CREATE_DATE, OrderDailyKeys.PAY_AMOUNT, OrderDailyKeys.ORDER_COUNT);
        List<OrderDailyStatVO> list = orderDashboard.selectDailyStat(query, keys);
        if (query.getCreateDateRange() != null && query.getCreateDateRange().size() >= 2) {
            list = CollectionUtils.fillVoids(list, OrderDailyStatVO::getDate, (date) -> {
                OrderDailyStatVO vo = new OrderDailyStatVO();
                vo.setDate(date);
                return vo;
            }, query.getCreateDateRange().get(0), query.getCreateDateRange().get(1));
        }
        return success(list);
    }

    // 查询每日订单退款金额
    @PreAuthorize("@ss.hasPermi('dashboard:order:dailyRefundAmount')")
    @GetMapping("/dailyRefundAmount")
    public AjaxResult getDailyRefundAmount(OrderRefundQuery query) {
        query.setStatusList(OrderStatus.valid());
        query.setRefundStatusList(RefundStatus.valid());
        query.setScope(true);
        List<String> keys = Arrays.asList(OrderDailyKeys.REFUND_CREATE_DATE, OrderDailyKeys.REFUND_AMOUNT);
        List<OrderDailyRefundStatVO> list = orderDashboard.selectDailyRefundStat(query, keys);
        if (query.getRefundCreateDateRange() != null && query.getRefundCreateDateRange().size() >= 2) {
            list = CollectionUtils.fillVoids(list, OrderDailyRefundStatVO::getDate, (date) -> {
                OrderDailyRefundStatVO vo = new OrderDailyRefundStatVO();
                vo.setDate(date);
                return vo;
            }, query.getRefundCreateDateRange().get(0), query.getRefundCreateDateRange().get(1));
        }
        return success(list);
    }

    @PreAuthorize("@ss.hasPermi('dashboard:order:rank')")
    @GetMapping("/rank")
    public AjaxResult getRank(OrderQuery query) {
        query.setScope(true);
        query.setStatusList(OrderStatus.valid());
        PageHelper.startPage(1, 10);
        startOrderBy();
        List<OrderRankVO> list = orderDashboard.selectRank(query);
        return success(list);
    }

    @PreAuthorize("@ss.hasPermi('dashboard:order:stat')")
    @GetMapping("/stat")
    public AjaxResult getStat(OrderStatQuery query) {
        query.setScope(true);
        return success(orderDashboard.selectStat(query, query.getKeys()));
    }
}
