package com.sckw.payment.controller;

import com.sckw.core.annotation.RepeatSubmit;
import com.sckw.core.model.vo.TableTop;
import com.sckw.core.web.response.HttpResult;
import com.sckw.excel.utils.ExcelUtil;
import com.sckw.payment.model.constant.LedgerEnum;
import com.sckw.payment.model.constant.LogisticsUnitType;
import com.sckw.payment.model.dto.LedgerLogisticsDto;
import com.sckw.payment.model.vo.req.*;
import com.sckw.payment.model.vo.res.LedgerLogisticsVo;
import com.sckw.payment.service.KwpLedgerLogisticsService;
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 jakarta.validation.constraints.NotBlank;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 对账-物流订单
 *
 * @author xucaiqin
 */
@Tag(name = "物流对账管理", description = "提供物流对账相关操作接口")
@Validated
@RestController
@RequestMapping("/kwpLedgerLogistics")
public class KwpLedgerLogisticsController {
    @Resource
    private KwpLedgerLogisticsService kwpLedgerLogisticsService;

    /**
     * 物流对账单-发起对账(保存草稿)（承运方）
     *
     * @param logisticsReq 保存物流对账单参数
     * @return 操作结果
     */
    @Operation(summary = "发起对账(保存草稿)", description = "物流对账单-发起对账(保存草稿)（承运方）")
    @RepeatSubmit(interval = 1000,message ="前方拥堵，请稍后尝试")
    @PostMapping("sendDraft")
    public HttpResult sendDraft(@RequestBody @Valid LogisticsSendReq logisticsReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.sendLedgerDraft(logisticsReq));
    }

    /**
     * 物流对账单-发起对账（承运方）
     *
     * @param logisticsReq 保存物流对账单参数
     * @return 操作结果
     */
    @Operation(summary = "发起对账", description = "物流对账单-发起对账（承运方）")
    @PostMapping("send")
    @RepeatSubmit(interval = 1000,message ="前方拥堵，请稍后尝试")
    public HttpResult send(@RequestBody @Valid LogisticsSendReq logisticsReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.sendLedger(logisticsReq));
    }

    /**
     * 托运方物流对账单列表查询
     *
     * @param logisticsReq 物流订单查询参数
     * @return 对账单列表
     */
    @Operation(summary = "托运方物流对账单列表查询", description = "托运方物流对账单列表查询")
    @PostMapping("shipperList")
    public HttpResult shipperList(@RequestBody LogisticsReq logisticsReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.shipperList(logisticsReq));
    }

    /**
     * 托运方对账单关联订单
     *
     * @param ledgerListReq 对账单id
     * @return 关联订单列表
     */
    @Operation(summary = "托运方对账单关联订单", description = "托运方对账单关联订单")
    @GetMapping("shipperOrderList")
    public HttpResult shipperOrderList(@Valid LedgerListReq ledgerListReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.orderList(ledgerListReq));
    }

    /**
     * 承运方物流对账单列表查询（物流公司、收钱方）
     *
     * @param logisticsReq 物流订单查询参数
     * @return 对账单列表
     */
    @Operation(summary = "承运方物流对账单列表查询", description = "承运方物流对账单列表查询（物流公司、收钱方）")
    @PostMapping("carrierList")
    public HttpResult carrierList(@RequestBody LogisticsReq logisticsReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.carrierList(logisticsReq));
    }

    /**
     * 承运方对账单关联订单
     *
     * @param ledgerListReq 对账单id
     * @return 关联订单列表
     */
    @Operation(summary = "承运方对账单关联订单", description = "承运方对账单关联订单")
    @GetMapping("carrierOrderList")
    public HttpResult carrierOrderList(@Valid LedgerListReq ledgerListReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.orderList(ledgerListReq));
    }

    /**
     * 承运方-查询物流对账单详情
     *
     * @param id 物流对账单id
     * @return 物流对账单详情
     */
    @Operation(summary = "承运方-查询物流对账单详情", description = "承运方-查询物流对账单详情")
    @Parameter(name = "id", description = "物流对账单id", required = true)
    @GetMapping("queryCarrierDetail")
    public HttpResult queryCarrierDetail(@RequestParam("id") @NotBlank(message = "物流对账单id不能为空") String id) {
        return HttpResult.ok(kwpLedgerLogisticsService.queryDetail(Long.valueOf(id), LogisticsUnitType.SHIPPER));
    }

    /**
     * 托运方-查询物流对账单详情
     *
     * @param id 物流对账单id
     * @return 物流对账单详情
     */
    @Operation(summary = "托运方-查询物流对账单详情", description = "托运方-查询物流对账单详情")
    @Parameter(name = "id", description = "物流对账单id", required = true)
    @GetMapping("queryShipperDetail")
    public HttpResult queryDetail(@RequestParam("id") @NotBlank(message = "物流对账单id不能为空") String id) {
        return HttpResult.ok(kwpLedgerLogisticsService.queryDetail(Long.valueOf(id), LogisticsUnitType.CARRIER));
    }

    /**
     * 修改物流对账单详情（修改物流对账单接口使用） 仅发起对账方支持修改
     *
     * @param id 物流对账单id
     * @return 物流对账单详情
     */
    @Operation(summary = "修改物流对账单详情", description = "修改物流对账单详情（修改物流对账单接口使用） 仅发起对账方支持修改")
    @Parameter(name = "id", description = "物流对账单id", required = true)
    @GetMapping("queryUpdate")
    public HttpResult queryUpdate(@RequestParam("id") @NotBlank(message = "物流对账单id不能为空") String id) {
        return HttpResult.ok(kwpLedgerLogisticsService.queryUpdate(Long.valueOf(id)));
    }

    /**
     * 托运方对账单统计
     *
     * @param logisticsReq 物流订单查询参数
     * @return 物流对账单统计数据
     */
    @Operation(summary = "托运方对账单统计", description = "托运方对账单统计")
    @PostMapping("shipperCount")
    public HttpResult shipperCount(@RequestBody LogisticsReq logisticsReq) {
        logisticsReq.setUnitType(LogisticsUnitType.CARRIER);
        logisticsReq.setUnitTypeTwo(LogisticsUnitType.SHIPPER);
        List<TableTop> tableTops = kwpLedgerLogisticsService.orderCount(logisticsReq);
        List<Integer> re = new ArrayList<>() {{
            add(LedgerEnum.SAVE.getStatus());
            add(LedgerEnum.REVOCATION.getStatus());
        }};
        return HttpResult.ok(tableTops.stream().filter(a -> !re.contains(a.getValue())).collect(Collectors.toList()));
    }

    /**
     * 承运方对账单统计
     *
     * @param logisticsReq 物流订单查询参数
     * @return 物流对账单统计数据
     */
    @Operation(summary = "承运方对账单统计", description = "承运方对账单统计")
    @PostMapping("carrierCount")
    public HttpResult carrierCount(@RequestBody LogisticsReq logisticsReq) {
        logisticsReq.setUnitType(LogisticsUnitType.SHIPPER);
        logisticsReq.setUnitTypeTwo(LogisticsUnitType.CARRIER);
        return HttpResult.ok(kwpLedgerLogisticsService.orderCount(logisticsReq));
    }

    /**
     * 删除对账单
     * 释放关联的订单（只有删除操作才释放订单）
     *
     * @param id 物流对账单id
     * @return 操作结果
     */
    @Operation(summary = "删除对账单", description = "删除对账单，释放关联的订单（只有删除操作才释放订单）")
    @Parameter(name = "id", description = "物流对账单id", required = true)
    @DeleteMapping("delete")
    public HttpResult delete(@RequestParam("id") @NotBlank(message = "物流对账单id不能为空") String id) {
        return HttpResult.ok(kwpLedgerLogisticsService.remove(Long.valueOf(id)));
    }

    /**
     * 物流对账单-撤回对账
     *
     * @param id 物流对账单id
     * @return 操作结果
     */
    @Operation(summary = "撤回对账", description = "物流对账单-撤回对账")
    @Parameter(name = "id", description = "物流对账单id", required = true)
    @PostMapping("back")
    public HttpResult back(@RequestParam("id") @NotBlank(message = "物流对账单id不能为空") String id) {
        return HttpResult.ok(kwpLedgerLogisticsService.backOrder(Long.valueOf(id)));
    }

    /**
     * 物流对账单-对账确认
     *
     * @param confirmReq 确认参数
     * @return 操作结果
     */
    @Operation(summary = "对账确认", description = "物流对账单-对账确认")
    @PostMapping("confirm")
    public HttpResult confirm(@RequestBody @Valid LedgerConfirmReq confirmReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.confirmOrder(confirmReq));
    }

    /**
     * 物流对账单-驳回查询
     *
     * @param id 物流对账单id
     * @return 驳回信息
     */
    @Operation(summary = "驳回查询", description = "物流对账单-驳回查询")
    @Parameter(name = "id", description = "物流对账单id", required = true)
    @GetMapping("queryBack")
    public HttpResult queryBack(@RequestParam("id") @NotBlank(message = "物流对账单id不能为空") String id) {
        return HttpResult.ok(kwpLedgerLogisticsService.queryBack(Long.valueOf(id)));
    }

    /**
     * 物流对账单-驳回
     *
     * @param ledgerReq 驳回参数
     * @return 操作结果
     */
    @Operation(summary = "驳回对账单", description = "物流对账单-驳回")
    @PostMapping("doBack")
    public HttpResult doBack(@RequestBody @Valid LedgerReq ledgerReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.doBack(ledgerReq));
    }

    /**
     * 物流对账单-对账完成
     * <p>
     * 生成结算单
     * </p>
     *
     * @param ledgerReq 对账完成参数
     * @return 操作结果
     */
    @Operation(summary = "对账完成", description = "物流对账单-对账完成，生成结算单")
    @PostMapping("success")
    public HttpResult orderSuccess(@RequestBody @Valid LedgerSuccessReq ledgerReq) {
        return HttpResult.ok(kwpLedgerLogisticsService.orderSuccess(ledgerReq));
    }

    /**
     * 托运方-导出对账单
     *
     * @param response HTTP响应
     * @param logisticsReq 查询参数
     * @return 操作结果
     */
    @Operation(summary = "托运方-导出对账单", description = "托运方-导出")
    @PostMapping("exportShipper")
    public HttpResult exportShipper(HttpServletResponse response, @RequestBody LogisticsReq logisticsReq) {
        logisticsReq.setUnitType(LogisticsUnitType.CARRIER);
        logisticsReq.setUnitTypeTwo(LogisticsUnitType.SHIPPER);
        List<LedgerLogisticsDto> list = kwpLedgerLogisticsService.exportList(logisticsReq, logisticsReq.getIdList());
        if (CollectionUtils.isEmpty(list)) {
            return HttpResult.error("暂无数据，请确认");
        }
        List<LedgerLogisticsVo> collect = list.stream().map(a -> {
            LedgerLogisticsVo ledgerLogisticsVo = new LedgerLogisticsVo();
            BeanUtils.copyProperties(a, ledgerLogisticsVo);
            return ledgerLogisticsVo;
        }).collect(Collectors.toList());

        ExcelUtil.downData(response, LedgerLogisticsVo.class, collect);
        return null;
    }

    /**
     * 承运方-导出对账单
     *
     * @param response HTTP响应
     * @param logisticsReq 查询参数
     * @return 操作结果
     */
    @Operation(summary = "承运方-导出对账单", description = "承运方-导出对账单")
    @PostMapping("exportCarrier")
    public HttpResult exportCarrier(HttpServletResponse response, @RequestBody LogisticsReq logisticsReq) {
        logisticsReq.setUnitType(LogisticsUnitType.SHIPPER);
        logisticsReq.setUnitTypeTwo(LogisticsUnitType.CARRIER);
        List<LedgerLogisticsDto> list = kwpLedgerLogisticsService.exportList(logisticsReq, logisticsReq.getIdList());
        if (CollectionUtils.isEmpty(list)) {
            return HttpResult.error("暂无数据，请确认");
        }
        List<LedgerLogisticsVo> collect = list.stream().map(a -> {
            LedgerLogisticsVo ledgerLogisticsVo = new LedgerLogisticsVo();
            BeanUtils.copyProperties(a, ledgerLogisticsVo);
            return ledgerLogisticsVo;
        }).collect(Collectors.toList());

        ExcelUtil.downData(response, LedgerLogisticsVo.class, collect);
        return null;
    }
}