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 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
 */
@RestController
@RequestMapping(name = "结算-销售采购订单", path = "/kwpSettlementTrade")
public class KwpSettlementTradeController {

    @Resource
    private KwpSettlementTradeService kwpSettlementTradeService;

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

    /**
     * 销售分页列表
     *
     * @param settlementReq
     * @return
     */
    @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
     */
    @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
     */
    @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
     * @param settlementReq
     * @return
     */
    @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
     */
    @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
     */
    @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
     */
    @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
     * @param settlementReq
     * @return
     */
    @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;
    }


}
