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 io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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
 */
@Tag(name = "物流结算管理", description = "提供物流订单结算相关操作接口")
@RestController
@Slf4j
@RequestMapping(path = "/kwpSettlementLogistics")
public class KwpSettlementLogisticsController {

    @Resource
    private KwpSettlementLogisticsService kwpSettlementLogisticsService;

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

    @Operation(summary = "运费收款-物流订单各状态列表总数", description = "统计运费收款的物流订单各状态列表总数（承运方）")
    @PostMapping(path = "getCountListCollection")
    public HttpResult getCountListCollection(@RequestBody @Valid SettlementReq settlementReq) {
        return HttpResult.ok(kwpSettlementLogisticsService.getCountListCollection(settlementReq));
    }

    /**
     * 承运方导出
     *
     * @param response      HTTP响应
     * @param settlementReq 查询参数
     * @return 操作结果
     */
    @Operation(summary = "运费收款-导出物流订单表数据", description = "根据条件导出运费收款的物流订单表数据（承运方）")
    @PostMapping(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);
    }

    @Operation(summary = "运费收款-物流订单详情", description = "查询运费收款的物流订单详情（承运方）")
    @Parameter(name = "id", description = "订单ID", required = true)
    @GetMapping(path = "detailCollection")
    public HttpResult detailCollection(@RequestParam("id") Long id) {
        return HttpResult.ok(kwpSettlementLogisticsService.detailCollection(id));
    }


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

    @Operation(summary = "运费付款-物流订单各状态列表总数", description = "统计运费付款的物流订单各状态列表总数（托运方）")
    @PostMapping(path = "getCountListPayment")
    public HttpResult getCountListPayment(@RequestBody @Valid SettlementReq settlementReq) {
        return HttpResult.ok(kwpSettlementLogisticsService.getCountListPayment(settlementReq));
    }

    /**
     * 托运方导出
     *
     * @param response      HTTP响应
     * @param settlementReq 查询参数
     * @return 操作结果
     */
    @Operation(summary = "运费付款-导出物流订单表数据", description = "根据条件导出运费付款的物流订单表数据（托运方）")
    @PostMapping(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);
    }

    @Operation(summary = "物流订单详情", description = "查询物流订单详情（托运方）")
    @Parameter(name = "id", description = "订单ID", required = true)
    @GetMapping(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;
    }


}