package com.sckw.payment.controller.app;

import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.core.web.response.HttpResult;
import com.sckw.payment.model.constant.TradeUnitType;
import com.sckw.payment.model.vo.req.SettlementTradeReq;
import com.sckw.payment.service.KwpSettlementTradeService;
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.*;

/**
 * 结算-交易订单
 *
 * @author xucaiqin
 */
@Tag(name = "App交易结算管理", description = "提供App端交易订单结算相关操作接口")
@Validated
@RestController
@RequestMapping("/appKwpSettlementTrade")
public class AppKwpSettlementTradeController {

    @Resource
    private KwpSettlementTradeService kwpSettlementTradeService;

    //   ========= 收款 销售 =========

    /**
     * 销售分页列表
     *
     * @param settlementReq 查询参数
     * @return 结算单列表
     */
    @Operation(summary = "收款-销售结算单列表", description = "收款-销售结算单列表")
    @PostMapping(path = "pageSellList")
    public HttpResult pageListCollection(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.setUnitType(TradeUnitType.PURCHASE);
        settlementReq.setUnitTypeTwo(TradeUnitType.SELL);
        settlementReq.check();
        return HttpResult.ok(kwpSettlementTradeService.pageListCollection(settlementReq));
    }

    /**
     * 待结算、部分结算、全部结算
     *
     * @param settlementReq 查询参数
     * @return 统计结果
     */
    @Operation(summary = "收款-表头统计数量", description = "待结算、部分结算、全部结算统计")
    @PostMapping(path = "getSellCount")
    public HttpResult getCount(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.setUnitType(TradeUnitType.PURCHASE);
        settlementReq.setUnitTypeTwo(TradeUnitType.SELL);
        settlementReq.setEntId(LoginUserHolder.getEntId());
        settlementReq.check();
        return HttpResult.ok(kwpSettlementTradeService.getCount(settlementReq));
    }

    /**
     * 销售方查询结算单详情
     * @param id 结算单ID
     * @return 结算单详情
     */
    @Operation(summary = "销售方结算单详情", description = "销售方查询结算单详情")
    @Parameter(name = "id", description = "结算单ID", required = true)
    @GetMapping("sellDetail")
    public HttpResult sellDetail(@NotBlank(message = "结算单id不能为空") @RequestParam("id") String id) {
        return HttpResult.ok(kwpSettlementTradeService.queryDetail(Long.parseLong(id), TradeUnitType.PURCHASE));
    }
    
    //   ========= 付款 采购 =========

    /**
     * 采购分页列表
     *
     * @param settlementReq 查询参数
     * @return 结算单列表
     */
    @Operation(summary = "付款-采购结算单列表", description = "付款-采购结算单列表")
    @PostMapping(path = "pagePurchaseList")
    public HttpResult pageListPayment(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        settlementReq.setUnitType(TradeUnitType.SELL);
        settlementReq.setUnitTypeTwo(TradeUnitType.PURCHASE);
        return HttpResult.ok(kwpSettlementTradeService.pageListCollection(settlementReq));
    }

    /**
     * 待结算、部分结算、全部结算
     *
     * @param settlementReq 查询参数
     * @return 统计结果
     */
    @Operation(summary = "付款-表头统计数量", description = "待结算、部分结算、全部结算统计")
    @PostMapping(path = "getPurchaseCount")
    public HttpResult getPurchaseCount(@RequestBody @Valid SettlementTradeReq settlementReq) {
        settlementReq.check();
        settlementReq.setUnitType(TradeUnitType.SELL);
        settlementReq.setUnitTypeTwo(TradeUnitType.PURCHASE);
        settlementReq.setEntId(LoginUserHolder.getEntId());
        return HttpResult.ok(kwpSettlementTradeService.getCount(settlementReq));
    }
    
    /**
     * 采购方查询结算单详情
     * @param id 结算单id
     * @return 结算单详情
     */
    @Operation(summary = "采购方结算单详情", description = "采购方查询结算单详情")
    @Parameter(name = "id", description = "结算单ID", required = true)
    @GetMapping("purchaseDetail")
    public HttpResult purchaseDetail(@NotBlank(message = "结算单id不能为空") @RequestParam("id") String id) {
        return HttpResult.ok(kwpSettlementTradeService.queryDetail(Long.parseLong(id), TradeUnitType.SELL));
    }

}