package com.czg.controller.admin;

import com.czg.annotation.SaStaffCheckPermission;
import com.czg.log.annotation.OperationLog;
import com.czg.order.entity.ShopOrderStatistic;
import com.czg.order.param.DataSummaryProductSaleParam;
import com.czg.order.param.DataSummaryTradeParam;
import com.czg.order.service.DataSummaryService;
import com.czg.order.vo.DataSummaryDateAmountVo;
import com.czg.order.vo.DataSummaryPayTypeVo;
import com.czg.order.vo.DataSummaryProductSaleRankingVo;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.AssertUtil;
import com.czg.validator.ValidatorUtil;
import com.czg.validator.group.DefaultGroup;
import com.mybatisflex.core.paginate.Page;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 数据统计
 *
 * @author tankaikai
 * @since 2025-03-07 15:25
 */
@AllArgsConstructor
@RestController
@RequestMapping("/admin/data/summary")
public class DataSummaryController {

    private final DataSummaryService dataSummaryService;

    /**
     * 营业板块-上半部分
     */
    @GetMapping("trade")
    @OperationLog("营业板块-上半部分")
    @SaStaffCheckPermission("yun_xu_cha_kan_jing_ying_shu_ju")
    //@SaAdminCheckPermission("dataSummary:trade")
    public CzgResult<ShopOrderStatistic> getTradeData(DataSummaryTradeParam param) {
        ValidatorUtil.validateEntity(param, DefaultGroup.class);
        Long shopId = StpKit.USER.getShopId(0L);
        if (param.getShopId() == null) {
            param.setShopId(shopId);
        }
        ShopOrderStatistic data = dataSummaryService.getTradeData(param);
        return CzgResult.success(data);
    }

    /**
     * 商品销售 右下
     */
    @GetMapping("productSaleDate")
    @OperationLog("商品销售-右下")
    @SaStaffCheckPermission("yun_xu_cha_kan_jing_ying_shu_ju")
    //@SaAdminCheckPermission("dataSummary:productSaleData")
    public CzgResult<Page<DataSummaryProductSaleRankingVo>> getProductSaleData(DataSummaryProductSaleParam param) {
        ValidatorUtil.validateEntity(param, DefaultGroup.class);
        Long shopId = StpKit.USER.getShopId(0L);
        if (param.getShopId() == null) {
            param.setShopId(shopId);
        }
        Page<DataSummaryProductSaleRankingVo> data = dataSummaryService.getProductSaleRankingPage(param);
        return CzgResult.success(data);
    }

    /**
     * 销售趋势柱状图 左下
     *
     * @param day    天数
     * @param shopId 店铺id
     */
    @GetMapping("dateAmount")
    @OperationLog("销售趋势柱状图 左下")
    @SaStaffCheckPermission("yun_xu_cha_kan_jing_ying_shu_ju")
    //@SaAdminCheckPermission("dataSummary:dateAmount")
    public CzgResult<DataSummaryDateAmountVo> getDateAmount(@RequestParam Integer day, @RequestParam(required = false) Long shopId) {
        AssertUtil.isNull(day, "天数不能为空");
        if (shopId == null) {
            shopId = StpKit.USER.getShopId(0L);
        }
        DataSummaryDateAmountVo data = dataSummaryService.getSummaryAmountData(shopId, day);
        return CzgResult.success(data);
    }

    /**
     * 支付占比饼图 左下
     *
     * @param day 天数
     * @param shopId 店铺id
     */
    @GetMapping("datePayType")
    @OperationLog("支付占比饼图 左下")
    @SaStaffCheckPermission("yun_xu_cha_kan_jing_ying_shu_ju")
    //@SaAdminCheckPermission("dataSummary:datePayType")
    public CzgResult<DataSummaryPayTypeVo> shopSummaryPayType(@RequestParam Integer day, @RequestParam(required = false) Long shopId) {
        if (shopId == null) {
            shopId = StpKit.USER.getShopId(0L);
        }
        DataSummaryPayTypeVo data = dataSummaryService.getSummaryPayTypeData(shopId, day);
        return CzgResult.success(data);
    }
}
