package com.czg.controller.admin;

import com.czg.order.dto.FinanceStsDTO;
import com.czg.order.param.FinanceStsQueryParam;
import com.czg.order.param.SaleSummaryCountParam;
import com.czg.order.service.FinanceStsService;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.task.StatisticTask;
import com.czg.utils.FunUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
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.time.LocalDate;

/**
 * 管理端/财务报表
 * @author yjjie
 * @date 2026/2/2 10:09
 */
@RestController
@RequestMapping("/admin/finance")
public class FinanceStsController {

    @Resource
    private FinanceStsService financeStsService;

    @Resource
    private StatisticTask statisticTask;

    /**
     * 基础统计 预留重置统计
     *
     * @param date 日期yyyy-MM-dd
     */
    @GetMapping("/base")
    public String baseStatistic(@RequestParam LocalDate date, @RequestParam Long shopId, @RequestParam String type) {
        if ("order".equals(type)) {
            statisticTask.statisticAndInsertOrder(shopId, date);
        } else if ("prod".equals(type)) {
            statisticTask.statisticAndInsertProd(shopId, date);
        } else if ("table".equals(type)) {
            statisticTask.statisticAndInsertTable(shopId, date);
        } else if ("all".equals(type)) {
            FunUtils.safeRunVoid(() -> statisticTask.statisticAndInsertOrder(shopId, date));
            FunUtils.safeRunVoid(() -> statisticTask.statisticAndInsertProd(shopId, date));
            FunUtils.safeRunVoid(() -> statisticTask.statisticAndInsertTable(shopId, date));
        }
        return "success";
    }

    /**
     * 查询财务报表
     */
    @GetMapping("/sts")
    public CzgResult<FinanceStsDTO> getFinanceSts(@Validated(FinanceStsQueryParam.Query.class) FinanceStsQueryParam param) {
        return CzgResult.success(financeStsService.getFinanceSts(param));
    }

    /**
     * 导出财务报表
     */
    @GetMapping("/export")
    public void exportFinanceSts(@Validated(FinanceStsQueryParam.Export.class) FinanceStsQueryParam param, HttpServletResponse response) {
        financeStsService.exportFinanceSts(param, response);
    }


    /**
     * 打印经营日报
     */
    @GetMapping("/printDayReport")
    public CzgResult<Void> printDayReport(SaleSummaryCountParam param) {
        if (param.getShopId() == null) {
            param.setShopId(StpKit.USER.getShopId());
        }
        financeStsService.printDayReport(param);
        return CzgResult.success();
    }

    /**
     * 打印日结单
     */
    @GetMapping("/printDaySettle")
    public CzgResult<Void> printDaySettle(SaleSummaryCountParam param) {
        if (param.getShopId() == null) {
            param.setShopId(StpKit.USER.getShopId());
        }
        financeStsService.printDaySettle(param);
        return CzgResult.success();
    }
}
