Commit c54939de by 崔彦峰

refactor(goods): 重构商品上架审批服务中的规则处理逻辑

- 移除原有的单店修改原地更新模式,统一采用新建规则方式处理
- 商品和套餐的上架逻辑改为单门店时原地更新,多门店时拆分新建
- 财务和对账规则不再区分单多门店分流,统一走新建发布流程
- 删除旧的商品和套餐编码映射逻辑,统一在发布时解析真实编码
- 移除原地更新相关的验证方法和上下文状态管理字段
- 简化规则关系绑定逻辑,确保草稿编码与正式上架ID保持一致
- 删除废弃的旧规则清理和拆分相关服务方法
- 优化规则继承时的商品编码过滤机制,避免新旧商品同时生效问题
1 parent 6d20847e
......@@ -20,7 +20,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.util.*;
import java.util.stream.Collectors;
......@@ -209,73 +208,6 @@ public class GoodsListingReconciliationRuleService {
}
}
/**
* 单店修改型原地更新时复用对账主规则ID,旧对账品类、费用类目和商品关系先逻辑删除再重建。
*/
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = RpcServiceException.class)
public void deleteByReconciliationRuleId(Integer reconciliationRuleId, String updatedBy) {
RpcAssert.assertNotNull(reconciliationRuleId, DefaultErrorEnum.DATA_NULL, "reconciliationRuleId is null");
RpcAssert.assertNotEmpty(updatedBy, DefaultErrorEnum.DATA_NULL, "updatedBy is null");
try {
Date now = new Date();
List<MerchantGoodsTypeGroupReconciliationRule> reconciliationGroupRules = listOldReconciliationGroupRules(reconciliationRuleId);
List<Integer> reconciliationGroupRuleIds = collectIds(reconciliationGroupRules, MerchantGoodsTypeGroupReconciliationRule::getId);
deleteExpenseCategoryRules(reconciliationGroupRuleIds, updatedBy, now);
deleteGoodsTypeGroupRelations(reconciliationGroupRuleIds, updatedBy, now);
deleteReconciliationGroupRules(reconciliationGroupRuleIds, updatedBy, now);
} catch (RpcServiceException e) {
throw e;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RpcServiceException(DefaultErrorEnum.DATABASE_ERROR, e);
}
}
/**
* 多门店拆分时,当前门店旧对账规则下本次修改的原上架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);
// 只剔除当前门店旧对账规则中命中的原上架ID,不能扩大到其它门店规则。
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) {
List<MerchantGoodsTypeGroupReconciliationRule> list = smartcarMerchantGoodsTypeGroupReconciliationRuleMapper
.select(MerchantGoodsTypeGroupReconciliationRule.builder()
.reconciliationRuleId(reconciliationRuleId)
.deletedFlag(DeletedFlagEnum.N.name())
.build());
return CollectionUtils.isEmpty(list) ? Collections.emptyList() : list;
}
private <T> List<Integer> collectIds(List<T> list, java.util.function.Function<T, Integer> mapper) {
if(CollectionUtils.isEmpty(list)) {
return Collections.emptyList();
}
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) {
......@@ -292,28 +224,6 @@ public class GoodsListingReconciliationRuleService {
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()
// 命中的旧对账组后续会整体逻辑删除,并由Excel新规则重建。
.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) {
......@@ -325,54 +235,6 @@ public class GoodsListingReconciliationRuleService {
&& 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) {
if(CollectionUtils.isEmpty(goodsTypeGroupRuleIds)) {
return;
}
MerchantExpenseCategoryRule record = new MerchantExpenseCategoryRule();
record.setDeletedFlag(DeletedFlagEnum.D.name());
record.setUpdatedBy(updatedBy);
record.setUpdatedDate(now);
Example example = new Example(MerchantExpenseCategoryRule.class);
example.createCriteria().andIn("goodsTypeGroupRuleId", goodsTypeGroupRuleIds)
.andEqualTo("ruleType", RuleTypeEnum.RECONCILIATION_RULE.getValue())
.andEqualTo("deletedFlag", DeletedFlagEnum.N.name());
smartcarMerchantExpenseCategoryRuleMapper.updateByExampleSelective(record, example);
}
private void deleteGoodsTypeGroupRelations(List<Integer> goodsTypeGroupRuleIds, String updatedBy, Date now) {
if(CollectionUtils.isEmpty(goodsTypeGroupRuleIds)) {
return;
}
GoodsTypeGroupRelation record = new GoodsTypeGroupRelation();
record.setDeletedFlag(DeletedFlagEnum.D.name());
record.setUpdatedBy(updatedBy);
record.setUpdatedDate(now);
Example example = new Example(GoodsTypeGroupRelation.class);
example.createCriteria().andIn("goodsTypeGroupRuleId", goodsTypeGroupRuleIds)
.andEqualTo("ruleType", RuleTypeEnum.RECONCILIATION_RULE.getValue())
.andEqualTo("deletedFlag", DeletedFlagEnum.N.name());
smartcarGoodsTypeGroupRelationMapper.updateByExampleSelective(record, example);
}
private void deleteReconciliationGroupRules(List<Integer> ids, String updatedBy, Date now) {
if(CollectionUtils.isEmpty(ids)) {
return;
}
MerchantGoodsTypeGroupReconciliationRule record = new MerchantGoodsTypeGroupReconciliationRule();
record.setDeletedFlag(DeletedFlagEnum.D.name());
record.setUpdatedBy(updatedBy);
record.setUpdatedDate(now);
Example example = new Example(MerchantGoodsTypeGroupReconciliationRule.class);
example.createCriteria().andIn("id", ids)
.andEqualTo("deletedFlag", DeletedFlagEnum.N.name());
smartcarMerchantGoodsTypeGroupReconciliationRuleMapper.updateByExampleSelective(record, example);
}
public void validConfirmStep(GoodsListingApproval goodsListingApproval) {
try {
List<GoodsListingExpenseCategoryRule> allExpenseCategoryRuleList = goodsListingExpenseCategoryRuleService.listByBusinessCode(goodsListingApproval.getBusinessCode());
......
  • @wangxw1
    commented
    Master

    你好!这是一份针对你提交代码的专业审查报告。本次重构大幅简化了单/多门店的规则更新逻辑,移除了繁杂的“原地更新”与“旧规则清理”代码,统一走向“新建发布”模式,整体方向有助于降低代码复杂度和维护成本。

    但在仔细审查后,发现几个在系统长期运行中可能产生隐患的关键问题,建议进行优化。

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

    1. 历史废弃规则数据“只建不删”引发的数据膨胀与一致性风险

    • 问题描述:在 GoodsListingSettleRuleService 和 GoodsListingReconciliationRuleService 中,你删除了大量的 deleteBy... 方法。代码注释明确写道:“单店修改型无论旧规则绑定一个还是多个门店,规则都走新建发布……旧规则表本身不做删除”。
    • 影响分析:
      • 性能与存储:如果旧规则不再被任何门店或商品引用,但不做逻辑删除,随着商品频繁修改,数据库中会产生大量孤儿数据(Orphan Data),长期会导致单表数据量暴增,影响查询性能。
      • 业务一致性:如果下游系统或其它历史遗留接口依然通过原商品ID去反查规则,可能会查到多个历史版本的规则,导致分账或对账逻辑混乱。
    • 优化建议:
      • 如果业务上确实需要保留历史规则以供追溯,建议引入版本号机制或生效/失效时间区间。
      • 如果不需要追溯,建议在新建规则并绑定关系后,通过异步任务或定时清理脚本,将不再被任何关联表引用的旧主/子规则进行逻辑删除(deleted_flag = 'Y')。

    2. 核心业务编码解析缺少非空校验(Robustness)

    • 问题描述:在 GoodsListingApprovalService 的 buildOldGoodsRelation 方法中,商品编码和套餐编码的赋值改为了依赖 resolveRealMerchantGoodsCode 和 resolveRealPackageCode 方法的返回值。
    • 影响分析:若由于草稿数据异常、Excel导入解析遗漏等边界情况,导致 resolveReal... 方法返回 null 或空字符串,该空值将被直接 set 进关系对象中并落库。这会破坏数据库的数据完整性,并直接导致后续基于编码的对账和财务分账匹配失败。
    • 优化建议:在赋值前或赋值后增加严格的断言(Assert)校验,尽早暴露脏数据问题。 java // 优化示例 String realGoodsCode = resolveRealMerchantGoodsCode(context, merchantRef, relation); RpcAssert.assertNotEmpty(realGoodsCode, DefaultErrorEnum.DATA_INVALID, "解析真实商品上架编码失败"); oldRelation.setMerchantGoodsCode(realGoodsCode);

    3. 上下文对象(Context)字段粗暴移除可能引发的隐式依赖断裂

    • 问题描述:在 ApprovalPublishContext 内部类中,移除了 settleRuleInPlaceUpdateMerchantMap 等 4 个控制分流的 Map 字段。
    • 影响分析:虽然当前 diff 显示在 GoodsListingApprovalService 中已经不再向这些 Map 中 put 数据,并移除了相关的 if/else 分支。但 context 对象通常会在整个发布链路的多个 Service 之间传递。如果未展示的其他类(如 goodsListingSettleRuleService 的某些未修改方法)依然在隐式 get 这些字段,将会导致编译报错或运行时空指针异常。
    • 优化建议:
      • 请全局检索 settleRuleInPlaceUpdateMerchantMap 等被删字段,确保整个工程(包含其他相关的 Service 和 Helper 类)中没有任何遗留的调用。
      • 建议将 ApprovalPublishContext 瘦身,提取出真正跨层传递的共享状态,避免将其作为“上帝对象”滥用。

    二、评分明细

    • 功能实现的正确性与健壮性(35/40分):逻辑重构方向正确,大幅度解耦了复杂的单/多店判断逻辑。扣分项在于“废弃规则未清理”导致的潜在孤儿数据问题,以及新引入的编码解析逻辑缺少防御性编程(非空校验)。
    • 安全性与潜在风险(30/30分):未发现 SQL 注入、越权、并发安全等直接的安全漏洞。MyBatis 未使用子查询,Dubbo 接口未见违规变更。
    • 最佳实践与可维护性(17/20分):大刀阔斧地删除了冗余的清理逻辑,代码行数减少,可读性显著提升。扣分项在于异常作为业务控制流的轻微滥用(validateModifyRuleBoundToCurrentMerchant 内部既做查询又抛出异常)。
    • 性能与资源利用(4/5分):移除了大量的数据库 delete 和 update 操作,当前请求的 RT(响应时间)会变短。但长期来看存在数据表膨胀的性能隐患。
    • 提交信息质量(5/5分):Commit Message 格式规范,非常清晰地列出了重构的 8 个核心点,具备极高的可追溯性,值得表扬。

    总分:91分

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