package com.sckw.payment.controller;

import com.sckw.core.exception.BusinessException;
import com.sckw.core.model.page.PageRes;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.web.response.HttpResult;
import com.sckw.excel.utils.ExcelUtil;
import com.sckw.payment.model.dto.ILedger;
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.List;
import java.util.stream.Collectors;

/**
 * 对账-交易订单
 *
 * @author xucaiqin
 */
@RestController
@RequestMapping("/kwpLedgerTrade")
@Validated
public class KwpLedgerTradeController {
    @Resource
    private KwpLedgerTradeService kwpLedgerTradeService;

    private void fill(TradeSendReq tradeSendReq) {
        String ids = tradeSendReq.getIds();
        List<Long> longs = StringUtils.splitStrToList(ids, Long.class);
        if (CollectionUtils.isEmpty(longs)) {
            throw new BusinessException("销售订单不能为空");
        }
        tradeSendReq.setOrders(longs);
    }

    /**
     * 贸易对账单-发起对账(保存草稿)
     *
     * @param tradeSendReq
     * @return
     */
    @PostMapping("sendDraft")
    public HttpResult sendDraft(@RequestBody @Valid TradeSendReq tradeSendReq) {
        fill(tradeSendReq);
        return HttpResult.ok(kwpLedgerTradeService.sendLedgerDraft(tradeSendReq));
    }

    /**
     * 贸易对账单-发起对账
     *
     * @param tradeSendReq
     * @return
     */
    @PostMapping("send")
    public HttpResult send(@RequestBody @Valid TradeSendReq tradeSendReq) {
        fill(tradeSendReq);
        return HttpResult.ok(kwpLedgerTradeService.sendLedger(tradeSendReq));
    }

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

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

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

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

    /**
     * 贸易订单数分类统计
     *
     * @return
     */
    @GetMapping("sellCount")
    public HttpResult sellCount() {
        return HttpResult.ok(kwpLedgerTradeService.orderCount(1));
    }

    /**
     * 贸易订单数分类统计
     *
     * @return
     */
    @GetMapping("purchaseCount")
    public HttpResult purchaseCount() {
        return HttpResult.ok(kwpLedgerTradeService.orderCount(2));
    }

    /**
     * 删除对账单
     *
     * @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 @Valid TradeReq tradeReq) {
        List<ILedger> list;
        String ids = tradeReq.getIds();
        List<Long> longs = StringUtils.splitStrToList(ids, Long.class);

        if (CollectionUtils.isEmpty(longs)) {
            PageRes<ILedger> pageResult = kwpLedgerTradeService.sellList(tradeReq);
            list = pageResult.getList();
        } else {
            list = kwpLedgerTradeService.selectSellList(longs);
        }

        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 @Valid TradeReq tradeReq) {
        List<ILedger> list;
        String ids = tradeReq.getIds();
        List<Long> longs = StringUtils.splitStrToList(ids, Long.class);

        if (CollectionUtils.isEmpty(longs)) {
            PageRes<ILedger> pageResult = kwpLedgerTradeService.purchaseList(tradeReq);
            list = pageResult.getList();
        } else {
            list = kwpLedgerTradeService.selectPurchaseList(longs);
        }

        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;
    }
}
