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 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
 */
@RestController
@RequestMapping("/kwpLedgerTrade")
@Validated
public class KwpLedgerTradeController {
    @Resource
    private KwpLedgerTradeService kwpLedgerTradeService;


    /**
     * 贸易对账单-发起对账(保存草稿)
     *
     * @param tradeSendReq
     * @return
     */
    @RepeatSubmit(interval = 3000,message ="两次请求间隔未超过3秒")
    @PostMapping("sendDraft")
    public HttpResult sendDraft(@RequestBody @Valid TradeSendReq tradeSendReq) {
        return HttpResult.ok(kwpLedgerTradeService.sendLedgerDraft(tradeSendReq));
    }

    /**
     * 贸易对账单-发起对账
     *
     * @param tradeSendReq
     * @return
     */
    @RepeatSubmit(interval = 3000,message ="两次请求间隔未超过3秒")
    @PostMapping("send")
    public HttpResult send(@RequestBody @Valid TradeSendReq tradeSendReq) {
        return HttpResult.ok(kwpLedgerTradeService.sendLedger(tradeSendReq));
    }

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

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

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

    /**
     * 对账单关联订单
     *
     * @param ledgerListReq 对账单id
     * @return
     */
    @GetMapping("purchaseOrderList")
    public HttpResult purchaseOrderList(@Valid LedgerListReq ledgerListReq) {
        return HttpResult.ok(kwpLedgerTradeService.orderList(ledgerListReq));
    }

    /**
     * 销售-查询贸易对账单详情
     *
     * @param id 对账单id
     * @return
     */
    @GetMapping("querySellDetail")
    public HttpResult querySellDetail(@RequestParam("id") String id) {
        return HttpResult.ok(kwpLedgerTradeService.queryDetail(Long.valueOf(id), TradeUnitType.PURCHASE));
    }

    /**
     * 采购-查询贸易对账单详情
     *
     * @param id
     * @return
     */
    @GetMapping("queryPurchaseDetail")
    public HttpResult queryPurchaseDetail(@RequestParam("id") String id) {
        return HttpResult.ok(kwpLedgerTradeService.queryDetail(Long.valueOf(id), TradeUnitType.SELL));
    }

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

    /**
     * 贸易订单数分类统计
     *
     * @return
     */
    @PostMapping("sellCount")
    public HttpResult sellCount(@RequestBody TradeReq tradeReq) {
        tradeReq.setUnitType(TradeUnitType.PURCHASE);
        tradeReq.setUnitTypeTwo(TradeUnitType.SELL);
        return HttpResult.ok(kwpLedgerTradeService.orderCount(tradeReq));
    }

    /**
     * 贸易订单数分类统计
     *
     * @return
     */
    @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
     * @return
     */
    @DeleteMapping("delete")
    public HttpResult delete(@NotBlank(message = "id不能为空") String id) {
        return HttpResult.ok(kwpLedgerTradeService.remove(Long.valueOf(id)));
    }

    /**
     * 贸易对账单-撤回对账
     *
     * @param id
     * @return
     */
    @PostMapping("back")
    public HttpResult back(@NotBlank(message = "id不能为空") @RequestParam("id") String id) {
        return HttpResult.ok(kwpLedgerTradeService.backOrder(Long.valueOf(id)));
    }

    /**
     * 贸易对账单-对账确认
     *
     * @param confirmReq
     * @return
     */
    @PostMapping("confirm")
    public HttpResult confirm(@RequestBody @Valid LedgerConfirmReq confirmReq) {
        return HttpResult.ok(kwpLedgerTradeService.confirmOrder(confirmReq));
    }

    /**
     * 贸易对账单-驳回查询
     *
     * @param id
     * @return
     */
    @GetMapping("queryBack")
    public HttpResult queryBack(@NotBlank(message = "id不能为空") @RequestParam("id") String id) {
        return HttpResult.ok(kwpLedgerTradeService.queryBack(Long.valueOf(id)));
    }

    /**
     * 贸易对账单-驳回对账
     *
     * @param ledgerReq
     * @return
     */
    @PostMapping("doBack")
    public HttpResult doBack(@RequestBody @Valid LedgerReq ledgerReq) {
        return HttpResult.ok(kwpLedgerTradeService.doBack(ledgerReq));
    }

    /**
     * 贸易对账单-对账完成
     * <p>
     * 生成结算单
     * </p>
     *
     * @param ledgerReq
     * @return
     */
    @PostMapping("success")
    public HttpResult orderSuccess(@RequestBody @Valid LedgerSuccessReq ledgerReq) {
        return HttpResult.ok(kwpLedgerTradeService.orderSuccess(ledgerReq));
    }

    /**
     * 导出
     *
     * @param response
     * @param tradeReq
     * @return
     */
    @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
     * @param tradeReq
     * @return
     */
    @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;
    }
}
