增加规则配置

This commit is contained in:
wuweihua
2026-10-16 13:40:03 +08:00
parent b3f79ffc4c
commit b54cb1b018
16 changed files with 1376 additions and 7 deletions
@@ -35,6 +35,7 @@ import com.bms.common.utils.uuid.IdUtils;
import com.bms.customer.customerinfo.domain.BmsCustomer;
import com.bms.customer.customerinfo.mapper.BmsCustomerMapper;
import com.bms.system.mapper.SysUserMapper;
// import com.bms.system.service.IBmsBizRuleService;
import com.bms.price.domain.PrcIncomeBill;
import com.bms.price.domain.PrcIncomeBillFee;
import com.bms.price.domain.PrcOrderBill;
@@ -142,6 +143,10 @@ public class PrcOrderBillServiceImpl implements IPrcOrderBillService
@Autowired
private PrcRecalcBillPriceAsyncRunner recalcBillPriceAsyncRunner;
// TODO: reopen with three-side / biz weight rules
// @Autowired
// private IBmsBizRuleService bizRuleService;
private static final int BATCH_ADD_FEE_MAX = 5000;
private static final int BATCH_ADD_FEE_PREVIEW_LIMIT = 20;
private static final int CHARGE_WEIGHT_IMPORT_MAX = 5000;
@@ -167,6 +172,16 @@ public class PrcOrderBillServiceImpl implements IPrcOrderBillService
PrcOrderBill existed = orderBillMapper.selectPrcOrderBillByOrderNo(request.getOrderNo());
BigDecimal systemWeight = request.getWeight();
BigDecimal calcWeight = resolveCalcWeight(existed, systemWeight);
// TODO: reopen three-side / biz weight rules later
// Manual charge_weight wins; otherwise apply active biz rules (e.g. three-side).
// if (!isManualChargeWeight(existed))
// {
// BigDecimal ruled = bizRuleService.resolveWeightByRule(request, systemWeight);
// if (ruled != null)
// {
// calcWeight = ruled;
// }
// }
request.setWeight(calcWeight);
TmsPriceQuoteResult result = tmsPriceQuoteService.calculate(request);
persistBill(request, systemWeight, bizDate, result);
@@ -5,7 +5,7 @@ import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 公共业务附件 bms_biz_attachment
* 公共业务附件 biz_attachment
*/
@Data
@EqualsAndHashCode(callSuper = true)
@@ -0,0 +1,113 @@
package com.bms.system.domain;
import java.math.BigDecimal;
import java.util.List;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.bms.common.annotation.Excel;
import com.bms.common.core.domain.BaseEntity;
/**
* 业务规则配置 biz_rule(产品/国家/客户维度)
*/
public class BmsBizRule extends BaseEntity
{
private static final long serialVersionUID = 1L;
private Long ruleId;
/** 规则名称 */
@Excel(name = "规则名称")
private String ruleName;
/** 规则类型(字典 bms_biz_rule_type) */
@Excel(name = "规则类型")
private String ruleType;
/** 产品代码(空=全部) */
@Excel(name = "产品")
private String productCode;
/** 国家ISO2(空=全部) */
@Excel(name = "国家")
private String countryCode;
/** 客户代码(空=全部) */
@Excel(name = "客户")
private String customerCode;
/** 重量段最小KG(含) */
private BigDecimal weightMin;
/** 重量段最大KG(含) */
private BigDecimal weightMax;
/** 取值字段(字典 bms_biz_weight_source) */
@Excel(name = "取值字段")
private String weightSource;
/** 三边和上限(三边规则) */
private BigDecimal sideSumMax;
/** 状态(0正常 1停用) */
private String status;
private String delFlag;
/** 新增时多产品展开(非表字段) */
private List<String> productCodes;
/** 新增时多国家展开(非表字段) */
private List<String> countryCodes;
/** 新增时多客户展开(非表字段) */
private List<String> customerCodes;
public Long getRuleId() { return ruleId; }
public void setRuleId(Long ruleId) { this.ruleId = ruleId; }
public String getRuleName() { return ruleName; }
public void setRuleName(String ruleName) { this.ruleName = ruleName; }
public String getRuleType() { return ruleType; }
public void setRuleType(String ruleType) { this.ruleType = ruleType; }
public String getProductCode() { return productCode; }
public void setProductCode(String productCode) { this.productCode = productCode; }
public String getCountryCode() { return countryCode; }
public void setCountryCode(String countryCode) { this.countryCode = countryCode; }
public String getCustomerCode() { return customerCode; }
public void setCustomerCode(String customerCode) { this.customerCode = customerCode; }
public BigDecimal getWeightMin() { return weightMin; }
public void setWeightMin(BigDecimal weightMin) { this.weightMin = weightMin; }
public BigDecimal getWeightMax() { return weightMax; }
public void setWeightMax(BigDecimal weightMax) { this.weightMax = weightMax; }
public String getWeightSource() { return weightSource; }
public void setWeightSource(String weightSource) { this.weightSource = weightSource; }
public BigDecimal getSideSumMax() { return sideSumMax; }
public void setSideSumMax(BigDecimal sideSumMax) { this.sideSumMax = sideSumMax; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
public String getDelFlag() { return delFlag; }
public void setDelFlag(String delFlag) { this.delFlag = delFlag; }
public List<String> getProductCodes() { return productCodes; }
public void setProductCodes(List<String> productCodes) { this.productCodes = productCodes; }
public List<String> getCountryCodes() { return countryCodes; }
public void setCountryCodes(List<String> countryCodes) { this.countryCodes = countryCodes; }
public List<String> getCustomerCodes() { return customerCodes; }
public void setCustomerCodes(List<String> customerCodes) { this.customerCodes = customerCodes; }
@Override
public String toString()
{
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
.append("ruleId", getRuleId())
.append("ruleName", getRuleName())
.append("ruleType", getRuleType())
.append("productCode", getProductCode())
.append("countryCode", getCountryCode())
.append("customerCode", getCustomerCode())
.append("weightMin", getWeightMin())
.append("weightMax", getWeightMax())
.append("weightSource", getWeightSource())
.append("sideSumMax", getSideSumMax())
.append("status", getStatus())
.append("remark", getRemark())
.toString();
}
}
@@ -0,0 +1,29 @@
package com.bms.system.mapper;
import java.math.BigDecimal;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.bms.system.domain.BmsBizRule;
public interface BmsBizRuleMapper
{
List<BmsBizRule> selectList(BmsBizRule query);
BmsBizRule selectById(Long ruleId);
List<BmsBizRule> selectActiveByType(String ruleType);
int countByDim(@Param("ruleType") String ruleType,
@Param("productCode") String productCode,
@Param("countryCode") String countryCode,
@Param("customerCode") String customerCode,
@Param("weightMin") BigDecimal weightMin,
@Param("weightMax") BigDecimal weightMax,
@Param("excludeId") Long excludeId);
int insert(BmsBizRule row);
int update(BmsBizRule row);
int deleteByIds(Long[] ruleIds);
}
@@ -0,0 +1,28 @@
package com.bms.system.service;
import java.math.BigDecimal;
import java.util.List;
import com.bms.system.domain.BmsBizRule;
import com.bms.price.domain.vo.TmsPriceBillRequest;
/**
* 业务规则配置
*/
public interface IBmsBizRuleService
{
List<BmsBizRule> selectList(BmsBizRule query);
BmsBizRule selectById(Long ruleId);
int insert(BmsBizRule row);
int update(BmsBizRule row);
int deleteByIds(Long[] ruleIds);
/**
* Apply active weight rules to bill request.
* @return overridden weight when a rule matches; null when no rule applies
*/
BigDecimal resolveWeightByRule(TmsPriceBillRequest request, BigDecimal systemWeight);
}
@@ -0,0 +1,383 @@
package com.bms.system.service.impl;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bms.common.exception.ServiceException;
import com.bms.common.utils.StringUtils;
import com.bms.price.domain.vo.TmsPriceBillRequest;
import com.bms.system.domain.BmsBizRule;
import com.bms.system.mapper.BmsBizRuleMapper;
import com.bms.system.service.IBmsBizRuleService;
import com.bms.system.service.IBmsProductCodeMappingService;
@Service
public class BmsBizRuleServiceImpl implements IBmsBizRuleService
{
public static final String RULE_TYPE_THREE_SIDE = "THREE_SIDE";
public static final String WEIGHT_SOURCE_RECEIVE = "RECEIVE_WEIGHT";
public static final String WEIGHT_SOURCE_CHARGE = "CHARGE_WEIGHT";
@Autowired
private BmsBizRuleMapper bizRuleMapper;
@Autowired
private IBmsProductCodeMappingService productCodeMappingService;
@Override
public List<BmsBizRule> selectList(BmsBizRule query)
{
return bizRuleMapper.selectList(query);
}
@Override
public BmsBizRule selectById(Long ruleId)
{
return bizRuleMapper.selectById(ruleId);
}
@Override
@Transactional(rollbackFor = Exception.class)
public int insert(BmsBizRule row)
{
normalizeHeader(row);
validateHeader(row);
if (StringUtils.isEmpty(row.getStatus()))
{
row.setStatus("0");
}
List<String> products = dimValues(row.getProductCodes(), row.getProductCode());
List<String> countries = dimValues(row.getCountryCodes(), row.getCountryCode());
List<String> customers = dimValues(row.getCustomerCodes(), row.getCustomerCode());
int n = 0;
for (String product : products)
{
for (String country : countries)
{
for (String customer : customers)
{
BmsBizRule one = copyBase(row);
one.setProductCode(product);
one.setCountryCode(country);
one.setCustomerCode(customer);
assertUnique(one, null);
n += bizRuleMapper.insert(one);
}
}
}
return n;
}
@Override
public int update(BmsBizRule row)
{
if (row.getRuleId() == null)
{
throw new ServiceException("ruleId required");
}
normalizeHeader(row);
validateHeader(row);
// edit: single dimension row
if (row.getProductCodes() != null && !row.getProductCodes().isEmpty())
{
row.setProductCode(normalizeCode(row.getProductCodes().get(0)));
}
else
{
row.setProductCode(normalizeCode(row.getProductCode()));
}
if (row.getCountryCodes() != null && !row.getCountryCodes().isEmpty())
{
row.setCountryCode(normalizeCode(row.getCountryCodes().get(0)));
}
else
{
row.setCountryCode(normalizeCode(row.getCountryCode()));
}
if (row.getCustomerCodes() != null && !row.getCustomerCodes().isEmpty())
{
row.setCustomerCode(normalizeCode(row.getCustomerCodes().get(0)));
}
else
{
row.setCustomerCode(normalizeCode(row.getCustomerCode()));
}
assertUnique(row, row.getRuleId());
return bizRuleMapper.update(row);
}
@Override
public int deleteByIds(Long[] ruleIds)
{
return bizRuleMapper.deleteByIds(ruleIds);
}
@Override
public BigDecimal resolveWeightByRule(TmsPriceBillRequest request, BigDecimal systemWeight)
{
if (request == null)
{
return null;
}
List<BmsBizRule> rules = bizRuleMapper.selectActiveByType(RULE_TYPE_THREE_SIDE);
if (rules == null || rules.isEmpty())
{
return null;
}
for (BmsBizRule rule : rules)
{
if (matchThreeSide(rule, request, systemWeight))
{
return pickWeight(rule.getWeightSource(), request, systemWeight);
}
}
return null;
}
private void assertUnique(BmsBizRule row, Long excludeId)
{
int cnt = bizRuleMapper.countByDim(row.getRuleType(), row.getProductCode(), row.getCountryCode(),
row.getCustomerCode(), row.getWeightMin(), row.getWeightMax(), excludeId);
if (cnt > 0)
{
throw new ServiceException("同维度规则已存在(产品/国家/客户/重量段)");
}
}
private static BmsBizRule copyBase(BmsBizRule row)
{
BmsBizRule one = new BmsBizRule();
one.setRuleName(row.getRuleName());
one.setRuleType(row.getRuleType());
one.setWeightMin(row.getWeightMin());
one.setWeightMax(row.getWeightMax());
one.setWeightSource(row.getWeightSource());
one.setSideSumMax(row.getSideSumMax());
one.setStatus(row.getStatus());
one.setRemark(row.getRemark());
one.setCreateBy(row.getCreateBy());
return one;
}
/** empty list / blank => one null dimension (means all) */
private static List<String> dimValues(List<String> list, String single)
{
Set<String> set = new LinkedHashSet<String>();
if (list != null)
{
for (String s : list)
{
String c = normalizeCode(s);
if (c != null)
{
set.add(c);
}
}
}
if (set.isEmpty())
{
String c = normalizeCode(single);
if (c != null)
{
set.add(c);
}
}
if (set.isEmpty())
{
return Collections.singletonList(null);
}
return new ArrayList<String>(set);
}
private boolean matchThreeSide(BmsBizRule rule, TmsPriceBillRequest request, BigDecimal systemWeight)
{
if (rule == null)
{
return false;
}
if (!dimMatch(request.getProductCode(), rule.getProductCode(), true))
{
return false;
}
if (!dimMatch(request.getCountryCode(), rule.getCountryCode(), false))
{
return false;
}
if (!dimMatch(request.getCustomerCode(), rule.getCustomerCode(), false))
{
return false;
}
BigDecimal checkWeight = firstPositive(request.getReceiveWeight(), systemWeight, request.getWeight());
if (!inWeightRange(checkWeight, rule.getWeightMin(), rule.getWeightMax()))
{
return false;
}
if (rule.getSideSumMax() != null)
{
BigDecimal sum = sideSum(request);
if (sum == null || sum.compareTo(rule.getSideSumMax()) >= 0)
{
return false;
}
}
return true;
}
private boolean dimMatch(String requestCode, String ruleCode, boolean tryProductMap)
{
if (StringUtils.isEmpty(ruleCode))
{
return true;
}
if (StringUtils.isEmpty(requestCode))
{
return false;
}
String req = requestCode.trim().toUpperCase(Locale.ROOT);
String rule = ruleCode.trim().toUpperCase(Locale.ROOT);
if (req.equals(rule))
{
return true;
}
if (tryProductMap)
{
String mapped = productCodeMappingService.resolveBmsProductCode(requestCode);
return mapped != null && mapped.trim().toUpperCase(Locale.ROOT).equals(rule);
}
return false;
}
private static BigDecimal pickWeight(String weightSource, TmsPriceBillRequest request, BigDecimal systemWeight)
{
if (WEIGHT_SOURCE_RECEIVE.equalsIgnoreCase(weightSource))
{
BigDecimal rw = firstPositive(request.getReceiveWeight(), request.getActualWeight());
return rw != null ? rw : systemWeight;
}
if (WEIGHT_SOURCE_CHARGE.equalsIgnoreCase(weightSource))
{
return systemWeight != null ? systemWeight : request.getWeight();
}
return null;
}
private static boolean inWeightRange(BigDecimal weight, BigDecimal min, BigDecimal max)
{
if (min == null && max == null)
{
return true;
}
if (weight == null)
{
return false;
}
if (min != null && weight.compareTo(min) < 0)
{
return false;
}
if (max != null && weight.compareTo(max) > 0)
{
return false;
}
return true;
}
private static BigDecimal sideSum(TmsPriceBillRequest request)
{
BigDecimal l = nzDim(request.getPkgLength());
BigDecimal w = nzDim(request.getPkgWidth());
BigDecimal h = nzDim(request.getPkgHeight());
if (l == null || w == null || h == null)
{
return null;
}
return l.add(w).add(h);
}
private static BigDecimal nzDim(BigDecimal v)
{
if (v == null || v.compareTo(BigDecimal.ZERO) <= 0)
{
return null;
}
return v;
}
private static BigDecimal firstPositive(BigDecimal... vals)
{
if (vals == null)
{
return null;
}
for (BigDecimal v : vals)
{
if (v != null && v.compareTo(BigDecimal.ZERO) > 0)
{
return v;
}
}
return null;
}
private void normalizeHeader(BmsBizRule row)
{
if (row.getRuleName() != null)
{
row.setRuleName(row.getRuleName().trim());
}
if (row.getRuleType() != null)
{
row.setRuleType(row.getRuleType().trim().toUpperCase(Locale.ROOT));
}
if (row.getWeightSource() != null)
{
row.setWeightSource(row.getWeightSource().trim().toUpperCase(Locale.ROOT));
}
}
private static String normalizeCode(String code)
{
if (code == null)
{
return null;
}
String t = code.trim();
if (t.isEmpty())
{
return null;
}
return t.toUpperCase(Locale.ROOT);
}
private void validateHeader(BmsBizRule row)
{
if (StringUtils.isEmpty(row.getRuleName()))
{
throw new ServiceException("规则名称不能为空");
}
if (StringUtils.isEmpty(row.getRuleType()))
{
throw new ServiceException("规则类型不能为空");
}
if (StringUtils.isEmpty(row.getWeightSource()))
{
throw new ServiceException("取值字段不能为空");
}
if (RULE_TYPE_THREE_SIDE.equalsIgnoreCase(row.getRuleType()) && row.getSideSumMax() == null)
{
throw new ServiceException("三边规则必须填写三边和上限");
}
if (row.getWeightMin() != null && row.getWeightMax() != null
&& row.getWeightMin().compareTo(row.getWeightMax()) > 0)
{
throw new ServiceException("重量段最小值不能大于最大值");
}
}
}
@@ -0,0 +1,130 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bms.system.mapper.BmsBizRuleMapper">
<resultMap type="BmsBizRule" id="BmsBizRuleResult">
<id property="ruleId" column="rule_id" />
<result property="ruleName" column="rule_name" />
<result property="ruleType" column="rule_type" />
<result property="productCode" column="product_code" />
<result property="countryCode" column="country_code" />
<result property="customerCode" column="customer_code" />
<result property="weightMin" column="weight_min" />
<result property="weightMax" column="weight_max" />
<result property="weightSource" column="weight_source" />
<result property="sideSumMax" column="side_sum_max" />
<result property="status" column="status" />
<result property="remark" column="remark" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
<result property="delFlag" column="del_flag" />
</resultMap>
<sql id="selectVo">
select rule_id, rule_name, rule_type, product_code, country_code, customer_code,
weight_min, weight_max, weight_source, side_sum_max,
status, remark, create_by, create_time, update_by, update_time, del_flag
from biz_rule
</sql>
<select id="selectList" parameterType="BmsBizRule" resultMap="BmsBizRuleResult">
<include refid="selectVo"/>
<where>
del_flag = '0'
<if test="ruleName != null and ruleName != ''">
and rule_name like concat('%', #{ruleName}, '%')
</if>
<if test="ruleType != null and ruleType != ''">
and rule_type = #{ruleType}
</if>
<if test="productCode != null and productCode != ''">
and product_code = #{productCode}
</if>
<if test="countryCode != null and countryCode != ''">
and country_code = #{countryCode}
</if>
<if test="customerCode != null and customerCode != ''">
and customer_code = #{customerCode}
</if>
<if test="weightSource != null and weightSource != ''">
and weight_source = #{weightSource}
</if>
<if test="status != null and status != ''">
and status = #{status}
</if>
</where>
order by product_code asc, country_code asc, customer_code asc, rule_id desc
</select>
<select id="selectById" parameterType="Long" resultMap="BmsBizRuleResult">
<include refid="selectVo"/>
where rule_id = #{ruleId} and del_flag = '0'
</select>
<select id="selectActiveByType" parameterType="String" resultMap="BmsBizRuleResult">
<include refid="selectVo"/>
where del_flag = '0' and status = '0'
and rule_type = #{ruleType}
order by
(case when product_code is null or product_code = '' then 0 else 1 end
+ case when country_code is null or country_code = '' then 0 else 1 end
+ case when customer_code is null or customer_code = '' then 0 else 1 end) desc,
rule_id asc
</select>
<select id="countByDim" resultType="int">
select count(1) from biz_rule
where del_flag = '0'
and rule_type = #{ruleType}
and ifnull(product_code,'') = ifnull(#{productCode},'')
and ifnull(country_code,'') = ifnull(#{countryCode},'')
and ifnull(customer_code,'') = ifnull(#{customerCode},'')
and ((#{weightMin} is null and weight_min is null) or weight_min = #{weightMin})
and ((#{weightMax} is null and weight_max is null) or weight_max = #{weightMax})
<if test="excludeId != null">and rule_id != #{excludeId}</if>
</select>
<insert id="insert" parameterType="BmsBizRule" useGeneratedKeys="true" keyProperty="ruleId">
insert into biz_rule (
rule_name, rule_type, product_code, country_code, customer_code, weight_min, weight_max,
weight_source, side_sum_max, status, remark, create_by, create_time, del_flag
) values (
#{ruleName}, #{ruleType}, #{productCode}, #{countryCode}, #{customerCode}, #{weightMin}, #{weightMax},
#{weightSource}, #{sideSumMax}, #{status}, #{remark}, #{createBy}, sysdate(), '0'
)
</insert>
<update id="update" parameterType="BmsBizRule">
update biz_rule
<set>
<if test="ruleName != null and ruleName != ''">rule_name = #{ruleName},</if>
<if test="ruleType != null and ruleType != ''">rule_type = #{ruleType},</if>
product_code = #{productCode},
country_code = #{countryCode},
customer_code = #{customerCode},
weight_min = #{weightMin},
weight_max = #{weightMax},
<if test="weightSource != null and weightSource != ''">weight_source = #{weightSource},</if>
side_sum_max = #{sideSumMax},
<if test="status != null and status != ''">status = #{status},</if>
<if test="remark != null">remark = #{remark},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
update_time = sysdate()
</set>
where rule_id = #{ruleId} and del_flag = '0'
</update>
<update id="deleteByIds" parameterType="Long">
update biz_rule
set del_flag = '1',
update_time = sysdate()
where rule_id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</update>
</mapper>
@@ -24,7 +24,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<sql id="selectBmsBizAttachmentVo">
select attachment_id, biz_type, biz_id, file_name, file_path, file_size, content_type,
file_extension, remark, create_by, create_time, update_by, update_time, del_flag
from bms_biz_attachment
from biz_attachment
</sql>
<select id="selectBmsBizAttachmentById" parameterType="Long" resultMap="BmsBizAttachmentResult">
@@ -59,7 +59,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</select>
<insert id="insertBmsBizAttachment" parameterType="BmsBizAttachment" useGeneratedKeys="true" keyProperty="attachmentId">
insert into bms_biz_attachment (
insert into biz_attachment (
<if test="bizType != null and bizType != ''">biz_type,</if>
<if test="bizId != null">biz_id,</if>
<if test="fileName != null and fileName != ''">file_name,</if>
@@ -87,7 +87,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</insert>
<update id="updateBmsBizAttachment" parameterType="BmsBizAttachment">
update bms_biz_attachment
update biz_attachment
<set>
<if test="bizId != null">biz_id = #{bizId},</if>
<if test="remark != null">remark = #{remark},</if>
@@ -98,7 +98,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</update>
<update id="deleteBmsBizAttachmentByIds" parameterType="Long">
update bms_biz_attachment set del_flag = '2' where attachment_id in
update biz_attachment set del_flag = '2' where attachment_id in
<foreach collection="array" item="id" open="(" separator="," close=")">
#{id}
</foreach>