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.TradeUnitType;
import com.sckw.payment.model.dto.LedgerTradeDto;
import com.sckw.payment.model.vo.req.*;
import com.sckw.payment.model.vo.res.LedgerTradeVo;
import com.sckw.payment.service.KwpLedgerTradeService;
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 = "提供交易对账相关操作接口")
@RestController
@RequestMapping("/kwpLedgerTrade")
@Validated
public class KwpLedgerTradeController {
    @Resource
    private KwpLedgerTradeService kwpLedgerTradeService;


    /**
     * 贸易对账单-发起对账(保存草稿)
     *
     * @param tradeSendReq 对账单参数
     * @return 操作结果
     */
    @Operation(summary = "发起对账(保存草稿)", description = "贸易对账单-发起对账(保存草稿)")
    @RepeatSubmit(interval = 1000,message ="前方拥堵，请稍后尝试")
    @PostMapping("sendDraft")
    public HttpResult sendDraft(@RequestBody @Valid TradeSendReq tradeSendReq) {
        return HttpResult.ok(kwpLedgerTradeService.sendLedgerDraft(tradeSendReq));
    }

    /**
     * 贸易对账单-发起对账
     *
     * @param tradeSendReq 对账单参数
     * @return 操作结果
     */
    @Operation(summary = "发起对账", description = "贸易对账单-发起对账")
    @RepeatSubmit(interval = 1000,message ="前方拥堵，请稍后尝试")
    @PostMapping("send")
    public HttpResult send(@RequestBody @Valid TradeSendReq tradeSendReq) {
        return HttpResult.ok(kwpLedgerTradeService.sendLedger(tradeSendReq));
    }

    /**
     * 贸易对账单列表查询 销售方
     *
     * @param tradeReq 查询参数
     * @return 对账单列表
     */
    @Operation(summary = "销售方贸易对账单列表", description = "贸易对账单列表查询 销售方")
    @PostMapping("sellList")
    public HttpResult sellList(@RequestBody TradeReq tradeReq) {
        return HttpResult.ok(kwpLedgerTradeService.sellList(tradeReq));
    }

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

    /**
     * 贸易对账单列表查询 采购方
     *
     * @param tradeReq 查询参数
     * @return 对账单列表
     */
    @Operation(summary = "采购方贸易对账单列表", description = "贸易对账单列表查询 采购方")
    @PostMapping("purchaseList")
    public HttpResult purchaseList(@RequestBody TradeReq tradeReq) {
        return HttpResult.ok(kwpLedgerTradeService.purchaseList(tradeReq));
    }

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

    /**
     * 销售-查询贸易对账单详情
     *
     * @param id 对账单id
     * @return 对账单详情
     */
    @Operation(summary = "销售方贸易对账单详情", description = "销售-查询贸易对账单详情")
    @Parameter(name = "id", description = "对账单ID", required = true)
    @GetMapping("querySellDetail")
    public HttpResult querySellDetail(@RequestParam("id") String id) {
        return HttpResult.ok(kwpLedgerTradeService.queryDetail(Long.valueOf(id), TradeUnitType.PURCHASE));
    }

    /**
     * 采购-查询贸易对账单详情
     *
     * @param id 对账单id
     * @return 对账单详情
     */
    @Operation(summary = "采购方贸易对账单详情", description = "采购-查询贸易对账单详情")
    @Parameter(name = "id", description = "对账单ID", required = true)
    @GetMapping("queryPurchaseDetail")
    public HttpResult queryPurchaseDetail(@RequestParam("id") String id) {
        return HttpResult.ok(kwpLedgerTradeService.queryDetail(Long.valueOf(id), TradeUnitType.SELL));
    }

    /**
     * 修改贸易对账单详情（修改物流对账单接口使用）
     *
     * @param id 对账单id
     * @return 对账单详情
     */
    @Operation(summary = "修改贸易对账单详情", description = "修改贸易对账单详情（修改物流对账单接口使用）")
    @Parameter(name = "id", description = "对账单ID", required = true)
    @GetMapping("queryUpdate")
    public HttpResult queryUpdate(@RequestParam("id") String id) {
        return HttpResult.ok(kwpLedgerTradeService.queryUpdate(Long.valueOf(id)));
    }

