package com.sckw.payment.controller.app;

import com.sckw.core.web.response.HttpResult;
import com.sckw.payment.model.constant.TradeUnitType;
import com.sckw.payment.model.dto.Id;
import com.sckw.payment.model.vo.req.*;
import com.sckw.payment.service.KwpLedgerTradeService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * app端接口
 * 对账-交易订单
 *
 * @author xucaiqin
 */
@RestController
@RequestMapping("/appKwpLedgerTrade")
@Validated
public class AppKwpLedgerTradeController {
    @Resource
    private KwpLedgerTradeService kwpLedgerTradeService;


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

    /**
     * 贸易对账单-发起对账
     *
     * @param tradeSendReq
     * @return
     */
    @PostMapping("send")
    public HttpResult send(@RequestBody @Valid TradeSendReq 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("sellSum")
    public HttpResult sellSum(@RequestBody @Valid TradeReq tradeReq) {
        return HttpResult.ok(kwpLedgerTradeService.sellSum(tradeReq));
    }

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

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

    /**
     * 贸易对账单统计 采购方
     *
     * @param tradeReq
     * @return
     */
    @PostMapping("purchaseSum")
    public HttpResult purchaseSum(@RequestBody @Valid TradeReq tradeReq) {
        return HttpResult.ok(kwpLedgerTradeService.purchaseSum(tradeReq));
    }

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

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

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

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


    /**
     * 删除对账单
     *
     * @param id
     * @return
     */
    @DeleteMapping("delete")
    public HttpResult delete(@RequestBody @Valid Id id) {
        return HttpResult.ok(kwpLedgerTradeService.remove(id.getIdLong()));
    }

    /**
     * 贸易对账单-撤回对账
     *
     * @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));
    }

}
