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 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.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * app端接口
 * 对账-交易订单
 *
 * @author xucaiqin
 */
@Tag(name = "App交易对账管理", description = "提供App端交易对账相关操作接口")
@RestController
@RequestMapping("/appKwpLedgerTrade")
@Validated
public class AppKwpLedgerTradeController {
    @Resource
    private KwpLedgerTradeService kwpLedgerTradeService;


    /**
     * 贸易对账单-发起对账(保存草稿)
     *
     * @param tradeSendReq 对账单参数
     * @return 操作结果
     */
    @Operation(summary = "发起对账(保存草稿)", description = "贸易对账单-发起对账(保存草稿)")
    @PostMapping("sendDraft")
    public HttpResult sendDraft(@RequestBody @Valid TradeSendReq tradeSendReq) {
        return HttpResult.ok(kwpLedgerTradeService.sendLedgerDraft(tradeSendReq));
    }

    /**
     * 贸易对账单-发起对账
     *
     * @param tradeSendReq 对账单参数
     * @return 操作结果
     */
    @Operation(summary = "发起对账", description = "贸易对账单-发起对账")
    @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 @Valid TradeReq tradeReq) {
        return HttpResult.ok(kwpLedgerTradeService.sellList(tradeReq));
    }

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

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

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

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

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

    /**
     * 销售-查询贸易对账单详情
     *
     * @param id 对账单id
     * @return 对账单详情
     */
    @Operation(summary = "销售方贸易对账单详情", description = "销售-查询贸易对账单详情")
    @Parameter(name = "id", description = "对账单ID", required = true)
    @GetMapping("querySellDetail")
    public HttpResult querySellDetail(@NotBlank(message = "id不能为空") @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(@NotBlank(message = "id不能为空") @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(@NotBlank(message = "id不能为空") @RequestParam("id") String id) {
        return HttpResult.ok(kwpLedgerTradeService.queryUpdate(Long.valueOf(id)));
    }


    /**
     * 删除对账单
     *
     * @param id 对账单ID
     * @return 操作结果
     */
    @Operation(summary = "删除对账单", description = "删除对账单")
    @DeleteMapping("delete")
    public HttpResult delete(@RequestBody @Valid Id id) {
        return HttpResult.ok(kwpLedgerTradeService.remove(id.getIdLong()));
    }

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

}