应付配置的附加费调整

This commit is contained in:
liuminghui
2026-11-18 13:34:00 +08:00
parent ccfb05bf50
commit a72417b2c1
30 changed files with 1338 additions and 107 deletions
@@ -63,9 +63,27 @@ public class PaySurcharge
/** 邮编列表、重量阈值或尺寸表达式。续重时这里是公斤阈值 */
private String calcParam;
/** 偏远邮编规则。续重行的阈值在 calcParam,邮编放这里,避免互相覆盖 */
/** 偏远邮编规则。导入价卡仍可能把邮编写在这里 */
private String postcodeParam;
/** 来自附加费配置的长,厘米。本表不保存 */
private BigDecimal lengthCm;
/** 来自附加费配置的宽,厘米。本表不保存 */
private BigDecimal widthCm;
/** 来自附加费配置的高,厘米。本表不保存 */
private BigDecimal heightCm;
/** 来自附加费配置的超重阈值,公斤。本表不保存 */
private BigDecimal overWeightKg;
/** 来自附加费配置的续重起始重量,公斤。本表不保存 */
private BigDecimal extraWeightKg;
/** 来自附加费配置的命中邮编。本表不保存 */
private String postcodeText;
/** 适用重量下限,空表示不限。首重从 0 开始 */
private BigDecimal weightMin;
@@ -21,6 +21,18 @@ public class PrcSurcharge extends BaseEntity
/** 渠道产品(字典 payment_product_type.dict_value) */
private String dictValue;
private BigDecimal chargeValue;
/** 超尺寸长,厘米 */
private BigDecimal lengthCm;
/** 超尺寸宽,厘米 */
private BigDecimal widthCm;
/** 超尺寸高,厘米 */
private BigDecimal heightCm;
/** 超重阈值,公斤 */
private BigDecimal overWeightKg;
/** 超重续收起始重量,公斤 */
private BigDecimal extraWeightKg;
/** 命中邮编,多个用逗号分隔 */
private String postcodeText;
private String currency;
/** 字典 bms_round_mode:UP / DOWN / HALF_UP */
private String roundMode;
@@ -0,0 +1,66 @@
package com.bms.price.domain;
import java.math.BigDecimal;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
/**
* 产品与附加费配置的关联。应付、应收以及后续类型都按产品编号和类型查询。
*/
@Data
public class PrcSurchargeBind
{
public static final String TYPE_AP = "AP";
public static final String TYPE_AR = "AR";
private Long bindId;
private Long productId;
/** AP 应付,AR 应收,以及后续其它类型 */
private String productType;
private Long scId;
private Integer sortNo;
private String createdBy;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;
private String updatedBy;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date updateTime;
/** 关联查询带出的附加费配置 */
private String scName;
private String chargeType;
private BigDecimal chargeValue;
private String currency;
/** on 启用,of 停用,vd 作废 */
private String ssCode;
private String blockTitle;
private String scNote;
private BigDecimal lengthCm;
private BigDecimal widthCm;
private BigDecimal heightCm;
private BigDecimal overWeightKg;
private BigDecimal extraWeightKg;
private String postcodeText;
}
@@ -58,6 +58,10 @@ public interface PayProductMapper
int deleteSurchargesByTerm(@Param("termIds") List<Long> termIds);
int deleteSurchargesByTermSection(@Param("termId") Long termId, @Param("sectionName") String sectionName);
int refreshSurchargeCount(@Param("productId") Long productId);
int deleteExtraLinesByTerm(@Param("termIds") List<Long> termIds);
Long selectTemplateIdByType(@Param("dictValue") String dictValue, @Param("productType") String productType);
@@ -0,0 +1,23 @@
package com.bms.price.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.bms.price.domain.PrcSurchargeBind;
/**
* 产品与附加费配置的关联。
*/
public interface PrcSurchargeBindMapper
{
List<PrcSurchargeBind> selectByProduct(@Param("productId") Long productId, @Param("productType") String productType);
List<PrcSurchargeBind> selectByProductIds(@Param("productIds") List<Long> productIds);
int deleteByProduct(@Param("productId") Long productId, @Param("productType") String productType);
int deleteByProductIds(@Param("productIds") List<Long> productIds);
int insertBinds(@Param("list") List<PrcSurchargeBind> list);
int rebindScId(@Param("oldScId") Long oldScId, @Param("newScId") Long newScId);
}
@@ -24,4 +24,10 @@ public interface PrcSurchargeMapper
/** 状态原地更新(不新增数据):停用置 of,版本化作废旧记录置 vd */
public int updateSsCodeByIds(@Param("scIds") Long[] scIds, @Param("ssCode") String ssCode, @Param("operator") String operator);
/** 附加费改版后,应付价卡上的选择改指向新记录,并带上新的金额和说明。 */
public int rebindPayConfig(@Param("oldScId") Long oldScId, @Param("newScId") Long newScId,
@Param("feeName") String feeName, @Param("zoneName") String zoneName, @Param("amount") java.math.BigDecimal amount,
@Param("currency") String currency, @Param("feeDesc") String feeDesc, @Param("calcRule") String calcRule,
@Param("chargeText") String chargeText, @Param("priceUnit") String priceUnit);
}
@@ -338,7 +338,7 @@ public final class LabelArCostCalculator
String name = blank(fee.getFeeName());
if (PaySurchargeRules.POSTCODE.equals(rule))
{
String param = blank(fee.getPostcodeParam()).isEmpty() ? fee.getCalcParam() : fee.getPostcodeParam();
String param = PaySurchargeRules.feePostcodes(fee);
if (postcode == null || postcode.isBlank())
{
result.getSkipped().add(name + ":订单没有邮编");
@@ -358,9 +358,9 @@ public final class LabelArCostCalculator
}
else if (PaySurchargeRules.WEIGHT.equals(rule))
{
if (PaySurchargeRules.matchesWeight(fee.getCalcParam(), weight))
if (PaySurchargeRules.matchesFeeWeight(fee, weight))
{
addFlat(result.getFees(), fee, currency, "计费重超过 " + fee.getCalcParam() + "kg");
addFlat(result.getFees(), fee, currency, "计费重超过 " + PaySurchargeRules.weightText(fee) + "kg");
}
}
// 超出尺寸费用
@@ -370,7 +370,7 @@ public final class LabelArCostCalculator
{
result.getSkipped().add(name + ":订单没有长宽高");
}
else if (PaySurchargeRules.matchesSize(fee.getCalcParam(), length, width, height))
else if (PaySurchargeRules.matchesFeeSize(fee, length, width, height))
{
PayCostLine matched = matchedFee(fee, freight, currency, "尺寸命中 " + fee.getCalcParam());
if (matched != null)
@@ -459,12 +459,8 @@ public final class LabelArCostCalculator
{
return;
}
BigDecimal threshold;
try
{
threshold = new BigDecimal(fee.getCalcParam() == null ? "" : fee.getCalcParam().trim());
}
catch (NumberFormatException ex)
BigDecimal threshold = PaySurchargeRules.extraStartKg(fee);
if (threshold == null)
{
return;
}
@@ -517,16 +513,10 @@ public final class LabelArCostCalculator
{
continue;
}
try
BigDecimal start = PaySurchargeRules.extraStartKg(fee);
if (start != null && start.compareTo(threshold) == 0)
{
if (fee.getCalcParam() != null && new BigDecimal(fee.getCalcParam().trim()).compareTo(threshold) == 0)
{
return true;
}
}
catch (NumberFormatException ex)
{
continue;
return true;
}
}
return false;
@@ -593,11 +583,7 @@ public final class LabelArCostCalculator
/** 价卡这一档写了邮编时,订单邮编必须落在里面。续重的重量门槛不算邮编。 */
private static boolean feePostcodeMatches(PaySurcharge fee, String rule, String postcode)
{
String param = blank(fee.getPostcodeParam());
if (param.isEmpty() && !PaySurchargeRules.EXTRA_KG.equals(rule))
{
param = blank(fee.getCalcParam());
}
String param = PaySurchargeRules.EXTRA_KG.equals(rule) ? blank(fee.getPostcodeParam()) : PaySurchargeRules.feePostcodes(fee);
if (param.isEmpty())
{
return false;
@@ -363,7 +363,7 @@ public final class PayCostCalculator
}
else
{
String param = blank(fee.getPostcodeParam()).isEmpty() ? fee.getCalcParam() : fee.getPostcodeParam();
String param = PaySurchargeRules.feePostcodes(fee);
if (postcode == null || postcode.isBlank())
{
result.getSkipped().add(name + ":订单没有邮编");
@@ -400,9 +400,9 @@ public final class PayCostCalculator
}
else if (PaySurchargeRules.WEIGHT.equals(rule))
{
if (PaySurchargeRules.matchesWeight(fee.getCalcParam(), weight))
if (PaySurchargeRules.matchesFeeWeight(fee, weight))
{
addFlat(result.getFees(), fee, currency, "计费重超过 " + fee.getCalcParam() + "kg");
addFlat(result.getFees(), fee, currency, "计费重超过 " + PaySurchargeRules.weightText(fee) + "kg");
}
}
// 超出尺寸费用
@@ -509,12 +509,8 @@ public final class PayCostCalculator
{
return;
}
BigDecimal threshold;
try
{
threshold = new BigDecimal(fee.getCalcParam() == null ? "" : fee.getCalcParam().trim());
}
catch (NumberFormatException ex)
BigDecimal threshold = PaySurchargeRules.extraStartKg(fee);
if (threshold == null)
{
return;
}
@@ -567,16 +563,10 @@ public final class PayCostCalculator
{
continue;
}
try
BigDecimal start = PaySurchargeRules.extraStartKg(fee);
if (start != null && start.compareTo(threshold) == 0)
{
if (fee.getCalcParam() != null && new BigDecimal(fee.getCalcParam().trim()).compareTo(threshold) == 0)
{
return true;
}
}
catch (NumberFormatException ex)
{
continue;
return true;
}
}
return false;
@@ -601,7 +591,7 @@ public final class PayCostCalculator
List<Map<String, Object>> details = ruleDetails(product, PaySurchargeRules.SIZE);
if (details.isEmpty())
{
return PaySurchargeRules.matchesSize(fee.getCalcParam(), length, width, height);
return PaySurchargeRules.matchesFeeSize(fee, length, width, height);
}
for (Map<String, Object> detail : details)
{
@@ -711,11 +701,7 @@ public final class PayCostCalculator
/** 价卡这一档写了邮编时,订单邮编必须落在里面。续重的重量门槛不算邮编。 */
private static boolean feePostcodeMatches(PaySurcharge fee, String rule, String postcode)
{
String param = blank(fee.getPostcodeParam());
if (param.isEmpty() && !PaySurchargeRules.EXTRA_KG.equals(rule))
{
param = blank(fee.getCalcParam());
}
String param = PaySurchargeRules.EXTRA_KG.equals(rule) ? blank(fee.getPostcodeParam()) : PaySurchargeRules.feePostcodes(fee);
if (param.isEmpty())
{
return false;
@@ -180,25 +180,27 @@ public class PayOrderFeeCalculator
}
/**
* 运费仍按订单日期取版本。附加费明细改用这个产品今天生效的版本。
* 运费仍按订单日期取版本。附加费只取该价卡产品已绑定且启用的附加费配置。
*
* @param channelId 渠道 ID
* @param priced 订单日期已经命中的价卡,方法会改写它的附加费
*/
private void applyCurrentSurcharge(String channelId, PayProduct priced)
{
PayProduct source = priced;
try
{
PayProduct current = payProductService.selectPriceForDate(channelId, new Date());
if (current != null && current.getSurcharges() != null)
if (current != null)
{
priced.setSurcharges(current.getSurcharges());
source = current;
}
}
catch (ServiceException ignored)
{
// 今天没有生效版本时,附加费明细仍用订单日期命中的那一版
// 今天没有生效版本时,改查订单日期命中的那一版有没有绑定
}
priced.setSurcharges(payProductService.boundSurcharges(source));
}
/**
@@ -505,6 +505,82 @@ public final class PaySurchargeRules
}
}
/** 配置了长宽高时按三边箱规判断,否则仍认导入价卡里的尺寸表达式。 */
public static boolean matchesFeeSize(PaySurcharge fee, BigDecimal length, BigDecimal width, BigDecimal height)
{
if (fee != null && fee.getLengthCm() != null && fee.getWidthCm() != null && fee.getHeightCm() != null)
{
if (length == null || width == null || height == null)
{
return false;
}
return exceedsBox(desc(length, width, height), fee.getLengthCm(), fee.getWidthCm(), fee.getHeightCm());
}
return matchesSize(fee == null ? null : fee.getCalcParam(), length, width, height);
}
/** 配置了超重公斤时按该值判断,否则仍认导入价卡里的重量文本。 */
public static boolean matchesFeeWeight(PaySurcharge fee, BigDecimal weight)
{
if (fee != null && fee.getOverWeightKg() != null)
{
return weight != null && weight.compareTo(fee.getOverWeightKg()) > 0;
}
return matchesWeight(fee == null ? null : fee.getCalcParam(), weight);
}
/** 续重起始公斤。配置列优先,导入价卡仍从原条件文本读取。 */
public static BigDecimal extraStartKg(PaySurcharge fee)
{
if (fee == null)
{
return null;
}
if (fee.getExtraWeightKg() != null)
{
return fee.getExtraWeightKg();
}
if (fee.getCalcParam() == null || fee.getCalcParam().isBlank())
{
return null;
}
try
{
return new BigDecimal(fee.getCalcParam().trim());
}
catch (NumberFormatException ex)
{
return null;
}
}
/** 命中邮编。独立字段优先,其次是原来的邮编列和导入文本。 */
public static String feePostcodes(PaySurcharge fee)
{
if (fee == null)
{
return "";
}
if (fee.getPostcodeText() != null && !fee.getPostcodeText().isBlank())
{
return fee.getPostcodeText().trim();
}
if (fee.getPostcodeParam() != null && !fee.getPostcodeParam().isBlank())
{
return fee.getPostcodeParam().trim();
}
return fee.getCalcParam() == null ? "" : fee.getCalcParam().trim();
}
public static String weightText(PaySurcharge fee)
{
if (fee != null && fee.getOverWeightKg() != null)
{
return fee.getOverWeightKg().stripTrailingZeros().toPlainString();
}
return fee == null || fee.getCalcParam() == null ? "" : fee.getCalcParam();
}
public static boolean matchesWeight(String param, BigDecimal weight)
{
if (param == null || param.isBlank() || weight == null)
@@ -11,6 +11,7 @@ import com.bms.price.domain.ChannelProductMap;
import com.bms.price.domain.PayProduct;
import com.bms.price.domain.PayProductTerm;
import com.bms.price.domain.PaySaveCommand;
import com.bms.price.domain.PaySurcharge;
/**
* 应付价卡配置:预览后保存,列表查询和导出。
@@ -136,6 +137,18 @@ public interface IPayProductService
/** 产品及其重量段、附加费、邮编配置 */
PayProduct selectPayProductDetail(Long productId);
/** 这一版价卡上,从附加费配置勾选进来的费用 */
List<PaySurcharge> listConfigSurcharges(Long termId);
/**
* 应付计费用的附加费。只取这个产品在关联表里绑定、并且仍在启用的附加费配置。
* 没有绑定时返回空列表,价卡里导入的附加费不参与计算。
*/
List<PaySurcharge> boundSurcharges(PayProduct product);
/** 按附加费配置重写这一版勾选的费用,导入进来的附加费不动 */
void saveConfigSurcharges(Long termId, List<PaySurcharge> rows);
/** 确认这一版有源文件,返回原始文件名 */
String sourceFileName(Long productId);
@@ -229,21 +229,23 @@ public class PayOrderCostServiceImpl implements IPayOrderCostService
* @param product 当前价卡版本
* @return 名称能对上的邮编组,没有时为空列表
*/
/** 运费仍按订单日期取版本。附加费条件和附加费明细改用产品当前生效版本。 */
/** 运费仍按订单日期取版本。附加费只取该价卡产品已绑定且启用的附加费配置。 */
private void applyCurrentSurcharge(String channelId, PayProduct priced)
{
PayProduct source = priced;
try
{
PayProduct current = payProductService.selectPriceForDate(channelId, new Date());
if (current != null && current.getSurcharges() != null)
if (current != null)
{
priced.setSurcharges(current.getSurcharges());
source = current;
}
}
catch (ServiceException ignored)
{
// 今天没有生效版本时,附加费明细仍用订单日期命中的那一版
// 今天没有生效版本时,改查订单日期命中的那一版有没有绑定
}
priced.setSurcharges(payProductService.boundSurcharges(source));
}
private List<PayPostcodeGroup> linkedGroups(PayProduct product)
@@ -41,12 +41,14 @@ import com.bms.price.domain.PaySurcharge;
import com.bms.price.domain.PayWeightBand;
import com.bms.price.domain.PrcPriceSet;
import com.bms.price.domain.PrcSurcharge;
import com.bms.price.domain.PrcSurchargeBind;
import com.bms.masterdata.domain.ServiceChannel;
import com.bms.masterdata.mapper.ServiceChannelMapper;
import com.bms.price.mapper.ChannelProductMapMapper;
import com.bms.price.mapper.PayProductMapper;
import com.bms.price.mapper.PayProductTermMapper;
import com.bms.price.mapper.PrcPriceSetMapper;
import com.bms.price.mapper.PrcSurchargeBindMapper;
import com.bms.price.mapper.PrcSurchargeMapper;
import com.bms.price.pay.PayParseProgressListener;
import com.bms.price.pay.PayChargeMode;
@@ -84,6 +86,9 @@ public class PayProductServiceImpl implements IPayProductService
@Autowired
private PrcSurchargeMapper prcSurchargeMapper;
@Autowired
private PrcSurchargeBindMapper surchargeBindMapper;
@Autowired
private ChannelProductMapMapper channelProductMapMapper;
@@ -1002,15 +1007,302 @@ public class PayProductServiceImpl implements IPayProductService
product.setWeightBands(payProductMapper.selectWeightBandsByTerm(termIds));
product.setSurcharges(payProductMapper.selectSurchargesByTerm(termIds));
product.setExtraLines(payProductMapper.selectExtraLinesByTerm(termIds));
appendBoundSurcharges(product);
return product;
}
List<Long> ids = Collections.singletonList(productId);
product.setWeightBands(payProductMapper.selectWeightBands(ids));
product.setSurcharges(payProductMapper.selectSurcharges(ids));
product.setExtraLines(payProductMapper.selectExtraLines(ids));
appendBoundSurcharges(product);
return product;
}
private static final String CONFIG_SECTION = "附加费配置";
@Override
public List<PaySurcharge> listConfigSurcharges(Long termId)
{
PayProductTerm term = requireTerm(termId);
PayProduct product = payProductMapper.selectPayProductById(term.getProductId());
if (product == null)
{
throw new ServiceException("价卡不存在");
}
List<PaySurcharge> picked = new ArrayList<>();
for (PrcSurchargeBind bind : surchargeBindMapper.selectByProduct(product.getProductId(), bindType(product)))
{
picked.add(toBoundFee(bind));
}
return picked;
}
@Override
public List<PaySurcharge> boundSurcharges(PayProduct product)
{
List<PaySurcharge> fees = new ArrayList<>();
if (product == null || product.getProductId() == null)
{
return fees;
}
for (PrcSurchargeBind bind : surchargeBindMapper.selectByProduct(product.getProductId(), bindType(product)))
{
if (bind == null || bind.getSsCode() == null || !"on".equalsIgnoreCase(bind.getSsCode()))
{
continue;
}
fees.add(toBoundFee(bind, product.getCurrency()));
}
return fees;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void saveConfigSurcharges(Long termId, List<PaySurcharge> rows)
{
PayProductTerm term = requireTerm(termId);
PayProduct product = payProductMapper.selectPayProductById(term.getProductId());
if (product == null)
{
throw new ServiceException("价卡不存在");
}
List<PrcSurchargeBind> saving = new ArrayList<>();
Set<Long> seen = new LinkedHashSet<>();
int sort = 1;
String type = bindType(product);
for (PaySurcharge input : rows == null ? List.<PaySurcharge>of() : rows)
{
if (input == null || input.getScId() == null)
{
throw new ServiceException("请选择附加费");
}
if (!seen.add(input.getScId()))
{
throw new ServiceException("同一附加费不能重复添加");
}
PrcSurcharge config = prcSurchargeMapper.selectPrcSurchargeById(input.getScId());
if (config == null || !"on".equalsIgnoreCase(config.getSsCode()))
{
throw new ServiceException("附加费不存在或已停用");
}
if (config.getChargeValue() == null)
{
throw new ServiceException(config.getScName() + "还没有计费数值,请先到附加费配置里填写");
}
PaySurcharge check = new PaySurcharge();
check.setFeeName(config.getScName());
check.setCurrency(isBlank(config.getCurrency()) ? product.getCurrency() : config.getCurrency());
check.setCalcRule(calcRuleOf(config.getChargeType()));
check.setFeeDesc(config.getScNote());
applyConfigParam(check, config);
PrcSurchargeBind bind = new PrcSurchargeBind();
bind.setProductId(product.getProductId());
bind.setProductType(type);
bind.setScId(config.getScId());
bind.setSortNo(sort++);
bind.setCreatedBy(SecurityUtils.getUsername());
saving.add(bind);
}
surchargeBindMapper.deleteByProduct(product.getProductId(), type);
if (!saving.isEmpty())
{
surchargeBindMapper.insertBinds(saving);
}
payProductMapper.deleteSurchargesByTermSection(termId, CONFIG_SECTION);
payProductMapper.refreshSurchargeCount(product.getProductId());
}
/** 条件只认附加费配置上的字段,价卡行只记下说明文字。 */
private void applyConfigParam(PaySurcharge fee, PrcSurcharge config)
{
String rule = fee.getCalcRule();
String currency = isBlank(fee.getCurrency()) ? DEFAULT_CURRENCY : fee.getCurrency();
if ("SIZE".equals(rule))
{
BigDecimal length = positive(config.getLengthCm(), fee.getFeeName() + "的长");
BigDecimal width = positive(config.getWidthCm(), fee.getFeeName() + "的宽");
BigDecimal height = positive(config.getHeightCm(), fee.getFeeName() + "的高");
fee.setChargeText("超过 " + plain(length) + "×" + plain(width) + "×" + plain(height) + "cm");
fee.setPriceUnit(currency + "/ITEM");
return;
}
if ("WEIGHT".equals(rule))
{
BigDecimal threshold = positiveOrZero(config.getOverWeightKg(), fee.getFeeName() + "的超重重量");
fee.setChargeText("超过 " + plain(threshold) + "kg");
fee.setPriceUnit(currency + "/ITEM");
return;
}
if ("EXTRA_KG".equals(rule))
{
BigDecimal threshold = positiveOrZero(config.getExtraWeightKg(), fee.getFeeName() + "的续重起始重量");
fee.setChargeText("超出 " + plain(threshold) + "kg 后续收");
fee.setPriceUnit(currency + "/KG");
return;
}
if ("POSTCODE".equals(rule))
{
String postcodes = joinPostcodes(config.getPostcodeText());
if (postcodes.isEmpty())
{
throw new ServiceException(fee.getFeeName() + "请先到附加费配置里填写邮编");
}
fee.setChargeText(cut("邮编 " + postcodes, 500));
fee.setPriceUnit(currency + "/ITEM");
return;
}
fee.setPriceUnit(currency + "/ITEM");
fee.setChargeText(isBlank(fee.getFeeDesc()) ? "" : cut(fee.getFeeDesc(), 500));
}
private void appendBoundSurcharges(PayProduct product)
{
if (product == null || product.getProductId() == null)
{
return;
}
List<PaySurcharge> fees = product.getSurcharges();
if (fees == null)
{
fees = new ArrayList<>();
product.setSurcharges(fees);
}
fees.removeIf(fee -> fee != null && CONFIG_SECTION.equals(fee.getSectionName()));
for (PrcSurchargeBind bind : surchargeBindMapper.selectByProduct(product.getProductId(), bindType(product)))
{
fees.add(toBoundFee(bind, product.getCurrency()));
}
}
private PaySurcharge toBoundFee(PrcSurchargeBind bind)
{
return toBoundFee(bind, null);
}
private PaySurcharge toBoundFee(PrcSurchargeBind bind, String productCurrency)
{
PaySurcharge fee = new PaySurcharge();
fee.setProductId(bind.getProductId());
fee.setSectionName(CONFIG_SECTION);
fee.setFeeName(bind.getScName());
fee.setScId(bind.getScId());
fee.setZoneName(blankTo(bind.getBlockTitle(), ""));
fee.setAmount(bind.getChargeValue());
fee.setCurrency(isBlank(bind.getCurrency()) ? productCurrency : bind.getCurrency().trim().toUpperCase(Locale.ROOT));
fee.setFeeDesc(bind.getScNote());
fee.setCalcRule(calcRuleOf(bind.getChargeType()));
fee.setSortNo(bind.getSortNo());
fee.setLengthCm(bind.getLengthCm());
fee.setWidthCm(bind.getWidthCm());
fee.setHeightCm(bind.getHeightCm());
fee.setOverWeightKg(bind.getOverWeightKg());
fee.setExtraWeightKg(bind.getExtraWeightKg());
fee.setPostcodeText(bind.getPostcodeText());
PrcSurcharge config = new PrcSurcharge();
config.setChargeType(bind.getChargeType());
config.setLengthCm(bind.getLengthCm());
config.setWidthCm(bind.getWidthCm());
config.setHeightCm(bind.getHeightCm());
config.setOverWeightKg(bind.getOverWeightKg());
config.setExtraWeightKg(bind.getExtraWeightKg());
config.setPostcodeText(bind.getPostcodeText());
config.setScNote(bind.getScNote());
applyConfigParam(fee, config);
return fee;
}
private static String bindType(PayProduct product)
{
if (product == null || isBlank(product.getProductType()))
{
return PAY_TYPE_AP;
}
return product.getProductType().trim().toUpperCase(Locale.ROOT);
}
private PayProductTerm requireTerm(Long termId)
{
if (termId == null)
{
throw new ServiceException("请先为价卡设置有效期");
}
PayProductTerm term = payProductTermMapper.selectById(termId);
if (term == null || term.getPriceSetId() != null)
{
throw new ServiceException("价卡版本不存在");
}
return term;
}
private static String calcRuleOf(String chargeType)
{
if (chargeType == null)
{
return "MANUAL";
}
String code = chargeType.trim().toUpperCase(Locale.ROOT);
if ("FIXED".equals(code))
{
return "ALWAYS";
}
if ("RATE".equals(code))
{
return "MANUAL";
}
if ("ALWAYS".equals(code) || "POSTCODE".equals(code) || "ZONE".equals(code) || "EXTRA_KG".equals(code)
|| "WEIGHT".equals(code) || "SIZE".equals(code) || "RETURN".equals(code) || "MANUAL".equals(code))
{
return code;
}
return "MANUAL";
}
private static BigDecimal positive(BigDecimal value, String label)
{
if (value == null || value.compareTo(BigDecimal.ZERO) <= 0)
{
throw new ServiceException(label + "要大于 0,请先到附加费配置里填写");
}
return value;
}
private static BigDecimal positiveOrZero(BigDecimal value, String label)
{
if (value == null)
{
throw new ServiceException("请先到附加费配置里填写" + label);
}
if (value.compareTo(BigDecimal.ZERO) < 0)
{
throw new ServiceException(label + "不能小于 0");
}
return value;
}
private static String joinPostcodes(String raw)
{
if (raw == null)
{
return "";
}
String normalized = raw.replace(',', ',').replace('、', ',').replace(';', ',').replace(';', ',').replace('\n', ',').replace('\r', ',');
LinkedHashSet<String> tokens = new LinkedHashSet<>();
for (String part : normalized.split(","))
{
String value = part.trim();
if (!value.isEmpty())
{
tokens.add(value);
}
}
return String.join(",", tokens);
}
private static String plain(BigDecimal value)
{
return value.stripTrailingZeros().toPlainString();
}
@Override
public PayProduct selectPayProductDetailByTerm(Long termId)
{
@@ -1041,6 +1333,7 @@ public class PayProductServiceImpl implements IPayProductService
product.setWeightBands(payProductMapper.selectWeightBandsByTerm(termIds));
product.setSurcharges(payProductMapper.selectSurchargesByTerm(termIds));
product.setExtraLines(payProductMapper.selectExtraLinesByTerm(termIds));
appendBoundSurcharges(product);
return product;
}
@@ -1626,6 +1919,7 @@ public class PayProductServiceImpl implements IPayProductService
payProductTermMapper.deleteByProductIds(ids);
payProductMapper.deleteWeightBands(ids);
payProductMapper.deleteSurcharges(ids);
surchargeBindMapper.deleteByProductIds(ids);
payProductMapper.deleteExtraLines(ids);
return payProductMapper.deletePayProducts(ids);
}
@@ -1658,11 +1952,19 @@ public class PayProductServiceImpl implements IPayProductService
for (PaySurcharge fee : payProductMapper.selectSurcharges(ids))
{
PayProduct owner = map.get(fee.getProductId());
if (owner != null)
if (owner != null && (fee == null || !CONFIG_SECTION.equals(fee.getSectionName())))
{
owner.getSurcharges().add(fee);
}
}
for (PrcSurchargeBind bind : surchargeBindMapper.selectByProductIds(ids))
{
PayProduct owner = map.get(bind.getProductId());
if (owner != null && bindType(owner).equals(bind.getProductType()))
{
owner.getSurcharges().add(toBoundFee(bind, owner.getCurrency()));
}
}
for (PayExtraLine line : payProductMapper.selectExtraLines(ids))
{
PayProduct owner = map.get(line.getProductId());
@@ -1,5 +1,7 @@
package com.bms.price.service.impl;
import java.math.BigDecimal;
import java.util.LinkedHashSet;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -8,6 +10,7 @@ import com.bms.common.exception.ServiceException;
import com.bms.common.utils.MessageUtils;
import com.bms.common.utils.StringUtils;
import com.bms.price.domain.PrcSurcharge;
import com.bms.price.mapper.PrcSurchargeBindMapper;
import com.bms.price.mapper.PrcSurchargeMapper;
import com.bms.price.mapper.PrcSurchargeRelMapper;
import com.bms.price.service.IPrcSurchargeService;
@@ -33,6 +36,9 @@ public class PrcSurchargeServiceImpl implements IPrcSurchargeService
@Autowired
private PrcSurchargeRelMapper surchargeRelMapper;
@Autowired
private PrcSurchargeBindMapper surchargeBindMapper;
@Override
public PrcSurcharge selectPrcSurchargeById(Long scId)
{
@@ -48,6 +54,7 @@ public class PrcSurchargeServiceImpl implements IPrcSurchargeService
@Override
public int insertPrcSurcharge(PrcSurcharge surcharge)
{
applyCalcParam(surcharge);
validate(surcharge);
applyInsertDefaults(surcharge);
return surchargeMapper.insertPrcSurcharge(surcharge);
@@ -60,12 +67,17 @@ public class PrcSurchargeServiceImpl implements IPrcSurchargeService
@Transactional
public int updatePrcSurcharge(PrcSurcharge surcharge)
{
applyCalcParam(surcharge);
validate(surcharge);
PrcSurcharge old = surchargeMapper.selectPrcSurchargeById(surcharge.getScId());
if (old == null)
{
throw new ServiceException("附加费不存在或已被作废,无法修改");
}
if (StringUtils.isEmpty(surcharge.getDictValue()))
{
surcharge.setDictValue(old.getDictValue());
}
// 无实际变化:不置作废、不插新记录,返回 -1 由 Controller 提示"未修改"
if (isUnchanged(old, surcharge))
{
@@ -81,6 +93,12 @@ public class PrcSurchargeServiceImpl implements IPrcSurchargeService
fresh.setChargeType(surcharge.getChargeType());
fresh.setDictValue(surcharge.getDictValue());
fresh.setChargeValue(surcharge.getChargeValue());
fresh.setLengthCm(surcharge.getLengthCm());
fresh.setWidthCm(surcharge.getWidthCm());
fresh.setHeightCm(surcharge.getHeightCm());
fresh.setOverWeightKg(surcharge.getOverWeightKg());
fresh.setExtraWeightKg(surcharge.getExtraWeightKg());
fresh.setPostcodeText(surcharge.getPostcodeText());
fresh.setCurrency(surcharge.getCurrency());
fresh.setRoundMode(surcharge.getRoundMode());
fresh.setSsCode(old.getSsCode());
@@ -92,6 +110,7 @@ public class PrcSurchargeServiceImpl implements IPrcSurchargeService
int rows = surchargeMapper.insertPrcSurcharge(fresh);
// 3. 产品关联重指向新记录,保持产品附加费配置不失效
surchargeRelMapper.rebindRelScId(surcharge.getScId(), fresh.getScId(), operator);
syncPayCards(surcharge.getScId(), fresh);
return rows;
}
@@ -103,6 +122,12 @@ public class PrcSurchargeServiceImpl implements IPrcSurchargeService
&& eq(old.getChargeType(), updated.getChargeType())
&& eq(old.getDictValue(), updated.getDictValue())
&& eqDecimal(old.getChargeValue(), updated.getChargeValue())
&& eqDecimal(old.getLengthCm(), updated.getLengthCm())
&& eqDecimal(old.getWidthCm(), updated.getWidthCm())
&& eqDecimal(old.getHeightCm(), updated.getHeightCm())
&& eqDecimal(old.getOverWeightKg(), updated.getOverWeightKg())
&& eqDecimal(old.getExtraWeightKg(), updated.getExtraWeightKg())
&& eq(old.getPostcodeText(), updated.getPostcodeText())
&& eq(old.getCurrency(), updated.getCurrency())
&& eq(old.getRoundMode(), updated.getRoundMode())
&& eq(old.getCountryCode(), updated.getCountryCode())
@@ -190,4 +215,154 @@ public class PrcSurchargeServiceImpl implements IPrcSurchargeService
throw new ServiceException(MessageUtils.message("price.surcharge.chargeValue.negative"));
}
}
/** 只保留当前计费方式用到的字段,其余条件清空。 */
private void applyCalcParam(PrcSurcharge row)
{
String type = row.getChargeType() == null ? "" : row.getChargeType().trim().toUpperCase();
BigDecimal length = null;
BigDecimal width = null;
BigDecimal height = null;
BigDecimal overWeight = null;
BigDecimal extraWeight = null;
String postcodes = null;
if ("SIZE".equals(type))
{
length = requiredSize(row.getLengthCm(), "长");
width = requiredSize(row.getWidthCm(), "宽");
height = requiredSize(row.getHeightCm(), "高");
row.setBlockTitle(null);
}
else if ("WEIGHT".equals(type))
{
overWeight = requiredWeight(row.getOverWeightKg(), "超过重量");
row.setBlockTitle(null);
}
else if ("EXTRA_KG".equals(type))
{
extraWeight = requiredWeight(row.getExtraWeightKg(), "续重起始重量");
row.setBlockTitle(null);
}
else if ("POSTCODE".equals(type))
{
postcodes = joinPostcodes(row.getPostcodeText());
if (postcodes.isEmpty())
{
throw new ServiceException("请填写邮编,多个用逗号分隔");
}
row.setBlockTitle(null);
}
else if (!"ZONE".equals(type))
{
row.setBlockTitle(null);
}
row.setLengthCm(length);
row.setWidthCm(width);
row.setHeightCm(height);
row.setOverWeightKg(overWeight);
row.setExtraWeightKg(extraWeight);
row.setPostcodeText(postcodes);
}
private static BigDecimal requiredSize(BigDecimal value, String label)
{
if (value == null || value.compareTo(BigDecimal.ZERO) <= 0)
{
throw new ServiceException(label + "要大于 0");
}
return value;
}
private static BigDecimal requiredWeight(BigDecimal value, String label)
{
if (value == null)
{
throw new ServiceException("请填写" + label);
}
if (value.compareTo(BigDecimal.ZERO) < 0)
{
throw new ServiceException(label + "不能小于 0");
}
return value;
}
/** 价卡上的附加费改指向新配置,金额和条件说明跟着新记录走。 */
private void syncPayCards(Long oldScId, PrcSurcharge fresh)
{
String rule = calcRule(fresh.getChargeType());
String currency = StringUtils.isEmpty(fresh.getCurrency()) ? null : fresh.getCurrency().trim().toUpperCase();
String priceUnit = currency == null ? null : ("EXTRA_KG".equals(rule) ? currency + "/KG" : currency + "/ITEM");
surchargeMapper.rebindPayConfig(oldScId, fresh.getScId(), fresh.getScName(),
fresh.getBlockTitle() == null ? "" : fresh.getBlockTitle(), fresh.getChargeValue(), currency,
fresh.getScNote(), rule, chargeText(fresh, rule), priceUnit);
surchargeBindMapper.rebindScId(oldScId, fresh.getScId());
}
private static String calcRule(String chargeType)
{
if (chargeType == null)
{
return "MANUAL";
}
String code = chargeType.trim().toUpperCase();
if ("FIXED".equals(code))
{
return "ALWAYS";
}
if ("RATE".equals(code))
{
return "MANUAL";
}
return code;
}
private static String chargeText(PrcSurcharge row, String rule)
{
String text;
if ("SIZE".equals(rule))
{
text = "超过 " + plain(row.getLengthCm()) + "×" + plain(row.getWidthCm()) + "×" + plain(row.getHeightCm()) + "cm";
}
else if ("WEIGHT".equals(rule))
{
text = "超过 " + plain(row.getOverWeightKg()) + "kg";
}
else if ("EXTRA_KG".equals(rule))
{
text = "超出 " + plain(row.getExtraWeightKg()) + "kg 后续收";
}
else if ("POSTCODE".equals(rule))
{
text = "邮编 " + (row.getPostcodeText() == null ? "" : row.getPostcodeText());
}
else
{
text = row.getScNote() == null ? "" : row.getScNote();
}
return text.length() > 500 ? text.substring(0, 500) : text;
}
private static String plain(BigDecimal value)
{
return value == null ? "" : value.stripTrailingZeros().toPlainString();
}
private static String joinPostcodes(String raw)
{
if (raw == null)
{
return "";
}
LinkedHashSet<String> tokens = new LinkedHashSet<>();
String normalized = raw.replace(',', ',').replace('、', ',').replace(';', ',').replace(';', ',').replace('\n', ',').replace('\r', ',');
for (String part : normalized.split(","))
{
String value = part.trim();
if (!value.isEmpty())
{
tokens.add(value);
}
}
return String.join(",", tokens);
}
}
@@ -370,28 +370,34 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
order by product_id, sort_no, band_id
</select>
<sql id="surchargeColumns">
s.surcharge_id as surchargeId, s.product_id as productId, s.term_id as termId, s.version_no as versionNo,
s.section_name as sectionName, s.fee_name as feeName, s.sc_id as scId, s.zone_name as zoneName,
s.charge_text as chargeText, s.amount, s.currency, s.price_unit as priceUnit, s.fee_desc as feeDesc,
s.period_text as periodText, s.effective_start as effectiveStart, s.effective_end as effectiveEnd,
s.charge_timing as chargeTiming, s.calc_rule as calcRule, s.calc_param as calcParam,
s.weight_min as weightMin, s.weight_max as weightMax, s.postcode_param as postcodeParam,
cfg.length_cm as lengthCm, cfg.width_cm as widthCm, cfg.height_cm as heightCm,
cfg.over_weight_kg as overWeightKg, cfg.extra_weight_kg as extraWeightKg, cfg.postcode_text as postcodeText,
s.sort_no as sortNo
</sql>
<select id="selectSurcharges" resultType="com.bms.price.domain.PaySurcharge">
select surcharge_id as surchargeId, product_id as productId, term_id as termId, version_no as versionNo, section_name as sectionName, fee_name as feeName,
sc_id as scId, zone_name as zoneName, charge_text as chargeText, amount, currency, price_unit as priceUnit, fee_desc as feeDesc,
period_text as periodText, effective_start as effectiveStart, effective_end as effectiveEnd,
charge_timing as chargeTiming, calc_rule as calcRule, calc_param as calcParam,
weight_min as weightMin, weight_max as weightMax, postcode_param as postcodeParam, sort_no as sortNo
from pay_surcharge
where product_id in
select <include refid="surchargeColumns"/>
from pay_surcharge s
left join prc_surcharge cfg on cfg.sc_id = s.sc_id and s.section_name = '附加费配置' and ifnull(cfg.is_del, '0') = '0'
where s.product_id in
<foreach collection="productIds" item="id" open="(" separator="," close=")">#{id}</foreach>
order by product_id, sort_no, surcharge_id
order by s.product_id, s.sort_no, s.surcharge_id
</select>
<select id="selectSurchargesByTerm" resultType="com.bms.price.domain.PaySurcharge">
select surcharge_id as surchargeId, product_id as productId, term_id as termId, version_no as versionNo, section_name as sectionName, fee_name as feeName,
sc_id as scId, zone_name as zoneName, charge_text as chargeText, amount, currency, price_unit as priceUnit, fee_desc as feeDesc,
period_text as periodText, effective_start as effectiveStart, effective_end as effectiveEnd,
charge_timing as chargeTiming, calc_rule as calcRule, calc_param as calcParam,
weight_min as weightMin, weight_max as weightMax, postcode_param as postcodeParam, sort_no as sortNo
from pay_surcharge
where term_id in
select <include refid="surchargeColumns"/>
from pay_surcharge s
left join prc_surcharge cfg on cfg.sc_id = s.sc_id and s.section_name = '附加费配置' and ifnull(cfg.is_del, '0') = '0'
where s.term_id in
<foreach collection="termIds" item="id" open="(" separator="," close=")">#{id}</foreach>
order by product_id, sort_no, surcharge_id
order by s.product_id, s.sort_no, s.surcharge_id
</select>
<select id="selectExtraLines" resultType="com.bms.price.domain.PayExtraLine">
@@ -418,6 +424,23 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
delete from pay_surcharge where term_id in
<foreach collection="termIds" item="id" open="(" separator="," close=")">#{id}</foreach>
</delete>
<delete id="deleteSurchargesByTermSection">
delete from pay_surcharge
where term_id = #{termId}
and section_name = #{sectionName}
</delete>
<update id="refreshSurchargeCount">
update pay_product
set surcharge_count = (
select count(1) from pay_surcharge where product_id = #{productId}
) + (
select count(1) from prc_surcharge_bind where product_id = #{productId}
),
update_time = sysdate()
where product_id = #{productId}
</update>
<delete id="deleteExtraLinesByTerm">
delete from pay_extra_line where term_id in
<foreach collection="termIds" item="id" open="(" separator="," close=")">#{id}</foreach>
@@ -0,0 +1,60 @@
<?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.price.mapper.PrcSurchargeBindMapper">
<sql id="bindColumns">
b.bind_id as bindId, b.product_id as productId, b.product_type as productType, b.sc_id as scId, b.sort_no as sortNo,
b.created_by as createdBy, b.create_time as createTime, b.updated_by as updatedBy, b.update_time as updateTime,
c.sc_name as scName, c.charge_type as chargeType, c.charge_value as chargeValue, c.currency, c.ss_code as ssCode,
c.block_title as blockTitle, c.sc_note as scNote,
c.length_cm as lengthCm, c.width_cm as widthCm, c.height_cm as heightCm,
c.over_weight_kg as overWeightKg, c.extra_weight_kg as extraWeightKg, c.postcode_text as postcodeText
</sql>
<select id="selectByProduct" resultType="com.bms.price.domain.PrcSurchargeBind">
select <include refid="bindColumns"/>
from prc_surcharge_bind b
inner join prc_surcharge c on c.sc_id = b.sc_id and ifnull(c.is_del, '0') = '0'
where b.product_id = #{productId}
and b.product_type = #{productType}
order by b.sort_no, b.bind_id
</select>
<select id="selectByProductIds" resultType="com.bms.price.domain.PrcSurchargeBind">
select <include refid="bindColumns"/>
from prc_surcharge_bind b
inner join prc_surcharge c on c.sc_id = b.sc_id and ifnull(c.is_del, '0') = '0'
where b.product_id in
<foreach collection="productIds" item="id" open="(" separator="," close=")">#{id}</foreach>
order by b.product_id, b.sort_no, b.bind_id
</select>
<delete id="deleteByProduct">
delete from prc_surcharge_bind
where product_id = #{productId}
and product_type = #{productType}
</delete>
<delete id="deleteByProductIds">
delete from prc_surcharge_bind
where product_id in
<foreach collection="productIds" item="id" open="(" separator="," close=")">#{id}</foreach>
</delete>
<insert id="insertBinds">
insert into prc_surcharge_bind (product_id, product_type, sc_id, sort_no, created_by, create_time)
values
<foreach collection="list" item="row" separator=",">
(#{row.productId}, #{row.productType}, #{row.scId}, ifnull(#{row.sortNo}, 0), #{row.createdBy}, sysdate())
</foreach>
</insert>
<update id="rebindScId">
update prc_surcharge_bind
set sc_id = #{newScId}, update_time = sysdate()
where sc_id = #{oldScId}
</update>
</mapper>
@@ -11,6 +11,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="chargeType" column="charge_type" />
<result property="dictValue" column="dict_value" />
<result property="chargeValue" column="charge_value" />
<result property="lengthCm" column="length_cm" />
<result property="widthCm" column="width_cm" />
<result property="heightCm" column="height_cm" />
<result property="overWeightKg" column="over_weight_kg" />
<result property="extraWeightKg" column="extra_weight_kg" />
<result property="postcodeText" column="postcode_text" />
<result property="currency" column="currency" />
<result property="roundMode" column="round_mode" />
<result property="ssCode" column="ss_code" />
@@ -26,7 +32,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</resultMap>
<sql id="selectPrcSurchargeVo">
select sc_id, sc_name, fee_type, charge_type, dict_value, charge_value, currency, round_mode,
select sc_id, sc_name, fee_type, charge_type, dict_value, charge_value,
length_cm, width_cm, height_cm, over_weight_kg, extra_weight_kg, postcode_text, currency, round_mode,
ss_code, is_del, country_code, block_title, sc_note, tms_id, created_by, create_time, updated_by, update_time
from prc_surcharge
</sql>
@@ -92,6 +99,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="chargeType != null and chargeType != ''">charge_type,</if>
<if test="dictValue != null">dict_value,</if>
<if test="chargeValue != null">charge_value,</if>
<if test="lengthCm != null">length_cm,</if>
<if test="widthCm != null">width_cm,</if>
<if test="heightCm != null">height_cm,</if>
<if test="overWeightKg != null">over_weight_kg,</if>
<if test="extraWeightKg != null">extra_weight_kg,</if>
<if test="postcodeText != null">postcode_text,</if>
<if test="currency != null and currency != ''">currency,</if>
<if test="roundMode != null and roundMode != ''">round_mode,</if>
<if test="ssCode != null and ssCode != ''">ss_code,</if>
@@ -108,6 +121,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="chargeType != null and chargeType != ''">#{chargeType},</if>
<if test="dictValue != null">#{dictValue},</if>
<if test="chargeValue != null">#{chargeValue},</if>
<if test="lengthCm != null">#{lengthCm},</if>
<if test="widthCm != null">#{widthCm},</if>
<if test="heightCm != null">#{heightCm},</if>
<if test="overWeightKg != null">#{overWeightKg},</if>
<if test="extraWeightKg != null">#{extraWeightKg},</if>
<if test="postcodeText != null">#{postcodeText},</if>
<if test="currency != null and currency != ''">#{currency},</if>
<if test="roundMode != null and roundMode != ''">#{roundMode},</if>
<if test="ssCode != null and ssCode != ''">#{ssCode},</if>
@@ -132,4 +151,19 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</foreach>
</update>
<update id="rebindPayConfig">
update pay_surcharge
set sc_id = #{newScId},
fee_name = #{feeName},
zone_name = #{zoneName},
amount = #{amount},
currency = ifnull(#{currency}, currency),
fee_desc = #{feeDesc},
calc_rule = #{calcRule},
charge_text = #{chargeText},
price_unit = ifnull(#{priceUnit}, price_unit)
where sc_id = #{oldScId}
and section_name = '附加费配置'
</update>
</mapper>