package com.sckw.payment.controller;

import com.sckw.core.model.page.PageRes;
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.dto.ISettlement;
import com.sckw.payment.model.vo.req.SettlementTradeReq;
import com.sckw.payment.model.vo.res.SettlementLogisticsVo;
import com.sckw.payment.service.KwpSettlementTradeService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 结算-交易订单
 *
 * @author xucaiqin
 */
@RestController
@RequestMapping(name = "结算-销售采购订单", path = "/kwpSettlementTrade")
public class KwpSettlementTradeController {

    @Resource
    private KwpSettlementTradeService kwpSettlementTradeService;

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

    /**
     * 销售分页列表
     *
     * @param settlementReq
     * @return
     */
    @PostMapping(name = "收款-销售结算单列表", path = "pageSellList")
    public HttpResult pageListCollection(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.setUnitType(TradeUnitType.PURCHASE);
        settlementReq.check();
        return HttpResult.ok(kwpSettlementTradeService.pageListCollection(settlementReq));
    }

    /**
     * 待结算、部分结算、全部结算
     *
     * @param settlementReq
     * @return
     */
    @PostMapping(name = "收款-表头统计数量", path = "getSellCount")
    public HttpResult getCount(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.setUnitType(TradeUnitType.PURCHASE);
        settlementReq.check();
        return HttpResult.ok(kwpSettlementTradeService.getCount(settlementReq));
    }

    /**
     * 表尾金额结算数据
     *
     * @param settlementReq
     * @return
     */
    @PostMapping(name = "收款-表尾金额结算数据", path = "getSellSum")
    public HttpResult getSellSum(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        settlementReq.setUnitType(TradeUnitType.PURCHASE);
        return HttpResult.ok(kwpSettlementTradeService.getCountListCollection(settlementReq));
    }

    @PostMapping(name = "收款-根据ids导出销售结算单列表", path = "exportCollection")
    public HttpResult exportCollection(HttpServletResponse response, @RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        List<ISettlement> list;
        settlementReq.setUnitType(TradeUnitType.PURCHASE);
        if (CollectionUtils.isEmpty(settlementReq.getIdList())) {
            PageRes<ISettlement> pageResult = kwpSettlementTradeService.pageListCollection(settlementReq);
            list = pageResult.getList();
        } else {
            list = kwpSettlementTradeService.selectList(settlementReq.getIdList(),TradeUnitType.PURCHASE);
        }
        return this.export(response, list);
    }



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

    /**
     * 采购分页列表
     *
     * @param settlementReq
     * @return
     */
    @PostMapping(name = "付款-采购结算单列表", path = "pagePurchaseList")
    public HttpResult pageListPayment(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        settlementReq.setUnitType(TradeUnitType.SELL);
        return HttpResult.ok(kwpSettlementTradeService.pageListCollection(settlementReq));
    }

    /**
     * 待结算、部分结算、全部结算
     *
     * @param settlementReq
     * @return
     */
    @PostMapping(name = "收款-表头统计数量", path = "getPurchaseCount")
    public HttpResult getPurchaseCount(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        settlementReq.setUnitType(TradeUnitType.SELL);
        return HttpResult.ok(kwpSettlementTradeService.getCount(settlementReq));
    }

    /**
     * 表尾金额结算数据
     *
     * @param settlementReq
     * @return
     */
    @PostMapping(name = "收款-物流结算订单列表头部汇总数量", path = "getPurchaseSum")
    public HttpResult getCountListPayment(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        settlementReq.setUnitType(TradeUnitType.SELL);
        return HttpResult.ok(kwpSettlementTradeService.getCountListCollection(settlementReq));
    }

    @PostMapping(name = "付款-根据ids导出采购结算单列表", path = "exportPayment")
    public HttpResult exportPayment(HttpServletResponse response, @RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        List<ISettlement> list;
        settlementReq.setUnitType(TradeUnitType.SELL);
        if (CollectionUtils.isEmpty(settlementReq.getIdList())) {
            PageRes<ISettlement> pageResult = kwpSettlementTradeService.pageListCollection(settlementReq);
            list = pageResult.getList();
        } else {
            list = kwpSettlementTradeService.selectList(settlementReq.getIdList(),TradeUnitType.SELL);
        }
        return this.export(response, list);
    }

    @GetMapping(name = "付款-采购结算订单详情", path = "detailPayment")
    public HttpResult detailPayment(@RequestParam("id") Long id) {
        return HttpResult.ok(kwpSettlementTradeService.detailPayment(id));
    }

    /**
     * 导出
     *
     * @author Aick Spt
     * @date 2023-07-24 15:49
     */
    private HttpResult export(HttpServletResponse response, List<ISettlement> list) {
        if (CollectionUtils.isEmpty(list)) {
            return HttpResult.error("没有可导出的数据");
        }
        List<SettlementLogisticsVo> collect = list.stream().map(a -> {
            SettlementLogisticsVo settlementLogisticsVo = new SettlementLogisticsVo();
            BeanUtils.copyProperties(a, settlementLogisticsVo);
            return settlementLogisticsVo;
        }).collect(Collectors.toList());
        ExcelUtil.downData(response, SettlementLogisticsVo.class, collect);
        return null;
    }


}
