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.dto.SettlementTradeDto;
import com.sckw.payment.model.vo.req.SettlementReq;
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;

    //   ========= 收款 销售 =========
    @PostMapping(name = "收款-销售结算单列表", path = "pageListCollection")
    public HttpResult pageListCollection(@RequestBody @Valid SettlementReq settlementReq) {
        return HttpResult.ok(kwpSettlementTradeService.pageListCollection(settlementReq));
    }

    @GetMapping(name = "收款-销售结算单列表头部汇总数量", path = "getCountListCollection")
    public HttpResult getCountListCollection(@RequestBody @Valid SettlementReq settlementReq) {
        return HttpResult.ok(kwpSettlementTradeService.getCountListCollection(settlementReq));
    }

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

    @GetMapping(name = "收款-销售结算订单详情", path = "detailCollection")
    public HttpResult detailCollection(@RequestParam("id") Long id) {
        return HttpResult.ok(kwpSettlementTradeService.detailCollection(id));
    }


    //   ========= 付款 采购 =========
    @PostMapping(name = "付款-采购结算单列表", path = "pageListPayment")
    public HttpResult pageListPayment(@RequestBody @Valid SettlementReq settlementReq) {
        return HttpResult.ok(kwpSettlementTradeService.pageListPayment(settlementReq));
    }

    @GetMapping(name = "收款-物流结算订单列表头部汇总数量", path = "getCountListPayment")
    public HttpResult getCountListPayment(@RequestBody @Valid SettlementReq settlementReq) {
        return HttpResult.ok(kwpSettlementTradeService.getCountListPayment(settlementReq));
    }

    @PostMapping(name = "付款-根据ids导出采购结算单列表", path = "exportPayment")
    public HttpResult exportPayment(HttpServletResponse response, @RequestBody @Valid SettlementReq settlementReq) {
        List<SettlementTradeDto> list;
        if (CollectionUtils.isEmpty(settlementReq.getIds())) {
            PageRes<SettlementTradeDto> pageResult = kwpSettlementTradeService.pageListPayment(settlementReq);
            list = pageResult.getList();
        } else {
            list = kwpSettlementTradeService.selectList(settlementReq.getIds());
        }
        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<SettlementTradeDto> 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;
    }


}
