package com.czg.controller.admin;

import com.czg.annotation.SaAdminCheckPermission;
import com.czg.order.entity.ShopOrderStatistic;
import com.czg.order.entity.ShopProdStatistic;
import com.czg.order.param.DataSummaryTradeParam;
import com.czg.order.service.ShopOrderStatisticService;
import com.czg.order.service.ShopProdStatisticService;
import com.czg.order.vo.CostLineChartVO;
import com.czg.order.vo.CountPayTypeVo;
import com.czg.order.vo.ProfitRateVO;
import com.czg.order.vo.TotalVo;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.service.RedisService;
import com.czg.utils.AssertUtil;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
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;

import java.util.List;

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

    @Resource
    private RedisService redisService;
    @Resource
    private ShopOrderStatisticService orderStatisticService;
    @Resource
    private ShopProdStatisticService prodStatisticService;


    /**
     * 营业板块-上半部分
     */
    @GetMapping("trade")
    @SaAdminCheckPermission(parentName = "营业板块",value = "dataSummary:trade", name = "营业板块-上半部分")
    public CzgResult<ShopOrderStatistic> getTradeData(@Validated DataSummaryTradeParam param) {
        Boolean hasKey = redisService.hasKey("task:statistic:date:");
        if (hasKey) {
            return CzgResult.failure("数据统计任务正在运行中，请稍后再试");
        }
        if (param.getShopId() == null) {
            param.setShopId(StpKit.USER.getShopId());
        }
        ShopOrderStatistic data = orderStatisticService.getArchiveTradeData(param.getShopId(), param.getRangeType(), param.getBeginDate(), param.getEndDate());
        return CzgResult.success(data);
    }

    /**
     * 商品销售 右下
     */
    @GetMapping("productSaleDate")
    @SaAdminCheckPermission(parentName = "营业板块",value = "dataSummary:productSaleData", name = "商品销售-右下")
    public CzgResult<List<ShopProdStatistic>> getProductSaleData(@RequestParam Integer day,@RequestParam(required = false) Long shopId) {
        if (shopId == null) {
            shopId = StpKit.USER.getShopId();
        }
        List<ShopProdStatistic> data = prodStatisticService.getArchiveTradeDataBy10(shopId,day);
        return CzgResult.success(data);
    }

    /**
     * 销售趋势柱状图 左下1
     *
     * @param day    天数
     * @param shopId 店铺id
     */
    @GetMapping("dateAmount")
    @SaAdminCheckPermission(parentName = "营业板块",value = "dataSummary:dateAmount", name = "销售趋势柱状图 左下")
    public CzgResult<List<TotalVo>> getDateAmount(@RequestParam Integer day, @RequestParam(required = false) Long shopId) {
        AssertUtil.isNull(day, "天数不能为空");
        if (shopId == null) {
            shopId = StpKit.USER.getShopId();
        }
        List<TotalVo> data = orderStatisticService.getDateAmount(shopId, day);
        return CzgResult.success(data);
    }

    /**
     * 支付占比饼图 左下2
     *
     * @param day    天数
     * @param shopId 店铺id
     */
    @GetMapping("datePayType")
    @SaAdminCheckPermission(parentName = "营业板块",value = "dataSummary:datePayType", name = "支付占比饼图 左下2")
    public CzgResult<List<CountPayTypeVo>> shopSummaryPayType(@RequestParam Integer day, @RequestParam(required = false) Long shopId) {
        if (shopId == null) {
            shopId = StpKit.USER.getShopId();
        }
        List<CountPayTypeVo> data = orderStatisticService.getSummaryPayTypeData(shopId, day);
        return CzgResult.success(data);
    }

    /**
     * 毛利率/净利率 柱状图 左下下
     * 利润率柱状图
     * @param day    天数
     * @param shopId 店铺id
     */
    @GetMapping("profitRateBarChart")
    @SaAdminCheckPermission(parentName = "营业板块",value = "dataSummary:profitRateBarChart", name = "毛利率/净利率柱状图 左下下")
    public CzgResult<List<ProfitRateVO>> profitRateBarChart(@RequestParam Integer day, @RequestParam(required = false) Long shopId) {
        AssertUtil.isNull(day, "天数不能为空");
        if (shopId == null) {
            shopId = StpKit.USER.getShopId();
        }
        List<ProfitRateVO> data = orderStatisticService.profitRateBarChart(shopId, day);
        return CzgResult.success(data);
    }

    /**
     * 成本折线图 右下下
     * @param day    天数
     * @param shopId 店铺id
     */
    @GetMapping("costLineChart")
    @SaAdminCheckPermission(parentName = "营业板块",value = "dataSummary:costLineChart", name = "成本折线图 右下下")
    public CzgResult<List<CostLineChartVO>> costLineChart(@RequestParam Integer day, @RequestParam(required = false) Long shopId) {
        AssertUtil.isNull(day, "天数不能为空");
        if (shopId == null) {
            shopId = StpKit.USER.getShopId();
        }
        List<CostLineChartVO> data = orderStatisticService.costLineChart(shopId, day);
        return CzgResult.success(data);
    }

}
