Commit ca39ff8c by 崔彦峰

修改覆盖旧规则目标分账归属。

1 parent 466e51d3
...@@ -918,7 +918,7 @@ public class GoodsListingApprovalService { ...@@ -918,7 +918,7 @@ public class GoodsListingApprovalService {
smartcarMerchantDivisionRuleMapper.insertUseGeneratedKeys(divisionRule); smartcarMerchantDivisionRuleMapper.insertUseGeneratedKeys(divisionRule);
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();
goodsListingSettleRuleService.copyOld(settleRule, divisionRule, divisionRuleEffectRecords.get(0)); goodsListingSettleRuleService.copyOld(settleRule, divisionRule, divisionRuleEffectRecords.get(0), context.baseRule, merchantRef);
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);
// 旧自动分账配置不复制,避免和本次导入生成的支付渠道配置重复。 // 旧自动分账配置不复制,避免和本次导入生成的支付渠道配置重复。
insertDivisionRuleAutos(context, divisionRule.getId()); insertDivisionRuleAutos(context, divisionRule.getId());
......
...@@ -5,10 +5,7 @@ import cn.com.glsx.dj.goods.listing.dto.GoodsListingGoodsTypeGroupDTO; ...@@ -5,10 +5,7 @@ import cn.com.glsx.dj.goods.listing.dto.GoodsListingGoodsTypeGroupDTO;
import cn.com.glsx.dj.goods.listing.mapper.old.*; import cn.com.glsx.dj.goods.listing.mapper.old.*;
import cn.com.glsx.dj.goods.listing.model.*; import cn.com.glsx.dj.goods.listing.model.*;
import cn.com.glsx.dj.smartcarlife.dto.DivisionRuleEffectRecordDTO; import cn.com.glsx.dj.smartcarlife.dto.DivisionRuleEffectRecordDTO;
import cn.com.glsx.dj.smartcarlife.enums.BaseSceneEnum; import cn.com.glsx.dj.smartcarlife.enums.*;
import cn.com.glsx.dj.smartcarlife.enums.DistributionRuleTypeEnum;
import cn.com.glsx.dj.smartcarlife.enums.RpcErrorEnum;
import cn.com.glsx.dj.smartcarlife.enums.RuleTypeEnum;
import cn.com.glsx.dj.smartcarlife.model.*; import cn.com.glsx.dj.smartcarlife.model.*;
import cn.com.glsx.framework.core.beans.BaseModel.DeletedFlagEnum; import cn.com.glsx.framework.core.beans.BaseModel.DeletedFlagEnum;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum; import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
...@@ -103,7 +100,8 @@ public class GoodsListingSettleRuleService { ...@@ -103,7 +100,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, DivisionRuleEffectRecord effectRecord) { public void copyOld(MerchantSettleAccountRule newSettleRule, MerchantDivisionRule newDivisionRule,
DivisionRuleEffectRecord effectRecord, GoodsListingBaseRule baseRule, GoodsListingMerchantRef merchantRef) {
try { try {
// DivisionRuleEffectRecord effectRecord = getByEffectDate(newSettleRule.getMerchantId(), newSettleRule.getBaseSceneCode()); // DivisionRuleEffectRecord effectRecord = getByEffectDate(newSettleRule.getMerchantId(), newSettleRule.getBaseSceneCode());
if(Objects.isNull(effectRecord)) { if(Objects.isNull(effectRecord)) {
...@@ -138,7 +136,8 @@ public class GoodsListingSettleRuleService { ...@@ -138,7 +136,8 @@ public class GoodsListingSettleRuleService {
copiedRelationList, now, updatedBy); copiedRelationList, now, updatedBy);
Integer newDivisionRuleId = newDivisionRule == null ? null : newDivisionRule.getId(); Integer newDivisionRuleId = newDivisionRule == null ? null : newDivisionRule.getId();
copyOldDivisionGroups(oldDivisionGroupRuleList, newDivisionRuleId, goodsTypeGroupMap, oldGoodsTypeGroupMap, copyOldDivisionGroups(oldDivisionGroupRuleList, newDivisionRuleId, goodsTypeGroupMap, oldGoodsTypeGroupMap,
oldGoodsTypeGroupRelationMap, oldDistributionRuleMap, copiedRelationList, copiedDistributionRuleList, now, updatedBy); oldGoodsTypeGroupRelationMap, oldDistributionRuleMap, copiedRelationList, copiedDistributionRuleList,
now, updatedBy, baseRule, merchantRef);
copyOldExpenseCategoryRulesBatch(settleGroupRuleIdMap, oldExpenseCategoryRuleMap, now, updatedBy); copyOldExpenseCategoryRulesBatch(settleGroupRuleIdMap, oldExpenseCategoryRuleMap, now, updatedBy);
insertCopiedGoodsTypeGroupRelations(copiedRelationList); insertCopiedGoodsTypeGroupRelations(copiedRelationList);
insertCopiedDistributionRules(copiedDistributionRuleList); insertCopiedDistributionRules(copiedDistributionRuleList);
...@@ -215,7 +214,7 @@ public class GoodsListingSettleRuleService { ...@@ -215,7 +214,7 @@ public class GoodsListingSettleRuleService {
Map<Integer, List<GoodsTypeGroupRelation>> oldGoodsTypeGroupRelationMap, Map<Integer, List<GoodsTypeGroupRelation>> oldGoodsTypeGroupRelationMap,
Map<Integer, List<MerchantDistributionRule>> oldDistributionRuleMap, Map<Integer, List<MerchantDistributionRule>> oldDistributionRuleMap,
List<GoodsTypeGroupRelation> copiedRelationList, List<MerchantDistributionRule> copiedDistributionRuleList, List<GoodsTypeGroupRelation> copiedRelationList, List<MerchantDistributionRule> copiedDistributionRuleList,
Date now, String updatedBy) { Date now, String updatedBy, GoodsListingBaseRule baseRule, GoodsListingMerchantRef merchantRef) {
if(CollectionUtils.isEmpty(oldDivisionGroupRuleList)) { if(CollectionUtils.isEmpty(oldDivisionGroupRuleList)) {
return ; return ;
} }
...@@ -235,7 +234,7 @@ public class GoodsListingSettleRuleService { ...@@ -235,7 +234,7 @@ public class GoodsListingSettleRuleService {
assertGeneratedId(copiedRule.getId(), "复制分账品类规则异常"); assertGeneratedId(copiedRule.getId(), "复制分账品类规则异常");
Integer oldGoodsTypeGroupRuleId = oldGoodsTypeGroupRuleIds.get(i); Integer oldGoodsTypeGroupRuleId = oldGoodsTypeGroupRuleIds.get(i);
prepareOldDistributionRules(oldGoodsTypeGroupRuleId, copiedRule.getId(), oldDistributionRuleMap.get(oldGoodsTypeGroupRuleId), prepareOldDistributionRules(oldGoodsTypeGroupRuleId, copiedRule.getId(), oldDistributionRuleMap.get(oldGoodsTypeGroupRuleId),
copiedDistributionRuleList, now, updatedBy); copiedDistributionRuleList, now, updatedBy, baseRule, merchantRef);
} }
} }
...@@ -357,11 +356,12 @@ public class GoodsListingSettleRuleService { ...@@ -357,11 +356,12 @@ public class GoodsListingSettleRuleService {
*/ */
private void prepareOldDistributionRules(Integer oldGoodsTypeGroupRuleId, Integer newGoodsTypeGroupRuleId, private void prepareOldDistributionRules(Integer oldGoodsTypeGroupRuleId, Integer newGoodsTypeGroupRuleId,
List<MerchantDistributionRule> oldDistributionRuleList, List<MerchantDistributionRule> copiedDistributionRuleList, List<MerchantDistributionRule> oldDistributionRuleList, List<MerchantDistributionRule> copiedDistributionRuleList,
Date now, String updatedBy) { Date now, String updatedBy,GoodsListingBaseRule baseRule, GoodsListingMerchantRef merchantRef) {
if(CollectionUtils.isEmpty(oldDistributionRuleList)) { if(CollectionUtils.isEmpty(oldDistributionRuleList)) {
return ; return ;
} }
oldDistributionRuleList.forEach(item -> copiedDistributionRuleList.add(buildCopiedDistributionRule(item, newGoodsTypeGroupRuleId, now, updatedBy))); oldDistributionRuleList.forEach(item ->
copiedDistributionRuleList.add(buildCopiedDistributionRule(item, newGoodsTypeGroupRuleId, now, updatedBy, baseRule, merchantRef)));
} }
/** /**
...@@ -487,22 +487,56 @@ public class GoodsListingSettleRuleService { ...@@ -487,22 +487,56 @@ public class GoodsListingSettleRuleService {
* 构建新的分账分配明细,明细自身ID后续不被引用,可在唯一性归一后批量入库。 * 构建新的分账分配明细,明细自身ID后续不被引用,可在唯一性归一后批量入库。
*/ */
private MerchantDistributionRule buildCopiedDistributionRule(MerchantDistributionRule oldRule, private MerchantDistributionRule buildCopiedDistributionRule(MerchantDistributionRule oldRule,
Integer newGoodsTypeGroupRuleId, Date now, String updatedBy) { Integer newGoodsTypeGroupRuleId, Date now, String updatedBy,GoodsListingBaseRule baseRule, GoodsListingMerchantRef merchantRef) {
return MerchantDistributionRule.builder()
.goodsTypeGroupRuleId(newGoodsTypeGroupRuleId) MerchantDistributionRule merchantDistributionRule = new MerchantDistributionRule();
.ruleType(oldRule.getRuleType()) merchantDistributionRule.setGoodsTypeGroupRuleId(newGoodsTypeGroupRuleId);
.targetMerchantType(oldRule.getTargetMerchantType()) merchantDistributionRule.setRuleType(oldRule.getRuleType());
.targetMerchantLevel(oldRule.getTargetMerchantLevel()) merchantDistributionRule.setTargetMerchantType(oldRule.getTargetMerchantType());
.merchantId(oldRule.getMerchantId()) merchantDistributionRule.setExpenseCategoryId(oldRule.getExpenseCategoryId());
.merchantName(oldRule.getMerchantName()) merchantDistributionRule.setExpenseCategoryName(oldRule.getExpenseCategoryName());
.expenseCategoryId(oldRule.getExpenseCategoryId()) merchantDistributionRule.setCreatedBy(updatedBy);
.expenseCategoryName(oldRule.getExpenseCategoryName()) merchantDistributionRule.setCreatedDate(now);
.createdBy(updatedBy) merchantDistributionRule.setUpdatedBy(updatedBy);
.createdDate(now) merchantDistributionRule.setUpdatedDate(now);
.updatedBy(updatedBy) merchantDistributionRule.setDeletedFlag(DeletedFlagEnum.N.name());
.updatedDate(now) merchantDistributionRule.setTargetMerchantLevel(oldRule.getTargetMerchantLevel());
.deletedFlag(DeletedFlagEnum.N.name()) merchantDistributionRule.setMerchantId(oldRule.getMerchantId());
.build(); merchantDistributionRule.setMerchantName(oldRule.getMerchantName());
if (Objects.equals(baseRule.getShopMerchantDivideTarget(), MerchantDistributionRuleTargetMerchantTypeEnum.SHOP4S.getValue())) {
merchantDistributionRule.setTargetMerchantLevel(baseRule.getRealityShopLevel());
if (Objects.equals(TargetMerchantLevelEnum.SUPERIOR.getValue(),baseRule.getRealityShopLevel())) {
merchantDistributionRule.setMerchantId(merchantRef.getParentMerchantId());
merchantDistributionRule.setMerchantName(merchantRef.getParentMerchantName());
}
else {
merchantDistributionRule.setMerchantId(merchantRef.getMerchantId());
merchantDistributionRule.setMerchantName(merchantRef.getMerchantName());
}
}
if (Objects.equals(baseRule.getShopMerchantDivideTarget(), MerchantDistributionRuleTargetMerchantTypeEnum.PLATFORM.getValue())) {
merchantDistributionRule.setMerchantId(merchantRef.getContractingMerchantId());
merchantDistributionRule.setMerchantName(merchantRef.getContractingMerchantName());
}
return merchantDistributionRule;
// return MerchantDistributionRule.builder()
// .goodsTypeGroupRuleId(newGoodsTypeGroupRuleId)
// .ruleType(oldRule.getRuleType())
// .targetMerchantType(oldRule.getTargetMerchantType())
// .targetMerchantLevel(oldRule.getTargetMerchantLevel())
// .merchantId(oldRule.getMerchantId())
// .merchantName(oldRule.getMerchantName())
// .expenseCategoryId(oldRule.getExpenseCategoryId())
// .expenseCategoryName(oldRule.getExpenseCategoryName())
// .createdBy(updatedBy)
// .createdDate(now)
// .updatedBy(updatedBy)
// .updatedDate(now)
// .deletedFlag(DeletedFlagEnum.N.name())
// .build();
} }
/** /**
......
  • @wangxw1
    commented
    Master

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

    1. 【功能健壮性】潜在的 NullPointerException (NPE) 风险

    • 问题描述:在 GoodsListingSettleRuleService.java 的 buildCopiedDistributionRule 方法中,新增了对 baseRule 和 merchantRef 对象的直接方法调用(如 baseRule.getShopMerchantDivideTarget()、merchantRef.getParentMerchantId() 等),但在整个调用链路中并未对这两个对象进行判空处理。
    • 影响分析:如果上游传入的 context.baseRule 或 merchantRef 为 null,程序运行至此处将抛出 NullPointerException,导致整个复制旧规则的事务回滚,引发核心业务中断。
    • 优化建议:在使用对象属性前增加防御性判空逻辑;或者在 copyOld 方法入口处进行参数校验(Fail-fast)。

    代码示例:

    // 在 buildCopiedDistributionRule 方法中增加判空
    if (baseRule != null && Objects.equals(baseRule.getShopMerchantDivideTarget(), MerchantDistributionRuleTargetMerchantTypeEnum.SHOP4S.getValue())) {
        merchantDistributionRule.setTargetMerchantLevel(baseRule.getRealityShopLevel());
        if (merchantRef != null) {
            // ... 继续使用 merchantRef 的逻辑
        }
    }
    

    2. 【代码可维护性】遗留大段被注释的废弃代码

    • 问题描述:在 buildCopiedDistributionRule 方法的末尾,原有的 MerchantDistributionRule.builder()...build() 代码被大段注释掉,而没有直接删除。
    • 影响分析:将废弃代码注释保留在源文件中是典型的“代码坏味道”(Bad Smell)。这会降低代码的可读性,干扰其他开发者的视线,且 Git 等版本控制系统已经具备完善的历史追溯功能,无需通过注释保留旧代码。
    • 优化建议:直接删除被注释掉的旧版本 return MerchantDistributionRule.builder()... 代码块。

    3. 【工程规范】破坏了原有的 Builder 模式并引入通配符导入

    • 问题描述:
      1. buildCopiedDistributionRule 方法原本使用了 Lombok 的 @Builder 模式进行对象构建,修改后退化成了繁琐的 new Object() + 连续 set 的方式。
      2. 文件头部的导入语句被修改为了通配符导入 import cn.com.glsx.dj.smartcarlife.enums.*;。
    • 影响分析:放弃 Builder 模式导致代码行数激增,降低了代码的优雅性和连贯性;通配符导入(Wildcard imports)可能会导致类名冲突,增加类加载的解析成本,不符合主流 Java 编码规范。
    • 优化建议:恢复明确的类导入;继续使用 Builder 模式构建基础对象,然后根据条件覆盖特定字段。

    代码示例:

    // 推荐的 Builder 结合条件赋值的写法
    MerchantDistributionRule.MerchantDistributionRuleBuilder builder = MerchantDistributionRule.builder()
            .goodsTypeGroupRuleId(newGoodsTypeGroupRuleId)
            .ruleType(oldRule.getRuleType())
            // ... 设置其他基础属性
            .merchantName(oldRule.getMerchantName());
    
    if (baseRule != null && merchantRef != null) {
        if (Objects.equals(baseRule.getShopMerchantDivideTarget(), MerchantDistributionRuleTargetMerchantTypeEnum.SHOP4S.getValue())) {
            builder.targetMerchantLevel(baseRule.getRealityShopLevel());
            if (Objects.equals(TargetMerchantLevelEnum.SUPERIOR.getValue(), baseRule.getRealityShopLevel())) {
                builder.merchantId(merchantRef.getParentMerchantId())
                       .merchantName(merchantRef.getParentMerchantName());
            } else {
                builder.merchantId(merchantRef.getMerchantId())
                       .merchantName(merchantRef.getMerchantName());
            }
        } else if (Objects.equals(baseRule.getShopMerchantDivideTarget(), MerchantDistributionRuleTargetMerchantTypeEnum.PLATFORM.getValue())) {
            builder.merchantId(merchantRef.getContractingMerchantId())
                   .merchantName(merchantRef.getContractingMerchantName());
        }
    }
    return builder.build();
    

    二、评分明细

    • 功能实现的正确性与健壮性(35/40分):业务逻辑修改符合预期,正确实现了根据 baseRule 动态覆盖分账归属的需求。但由于缺乏对新增入参对象的判空处理,存在 NPE 隐患,扣除 5 分。(注:未发现 MyBatis 子查询及 Dubbo Remote 接口参数变更违规)。
    • 安全性与潜在风险(30/30分):本次变更属于内部业务逻辑调整,未引入 SQL 注入、越权等安全漏洞。
    • 最佳实践与可维护性(12/20分):代码中遗留了大段无用的注释代码,且将原有的 Builder 模式改为了冗长的 setter 模式,同时引入了通配符导入(.*),不符合整洁代码(Clean Code)原则,扣除 8 分。
    • 性能与资源利用(5/5分):对象创建和赋值操作对性能影响微乎其微,无性能瓶颈。
    • 提交信息质量(4/5分):Commit 信息“修改覆盖旧规则目标分账归属”清晰说明了修改内容(What),但若能简单补充修改的原因(Why)会更加完美,扣除 1 分。

    总分:86分

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