package com.sckw.payment.controller;

import com.sckw.core.web.response.HttpResult;
import com.sckw.excel.utils.ExcelUtil;
import com.sckw.payment.model.constant.LogisticsUnitType;
import com.sckw.payment.model.dto.SettlementLogisticsDto;
import com.sckw.payment.model.vo.req.SettlementReq;
import com.sckw.payment.model.vo.res.SettlementLogisticsVo;
import com.sckw.payment.service.KwpSettlementLogisticsService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
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 Aick Spt
 * @date 2023-07-18 16:34
 */
@RestController
@Slf4j
@RequestMapping(name = "结算-物流运费订单", path = "/kwpSettlementLogistics")
public class KwpSettlementLogisticsController {

    @Resource
    private KwpSettlementLogisticsService kwpSettlementLogisticsService;

    //   ========= 运费收款 承运方 =========
    @PostMapping(name = "运费收款-物流订单列表", path = "pageListCollection")
    public HttpResult pageSelectCollection(@RequestBody @Valid SettlementReq settlementReq) {
        return HttpResult.ok(kwpSettlementLogisticsService.pageSelectCollection(settlementReq));
    }

    @PostMapping(name = "运费收款-物流订单各状态列表总数", path = "getCountListCollection")
    public HttpResult getCountListCollection(@RequestBody @Valid SettlementReq settlementReq) {
        return HttpResult.ok(kwpSettlementLogisticsService.getCountListCollection(settlementReq));
    }

    /**
     * 承运方导出
     *
     * @param response
     * @param settlementReq
     * @return
     */
    @PostMapping(name = "运费收款-根据条件导出物流订单表数据", path = "exportCollection")
    public HttpResult exportCollection(HttpServletResponse response, @RequestBody @Valid SettlementReq settlementReq) {
        settlementReq.setUnitType(LogisticsUnitType.SHIPPER);
        settlementReq.setUnitTypeTwo(LogisticsUnitType.CARRIER);
        List<SettlementLogisticsDto> list;
        list = kwpSettlementLogisticsService.selectList(settlementReq);
        return this.export(response, list);
    }

    @GetMapping(name = "运费收款-物流订单详情", path = "detailCollection")
    public HttpResult detailCollection(@RequestParam("id") Long id) {
        return HttpResult.ok(kwpSettlementLogisticsService.detailCollection(id));
    }


    //   ========= 运费付款 托运方 =========
    @PostMapping(name = "运费付款-物流订单列表", path = "pageListPayment")
    public HttpResult pageSelectPayment(@RequestBody @Valid SettlementReq settlementReq) {
        return HttpResult.ok(kwpSettlementLogisticsService.pageSelectPayment(settlementReq));
    }

    @PostMapping(name = "运费付款-物流订单各状态列表总数", path = "getCountListPayment")
    public HttpResult getCountListPayment(@RequestBody @Valid SettlementReq settlementReq) {
        return HttpResult.ok(kwpSettlementLogisticsService.getCountListPayment(settlementReq));
    }

    /**
     * 托运方导出
     *
     * @param response
     * @param settlementReq
     * @return
     */
    @PostMapping(name = "运费付款-根据条件导出物流订单表数据", path = "exportPayment")
    public HttpResult exportPayment(HttpServletResponse response, @RequestBody @Valid SettlementReq settlementReq) {
        settlementReq.setUnitType(LogisticsUnitType.CARRIER);
        settlementReq.setUnitTypeTwo(LogisticsUnitType.SHIPPER);
        List<SettlementLogisticsDto> list;
        list = kwpSettlementLogisticsService.selectList(settlementReq);
        return this.export(response, list);
    }

    @GetMapping(name = "物流订单详情", path = "detailPayment")
    public HttpResult detailPayment(@RequestParam("id") Long id) {
        return HttpResult.ok(kwpSettlementLogisticsService.detailPayment(id));
    }

    /**
     * 导出
     *
     * @author Aick Spt
     * @date 2023-07-24 15:49
     */
    private HttpResult export(HttpServletResponse response, List<SettlementLogisticsDto> 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;
    }


}