    /**
     * 贸易订单数分类统计(销售方)
     *
     * @param tradeReq 查询参数
     * @return 统计结果
     */
    @Operation(summary = "销售方贸易订单分类统计", description = "贸易订单数分类统计(销售方)")
    @PostMapping("sellCount")
    public HttpResult sellCount(@RequestBody TradeReq tradeReq) {
        tradeReq.setUnitType(TradeUnitType.PURCHASE);
        tradeReq.setUnitTypeTwo(TradeUnitType.SELL);
        return HttpResult.ok(kwpLedgerTradeService.orderCount(tradeReq));
    }

    /**
     * 贸易订单数分类统计(采购方)
     *
     * @param tradeReq 查询参数
     * @return 统计结果
     */
    @Operation(summary = "采购方贸易订单分类统计", description = "贸易订单数分类统计(采购方)")
    @PostMapping("purchaseCount")
    public HttpResult purchaseCount(@RequestBody TradeReq tradeReq) {
        tradeReq.setUnitType(TradeUnitType.SELL);
        tradeReq.setUnitTypeTwo(TradeUnitType.PURCHASE);
        List<TableTop> tableTops = kwpLedgerTradeService.orderCount(tradeReq);
        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 id 对账单ID
     * @return 操作结果
     */
    @Operation(summary = "删除对账单", description = "删除对账单")
    @Parameter(name = "id", description = "对账单ID", required = true)
    @DeleteMapping("delete")
    public HttpResult delete(@NotBlank(message = "id不能为空") String id) {
        return HttpResult.ok(kwpLedgerTradeService.remove(Long.valueOf(id)));
    }

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

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

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

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

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

    /**
     * 导出销售方对账单
     *
     * @param response HTTP响应
     * @param tradeReq 查询参数
     * @return 操作结果
     */
    @Operation(summary = "导出销售方对账单", description = "导出销售方对账单")
    @PostMapping("sellExport")
    public HttpResult sellExport(HttpServletResponse response, @RequestBody TradeReq tradeReq) {
        tradeReq.setUnitType(TradeUnitType.PURCHASE);
        tradeReq.setUnitTypeTwo(TradeUnitType.SELL);
        List<LedgerTradeDto> list = kwpLedgerTradeService.exportList(tradeReq, tradeReq.getIdList());
        if (CollectionUtils.isEmpty(list)) {
            return HttpResult.error("暂无数据，请确认");
        }
        List<LedgerTradeVo> collect = list.stream().map(a -> {
            LedgerTradeVo ledgerTradeVo = new LedgerTradeVo();
            BeanUtils.copyProperties(a, ledgerTradeVo);
            return ledgerTradeVo;
        }).collect(Collectors.toList());
        ExcelUtil.downData(response, LedgerTradeVo.class, collect);
        return null;
    }

    /**
     * 导出采购方对账单
     *
     * @param response HTTP响应
     * @param tradeReq 查询参数
     * @return 操作结果
     */
    @Operation(summary = "导出采购方对账单", description = "导出采购方对账单")
    @PostMapping("purchaseExport")
    public HttpResult purchaseExport(HttpServletResponse response, @RequestBody TradeReq tradeReq) {
        tradeReq.setUnitType(TradeUnitType.SELL);
        tradeReq.setUnitTypeTwo(TradeUnitType.PURCHASE);
        List<LedgerTradeDto> list = kwpLedgerTradeService.exportList(tradeReq, tradeReq.getIdList());
        if (CollectionUtils.isEmpty(list)) {
            return HttpResult.error("暂无数据，请确认");
        }
        List<LedgerTradeVo> collect = list.stream().map(a -> {
            LedgerTradeVo ledgerTradeVo = new LedgerTradeVo();
            BeanUtils.copyProperties(a, ledgerTradeVo);
            return ledgerTradeVo;
        }).collect(Collectors.toList());
        ExcelUtil.downData(response, LedgerTradeVo.class, collect);
        return null;
    }
}