package com.sckw.payment.controller.operate;

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.vo.SettlementLogistics;
import com.sckw.payment.model.vo.excel.SettlementLogisticsExportVo;
import com.sckw.payment.model.vo.req.SettlementReq;
import com.sckw.payment.service.operateService.SettlementLogisticsService;
import io.swagger.v3.oas.annotations.Operation;
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.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;
import java.util.stream.Collectors;

/**
 * 结算-物流订单
 *
 * @author Aick Spt
 * @date 2023-07-18 16:34
 */
@Tag(name = "运营物流结算管理", description = "提供运营端物流订单结算相关操作接口")
@RestController
@Slf4j
@RequestMapping("/settlementLogistics")
public class SettlementLogisticsController {
    @Resource
    private SettlementLogisticsService settlementLogisticsService;

    /**
     * 结算列表
     *
     * @param settlementReq 查询参数
     * @return 结算列表
     */
    @Operation(summary = "物流结算列表", description = "查询物流订单结算列表")
    @PostMapping("list")
    public HttpResult list(@RequestBody @Valid SettlementReq settlementReq) {
        return HttpResult.ok(settlementLogisticsService.list(settlementReq));
    }

    /**
     * 结算统计
     *
     * @param settlementReq 查询参数
     * @return 统计结果
     */
    @Operation(summary = "物流结算统计", description = "统计物流订单结算数据")
    @PostMapping("count")
    public HttpResult count(@RequestBody @Valid SettlementReq settlementReq) {
        return HttpResult.ok(settlementLogisticsService.count(settlementReq));
    }

    /**
     * 导出结算列表
     *
     * @param response      HTTP响应
     * @param settlementReq 查询参数
     * @return 操作结果
     */
    @Operation(summary = "导出物流结算列表", description = "导出物流订单结算列表数据")
    @PostMapping("export")
    public HttpResult export(HttpServletResponse response, @RequestBody @Valid SettlementReq settlementReq) {
        settlementReq.setUnitType(LogisticsUnitType.SHIPPER);
        settlementReq.setUnitTypeTwo(LogisticsUnitType.CARRIER);
        List<SettlementLogistics> list = settlementLogisticsService.exportList(settlementReq);
        if (CollectionUtils.isEmpty(list)) {
            return HttpResult.error("暂无数据，请确认");
        }
        List<SettlementLogisticsExportVo> collect = list.stream().map(a -> {
            SettlementLogisticsExportVo settlementLogisticsVo = new SettlementLogisticsExportVo();
            BeanUtils.copyProperties(a, settlementLogisticsVo);
            return settlementLogisticsVo;
        }).collect(Collectors.toList());
        ExcelUtil.downData(response, SettlementLogisticsExportVo.class, collect);
        return null;
    }

}