Commit 703df24c by 崔彦峰

refactor(goods-listing): 优化商品上架审批服务中的规则处理逻辑

- 按库存模式对商品详情进行排序,虚拟在前、实物在后
- 新增多门店拆分时的规则处理逻辑,支持当前门店从原规则链路中剔除
- 添加排除商品代码的映射表,用于过滤修改的商品和套餐
- 优化旧规则复制逻辑,增加排除条件过滤
- 实现商品类型组关系的过滤和匹配功能
- 修复复制商品类型组时的空值处理问题
1 parent 5d365d6c
...@@ -464,7 +464,15 @@ public class GoodsListingApprovalService { ...@@ -464,7 +464,15 @@ public class GoodsListingApprovalService {
try { try {
taskExecutor.execute(() -> { taskExecutor.execute(() -> {
if(CollectionUtils.isNotEmpty(context.details)){ if(CollectionUtils.isNotEmpty(context.details)){
context.details.forEach(item ->{ //context.details按照库存模式升级 虚拟在前、实物在后
List<GoodsListingProductDetailDTO> sortedDetails = context.details.stream()
.sorted(Comparator.comparing(
GoodsListingProductDetailDTO::getInventoryType,
Comparator.nullsLast(Byte::compareTo)
))
.collect(Collectors.toList());
//批量处理
sortedDetails.forEach(item ->{
MerchantGoodsInfoDTO merchantGoodsInfo = MerchantGoodsInfoDTO.builder() MerchantGoodsInfoDTO merchantGoodsInfo = MerchantGoodsInfoDTO.builder()
.updatedBy(updatedBy) .updatedBy(updatedBy)
.merchantGoodsCodeList(Arrays.asList(item.getMerchantGoodsCode())) .merchantGoodsCodeList(Arrays.asList(item.getMerchantGoodsCode()))
...@@ -1378,6 +1386,11 @@ public class GoodsListingApprovalService { ...@@ -1378,6 +1386,11 @@ public class GoodsListingApprovalService {
ruleIds.divisionRuleId = divisionRule.getId(); ruleIds.divisionRuleId = divisionRule.getId();
return; return;
} }
if (Boolean.TRUE.equals(context.settleRuleSplitMerchantMap.get(merchantRef.getMerchantId())) && effectRecord != null) {
// 多门店共用旧规则时,当前门店先从原上架ID对应的旧规则链路剔除,再走新规则发布。
goodsListingSettleRuleService.removeCurrentMerchantModifyRelations(effectRecord.getRuleId(), effectRecord.getDivisionRuleId(),
context.modifyExcludeMerchantGoodsCodes, context.modifyExcludePackageCodes, context.updatedBy);
}
MerchantSettleAccountRule settleRule = buildMerchantSettleAccountRule(context, merchantRef); MerchantSettleAccountRule settleRule = buildMerchantSettleAccountRule(context, merchantRef);
smartcarMerchantSettleAccountRuleMapper.insertUseGeneratedKeys(settleRule); smartcarMerchantSettleAccountRuleMapper.insertUseGeneratedKeys(settleRule);
MerchantDivisionRule divisionRule = buildMerchantDivisionRule(context, merchantRef); MerchantDivisionRule divisionRule = buildMerchantDivisionRule(context, merchantRef);
...@@ -1385,7 +1398,8 @@ public class GoodsListingApprovalService { ...@@ -1385,7 +1398,8 @@ public class GoodsListingApprovalService {
log.info("copyOldSettleRules start,merchantId:{},businessCode:{}", merchantRef.getMerchantId(), context.businessCode); log.info("copyOldSettleRules start,merchantId:{},businessCode:{}", merchantRef.getMerchantId(), context.businessCode);
long l = System.currentTimeMillis(); long l = System.currentTimeMillis();
if (CollectionUtils.isNotEmpty(divisionRuleEffectRecords)) { if (CollectionUtils.isNotEmpty(divisionRuleEffectRecords)) {
goodsListingSettleRuleService.copyOld(settleRule, divisionRule, divisionRuleEffectRecords.get(0), context.baseRule, merchantRef); goodsListingSettleRuleService.copyOld(settleRule, divisionRule, divisionRuleEffectRecords.get(0), context.baseRule,
merchantRef, context.modifyExcludeMerchantGoodsCodes, context.modifyExcludePackageCodes);
} }
log.info("copyOldSettleRules end,merchantId:{},businessCode:{},cost:{}", merchantRef.getMerchantId(), context.businessCode, System.currentTimeMillis() - l); log.info("copyOldSettleRules end,merchantId:{},businessCode:{},cost:{}", merchantRef.getMerchantId(), context.businessCode, System.currentTimeMillis() - l);
// 旧自动分账配置不复制,避免和本次导入生成的支付渠道配置重复。 // 旧自动分账配置不复制,避免和本次导入生成的支付渠道配置重复。
...@@ -1590,12 +1604,18 @@ public class GoodsListingApprovalService { ...@@ -1590,12 +1604,18 @@ public class GoodsListingApprovalService {
context.ruleIdMap.computeIfAbsent(merchantRef.getId(), key -> new ApprovalRuleIds()).reconciliationRuleId = reconciliationRule.getId(); context.ruleIdMap.computeIfAbsent(merchantRef.getId(), key -> new ApprovalRuleIds()).reconciliationRuleId = reconciliationRule.getId();
return; return;
} }
if (Boolean.TRUE.equals(context.reconciliationRuleSplitMerchantMap.get(merchantRef.getMerchantId())) && effectRecord != null) {
// 多门店共用旧规则时,只剔除当前门店旧对账规则中本次修改的原上架ID关系。
goodsListingReconciliationRuleService.removeCurrentMerchantModifyRelations(effectRecord.getRuleId(),
context.modifyExcludeMerchantGoodsCodes, context.modifyExcludePackageCodes, context.updatedBy);
}
MerchantReconciliationRule reconciliationRule = buildMerchantReconciliationRule(context, merchantRef); MerchantReconciliationRule reconciliationRule = buildMerchantReconciliationRule(context, merchantRef);
smartcarMerchantReconciliationRuleMapper.insertUseGeneratedKeys(reconciliationRule); smartcarMerchantReconciliationRuleMapper.insertUseGeneratedKeys(reconciliationRule);
log.info("copyOldReconciliationRules start,merchantId:{},businessCode:{}", merchantRef.getMerchantId(), context.businessCode); log.info("copyOldReconciliationRules start,merchantId:{},businessCode:{}", merchantRef.getMerchantId(), context.businessCode);
long l = System.currentTimeMillis(); long l = System.currentTimeMillis();
if (CollectionUtils.isNotEmpty(divisionRuleEffectRecords)) { if (CollectionUtils.isNotEmpty(divisionRuleEffectRecords)) {
goodsListingReconciliationRuleService.copyOld(reconciliationRule, divisionRuleEffectRecords.get(0)); goodsListingReconciliationRuleService.copyOld(reconciliationRule, divisionRuleEffectRecords.get(0),
context.modifyExcludeMerchantGoodsCodes, context.modifyExcludePackageCodes);
} }
log.info("copyOldReconciliationRules end,merchantId:{},businessCode:{},cost:{}", merchantRef.getMerchantId(), context.businessCode, System.currentTimeMillis() - l); log.info("copyOldReconciliationRules end,merchantId:{},businessCode:{},cost:{}", merchantRef.getMerchantId(), context.businessCode, System.currentTimeMillis() - l);
...@@ -2219,12 +2239,37 @@ public class GoodsListingApprovalService { ...@@ -2219,12 +2239,37 @@ public class GoodsListingApprovalService {
RuleTypeEnum.SETTLE_ACCOUNT_RULE.getValue()); RuleTypeEnum.SETTLE_ACCOUNT_RULE.getValue());
boolean reconciliationRuleNeedUpdateInPlace = shouldUpdateModifyRuleInPlace(context, currentMerchantRef, boolean reconciliationRuleNeedUpdateInPlace = shouldUpdateModifyRuleInPlace(context, currentMerchantRef,
RuleTypeEnum.RECONCILIATION_RULE.getValue()); RuleTypeEnum.RECONCILIATION_RULE.getValue());
fillModifyExcludeOldCodes(context);
// 财务和对账沿各自旧规则链路独立判断,不再互相联动分流结果。 // 财务和对账沿各自旧规则链路独立判断,不再互相联动分流结果。
if (settleRuleNeedUpdateInPlace) { if (settleRuleNeedUpdateInPlace) {
context.settleRuleInPlaceUpdateMerchantMap.put(currentMerchantRef.getMerchantId(), Boolean.TRUE); context.settleRuleInPlaceUpdateMerchantMap.put(currentMerchantRef.getMerchantId(), Boolean.TRUE);
} else {
context.settleRuleSplitMerchantMap.put(currentMerchantRef.getMerchantId(), Boolean.TRUE);
} }
if (reconciliationRuleNeedUpdateInPlace) { if (reconciliationRuleNeedUpdateInPlace) {
context.reconciliationRuleInPlaceUpdateMerchantMap.put(currentMerchantRef.getMerchantId(), Boolean.TRUE); context.reconciliationRuleInPlaceUpdateMerchantMap.put(currentMerchantRef.getMerchantId(), Boolean.TRUE);
} else {
context.reconciliationRuleSplitMerchantMap.put(currentMerchantRef.getMerchantId(), Boolean.TRUE);
}
}
private void fillModifyExcludeOldCodes(ApprovalPublishContext context) {
if (context == null) {
return;
}
List<GoodsListingProductBaseDTO> modifyProducts = listModifyProducts(context);
if (CollectionUtils.isEmpty(modifyProducts)) {
return;
}
for (GoodsListingProductBaseDTO product : modifyProducts) {
if (StringUtils.isBlank(product.getOldMerchantGoodsCode())) {
continue;
}
if (Objects.equals(product.getProductType(), ProductTypeEnum.PACKAGE.getValue())) {
context.modifyExcludePackageCodes.add(product.getOldMerchantGoodsCode());
} else {
context.modifyExcludeMerchantGoodsCodes.add(product.getOldMerchantGoodsCode());
}
} }
} }
...@@ -2703,6 +2748,10 @@ public class GoodsListingApprovalService { ...@@ -2703,6 +2748,10 @@ public class GoodsListingApprovalService {
private Map<Integer, List<DivisionRuleEffectRecord>> settleRulEffectRecordByMerchantIdMap = Collections.emptyMap(); private Map<Integer, List<DivisionRuleEffectRecord>> settleRulEffectRecordByMerchantIdMap = Collections.emptyMap();
private Map<Integer, Boolean> reconciliationRuleInPlaceUpdateMerchantMap = new HashMap<>(); private Map<Integer, Boolean> reconciliationRuleInPlaceUpdateMerchantMap = new HashMap<>();
private Map<Integer, Boolean> settleRuleInPlaceUpdateMerchantMap = new HashMap<>(); private Map<Integer, Boolean> settleRuleInPlaceUpdateMerchantMap = new HashMap<>();
private Map<Integer, Boolean> reconciliationRuleSplitMerchantMap = new HashMap<>();
private Map<Integer, Boolean> settleRuleSplitMerchantMap = new HashMap<>();
private Set<String> modifyExcludeMerchantGoodsCodes = new HashSet<>();
private Set<String> modifyExcludePackageCodes = new HashSet<>();
private List<DivisionRuleEffectRecord> pendingRuleEffectRecords = new ArrayList<>(); private List<DivisionRuleEffectRecord> pendingRuleEffectRecords = new ArrayList<>();
} }
......
...@@ -67,7 +67,8 @@ public class GoodsListingReconciliationRuleService { ...@@ -67,7 +67,8 @@ public class GoodsListingReconciliationRuleService {
} }
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = RpcServiceException.class) @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RpcServiceException.class)
public void copyOld(MerchantReconciliationRule newReconciliationRule, DivisionRuleEffectRecord divisionRuleEffectRecord) { public void copyOld(MerchantReconciliationRule newReconciliationRule, DivisionRuleEffectRecord divisionRuleEffectRecord,
Set<String> excludeMerchantGoodsCodes, Set<String> excludePackageCodes) {
try { try {
// DivisionRuleEffectRecord divisionRuleEffectRecord = getByEffectDate(newReconciliationRule.getMerchantId(), newReconciliationRule.getBaseSceneCode()); // DivisionRuleEffectRecord divisionRuleEffectRecord = getByEffectDate(newReconciliationRule.getMerchantId(), newReconciliationRule.getBaseSceneCode());
if(Objects.isNull(divisionRuleEffectRecord)) { if(Objects.isNull(divisionRuleEffectRecord)) {
...@@ -86,6 +87,12 @@ public class GoodsListingReconciliationRuleService { ...@@ -86,6 +87,12 @@ public class GoodsListingReconciliationRuleService {
if(CollectionUtils.isEmpty(oldReconciliationGroupRuleList)) { if(CollectionUtils.isEmpty(oldReconciliationGroupRuleList)) {
return ; return ;
} }
oldReconciliationGroupRuleList = oldReconciliationGroupRuleList.stream()
.filter(item -> hasCopyableRelations(item.getId(), excludeMerchantGoodsCodes, excludePackageCodes))
.collect(Collectors.toList());
if(CollectionUtils.isEmpty(oldReconciliationGroupRuleList)) {
return ;
}
List<MerchantGoodsTypeGroupReconciliationRule> newGroupRuleList = new ArrayList<>(); List<MerchantGoodsTypeGroupReconciliationRule> newGroupRuleList = new ArrayList<>();
for(MerchantGoodsTypeGroupReconciliationRule item : oldReconciliationGroupRuleList) { for(MerchantGoodsTypeGroupReconciliationRule item : oldReconciliationGroupRuleList) {
...@@ -145,8 +152,14 @@ public class GoodsListingReconciliationRuleService { ...@@ -145,8 +152,14 @@ public class GoodsListingReconciliationRuleService {
List<GoodsTypeGroupRelation> oldGoodsTypeGroupRelationList = smartcarGoodsTypeGroupRelationMapper.select(GoodsTypeGroupRelation.builder() List<GoodsTypeGroupRelation> oldGoodsTypeGroupRelationList = smartcarGoodsTypeGroupRelationMapper.select(GoodsTypeGroupRelation.builder()
.goodsTypeGroupRuleId(oldGoodsTypeGroupRuleId) .goodsTypeGroupRuleId(oldGoodsTypeGroupRuleId)
.ruleType(RuleTypeEnum.RECONCILIATION_RULE.getValue()) .ruleType(RuleTypeEnum.RECONCILIATION_RULE.getValue())
.deletedFlag(DeletedFlagEnum.N.name())
.build()); .build());
if(CollectionUtils.isNotEmpty(oldGoodsTypeGroupRelationList)) { if(CollectionUtils.isNotEmpty(oldGoodsTypeGroupRelationList)) {
oldGoodsTypeGroupRelationList = oldGoodsTypeGroupRelationList.stream()
.filter(item -> !isExcludedRelation(item, excludeMerchantGoodsCodes, excludePackageCodes))
.collect(Collectors.toList());
}
if(CollectionUtils.isNotEmpty(oldGoodsTypeGroupRelationList)) {
for(GoodsTypeGroupRelation relation : oldGoodsTypeGroupRelationList) { for(GoodsTypeGroupRelation relation : oldGoodsTypeGroupRelationList) {
GoodsTypeGroupRelation newRelation = GoodsTypeGroupRelation.builder() GoodsTypeGroupRelation newRelation = GoodsTypeGroupRelation.builder()
.ruleType(relation.getRuleType()).goodsTypeGroupRuleId(newGoodsTypeGroupRuleId) .ruleType(relation.getRuleType()).goodsTypeGroupRuleId(newGoodsTypeGroupRuleId)
...@@ -213,6 +226,34 @@ public class GoodsListingReconciliationRuleService { ...@@ -213,6 +226,34 @@ public class GoodsListingReconciliationRuleService {
} }
} }
/**
* 多门店拆分时,当前门店旧对账规则下本次修改的原上架ID不再继续挂在旧对账链路上。
*/
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = RpcServiceException.class)
public void removeCurrentMerchantModifyRelations(Integer reconciliationRuleId,
Set<String> excludeMerchantGoodsCodes, Set<String> excludePackageCodes, String updatedBy) {
RpcAssert.assertNotNull(reconciliationRuleId, DefaultErrorEnum.DATA_NULL, "reconciliationRuleId is null");
RpcAssert.assertNotEmpty(updatedBy, DefaultErrorEnum.DATA_NULL, "updatedBy is null");
if(isEmptyExcludeCodes(excludeMerchantGoodsCodes, excludePackageCodes)) {
return;
}
try {
Date now = new Date();
List<MerchantGoodsTypeGroupReconciliationRule> reconciliationGroupRules = listOldReconciliationGroupRules(reconciliationRuleId);
List<Integer> reconciliationGroupRuleIds = collectIds(reconciliationGroupRules, MerchantGoodsTypeGroupReconciliationRule::getId);
List<Integer> matchedGroupRuleIds = listMatchedReconciliationGroupRuleIds(reconciliationGroupRuleIds,
excludeMerchantGoodsCodes, excludePackageCodes);
deleteExpenseCategoryRules(matchedGroupRuleIds, updatedBy, now);
deleteGoodsTypeGroupRelations(matchedGroupRuleIds, updatedBy, now);
deleteReconciliationGroupRules(matchedGroupRuleIds, updatedBy, now);
} catch (RpcServiceException e) {
throw e;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RpcServiceException(DefaultErrorEnum.DATABASE_ERROR, e);
}
}
private List<MerchantGoodsTypeGroupReconciliationRule> listOldReconciliationGroupRules(Integer reconciliationRuleId) { private List<MerchantGoodsTypeGroupReconciliationRule> listOldReconciliationGroupRules(Integer reconciliationRuleId) {
List<MerchantGoodsTypeGroupReconciliationRule> list = smartcarMerchantGoodsTypeGroupReconciliationRuleMapper List<MerchantGoodsTypeGroupReconciliationRule> list = smartcarMerchantGoodsTypeGroupReconciliationRuleMapper
.select(MerchantGoodsTypeGroupReconciliationRule.builder() .select(MerchantGoodsTypeGroupReconciliationRule.builder()
...@@ -229,6 +270,58 @@ public class GoodsListingReconciliationRuleService { ...@@ -229,6 +270,58 @@ public class GoodsListingReconciliationRuleService {
return list.stream().map(mapper).filter(Objects::nonNull).collect(Collectors.toList()); return list.stream().map(mapper).filter(Objects::nonNull).collect(Collectors.toList());
} }
private boolean hasCopyableRelations(Integer goodsTypeGroupRuleId, Set<String> excludeMerchantGoodsCodes,
Set<String> excludePackageCodes) {
if(goodsTypeGroupRuleId == null) {
return false;
}
List<GoodsTypeGroupRelation> list = smartcarGoodsTypeGroupRelationMapper.select(GoodsTypeGroupRelation.builder()
.goodsTypeGroupRuleId(goodsTypeGroupRuleId)
.ruleType(RuleTypeEnum.RECONCILIATION_RULE.getValue())
.deletedFlag(DeletedFlagEnum.N.name())
.build());
if(CollectionUtils.isEmpty(list)) {
return false;
}
return list.stream().anyMatch(item -> !isExcludedRelation(item, excludeMerchantGoodsCodes, excludePackageCodes));
}
private List<Integer> listMatchedReconciliationGroupRuleIds(List<Integer> goodsTypeGroupRuleIds,
Set<String> excludeMerchantGoodsCodes, Set<String> excludePackageCodes) {
if(CollectionUtils.isEmpty(goodsTypeGroupRuleIds) || isEmptyExcludeCodes(excludeMerchantGoodsCodes, excludePackageCodes)) {
return Collections.emptyList();
}
Example example = new Example(GoodsTypeGroupRelation.class);
example.createCriteria().andIn("goodsTypeGroupRuleId", goodsTypeGroupRuleIds)
.andEqualTo("ruleType", RuleTypeEnum.RECONCILIATION_RULE.getValue())
.andEqualTo("deletedFlag", DeletedFlagEnum.N.name());
List<GoodsTypeGroupRelation> list = smartcarGoodsTypeGroupRelationMapper.selectByExample(example);
if(CollectionUtils.isEmpty(list)) {
return Collections.emptyList();
}
return list.stream()
.filter(item -> isExcludedRelation(item, excludeMerchantGoodsCodes, excludePackageCodes))
.map(GoodsTypeGroupRelation::getGoodsTypeGroupRuleId)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList());
}
private boolean isExcludedRelation(GoodsTypeGroupRelation relation, Set<String> excludeMerchantGoodsCodes,
Set<String> excludePackageCodes) {
if(relation == null) {
return false;
}
return (CollectionUtils.isNotEmpty(excludeMerchantGoodsCodes)
&& excludeMerchantGoodsCodes.contains(relation.getMerchantGoodsCode()))
|| (CollectionUtils.isNotEmpty(excludePackageCodes)
&& excludePackageCodes.contains(relation.getPackageCode()));
}
private boolean isEmptyExcludeCodes(Set<String> excludeMerchantGoodsCodes, Set<String> excludePackageCodes) {
return CollectionUtils.isEmpty(excludeMerchantGoodsCodes) && CollectionUtils.isEmpty(excludePackageCodes);
}
private void deleteExpenseCategoryRules(List<Integer> goodsTypeGroupRuleIds, String updatedBy, Date now) { private void deleteExpenseCategoryRules(List<Integer> goodsTypeGroupRuleIds, String updatedBy, Date now) {
if(CollectionUtils.isEmpty(goodsTypeGroupRuleIds)) { if(CollectionUtils.isEmpty(goodsTypeGroupRuleIds)) {
return; return;
......
...@@ -101,7 +101,8 @@ public class GoodsListingSettleRuleService { ...@@ -101,7 +101,8 @@ public class GoodsListingSettleRuleService {
*/ */
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = RpcServiceException.class) @Transactional(propagation = Propagation.REQUIRED, rollbackFor = RpcServiceException.class)
public void copyOld(MerchantSettleAccountRule newSettleRule, MerchantDivisionRule newDivisionRule, public void copyOld(MerchantSettleAccountRule newSettleRule, MerchantDivisionRule newDivisionRule,
DivisionRuleEffectRecord effectRecord, GoodsListingBaseRule baseRule, GoodsListingMerchantRef merchantRef) { DivisionRuleEffectRecord effectRecord, GoodsListingBaseRule baseRule, GoodsListingMerchantRef merchantRef,
Set<String> excludeMerchantGoodsCodes, Set<String> excludePackageCodes) {
try { try {
// DivisionRuleEffectRecord effectRecord = getByEffectDate(newSettleRule.getMerchantId(), newSettleRule.getBaseSceneCode()); // DivisionRuleEffectRecord effectRecord = getByEffectDate(newSettleRule.getMerchantId(), newSettleRule.getBaseSceneCode());
if(Objects.isNull(effectRecord)) { if(Objects.isNull(effectRecord)) {
...@@ -122,7 +123,8 @@ public class GoodsListingSettleRuleService { ...@@ -122,7 +123,8 @@ public class GoodsListingSettleRuleService {
: listOldDivisionGroupRules(oldDivisionRule.getId()); : listOldDivisionGroupRules(oldDivisionRule.getId());
Set<Integer> oldGoodsTypeGroupIds = collectOldGoodsTypeGroupIds(oldSettleGroupRuleList, oldDivisionGroupRuleList); Set<Integer> oldGoodsTypeGroupIds = collectOldGoodsTypeGroupIds(oldSettleGroupRuleList, oldDivisionGroupRuleList);
Map<Integer, GoodsTypeGroup> oldGoodsTypeGroupMap = listOldGoodsTypeGroupMap(oldGoodsTypeGroupIds); Map<Integer, GoodsTypeGroup> oldGoodsTypeGroupMap = listOldGoodsTypeGroupMap(oldGoodsTypeGroupIds);
Map<Integer, List<GoodsTypeGroupRelation>> oldGoodsTypeGroupRelationMap = listOldGoodsTypeGroupRelationMap(oldGoodsTypeGroupIds); Map<Integer, List<GoodsTypeGroupRelation>> oldGoodsTypeGroupRelationMap = listOldGoodsTypeGroupRelationMap(oldGoodsTypeGroupIds,
excludeMerchantGoodsCodes, excludePackageCodes);
Map<Integer, List<MerchantExpenseCategoryRule>> oldExpenseCategoryRuleMap = listOldExpenseCategoryRuleMap(oldSettleGroupRuleList); Map<Integer, List<MerchantExpenseCategoryRule>> oldExpenseCategoryRuleMap = listOldExpenseCategoryRuleMap(oldSettleGroupRuleList);
Map<Integer, List<MerchantDistributionRule>> oldDistributionRuleMap = listOldDistributionRuleMap(oldDivisionGroupRuleList); Map<Integer, List<MerchantDistributionRule>> oldDistributionRuleMap = listOldDistributionRuleMap(oldDivisionGroupRuleList);
// 财务规则和分账规则引用的是同一个业务品类组。 // 财务规则和分账规则引用的是同一个业务品类组。
...@@ -179,6 +181,50 @@ public class GoodsListingSettleRuleService { ...@@ -179,6 +181,50 @@ public class GoodsListingSettleRuleService {
} }
} }
/**
* 多门店拆分时,当前门店旧规则下本次修改的原上架ID不再继续挂在旧财务/分账规则上。
*/
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = RpcServiceException.class)
public void removeCurrentMerchantModifyRelations(Integer settleRuleId, Integer divisionRuleId,
Set<String> excludeMerchantGoodsCodes, Set<String> excludePackageCodes, String updatedBy) {
RpcAssert.assertNotNull(settleRuleId, DefaultErrorEnum.DATA_NULL, "settleRuleId is null");
RpcAssert.assertNotNull(divisionRuleId, DefaultErrorEnum.DATA_NULL, "divisionRuleId is null");
RpcAssert.assertNotEmpty(updatedBy, DefaultErrorEnum.DATA_NULL, "updatedBy is null");
if(isEmptyExcludeCodes(excludeMerchantGoodsCodes, excludePackageCodes)) {
return;
}
try {
Date now = new Date();
List<MerchantGoodsTypeGroupSettleRule> settleGroupRules = listOldSettleGroupRules(settleRuleId);
List<MerchantGoodsTypeGroupDivisionRule> divisionGroupRules = listOldDivisionGroupRules(divisionRuleId);
Set<Integer> oldGoodsTypeGroupIds = collectOldGoodsTypeGroupIds(settleGroupRules, divisionGroupRules);
Set<Integer> matchedGoodsTypeGroupIds = listMatchedGoodsTypeGroupIds(oldGoodsTypeGroupIds,
excludeMerchantGoodsCodes, excludePackageCodes);
if(CollectionUtils.isEmpty(matchedGoodsTypeGroupIds)) {
return;
}
List<Integer> settleGroupRuleIds = settleGroupRules.stream()
.filter(item -> item != null && matchedGoodsTypeGroupIds.contains(item.getGoodsTypeGroupId()))
.map(MerchantGoodsTypeGroupSettleRule::getId)
.filter(Objects::nonNull)
.collect(Collectors.toList());
List<Integer> divisionGroupRuleIds = divisionGroupRules.stream()
.filter(item -> item != null && matchedGoodsTypeGroupIds.contains(item.getGoodsTypeGroupId()))
.map(MerchantGoodsTypeGroupDivisionRule::getId)
.filter(Objects::nonNull)
.collect(Collectors.toList());
deleteExpenseCategoryRules(settleGroupRuleIds, RuleTypeEnum.SETTLE_ACCOUNT_RULE.getValue(), updatedBy, now);
deleteDistributionRules(divisionGroupRuleIds, updatedBy, now);
deleteSettleGroupRules(settleGroupRuleIds, updatedBy, now);
deleteDivisionGroupRules(divisionGroupRuleIds, updatedBy, now);
} catch (RpcServiceException e) {
throw e;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RpcServiceException(DefaultErrorEnum.DATABASE_ERROR, e);
}
}
private <T> List<Integer> collectIds(List<T> list, java.util.function.Function<T, Integer> mapper) { private <T> List<Integer> collectIds(List<T> list, java.util.function.Function<T, Integer> mapper) {
if(CollectionUtils.isEmpty(list)) { if(CollectionUtils.isEmpty(list)) {
return Collections.emptyList(); return Collections.emptyList();
...@@ -313,11 +359,17 @@ public class GoodsListingSettleRuleService { ...@@ -313,11 +359,17 @@ public class GoodsListingSettleRuleService {
List<Integer> oldGoodsTypeGroupRuleIds = new ArrayList<>(); List<Integer> oldGoodsTypeGroupRuleIds = new ArrayList<>();
List<MerchantGoodsTypeGroupSettleRule> copiedSettleGroupRuleList = new ArrayList<>(); List<MerchantGoodsTypeGroupSettleRule> copiedSettleGroupRuleList = new ArrayList<>();
for(MerchantGoodsTypeGroupSettleRule item : oldSettleGroupRuleList) { for(MerchantGoodsTypeGroupSettleRule item : oldSettleGroupRuleList) {
oldGoodsTypeGroupRuleIds.add(item.getId());
Integer newGoodsTypeGroupId = copyGoodsTypeGroupIfNecessary(item.getGoodsTypeGroupId(), goodsTypeGroupMap, Integer newGoodsTypeGroupId = copyGoodsTypeGroupIfNecessary(item.getGoodsTypeGroupId(), goodsTypeGroupMap,
oldGoodsTypeGroupMap, oldGoodsTypeGroupRelationMap, copiedRelationList, now, updatedBy); oldGoodsTypeGroupMap, oldGoodsTypeGroupRelationMap, copiedRelationList, now, updatedBy);
if(newGoodsTypeGroupId == null) {
continue;
}
oldGoodsTypeGroupRuleIds.add(item.getId());
copiedSettleGroupRuleList.add(buildCopiedSettleGroupRule(item, newSettleRuleId, newGoodsTypeGroupId, now, updatedBy)); copiedSettleGroupRuleList.add(buildCopiedSettleGroupRule(item, newSettleRuleId, newGoodsTypeGroupId, now, updatedBy));
} }
if(CollectionUtils.isEmpty(copiedSettleGroupRuleList)) {
return Collections.emptyMap();
}
// 财务品类规则自身ID会被费用类目引用;这里批量插入后必须校验主键回填。 // 财务品类规则自身ID会被费用类目引用;这里批量插入后必须校验主键回填。
smartcarMerchantGoodsTypeGroupSettleRuleMapper.insertList(copiedSettleGroupRuleList); smartcarMerchantGoodsTypeGroupSettleRuleMapper.insertList(copiedSettleGroupRuleList);
Map<Integer, Integer> settleGroupRuleIdMap = new HashMap<>(); Map<Integer, Integer> settleGroupRuleIdMap = new HashMap<>();
...@@ -345,11 +397,17 @@ public class GoodsListingSettleRuleService { ...@@ -345,11 +397,17 @@ public class GoodsListingSettleRuleService {
List<Integer> oldGoodsTypeGroupRuleIds = new ArrayList<>(); List<Integer> oldGoodsTypeGroupRuleIds = new ArrayList<>();
List<MerchantGoodsTypeGroupDivisionRule> copiedDivisionGroupRuleList = new ArrayList<>(); List<MerchantGoodsTypeGroupDivisionRule> copiedDivisionGroupRuleList = new ArrayList<>();
for(MerchantGoodsTypeGroupDivisionRule item : oldDivisionGroupRuleList) { for(MerchantGoodsTypeGroupDivisionRule item : oldDivisionGroupRuleList) {
oldGoodsTypeGroupRuleIds.add(item.getId());
Integer newGoodsTypeGroupId = copyGoodsTypeGroupIfNecessary(item.getGoodsTypeGroupId(), goodsTypeGroupMap, Integer newGoodsTypeGroupId = copyGoodsTypeGroupIfNecessary(item.getGoodsTypeGroupId(), goodsTypeGroupMap,
oldGoodsTypeGroupMap, oldGoodsTypeGroupRelationMap, copiedRelationList, now, updatedBy); oldGoodsTypeGroupMap, oldGoodsTypeGroupRelationMap, copiedRelationList, now, updatedBy);
if(newGoodsTypeGroupId == null) {
continue;
}
oldGoodsTypeGroupRuleIds.add(item.getId());
copiedDivisionGroupRuleList.add(buildCopiedDivisionGroupRule(item, newDivisionRuleId, newGoodsTypeGroupId, now, updatedBy)); copiedDivisionGroupRuleList.add(buildCopiedDivisionGroupRule(item, newDivisionRuleId, newGoodsTypeGroupId, now, updatedBy));
} }
if(CollectionUtils.isEmpty(copiedDivisionGroupRuleList)) {
return;
}
// 分账品类规则自身ID会被分账分配明细引用;这里批量插入后必须校验主键回填。 // 分账品类规则自身ID会被分账分配明细引用;这里批量插入后必须校验主键回填。
smartcarMerchantGoodsTypeGroupDivisionRuleMapper.insertList(copiedDivisionGroupRuleList); smartcarMerchantGoodsTypeGroupDivisionRuleMapper.insertList(copiedDivisionGroupRuleList);
for(int i = 0; i < copiedDivisionGroupRuleList.size(); i++) { for(int i = 0; i < copiedDivisionGroupRuleList.size(); i++) {
...@@ -374,6 +432,11 @@ public class GoodsListingSettleRuleService { ...@@ -374,6 +432,11 @@ public class GoodsListingSettleRuleService {
if(newGoodsTypeGroupId != null) { if(newGoodsTypeGroupId != null) {
return newGoodsTypeGroupId; return newGoodsTypeGroupId;
} }
List<GoodsTypeGroupRelation> oldRelationList = oldGoodsTypeGroupRelationMap.get(oldGoodsTypeGroupId);
if(CollectionUtils.isEmpty(oldRelationList)) {
// 本次修改的原上架ID已被过滤,旧品类组没有剩余商品/套餐关系时不再复制该组。
return null;
}
GoodsTypeGroup oldGoodsTypeGroup = oldGoodsTypeGroupMap.get(oldGoodsTypeGroupId); GoodsTypeGroup oldGoodsTypeGroup = oldGoodsTypeGroupMap.get(oldGoodsTypeGroupId);
RpcAssert.assertNotNull(oldGoodsTypeGroup, DefaultErrorEnum.SERVER_ERROR,"复制分账品类规则异常"); RpcAssert.assertNotNull(oldGoodsTypeGroup, DefaultErrorEnum.SERVER_ERROR,"复制分账品类规则异常");
GoodsTypeGroup copiedGoodsTypeGroup = buildCopiedGoodsTypeGroup(oldGoodsTypeGroup, now, updatedBy); GoodsTypeGroup copiedGoodsTypeGroup = buildCopiedGoodsTypeGroup(oldGoodsTypeGroup, now, updatedBy);
...@@ -383,7 +446,7 @@ public class GoodsListingSettleRuleService { ...@@ -383,7 +446,7 @@ public class GoodsListingSettleRuleService {
newGoodsTypeGroupId = copiedGoodsTypeGroup.getId(); newGoodsTypeGroupId = copiedGoodsTypeGroup.getId();
goodsTypeGroupMap.put(oldGoodsTypeGroupId, newGoodsTypeGroupId); goodsTypeGroupMap.put(oldGoodsTypeGroupId, newGoodsTypeGroupId);
prepareOldGoodsTypeGroupRelations(oldGoodsTypeGroupId, newGoodsTypeGroupId, prepareOldGoodsTypeGroupRelations(oldGoodsTypeGroupId, newGoodsTypeGroupId,
oldGoodsTypeGroupRelationMap.get(oldGoodsTypeGroupId), copiedRelationList, now, updatedBy); oldRelationList, copiedRelationList, now, updatedBy);
return newGoodsTypeGroupId; return newGoodsTypeGroupId;
} }
...@@ -768,7 +831,8 @@ public class GoodsListingSettleRuleService { ...@@ -768,7 +831,8 @@ public class GoodsListingSettleRuleService {
/** /**
* 批量查询旧品类组关系明细,并按旧品类组ID分组。 * 批量查询旧品类组关系明细,并按旧品类组ID分组。
*/ */
private Map<Integer, List<GoodsTypeGroupRelation>> listOldGoodsTypeGroupRelationMap(Set<Integer> oldGoodsTypeGroupIds) { private Map<Integer, List<GoodsTypeGroupRelation>> listOldGoodsTypeGroupRelationMap(Set<Integer> oldGoodsTypeGroupIds,
Set<String> excludeMerchantGoodsCodes, Set<String> excludePackageCodes) {
if(CollectionUtils.isEmpty(oldGoodsTypeGroupIds)) { if(CollectionUtils.isEmpty(oldGoodsTypeGroupIds)) {
return Collections.emptyMap(); return Collections.emptyMap();
} }
...@@ -780,9 +844,50 @@ public class GoodsListingSettleRuleService { ...@@ -780,9 +844,50 @@ public class GoodsListingSettleRuleService {
if(CollectionUtils.isEmpty(list)) { if(CollectionUtils.isEmpty(list)) {
return Collections.emptyMap(); return Collections.emptyMap();
} }
list = list.stream()
.filter(item -> !isExcludedRelation(item, excludeMerchantGoodsCodes, excludePackageCodes))
.collect(Collectors.toList());
if(CollectionUtils.isEmpty(list)) {
return Collections.emptyMap();
}
return list.stream().collect(Collectors.groupingBy(GoodsTypeGroupRelation::getGoodsTypeGroupRuleId)); return list.stream().collect(Collectors.groupingBy(GoodsTypeGroupRelation::getGoodsTypeGroupRuleId));
} }
private Set<Integer> listMatchedGoodsTypeGroupIds(Set<Integer> oldGoodsTypeGroupIds,
Set<String> excludeMerchantGoodsCodes, Set<String> excludePackageCodes) {
if(CollectionUtils.isEmpty(oldGoodsTypeGroupIds) || isEmptyExcludeCodes(excludeMerchantGoodsCodes, excludePackageCodes)) {
return Collections.emptySet();
}
Example example = new Example(GoodsTypeGroupRelation.class);
example.createCriteria().andIn("goodsTypeGroupRuleId", oldGoodsTypeGroupIds)
.andEqualTo("ruleType", RuleTypeEnum.SETTLE_ACCOUNT_RULE.getValue())
.andEqualTo("deletedFlag", DeletedFlagEnum.N.name());
List<GoodsTypeGroupRelation> list = smartcarGoodsTypeGroupRelationMapper.selectByExample(example);
if(CollectionUtils.isEmpty(list)) {
return Collections.emptySet();
}
return list.stream()
.filter(item -> isExcludedRelation(item, excludeMerchantGoodsCodes, excludePackageCodes))
.map(GoodsTypeGroupRelation::getGoodsTypeGroupRuleId)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
}
private boolean isExcludedRelation(GoodsTypeGroupRelation relation, Set<String> excludeMerchantGoodsCodes,
Set<String> excludePackageCodes) {
if(relation == null) {
return false;
}
return (CollectionUtils.isNotEmpty(excludeMerchantGoodsCodes)
&& excludeMerchantGoodsCodes.contains(relation.getMerchantGoodsCode()))
|| (CollectionUtils.isNotEmpty(excludePackageCodes)
&& excludePackageCodes.contains(relation.getPackageCode()));
}
private boolean isEmptyExcludeCodes(Set<String> excludeMerchantGoodsCodes, Set<String> excludePackageCodes) {
return CollectionUtils.isEmpty(excludeMerchantGoodsCodes) && CollectionUtils.isEmpty(excludePackageCodes);
}
/** /**
* 批量查询旧财务费用类目明细,并按旧财务品类规则ID分组。 * 批量查询旧财务费用类目明细,并按旧财务品类规则ID分组。
*/ */
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议(Top 3)

    1. 严重的嵌套循环查询(N+1 性能瓶颈)

    • 问题描述:在 GoodsListingApprovalService 的 shouldUpdateModifyRuleInPlace 校验链路中,存在多层嵌套的 for 循环数据库查询。遍历 modifyProducts -> 查询 GoodsTypeGroupRelation -> 遍历关系查询 MerchantGoodsTypeGroupSettleRule -> 遍历规则调用 selectByPrimaryKey。
    • 影响分析:这会产生 N * M * K 次数据库查询请求。当修改的商品列表较多,或关联规则较多时,会导致数据库连接数剧增,引发严重的性能瓶颈和响应超时。
    • 优化建议:重构查询逻辑,将循环内的单条查询改为批量查询。先收集所有的 oldMerchantGoodsCode 批量查询出关联的 goodsTypeGroupRuleId,再使用 IN 语句批量查询出所需的规则明细,最后在内存中进行数据分组(如使用 Collectors.groupingBy)与校验。

    2. 批量操作中 IN 查询集合未分批的潜在风险

    • 问题描述:在 GoodsListingReconciliationRuleService 和 GoodsListingSettleRuleService 的多个 deleteXXX 逻辑中,使用了 Example.createCriteria().andIn("goodsTypeGroupRuleId", goodsTypeGroupRuleIds) 进行批量更新(逻辑删除)。
    • 影响分析:如果业务场景下传入的 goodsTypeGroupRuleIds 集合较大(如超过 1000 个),在 MySQL 等数据库中会导致 SQL 语法错误或极差的执行计划,引发服务异常。
    • 优化建议:在执行 andIn 查询前,对大集合进行分批处理。 代码示例: java // 使用 Guava 的 Lists.partition 进行分批 List<List<Integer>> partitions = Lists.partition(goodsTypeGroupRuleIds, 500); for (List<Integer> batchIds : partitions) { Example example = new Example(MerchantExpenseCategoryRule.class); example.createCriteria().andIn("goodsTypeGroupRuleId", batchIds) .andEqualTo("ruleType", RuleTypeEnum.RECONCILIATION_RULE.getValue()) .andEqualTo("deletedFlag", DeletedFlagEnum.N.name()); smartcarMerchantExpenseCategoryRuleMapper.updateByExampleSelective(record, example); }

    3. 异常捕获范围过大且错误码包装不当

    • 问题描述:在服务类的删除方法中(如 deleteByReconciliationRuleId),使用了 catch (Exception e) 捕获所有异常,并统一包装为 RpcServiceException(DefaultErrorEnum.DATABASE_ERROR, e) 抛出。
    • 影响分析:如果代码中发生了非数据库异常(如 NullPointerException、IndexOutOfBoundsException 等),也会被错误地标记为 DATABASE_ERROR。这会严重误导后续的线上问题排查。
    • 优化建议:细化异常捕获类型。应当优先捕获预期的数据库异常(如 Spring 的 DataAccessException),对于未知的 RuntimeException 则直接抛出或赋予更准确的错误码,保持异常栈的纯粹性。

    二、评分明细

    • 功能实现的正确性与健壮性(36 / 40分): 核心业务逻辑(多门店拆分、原地更新、排序处理等)实现清晰。使用 Comparator.nullsLast 避免了潜在的 NPE,防御性编程(如空集合校验)做得较好。未发现 MyBatis 子查询及 Dubbo Remote 接口参数变更等危险实现。

    • 安全性与潜在风险(28 / 30分): 整体逻辑安全,未见 SQL 注入或越权隐患。扣分点在于过于宽泛的异常捕获机制可能会掩盖真实的运行时异常,增加线上运维风险。

    • 最佳实践与可维护性(16 / 20分): 代码结构较好,对复杂逻辑进行了方法抽取。但部分 Service 类存在逻辑过度膨胀的问题,且通过 build 对象后手动置空 createdBy 字段以适配 updateSelective 的写法略显生硬,建议构建专用的 UpdateDTO。

    • 性能与资源利用(1 / 5分): 存在严重的嵌套循环数据库查询(典型的 N+1 到 N*M*K 问题),以及未做分批保护的大集合 IN 查询,在数据量稍大时会导致明显的性能瓶颈,亟需重构。

    • 提交信息质量(5 / 5分): Commit 描述非常清晰,详细列出了重构点、新增方法及修复的内容,便于代码追溯和 Code Review。

    总分:86分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!