|
@@ -7,19 +7,20 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.sckw.core.common.enums.enums.ErrorCodeEnum;
|
|
import com.sckw.core.common.enums.enums.ErrorCodeEnum;
|
|
|
import com.sckw.core.exception.BusinessPlatfromException;
|
|
import com.sckw.core.exception.BusinessPlatfromException;
|
|
|
|
|
+import com.sckw.core.model.constant.Global;
|
|
|
|
|
+import com.sckw.core.model.enums.ParkingChangeStrategyEnum;
|
|
|
import com.sckw.core.utils.CollectionUtils;
|
|
import com.sckw.core.utils.CollectionUtils;
|
|
|
|
|
+import com.sckw.core.web.context.LoginUserHolder;
|
|
|
import com.sckw.core.web.response.result.PageDataResult;
|
|
import com.sckw.core.web.response.result.PageDataResult;
|
|
|
import com.sckw.system.api.RemoteSystemService;
|
|
import com.sckw.system.api.RemoteSystemService;
|
|
|
import com.sckw.system.api.model.dto.res.EntCacheResDto;
|
|
import com.sckw.system.api.model.dto.res.EntCacheResDto;
|
|
|
-import com.sckw.transport.model.KwtParkingWalletFee;
|
|
|
|
|
-import com.sckw.transport.model.KwtParkingWalletFeeBalance;
|
|
|
|
|
-import com.sckw.transport.model.ParkingWalletFeeSaveParam;
|
|
|
|
|
-import com.sckw.transport.model.ParkingWalletFeeTotalQueryParam;
|
|
|
|
|
|
|
+import com.sckw.transport.api.model.param.ParkingWalletFeeFreezeParam;
|
|
|
|
|
+import com.sckw.transport.api.model.vo.ParkingWalletFeeFreezeResult;
|
|
|
|
|
+import com.sckw.transport.model.*;
|
|
|
import com.sckw.transport.model.enuma.WalletTypEnum;
|
|
import com.sckw.transport.model.enuma.WalletTypEnum;
|
|
|
-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.model.param.*;
|
|
|
|
|
+import com.sckw.transport.repository.KwtParkingChangeStrategyRepository;
|
|
|
|
|
+import com.sckw.transport.repository.KwtParkingChangeStrategyUnitRepository;
|
|
|
import com.sckw.transport.repository.KwtParkingWalletFeeBalanceRepository;
|
|
import com.sckw.transport.repository.KwtParkingWalletFeeBalanceRepository;
|
|
|
import com.sckw.transport.repository.KwtParkingWalletFeeRepository;
|
|
import com.sckw.transport.repository.KwtParkingWalletFeeRepository;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
@@ -29,8 +30,8 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
-import java.util.Collections;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
|
|
+import java.math.RoundingMode;
|
|
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -48,9 +49,16 @@ public class ParkingWalletFeeService {
|
|
|
private final KwtParkingWalletFeeRepository parkingWalletFeeRepository;
|
|
private final KwtParkingWalletFeeRepository parkingWalletFeeRepository;
|
|
|
private final KwtParkingWalletFeeBalanceRepository parkingWalletFeeBalanceRepository;
|
|
private final KwtParkingWalletFeeBalanceRepository parkingWalletFeeBalanceRepository;
|
|
|
|
|
|
|
|
|
|
+ private final KwtParkingChangeStrategyRepository parkingChangeStrategyRepository;
|
|
|
|
|
+ private final KwtParkingChangeStrategyUnitRepository parkingChangeStrategyUnitRepository;
|
|
|
|
|
+
|
|
|
@DubboReference(version = "1.0.0", group = "design", check = false, timeout = 6000)
|
|
@DubboReference(version = "1.0.0", group = "design", check = false, timeout = 6000)
|
|
|
RemoteSystemService remoteSystemService;
|
|
RemoteSystemService remoteSystemService;
|
|
|
|
|
|
|
|
|
|
+ private static final BigDecimal ZERO_AMOUNT = BigDecimal.ZERO;
|
|
|
|
|
+ private static final int STRATEGY_OPEN = 1;
|
|
|
|
|
+ private static final int TRADE_TYPE_FREEZE = 4;
|
|
|
|
|
+ private static final int TRADE_TYPE_UNFREEZE = 5;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 查询汇总服务费
|
|
* 查询汇总服务费
|
|
@@ -298,4 +306,402 @@ public class ParkingWalletFeeService {
|
|
|
return entCacheResDto.getFirmName();
|
|
return entCacheResDto.getFirmName();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 查询服务费余额、本次预计服务费与最大可购买数量
|
|
|
|
|
+ * 规则:
|
|
|
|
|
+ * 1. 服务费余额 = 采购方当前服务费余额
|
|
|
|
|
+ * 2. 本次预计服务费 = 采购数量 * 采购方当前配置的收费策略单价
|
|
|
|
|
+ * 3. 最大可购买数量 = 收费策略开启时,服务费余额 / 策略单价;未开启时不返回余额约束
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param param 查询参数
|
|
|
|
|
+ * @return 服务费余额、本次预计服务费与最大可购买数量
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParkingWalletFeeEstimateResp queryEstimateServiceFee(ParkingWalletFeeEstimateQueryParam param) {
|
|
|
|
|
+ Long entId = LoginUserHolder.getEntId();
|
|
|
|
|
+ log.info("服务费预估开始,entId:{}, param:{}", entId, param);
|
|
|
|
|
+ if (param.getPurchaseQuantity() == null || param.getPurchaseQuantity().compareTo(ZERO_AMOUNT) < 0) {
|
|
|
|
|
+ log.warn("服务费预估参数异常,采购数量非法,entId:{}, purchaseQuantity:{}",
|
|
|
|
|
+ entId, param.getPurchaseQuantity());
|
|
|
|
|
+ throw new BusinessPlatfromException(ErrorCodeEnum.PARAM_ERROR, "采购数量不能为空且不能小于0");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 查询采购方当前服务费余额
|
|
|
|
|
+ BigDecimal serviceFeeBalance = queryServiceFeeBalance(entId, null);
|
|
|
|
|
+
|
|
|
|
|
+ // 查询当前企业生效中的收费策略(仅取状态开启的最新一条)
|
|
|
|
|
+ KwtParkingChargeStrategy currentStrategy = queryCurrentEnableStrategy(entId);
|
|
|
|
|
+ BigDecimal currentStrategyUnitFee = currentStrategy == null || currentStrategy.getMethod() == null
|
|
|
|
|
+ ? ZERO_AMOUNT : currentStrategy.getMethod();
|
|
|
|
|
+ log.info("服务费预估策略信息,entId:{}, strategyId:{}, strategyStatus:{}, unitFee:{}",
|
|
|
|
|
+ entId,
|
|
|
|
|
+ currentStrategy == null ? null : currentStrategy.getId(),
|
|
|
|
|
+ currentStrategy == null ? null : currentStrategy.getStatus(),
|
|
|
|
|
+ currentStrategyUnitFee);
|
|
|
|
|
+
|
|
|
|
|
+ // 本次预计服务费 = 采购数量 * 策略单价,金额统一保留2位
|
|
|
|
|
+ BigDecimal estimatedServiceFee = param.getPurchaseQuantity().multiply(currentStrategyUnitFee)
|
|
|
|
|
+ .setScale(2, RoundingMode.HALF_UP);
|
|
|
|
|
+
|
|
|
|
|
+ // 收费策略开关开启时,按服务费余额计算最大可购买数量
|
|
|
|
|
+ BigDecimal maxPurchaseQuantity = calculateMaxPurchaseQuantity(currentStrategy, currentStrategyUnitFee, serviceFeeBalance);
|
|
|
|
|
+ log.info("服务费预估计算完成,entId:{}, purchaseQuantity:{}, serviceFeeBalance:{}, estimatedServiceFee:{}, maxPurchaseQuantity:{}",
|
|
|
|
|
+ entId, param.getPurchaseQuantity(), serviceFeeBalance, estimatedServiceFee, maxPurchaseQuantity);
|
|
|
|
|
+
|
|
|
|
|
+ ParkingWalletFeeEstimateResp resp = new ParkingWalletFeeEstimateResp();
|
|
|
|
|
+ resp.setServiceFeeBalance(serviceFeeBalance);
|
|
|
|
|
+ resp.setEstimatedServiceFee(estimatedServiceFee);
|
|
|
|
|
+ resp.setMaxPurchaseQuantity(maxPurchaseQuantity);
|
|
|
|
|
+ log.info("服务费预估结束,entId:{}, resp:{}", entId, resp);
|
|
|
|
|
+ return resp;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 贸易订单下单应用收费策略并冻结服务费
|
|
|
|
|
+ * 规则:
|
|
|
|
|
+ * 1. 收费策略开关未开启时,不冻结服务费,订单记录为未应用收费策略
|
|
|
|
|
+ * 2. 收费策略开关开启时,冻结金额=采购数量*策略单价,并记录策略信息
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param param 冻结参数
|
|
|
|
|
+ * @return 收费策略应用结果
|
|
|
|
|
+ */
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public ParkingWalletFeeFreezeResult applyChargeStrategyFreeze(ParkingWalletFeeFreezeParam param) {
|
|
|
|
|
+ log.info("贸易订单应用收费策略开始,param:{}", param);
|
|
|
|
|
+ if (param == null || param.getProEntId() == null) {
|
|
|
|
|
+ throw new BusinessPlatfromException(ErrorCodeEnum.PARAM_ERROR, "采购方企业id不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (param.getPurchaseQuantity() == null || param.getPurchaseQuantity().compareTo(ZERO_AMOUNT) < 0) {
|
|
|
|
|
+ throw new BusinessPlatfromException(ErrorCodeEnum.PARAM_ERROR, "采购数量不能为空且不能小于0");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (param.getOrderNo() == null || param.getOrderNo().isBlank()) {
|
|
|
|
|
+ throw new BusinessPlatfromException(ErrorCodeEnum.PARAM_ERROR, "贸易订单号不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ParkingWalletFeeFreezeResult result = new ParkingWalletFeeFreezeResult();
|
|
|
|
|
+ result.setApplyChargeStrategy(Global.NO);
|
|
|
|
|
+ result.setFreezeAmount(ZERO_AMOUNT);
|
|
|
|
|
+
|
|
|
|
|
+ KwtParkingChargeStrategy currentStrategy = queryCurrentEnableStrategy(param.getProEntId());
|
|
|
|
|
+ if (currentStrategy == null || !Objects.equals(currentStrategy.getStatus(), STRATEGY_OPEN)) {
|
|
|
|
|
+ log.info("收费策略未开启或未匹配,跳过服务费冻结,proEntId:{}, orderNo:{}",
|
|
|
|
|
+ param.getProEntId(), param.getOrderNo());
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ BigDecimal unitFee = currentStrategy.getMethod() == null ? ZERO_AMOUNT : currentStrategy.getMethod();
|
|
|
|
|
+ if (unitFee.compareTo(ZERO_AMOUNT) <= 0) {
|
|
|
|
|
+ log.info("收费策略单价无效,跳过服务费冻结,strategyId:{}, unitFee:{}",
|
|
|
|
|
+ currentStrategy.getId(), unitFee);
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ BigDecimal freezeAmount = param.getPurchaseQuantity().multiply(unitFee).setScale(2, RoundingMode.HALF_UP);
|
|
|
|
|
+ KwtParkingWalletFee walletFee = queryWalletFee(param.getProEntId(), param.getSupEntId());
|
|
|
|
|
+ if (walletFee == null) {
|
|
|
|
|
+ log.warn("服务费账户不存在,proEntId:{}, supEntId:{}, orderNo:{}",
|
|
|
|
|
+ param.getProEntId(), param.getSupEntId(), param.getOrderNo());
|
|
|
|
|
+ throw new BusinessPlatfromException(ErrorCodeEnum.PARAM_ERROR, "采购方服务费账户不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ BigDecimal currentBalance = walletFee.getServiceFeeBalance() == null ? ZERO_AMOUNT : walletFee.getServiceFeeBalance();
|
|
|
|
|
+ if (currentBalance.compareTo(freezeAmount) < 0) {
|
|
|
|
|
+ log.warn("服务费余额不足,proEntId:{}, orderNo:{}, balance:{}, freezeAmount:{}",
|
|
|
|
|
+ param.getProEntId(), param.getOrderNo(), currentBalance, freezeAmount);
|
|
|
|
|
+ throw new BusinessPlatfromException(ErrorCodeEnum.PARAM_ERROR, "服务费余额不足,无法下单");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ BigDecimal currentTradingAmount = walletFee.getTradingAmount() == null ? ZERO_AMOUNT : walletFee.getTradingAmount();
|
|
|
|
|
+ walletFee.setServiceFeeBalance(currentBalance.subtract(freezeAmount));
|
|
|
|
|
+ walletFee.setTradingAmount(currentTradingAmount.add(freezeAmount));
|
|
|
|
|
+ parkingWalletFeeRepository.updateById(walletFee);
|
|
|
|
|
+
|
|
|
|
|
+ saveFreezeWalletBalanceLog(param, walletFee, freezeAmount);
|
|
|
|
|
+
|
|
|
|
|
+ result.setApplyChargeStrategy(Global.YES);
|
|
|
|
|
+ result.setChargeStrategyId(currentStrategy.getId());
|
|
|
|
|
+ result.setChargeStrategyDesc(buildStrategyMethodDesc(currentStrategy));
|
|
|
|
|
+ result.setFreezeAmount(freezeAmount);
|
|
|
|
|
+ result.setServiceFeeBalance(walletFee.getServiceFeeBalance());
|
|
|
|
|
+ log.info("贸易订单应用收费策略完成,orderNo:{}, result:{}", param.getOrderNo(), result);
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 贸易订单审核拒绝解冻服务费
|
|
|
|
|
+ * 规则:根据订单冻结记录解冻,返还服务费余额并写入钱包日志
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param param 解冻参数
|
|
|
|
|
+ * @return 解冻结果(含采购方服务费余额)
|
|
|
|
|
+ */
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public ParkingWalletFeeFreezeResult unfreezeChargeStrategy(ParkingWalletFeeFreezeParam param) {
|
|
|
|
|
+ log.info("贸易订单解冻服务费开始,param:{}", param);
|
|
|
|
|
+ validateUnfreezeParam(param);
|
|
|
|
|
+
|
|
|
|
|
+ ParkingWalletFeeFreezeResult result = new ParkingWalletFeeFreezeResult();
|
|
|
|
|
+ result.setApplyChargeStrategy(Global.YES);
|
|
|
|
|
+
|
|
|
|
|
+ if (existsUnfreezeRecord(param.getOrderNo(), param.getProEntId(), param.getSupEntId())) {
|
|
|
|
|
+ log.info("服务费已解冻,跳过重复处理,orderNo:{}, proEntId:{}", param.getOrderNo(), param.getProEntId());
|
|
|
|
|
+ result.setServiceFeeBalance(queryServiceFeeBalance(param.getProEntId(), param.getSupEntId()));
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ KwtParkingWalletFeeBalance freezeRecord = queryFreezeBalanceRecord(param);
|
|
|
|
|
+ if (freezeRecord == null) {
|
|
|
|
|
+ log.warn("未找到服务费冻结记录,orderNo:{}, proEntId:{}", param.getOrderNo(), param.getProEntId());
|
|
|
|
|
+ throw new BusinessPlatfromException(ErrorCodeEnum.PARAM_ERROR, "服务费冻结记录不存在,无法解冻");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ BigDecimal unfreezeAmount = freezeRecord.getTradeAmount() == null ? ZERO_AMOUNT : freezeRecord.getTradeAmount();
|
|
|
|
|
+ if (unfreezeAmount.compareTo(ZERO_AMOUNT) <= 0) {
|
|
|
|
|
+ throw new BusinessPlatfromException(ErrorCodeEnum.PARAM_ERROR, "服务费冻结金额异常,无法解冻");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ KwtParkingWalletFee walletFee = queryWalletFee(param.getProEntId(), param.getSupEntId());
|
|
|
|
|
+ if (walletFee == null) {
|
|
|
|
|
+ throw new BusinessPlatfromException(ErrorCodeEnum.PARAM_ERROR, "采购方服务费账户不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ BigDecimal currentTradingAmount = walletFee.getTradingAmount() == null ? ZERO_AMOUNT : walletFee.getTradingAmount();
|
|
|
|
|
+ if (currentTradingAmount.compareTo(unfreezeAmount) < 0) {
|
|
|
|
|
+ log.warn("服务费履约中金额不足,无法解冻,orderNo:{}, tradingAmount:{}, unfreezeAmount:{}",
|
|
|
|
|
+ param.getOrderNo(), currentTradingAmount, unfreezeAmount);
|
|
|
|
|
+ throw new BusinessPlatfromException(ErrorCodeEnum.PARAM_ERROR, "服务费履约中金额不足,无法解冻");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ BigDecimal currentBalance = walletFee.getServiceFeeBalance() == null ? ZERO_AMOUNT : walletFee.getServiceFeeBalance();
|
|
|
|
|
+ walletFee.setServiceFeeBalance(currentBalance.add(unfreezeAmount));
|
|
|
|
|
+ walletFee.setTradingAmount(currentTradingAmount.subtract(unfreezeAmount));
|
|
|
|
|
+ parkingWalletFeeRepository.updateById(walletFee);
|
|
|
|
|
+
|
|
|
|
|
+ saveUnfreezeWalletBalanceLog(param, walletFee, unfreezeAmount);
|
|
|
|
|
+ result.setFreezeAmount(unfreezeAmount);
|
|
|
|
|
+ result.setServiceFeeBalance(walletFee.getServiceFeeBalance());
|
|
|
|
|
+ log.info("贸易订单解冻服务费完成,orderNo:{}, unfreezeAmount:{}, balanceAfter:{}",
|
|
|
|
|
+ param.getOrderNo(), unfreezeAmount, walletFee.getServiceFeeBalance());
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 查询采购方服务费余额
|
|
|
|
|
+ */
|
|
|
|
|
+ public BigDecimal queryPurchaserServiceFeeBalance(Long proEntId, Long supEntId) {
|
|
|
|
|
+ return queryServiceFeeBalance(proEntId, supEntId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void validateUnfreezeParam(ParkingWalletFeeFreezeParam param) {
|
|
|
|
|
+ if (param == null || param.getProEntId() == null) {
|
|
|
|
|
+ throw new BusinessPlatfromException(ErrorCodeEnum.PARAM_ERROR, "采购方企业id不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (param.getOrderNo() == null || param.getOrderNo().isBlank()) {
|
|
|
|
|
+ throw new BusinessPlatfromException(ErrorCodeEnum.PARAM_ERROR, "贸易订单号不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean existsUnfreezeRecord(String orderNo, Long proEntId, Long supEntId) {
|
|
|
|
|
+ return parkingWalletFeeBalanceRepository.count(
|
|
|
|
|
+ Wrappers.<KwtParkingWalletFeeBalance>lambdaQuery()
|
|
|
|
|
+ .eq(KwtParkingWalletFeeBalance::getOrderNo, orderNo)
|
|
|
|
|
+ .eq(KwtParkingWalletFeeBalance::getProEntId, proEntId)
|
|
|
|
|
+ .eq(supEntId != null, KwtParkingWalletFeeBalance::getSupEntId, supEntId)
|
|
|
|
|
+ .eq(KwtParkingWalletFeeBalance::getTradeType, TRADE_TYPE_UNFREEZE)
|
|
|
|
|
+ .eq(KwtParkingWalletFeeBalance::getDelFlag, Global.NO)
|
|
|
|
|
+ ) > 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private KwtParkingWalletFeeBalance queryFreezeBalanceRecord(ParkingWalletFeeFreezeParam param) {
|
|
|
|
|
+ return parkingWalletFeeBalanceRepository.getOne(
|
|
|
|
|
+ Wrappers.<KwtParkingWalletFeeBalance>lambdaQuery()
|
|
|
|
|
+ .eq(KwtParkingWalletFeeBalance::getOrderNo, param.getOrderNo())
|
|
|
|
|
+ .eq(KwtParkingWalletFeeBalance::getProEntId, param.getProEntId())
|
|
|
|
|
+ .eq(param.getSupEntId() != null, KwtParkingWalletFeeBalance::getSupEntId, param.getSupEntId())
|
|
|
|
|
+ .eq(KwtParkingWalletFeeBalance::getTradeType, TRADE_TYPE_FREEZE)
|
|
|
|
|
+ .eq(KwtParkingWalletFeeBalance::getDelFlag, Global.NO)
|
|
|
|
|
+ .orderByDesc(KwtParkingWalletFeeBalance::getId)
|
|
|
|
|
+ .last("limit 1"),
|
|
|
|
|
+ false
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void saveUnfreezeWalletBalanceLog(ParkingWalletFeeFreezeParam param,
|
|
|
|
|
+ KwtParkingWalletFee walletFee,
|
|
|
|
|
+ BigDecimal unfreezeAmount) {
|
|
|
|
|
+ Long operatorId = resolveOperatorId(param.getOperatorId());
|
|
|
|
|
+ Date now = new Date();
|
|
|
|
|
+ KwtParkingWalletFeeBalance balanceRecord = new KwtParkingWalletFeeBalance();
|
|
|
|
|
+ balanceRecord.setOrderNo(param.getOrderNo());
|
|
|
|
|
+ balanceRecord.setProEntId(param.getProEntId());
|
|
|
|
|
+ balanceRecord.setSupEntId(param.getSupEntId());
|
|
|
|
|
+ balanceRecord.setTradeType(TRADE_TYPE_UNFREEZE);
|
|
|
|
|
+ balanceRecord.setTradeAmount(unfreezeAmount);
|
|
|
|
|
+ balanceRecord.setServiceFeeBalance(walletFee.getServiceFeeBalance());
|
|
|
|
|
+ balanceRecord.setTradingAmount(walletFee.getTradingAmount());
|
|
|
|
|
+ balanceRecord.setRemark("贸易订单号:" + param.getOrderNo() + ",审核拒绝解冻服务费");
|
|
|
|
|
+ balanceRecord.setDelFlag(Global.NO);
|
|
|
|
|
+ balanceRecord.setCreateUser(operatorId);
|
|
|
|
|
+ balanceRecord.setUpdateUser(operatorId);
|
|
|
|
|
+ balanceRecord.setCreateTime(now);
|
|
|
|
|
+ balanceRecord.setUpdateTime(now);
|
|
|
|
|
+ parkingWalletFeeBalanceRepository.save(balanceRecord);
|
|
|
|
|
+ log.info("服务费解冻钱包日志已写入kwt_parking_wallet_fee_balance,orderNo:{}, proEntId:{}, supEntId:{}, tradeType:{}, tradeAmount:{}, balanceAfter:{}",
|
|
|
|
|
+ param.getOrderNo(), param.getProEntId(), param.getSupEntId(), TRADE_TYPE_UNFREEZE,
|
|
|
|
|
+ unfreezeAmount, walletFee.getServiceFeeBalance());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String buildStrategyMethodDesc(KwtParkingChargeStrategy strategy) {
|
|
|
|
|
+ if (strategy == null || strategy.getMethod() == null) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (Objects.equals(ParkingChangeStrategyEnum.BY_TON.getCode(), strategy.getType())) {
|
|
|
|
|
+ return strategy.getMethod().stripTrailingZeros().toPlainString() + "元/吨";
|
|
|
|
|
+ }
|
|
|
|
|
+ if (Objects.equals(ParkingChangeStrategyEnum.BY_TIME.getCode(), strategy.getType())) {
|
|
|
|
|
+ return strategy.getMethod().stripTrailingZeros().toPlainString() + "元/次";
|
|
|
|
|
+ }
|
|
|
|
|
+ return strategy.getMethod().stripTrailingZeros().toPlainString() + "元/月";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 写入服务费钱包日志(kwt_parking_wallet_fee_balance)
|
|
|
|
|
+ */
|
|
|
|
|
+ private void saveFreezeWalletBalanceLog(ParkingWalletFeeFreezeParam param,
|
|
|
|
|
+ KwtParkingWalletFee walletFee,
|
|
|
|
|
+ BigDecimal freezeAmount) {
|
|
|
|
|
+ Long operatorId = resolveOperatorId(param.getOperatorId());
|
|
|
|
|
+ Date now = new Date();
|
|
|
|
|
+ KwtParkingWalletFeeBalance balanceRecord = new KwtParkingWalletFeeBalance();
|
|
|
|
|
+ balanceRecord.setOrderNo(param.getOrderNo());
|
|
|
|
|
+ balanceRecord.setProEntId(param.getProEntId());
|
|
|
|
|
+ balanceRecord.setSupEntId(param.getSupEntId());
|
|
|
|
|
+ balanceRecord.setTradeType(TRADE_TYPE_FREEZE);
|
|
|
|
|
+ balanceRecord.setTradeAmount(freezeAmount);
|
|
|
|
|
+ balanceRecord.setServiceFeeBalance(walletFee.getServiceFeeBalance());
|
|
|
|
|
+ balanceRecord.setTradingAmount(walletFee.getTradingAmount());
|
|
|
|
|
+ balanceRecord.setRemark("贸易订单号:" + param.getOrderNo() + ",下单冻结服务费");
|
|
|
|
|
+ balanceRecord.setDelFlag(Global.NO);
|
|
|
|
|
+ balanceRecord.setCreateUser(operatorId);
|
|
|
|
|
+ balanceRecord.setUpdateUser(operatorId);
|
|
|
|
|
+ balanceRecord.setCreateTime(now);
|
|
|
|
|
+ balanceRecord.setUpdateTime(now);
|
|
|
|
|
+ parkingWalletFeeBalanceRepository.save(balanceRecord);
|
|
|
|
|
+ log.info("服务费钱包日志已写入kwt_parking_wallet_fee_balance,orderNo:{}, proEntId:{}, supEntId:{}, tradeType:{}, tradeAmount:{}, balanceAfter:{}",
|
|
|
|
|
+ param.getOrderNo(), param.getProEntId(), param.getSupEntId(), TRADE_TYPE_FREEZE,
|
|
|
|
|
+ freezeAmount, walletFee.getServiceFeeBalance());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private KwtParkingWalletFee queryWalletFee(Long proEntId, Long supEntId) {
|
|
|
|
|
+ return parkingWalletFeeRepository.getOne(
|
|
|
|
|
+ Wrappers.<KwtParkingWalletFee>lambdaQuery()
|
|
|
|
|
+ .eq(KwtParkingWalletFee::getProEntId, proEntId)
|
|
|
|
|
+ .eq(supEntId != null, KwtParkingWalletFee::getSupEntId, supEntId)
|
|
|
|
|
+ .eq(KwtParkingWalletFee::getDelFlag, Global.NO)
|
|
|
|
|
+ .orderByDesc(KwtParkingWalletFee::getId)
|
|
|
|
|
+ .last("limit 1"),
|
|
|
|
|
+ false
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Long resolveOperatorId(Long operatorId) {
|
|
|
|
|
+ Long loginUserId = LoginUserHolder.getUserId();
|
|
|
|
|
+ if (loginUserId != null) {
|
|
|
|
|
+ return loginUserId;
|
|
|
|
|
+ }
|
|
|
|
|
+ return operatorId == null ? -1L : operatorId;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 计算服务费余额支持的最大可购买数量
|
|
|
|
|
+ * 仅在收费策略开关开启且单价大于0时计算:服务费余额 / 策略单价(向下取整,保留4位小数)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param currentStrategy 当前收费策略
|
|
|
|
|
+ * @param currentStrategyUnitFee 策略单价
|
|
|
|
|
+ * @param serviceFeeBalance 服务费余额
|
|
|
|
|
+ * @return 最大可购买数量;策略未开启时不做余额约束,返回null
|
|
|
|
|
+ */
|
|
|
|
|
+ private BigDecimal calculateMaxPurchaseQuantity(KwtParkingChargeStrategy currentStrategy,
|
|
|
|
|
+ BigDecimal currentStrategyUnitFee,
|
|
|
|
|
+ BigDecimal serviceFeeBalance) {
|
|
|
|
|
+ if (currentStrategy == null || !Objects.equals(currentStrategy.getStatus(), STRATEGY_OPEN)
|
|
|
|
|
+ || currentStrategyUnitFee.compareTo(ZERO_AMOUNT) <= 0) {
|
|
|
|
|
+ log.info("最大可购买数量未计算,原因:{}",
|
|
|
|
|
+ currentStrategy == null ? "未匹配到收费策略" : "收费策略未开启或单价<=0");
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ //向下取整
|
|
|
|
|
+ BigDecimal maxPurchaseQuantity = serviceFeeBalance.divide(currentStrategyUnitFee, 4, RoundingMode.DOWN);
|
|
|
|
|
+ log.info("最大可购买数量计算完成,serviceFeeBalance:{}, unitFee:{}, maxPurchaseQuantity:{}",
|
|
|
|
|
+ serviceFeeBalance, currentStrategyUnitFee, maxPurchaseQuantity);
|
|
|
|
|
+ return maxPurchaseQuantity.max(ZERO_AMOUNT);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 查询企业当前生效的收费策略
|
|
|
|
|
+ * 逻辑:先查企业绑定的策略,再查策略详情,最后取“开启状态”且id最大(最新)的一条
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param proEntId 采购企业id
|
|
|
|
|
+ * @return 生效策略;未匹配到则返回null
|
|
|
|
|
+ */
|
|
|
|
|
+ private KwtParkingChargeStrategy queryCurrentEnableStrategy(Long proEntId) {
|
|
|
|
|
+ List<KwtParkingChargeStrategyUnit> strategyUnitList = parkingChangeStrategyUnitRepository.list(
|
|
|
|
|
+ Wrappers.<KwtParkingChargeStrategyUnit>lambdaQuery()
|
|
|
|
|
+ .eq(KwtParkingChargeStrategyUnit::getEntId, proEntId)
|
|
|
|
|
+ .eq(KwtParkingChargeStrategyUnit::getDelFlag, Global.NO)
|
|
|
|
|
+ );
|
|
|
|
|
+ log.info("查询企业策略绑定关系,proEntId:{}, bindCount:{}",
|
|
|
|
|
+ proEntId, strategyUnitList == null ? 0 : strategyUnitList.size());
|
|
|
|
|
+
|
|
|
|
|
+ if (org.apache.commons.collections4.CollectionUtils.isEmpty(strategyUnitList)) {
|
|
|
|
|
+ log.info("企业未绑定任何策略,proEntId:{}, strategyUnitList:[]", proEntId);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ List<Long> strategyIds = strategyUnitList.stream()
|
|
|
|
|
+ .map(KwtParkingChargeStrategyUnit::getStrategyId)
|
|
|
|
|
+ .filter(Objects::nonNull)
|
|
|
|
|
+ .toList();
|
|
|
|
|
+ log.info("查询企业策略ID集合,proEntId:{}, strategyIds:{}", proEntId, strategyIds);
|
|
|
|
|
+ if (org.apache.commons.collections4.CollectionUtils.isEmpty(strategyIds)) {
|
|
|
|
|
+ log.info("企业绑定策略ID集合为空");
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ List<KwtParkingChargeStrategy> strategyList = parkingChangeStrategyRepository.list(
|
|
|
|
|
+ Wrappers.<KwtParkingChargeStrategy>lambdaQuery()
|
|
|
|
|
+ .in(KwtParkingChargeStrategy::getId, strategyIds)
|
|
|
|
|
+ .eq(KwtParkingChargeStrategy::getDelFlag, Global.NO)
|
|
|
|
|
+ .orderByDesc(KwtParkingChargeStrategy::getId)
|
|
|
|
|
+ );
|
|
|
|
|
+ log.info("查询策略详情完成,proEntId:{}, strategyCount:{}",
|
|
|
|
|
+ proEntId, strategyList == null ? 0 : strategyList.size());
|
|
|
|
|
+ if (org.apache.commons.collections4.CollectionUtils.isEmpty(strategyList)) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ KwtParkingChargeStrategy hitStrategy = strategyList.stream()
|
|
|
|
|
+ .filter(item -> Objects.equals(item.getStatus(), STRATEGY_OPEN))
|
|
|
|
|
+ .max(Comparator.comparing(KwtParkingChargeStrategy::getId))
|
|
|
|
|
+ .orElse(null);
|
|
|
|
|
+ log.info("匹配生效策略结果,proEntId:{}, hitStrategyId:{}",
|
|
|
|
|
+ proEntId, hitStrategy == null ? null : hitStrategy.getId());
|
|
|
|
|
+ return hitStrategy;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 查询企业当前可用服务费余额
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param proEntId 采购企业id
|
|
|
|
|
+ * @param supEntId 供应商企业id,可为空
|
|
|
|
|
+ * @return 服务费余额,无记录时返回0
|
|
|
|
|
+ */
|
|
|
|
|
+ private BigDecimal queryServiceFeeBalance(Long proEntId, Long supEntId) {
|
|
|
|
|
+ KwtParkingWalletFee walletFee = queryWalletFee(proEntId, supEntId);
|
|
|
|
|
+ if (walletFee == null || walletFee.getServiceFeeBalance() == null) {
|
|
|
|
|
+ log.info("查询服务费余额为空,按0处理,proEntId:{}, supEntId:{}", proEntId, supEntId);
|
|
|
|
|
+ return ZERO_AMOUNT;
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("查询服务费余额成功,proEntId:{}, supEntId:{}, serviceFeeBalance:{}",
|
|
|
|
|
+ proEntId, supEntId, walletFee.getServiceFeeBalance());
|
|
|
|
|
+ return walletFee.getServiceFeeBalance();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
}
|
|
}
|