优化计费重量规则,新增按收货重取重逻辑

This commit is contained in:
wuweihua
2026-11-17 17:00:03 +08:00
parent 8d45439931
commit 4f2954ebce
6 changed files with 130 additions and 8 deletions
@@ -235,7 +235,9 @@ public class PrcOrderBillServiceImpl implements IPrcOrderBillService
BigDecimal calcWeight = resolveBillCalcWeight(request, existed, systemWeight);
request.setWeight(calcWeight);
TmsPriceQuoteResult result = tmsPriceQuoteService.calculate(request);
persistBill(request, systemWeight, bizDate, result);
// 规则命中时覆盖落库 weight;有 charge_weight 覆盖时保留原系统 weight
BigDecimal persistWeight = resolvePersistSystemWeight(request, existed, systemWeight, calcWeight);
persistBill(request, persistWeight, bizDate, result);
return result;
}
catch (RuntimeException e)
@@ -2065,8 +2067,8 @@ public class PrcOrderBillServiceImpl implements IPrcOrderBillService
{
bill.setCountryName(result.getCountryName());
}
// weight=上游计费重(DIMENSIONAL_WEIGHT_FACTOR);charge_weight=客户确认重/人工修改
bill.setWeight(weight);
// weight:默认上游系统重;业务规则命中时写入规则计费重;charge_weight=客户确认重/人工修改
bill.setWeight(weight);
bill.setBizDate(bizDate);
bill.setTmsId(request.getTmsId() == null ? null : Long.valueOf(request.getTmsId()));
bill.setPriceSetId(result.getPriceSetId());
@@ -3116,6 +3118,25 @@ bill.setWeight(weight);
return applyBizRuleWeight(request, systemWeight);
}
/**
* 落库 weight:无 charge_weight 覆盖时写入计费用重(含规则命中结果);
* 有客户确认重/人工改重时保留原系统 weight。
*/
private BigDecimal resolvePersistSystemWeight(TmsPriceBillRequest request, PrcOrderBill existed,
BigDecimal systemWeight, BigDecimal calcWeight)
{
BigDecimal preferred = positiveOrNull(request == null ? null : request.getChargeWeight());
if (preferred == null && !isUpstreamSyncRequest(request) && hasChargeWeightOverride(existed))
{
preferred = existed.getChargeWeight();
}
if (preferred != null)
{
return systemWeight;
}
return calcWeight != null ? calcWeight : systemWeight;
}
/**
* 上游同步/软失败:weight=上游计费重;charge_weight=客户确认重(无值则 null)。
*/
@@ -3133,7 +3154,7 @@ bill.setWeight(weight);
}
/**
* 业务规则计费重量(THREE_SIDE / VOL_8000)。
* 业务规则计费重量(THREE_SIDE / USE_RECEIVE_WEIGHT / VOL_8000)。
* 如需禁用:注释掉 resolveWeightByRule 调用,始终返回 baseWeight。
*/
private BigDecimal applyBizRuleWeight(TmsPriceBillRequest request, BigDecimal baseWeight)
@@ -21,7 +21,7 @@ public interface IBmsBizRuleService
int deleteByIds(Long[] ruleIds);
/**
* 将生效的重量规则(THREE_SIDE / VOL_8000)应用到计费请求。
* 将生效的重量规则(THREE_SIDE / USE_RECEIVE_WEIGHT / VOL_8000)应用到计费请求。
* @return 规则命中时返回覆盖后的计费重量;无规则适用时返回 null
*/
BigDecimal resolveWeightByRule(TmsPriceBillRequest request, BigDecimal systemWeight);
@@ -27,6 +27,8 @@ public class BmsBizRuleServiceImpl implements IBmsBizRuleService
public static final String RULE_TYPE_THREE_SIDE = "THREE_SIDE";
/** 计费重量 = max(L*W*H/8000, weightSource)。 */
public static final String RULE_TYPE_VOL_8000 = "VOL_8000";
/** 命中后按取值字段取重(默认实重/收货重)。 */
public static final String RULE_TYPE_USE_RECEIVE_WEIGHT = "USE_RECEIVE_WEIGHT";
public static final String WEIGHT_SOURCE_RECEIVE = "RECEIVE_WEIGHT";
public static final String WEIGHT_SOURCE_CHARGE = "CHARGE_WEIGHT";
public static final String WEIGHT_SOURCE_VOLUME = "VOLUME_WEIGHT";
@@ -137,11 +139,17 @@ public class BmsBizRuleServiceImpl implements IBmsBizRuleService
return null;
}
// 先三边之和取重,未命中再走 VOL_8000
// 优先:三边 > 取实重(收货重) > VOL_8000
BigDecimal threeSide = resolveThreeSideWeight(request, systemWeight);
if (threeSide != null)
{
return threeSide;
}
BigDecimal useReceive = resolveUseReceiveWeight(request, systemWeight);
if (useReceive != null)
{
return useReceive;
}
BigDecimal vol8000 = resolveVol8000(request, systemWeight);
if (vol8000 != null)
{
@@ -223,6 +231,34 @@ public class BmsBizRuleServiceImpl implements IBmsBizRuleService
return null;
}
/**
* 取实重(收货重):按产品/国家/客户/重量段命中后,
* 按取值字段取重(默认 RECEIVE_WEIGHT:实重优先,次收货重)。
*/
private BigDecimal resolveUseReceiveWeight(TmsPriceBillRequest request, BigDecimal systemWeight)
{
List<BmsBizRule> rules = bizRuleMapper.selectActiveByType(RULE_TYPE_USE_RECEIVE_WEIGHT);
if (rules == null || rules.isEmpty())
{
return null;
}
for (BmsBizRule rule : rules)
{
if (!matchVol8000(rule, request, systemWeight))
{
continue;
}
String source = StringUtils.isEmpty(rule.getWeightSource())
? WEIGHT_SOURCE_RECEIVE : rule.getWeightSource();
BigDecimal picked = pickWeight(source, request, systemWeight);
if (picked != null)
{
return picked.setScale(3, RoundingMode.HALF_UP);
}
}
return null;
}
private boolean matchVol8000(BmsBizRule rule, TmsPriceBillRequest request, BigDecimal systemWeight)
{
if (rule == null)
@@ -571,7 +607,9 @@ public class BmsBizRuleServiceImpl implements IBmsBizRuleService
{
throw new ServiceException("规则类型不能为空");
}
if (RULE_TYPE_VOL_8000.equalsIgnoreCase(row.getRuleType()) && StringUtils.isEmpty(row.getWeightSource()))
if ((RULE_TYPE_VOL_8000.equalsIgnoreCase(row.getRuleType())
|| RULE_TYPE_USE_RECEIVE_WEIGHT.equalsIgnoreCase(row.getRuleType()))
&& StringUtils.isEmpty(row.getWeightSource()))
{
row.setWeightSource(WEIGHT_SOURCE_RECEIVE);
}
@@ -0,0 +1,7 @@
-- USE_RECEIVE_WEIGHT: hit by product/country/customer/weight range, use receive/actual weight
SET NAMES utf8mb4;
INSERT INTO sys_dict_data (dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_by, create_time, remark)
SELECT 3, N'取实重(收货重)', 'USE_RECEIVE_WEIGHT', 'bms_biz_rule_type', '', 'warning', 'N', '0', 'admin', sysdate(),
N'命中后按取值字段取重;实重优先 actual_weight,次收货重 receive_weight'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM sys_dict_data WHERE dict_type = 'bms_biz_rule_type' AND dict_value = 'USE_RECEIVE_WEIGHT');