package com.sckw.payment.controller;

import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.core.web.response.HttpResult;
import com.sckw.excel.utils.ExcelUtil;
import com.sckw.payment.model.constant.TradeUnitType;
import com.sckw.payment.model.vo.req.SettlementTradeReq;
import com.sckw.payment.model.vo.res.SettlementTradeVo;
import com.sckw.payment.service.KwpSettlementTradeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 结算-交易订单
 *
 * @author xucaiqin
 */
@Tag(name = "交易结算管理", description = "提供交易订单结算相关操作接口")
@RestController
@RequestMapping(path = "/kwpSettlementTrade")
public class KwpSettlementTradeController {

    @Resource
    private KwpSettlementTradeService kwpSettlementTradeService;

    //   ========= 收款 销售 =========

    /**
     * 销售分页列表
     *
     * @param settlementReq 查询参数
     * @return 结算单列表
     */
    @Operation(summary = "销售结算单列表", description = "查询销售结算单分页列表")
    @PostMapping("pageSellList")
    public HttpResult pageListCollection(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.setUnitType(TradeUnitType.PURCHASE);
        settlementReq.setUnitTypeTwo(TradeUnitType.SELL);
        settlementReq.check();
        return HttpResult.ok(kwpSettlementTradeService.pageListCollection(settlementReq));
    }

    /**
     * 待结算、部分结算、全部结算
     *
     * @param settlementReq 查询参数
     * @return 统计结果
     */
    @Operation(summary = "销售结算单统计", description = "统计销售结算单状态（待结算、部分结算、全部结算）")
    @PostMapping("getSellCount")
    public HttpResult getCount(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.setUnitType(TradeUnitType.PURCHASE);
        settlementReq.setUnitTypeTwo(TradeUnitType.SELL);
        settlementReq.setEntId(LoginUserHolder.getEntId());
        settlementReq.check();
        return HttpResult.ok(kwpSettlementTradeService.getCount(settlementReq));
    }

    /**
     * 表尾金额结算数据
     *
     * @param settlementReq 查询参数
     * @return 金额统计
     */
    @Operation(summary = "销售结算金额统计", description = "统计销售结算单表尾金额结算数据")
    @PostMapping("getSellSum")
    public HttpResult getSellSum(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        settlementReq.setUnitType(TradeUnitType.PURCHASE);
        settlementReq.setUnitTypeTwo(TradeUnitType.SELL);
        settlementReq.setEntId(LoginUserHolder.getEntId());
        return HttpResult.ok(kwpSettlementTradeService.getCountListCollection(settlementReq));
    }

    /**
     * 导出销售结算单列表
     *
     * @param response      HTTP响应
     * @param settlementReq 查询参数
     * @return 操作结果
     */
    @Operation(summary = "导出销售结算单", description = "导出销售结算单列表")
    @PostMapping("exportCollection")
    public HttpResult exportCollection(HttpServletResponse response, @RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        settlementReq.setUnitType(TradeUnitType.PURCHASE);
        settlementReq.setUnitTypeTwo(TradeUnitType.SELL);
        settlementReq.setEntId(LoginUserHolder.getEntId());
        List<SettlementTradeVo> list = kwpSettlementTradeService.exportList(settlementReq, settlementReq.getIdList());
        return this.export(response, list);
    }


    //   ========= 付款 采购 =========

    /**
     * 采购分页列表
     *
     * @param settlementReq 查询参数
     * @return 结算单列表
     */
    @Operation(summary = "采购结算单列表", description = "查询采购结算单分页列表")
    @PostMapping("pagePurchaseList")
    public HttpResult pageListPayment(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        settlementReq.setUnitType(TradeUnitType.SELL);
        settlementReq.setUnitTypeTwo(TradeUnitType.PURCHASE);
        return HttpResult.ok(kwpSettlementTradeService.pageListCollection(settlementReq));
    }

    /**
     * 待结算、部分结算、全部结算
     *
     * @param settlementReq 查询参数
     * @return 统计结果
     */
    @Operation(summary = "采购结算单统计", description = "统计采购结算单状态（待结算、部分结算、全部结算）")
    @PostMapping("getPurchaseCount")
    public HttpResult getPurchaseCount(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        settlementReq.setUnitType(TradeUnitType.SELL);
        settlementReq.setUnitTypeTwo(TradeUnitType.PURCHASE);
        settlementReq.setEntId(LoginUserHolder.getEntId());
        return HttpResult.ok(kwpSettlementTradeService.getCount(settlementReq));
    }

    /**
     * 表尾金额结算数据
     *
     * @param settlementReq 查询参数
     * @return 金额统计
     */
    @Operation(summary = "采购结算金额统计", description = "统计采购结算单表尾金额结算数据")
    @PostMapping("getPurchaseSum")
    public HttpResult getCountListPayment(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        settlementReq.setUnitType(TradeUnitType.SELL);
        settlementReq.setUnitTypeTwo(TradeUnitType.PURCHASE);
        settlementReq.setEntId(LoginUserHolder.getEntId());
        return HttpResult.ok(kwpSettlementTradeService.getCountListCollection(settlementReq));
    }

    /**
     * 导出采购结算单列表
     *
     * @param response      HTTP响应
     * @param settlementReq 查询参数
     * @return 操作结果
     */
    @Operation(summary = "导出采购结算单", description = "导出采购结算单列表")
    @PostMapping("exportPayment")
    public HttpResult exportPayment(HttpServletResponse response, @RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        settlementReq.setUnitType(TradeUnitType.SELL);
        settlementReq.setUnitTypeTwo(TradeUnitType.PURCHASE);
        settlementReq.setEntId(LoginUserHolder.getEntId());
        List<SettlementTradeVo> list = kwpSettlementTradeService.exportList(settlementReq, settlementReq.getIdList());
        return this.export(response, list);
    }

    /**
     * 导出
     *
     * @author Aick Spt
     * @date 2023-07-24 15:49
     */
    private HttpResult export(HttpServletResponse response, List<SettlementTradeVo> list) {
        if (CollectionUtils.isEmpty(list)) {
            return HttpResult.error("暂无数据，请确认");
        }
        ExcelUtil.downData(response, SettlementTradeVo.class, list);
        return null;
    }


}