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 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
 */
@Validated
@RestController
@RequestMapping("/appKwpSettlementTrade")
public class AppKwpSettlementTradeController {

    @Resource
    private KwpSettlementTradeService kwpSettlementTradeService;

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

    /**
     * 销售分页列表
     *
     * @param settlementReq
     * @return
     */
    @PostMapping(name = "收款-销售结算单列表", 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
     */
    @PostMapping(name = "收款-表头统计数量", 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
     * @return
     */
    @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
     */
    @PostMapping(name = "付款-采购结算单列表", 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
     */
    @PostMapping(name = "收款-表头统计数量", 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
     */
    @GetMapping( "purchaseDetail")
    public HttpResult purchaseDetail(@NotBlank(message = "结算单id不能为空")@RequestParam("id") String id) {
        return HttpResult.ok(kwpSettlementTradeService.queryDetail(Long.parseLong(id),TradeUnitType.SELL));
    }

}
