package com.sckw.transport.controller;


import com.sckw.core.web.response.BaseResult;
import com.sckw.core.web.response.result.PageDataResult;
import com.sckw.transport.api.model.param.ParkingWalletFeeFreezeParam;
import com.sckw.transport.api.model.vo.ParkingWalletFeeFreezeResult;
import com.sckw.transport.model.ParkingWalletFeeSaveParam;
import com.sckw.transport.model.ParkingWalletFeeTotalQueryParam;
import com.sckw.transport.model.param.ParkingWalletFeeBalanceQueryParam;
import com.sckw.transport.model.param.ParkingWalletFeeBalanceResp;
import com.sckw.transport.model.param.ParkingWalletFeeQueryParam;
import com.sckw.transport.model.param.ParkingWalletFeeResp;
import com.sckw.transport.service.ParkingWalletFeeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**s
 * Author: donglang
 * Time: 2026-01-05
 * Des: 服务费
 * Version: 1.0
 */

@RestController
@RequestMapping("/parking/wallet")
@RequiredArgsConstructor
@Tag(name = "服务费接口", description = "服务费接口")
@Slf4j
public class ParkingWalletFeeController {

    private final ParkingWalletFeeService parkingWalletFeeService;


    /**
     * 查询服务费汇总
     *
     * @param param
     * @return
     */
    @Operation(summary = "查询服务费汇总", description = "查询服务费汇总")
    @PostMapping("/queryTotalServerFee")
    public BaseResult<ParkingWalletFeeResp> queryTotalServerFee(@RequestBody @Valid ParkingWalletFeeTotalQueryParam param){
        ParkingWalletFeeResp gatekeeperOrder = parkingWalletFeeService.queryTotalServerFee(param);
        return BaseResult.success(gatekeeperOrder);
    }

    /**
     * 分页查询服务费
     *
     * @param param
     * @return
     */
    @Operation(summary = "分页查询服务费", description = "分页查询服务费")
    @PostMapping("/pageQueryServerFee")
    public BaseResult<PageDataResult<ParkingWalletFeeResp>> pageQueryServerFee(@RequestBody @Valid ParkingWalletFeeQueryParam param){
        PageDataResult<ParkingWalletFeeResp> gatekeeperOrderList = parkingWalletFeeService.pageQueryServerFee(param);
        return BaseResult.success(gatekeeperOrderList);
    }

    /**
     * 人工录入服务费
     *
     * @param param
     * @return
     */
    @Operation(summary = "人工录入服务费", description = "人工录入服务费")
    @PostMapping("/saveServerFee")
    public BaseResult saveServerFee(@RequestBody @Valid ParkingWalletFeeSaveParam param){
        parkingWalletFeeService.saveServerFee(param);
        return BaseResult.success();
    }

    /**
     * 分页查询服务费明细
     *
     * @param param
     * @return
     */
    @Operation(summary = "分页查询服务费明细", description = "分页查询服务费明细")
    @PostMapping("/pageQueryServerFeeDetails")
    public BaseResult<PageDataResult<ParkingWalletFeeBalanceResp>> pageQueryServerFeeDetails(@RequestBody @Valid ParkingWalletFeeBalanceQueryParam param){
        PageDataResult<ParkingWalletFeeBalanceResp> gatekeeperOrderList = parkingWalletFeeService.pageQueryServerFeeDetails(param);
        return BaseResult.success(gatekeeperOrderList);
    }

    /**
     * 交易订单下单时应用收费策略并冻结服务费余额。
     *
     * @param param 冻结参数
     * @return 收费策略应用结果
     */
    @PostMapping("/remote/apply-charge-strategy-freeze")
    public BaseResult<ParkingWalletFeeFreezeResult> applyChargeStrategyFreeze(@RequestBody @Valid ParkingWalletFeeFreezeParam param) {
        try {
            return BaseResult.success(parkingWalletFeeService.applyChargeStrategyFreeze(param));
        } catch (Exception e) {
            log.error("Feign调用服务费冻结失败，orderNo:{}，proEntId:{}，supEntId:{}",
                    param == null ? null : param.getOrderNo(),
                    param == null ? null : param.getProEntId(),
                    param == null ? null : param.getSupEntId(), e);
            return BaseResult.failed(e.getMessage());
        }
    }

    /**
     * 交易订单审核拒绝时解冻服务费并返还余额。
     *
     * @param param 解冻参数
     * @return 解冻结果
     */
    @PostMapping("/remote/unfreeze-charge-strategy")
    public BaseResult<ParkingWalletFeeFreezeResult> unfreezeChargeStrategy(@RequestBody @Valid ParkingWalletFeeFreezeParam param) {
        try {
            return BaseResult.success(parkingWalletFeeService.unfreezeChargeStrategy(param));
        } catch (Exception e) {
            log.error("Feign调用服务费解冻失败，orderNo:{}，proEntId:{}，supEntId:{}",
                    param == null ? null : param.getOrderNo(),
                    param == null ? null : param.getProEntId(),
                    param == null ? null : param.getSupEntId(), e);
            return BaseResult.failed(e.getMessage());
        }
    }

    /**
     * 交易订单完结时结算服务费。
     *
     * @param param 结算参数
     * @return 结算结果
     */
    @PostMapping("/remote/settle-charge-strategy")
    public BaseResult<ParkingWalletFeeFreezeResult> settleChargeStrategy(@RequestBody @Valid ParkingWalletFeeFreezeParam param) {
        try {
            log.info("Feign调用服务费完结结算，orderNo:{}，proEntId:{}，supEntId:{}",
                    param == null ? null : param.getOrderNo(),
                    param == null ? null : param.getProEntId(),
                    param == null ? null : param.getSupEntId());
            return BaseResult.success(parkingWalletFeeService.settleChargeStrategy(param));
        } catch (Exception e) {
            log.error("Feign调用服务费完结结算失败，orderNo:{}，proEntId:{}，supEntId:{}",
                    param == null ? null : param.getOrderNo(),
                    param == null ? null : param.getProEntId(),
                    param == null ? null : param.getSupEntId(), e);
            return BaseResult.failed(e.getMessage());
        }
    }

    /**
     * 查询采购方服务费余额。
     *
     * @param proEntId 采购方企业ID
     * @param supEntId 供应商企业ID，可为空
     * @return 服务费余额
     */
    @GetMapping("/remote/service-fee-balance")
    public BaseResult<BigDecimal> queryServiceFeeBalance(@RequestParam("proEntId") Long proEntId,
                                                         @RequestParam(value = "supEntId", required = false) Long supEntId) {
        try {
            if (proEntId == null) {
                return BaseResult.failed("采购方企业ID不能为空");
            }
            return BaseResult.success(parkingWalletFeeService.queryPurchaserServiceFeeBalance(proEntId, supEntId));
        } catch (Exception e) {
            log.error("Feign调用查询服务费余额失败，proEntId:{}，supEntId:{}", proEntId, supEntId, e);
            return BaseResult.failed(e.getMessage());
        }
    }


}
