chenxiaofei 5 ore fa
parent
commit
490ed6ff24

+ 31 - 17
sckw-modules/sckw-transport/src/main/java/com/sckw/transport/service/ParkingWalletFeeService.java

@@ -67,51 +67,65 @@ public class ParkingWalletFeeService {
     RemoteSystemService remoteSystemService;
 
     /**
-     * 查询服务费余额、本次预计服务费与最大可购买数量
-     * 规则:
-     * 1. 服务费余额 = 采购方当前服务费余额
-     * 2. 本次预计服务费 = 采购数量 * 采购方当前配置的收费策略单价
-     * 3. 最大可购买数量 = 收费策略开启时,服务费余额 / 策略单价;未开启时不返回余额约束
+     * 查询服务费余额、本次预计服务费与最大可购买数量。
+     * <p>
+     * 业务规则:
+     * 1. 服务费余额取当前登录采购企业的可用服务费余额;
+     * 2. 收费策略总开关关闭时,不计算预计服务费,最大可购买数量返回 null,表示不做余额约束;
+     * 3. 收费策略总开关开启时,按企业绑定的当前收费策略计算预计服务费;
+     * 4. 策略 method 为 0.00 时,实际单价使用 kwt_parking_strategy_switch.default_fee;
+     * 5. 最大可购买数量 = 服务费余额 / 实际策略单价,向下取整,避免超出余额。
      *
-     * @param param 查询参数
+     * @param param 服务费预估查询参数,purchaseQuantity 为本次采购数量
      * @return 服务费余额、本次预计服务费与最大可购买数量
      */
     public ParkingWalletFeeEstimateResp queryEstimateServiceFee(ParkingWalletFeeEstimateQueryParam param) {
         Long entId = LoginUserHolder.getEntId();
-        log.info("服务费预估开始,entId:{}, param:{}", entId, param);
+        log.info("服务费预估开始,企业id:{}, 查询参数:{}", entId, param);
+        if (param == null) {
+            log.warn("服务费预估参数为空,企业id:{}", entId);
+            throw new BusinessPlatfromException(ErrorCodeEnum.PARAM_ERROR, "服务费预估参数不能为空");
+        }
         if (param.getPurchaseQuantity() == null || param.getPurchaseQuantity().compareTo(ZERO_AMOUNT) < 0) {
-            log.warn("服务费预估参数异常,采购数量非法,entId:{}, purchaseQuantity:{}",
+            log.warn("服务费预估参数异常,采购数量不能为空且不能小于0,企业id:{}, 采购数量:{}",
                     entId, param.getPurchaseQuantity());
             throw new BusinessPlatfromException(ErrorCodeEnum.PARAM_ERROR, "采购数量不能为空且不能小于0");
         }
 
-        // 查询采购方当前服务费余额
+        // 查询采购企业当前可用服务费余额;supEntId 为空时按采购企业汇总口径取最新钱包记录。
         BigDecimal serviceFeeBalance = queryServiceFeeBalance(entId, null);
+        log.info("服务费预估余额查询完成,企业id:{}, 服务费余额:{}", entId, serviceFeeBalance);
+
+        // 收费策略总开关统一以 kwt_parking_strategy_switch.status 为准,策略表 status 不再作为计费开关。
+        boolean chargeStrategySwitchOpen = isChargeStrategySwitchOpen();
 
-        // 查询当前企业生效中的收费策略(仅取状态开启的最新一条)
+        // 查询当前企业绑定的最新收费策略;若 method 为 0.00,resolveStrategyUnitFee 会使用开关表 default_fee。
         KwtParkingChargeStrategy currentStrategy = queryCurrentEnableStrategy(entId);
         BigDecimal currentStrategyUnitFee = resolveStrategyUnitFee(currentStrategy);
-        log.info("服务费预估策略信息,entId:{}, strategyId:{}, switchOpen:{}, unitFee:{}",
+        log.info("服务费预估策略匹配完成,企业id:{}, 开关状态:{}, 策略id:{}, 策略原始单价:{}, 实际计费单价:{}",
                 entId,
+                chargeStrategySwitchOpen ? "开启" : "关闭",
                 currentStrategy == null ? null : currentStrategy.getId(),
-                isChargeStrategySwitchOpen(),
+                currentStrategy == null ? null : currentStrategy.getMethod(),
                 currentStrategyUnitFee);
 
-        // 本次预计服务费 = 采购数量 * 策略单价,金额统一保留2位
-        BigDecimal estimatedServiceFee = isChargeStrategySwitchOpen()
+        // 本次预计服务费 = 采购数量 * 实际计费单价,金额保留2位小数;开关关闭时固定返回0。
+        BigDecimal estimatedServiceFee = chargeStrategySwitchOpen
                 ? param.getPurchaseQuantity().multiply(currentStrategyUnitFee).setScale(2, RoundingMode.HALF_UP)
                 : ZERO_AMOUNT;
+        log.info("服务费预估金额计算完成,企业id:{}, 采购数量:{}, 实际计费单价:{}, 预计服务费:{}",
+                entId, param.getPurchaseQuantity(), currentStrategyUnitFee, estimatedServiceFee);
 
-        // 收费策略开关开启时,按服务费余额计算最大可购买数量
+        // 开关开启且存在有效策略单价时,按服务费余额计算最大可购买数量;否则返回 null。
         BigDecimal maxPurchaseQuantity = calculateMaxPurchaseQuantity(currentStrategy, currentStrategyUnitFee, serviceFeeBalance);
-        log.info("服务费预估计算完成,entId:{}, purchaseQuantity:{}, serviceFeeBalance:{}, estimatedServiceFee:{}, maxPurchaseQuantity:{}",
+        log.info("服务费预估计算完成,企业id:{}, 采购数量:{}, 服务费余额:{}, 预计服务费:{}, 最大可购买数量:{}",
                 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);
+        log.info("服务费预估结束,企业id:{}, 响应结果:{}", entId, resp);
         return resp;
     }