Commit 031f04c1 by 周立明

fix(merchantSettleDistribution): 优化分账规则测试的升级商品ID获取逻辑

1. 调整获取upgradeGoodsId的优先级,优先使用goodsTypeGroupRelation中的字段
2. 增加配置开关控制是否设置测试商户商品编码
3. 恢复并重构了远程查询商户商品信息的逻辑
1 parent 7f3d4f15
...@@ -19,6 +19,7 @@ import cn.com.glsx.framework.core.util.RpcAssert; ...@@ -19,6 +19,7 @@ import cn.com.glsx.framework.core.util.RpcAssert;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.oreframework.boot.env.OrePropertyConfigurer;
import org.oreframework.distribute.lock.annotation.GlsxDistributeLockable; import org.oreframework.distribute.lock.annotation.GlsxDistributeLockable;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
...@@ -297,15 +298,17 @@ public class DivisionRuleTestService { ...@@ -297,15 +298,17 @@ public class DivisionRuleTestService {
} }
switch (categoryTypeEnum) { switch (categoryTypeEnum) {
case UPGRADE_GOODS: case UPGRADE_GOODS:
testUpgradeGoodsId = goodsTypeGroupRelation.getUpgradeGoodsId();
if (StringUtils.isNotBlank(goodsTypeGroupRelation.getMerchantGoodsCode())) { if (StringUtils.isNotBlank(goodsTypeGroupRelation.getMerchantGoodsCode())) {
testMerchantGoodsCode = goodsTypeGroupRelation.getMerchantGoodsCode(); testMerchantGoodsCode = goodsTypeGroupRelation.getMerchantGoodsCode();
// MerchantGoodsInfoDTO merchantGoodsInfo = merchantGoodsInfoService.getDetailByMerchantGoodsCode( if(testUpgradeGoodsId == null) {
// goodsTypeGroupRelation.getMerchantGoodsCode(), record.getMerchantId()); MerchantGoodsInfoDTO merchantGoodsInfo = merchantGoodsInfoService.getDetailByMerchantGoodsCode(
// if (merchantGoodsInfo != null) { goodsTypeGroupRelation.getMerchantGoodsCode(), record.getMerchantId());
// testUpgradeGoodsId = merchantGoodsInfo.getUpgradeGoodsId(); if (merchantGoodsInfo != null) {
// } testUpgradeGoodsId = merchantGoodsInfo.getUpgradeGoodsId();
}
}
} }
testUpgradeGoodsId = goodsTypeGroupRelation.getUpgradeGoodsId();
break; break;
case GOODS_TYPE: case GOODS_TYPE:
testGoodsType = goodsTypeGroupRelation.getGoodsType(); testGoodsType = goodsTypeGroupRelation.getGoodsType();
...@@ -319,7 +322,9 @@ public class DivisionRuleTestService { ...@@ -319,7 +322,9 @@ public class DivisionRuleTestService {
} }
goodsOrder.setTestGoodsType(testGoodsType); goodsOrder.setTestGoodsType(testGoodsType);
goodsOrder.setTestUpgradeGoodsId(testUpgradeGoodsId); goodsOrder.setTestUpgradeGoodsId(testUpgradeGoodsId);
if(Objects.equals(true, OrePropertyConfigurer.getProperty("division.rule.test.open.real.goods", Boolean.class, true))) {
goodsOrder.setTestMerchantGoodsCode(testMerchantGoodsCode); goodsOrder.setTestMerchantGoodsCode(testMerchantGoodsCode);
}
goodsOrder.setTestPackageCode(testPackageCode); goodsOrder.setTestPackageCode(testPackageCode);
goodsOrder.setSceneType(groupDivisionRuleDTO.getSceneType()); goodsOrder.setSceneType(groupDivisionRuleDTO.getSceneType());
goodsOrder.setBaseSceneCode(groupDivisionRuleDTO.getBaseSceneCode()); goodsOrder.setBaseSceneCode(groupDivisionRuleDTO.getBaseSceneCode());
......
  • @wangxw1
    commented
    Master

    这是一份针对您提交代码的专业审查报告。代码整体逻辑清晰,提交信息规范,但在性能、工程规范和代码健壮性上还有进一步优化的空间。

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

    1. 潜在的 N+1 查询/RPC 调用性能瓶颈(重要性:高)

    • 问题描述:从代码上下文 goodsTypeGroupRelation 变量推测,该 switch 语句极有可能处于一个循环之中(遍历商品分组关系)。在 UPGRADE_GOODS 分支内,当 testUpgradeGoodsId == null 时,直接在循环内部调用了 merchantGoodsInfoService.getDetailByMerchantGoodsCode 进行单条数据查询。
    • 影响分析:如果循环次数较多,且存在多个 testUpgradeGoodsId 为空的记录,会导致在循环中频繁发起数据库查询或 RPC 调用(即 N+1 问题),严重拖慢接口响应时间,甚至耗尽连接池资源。
    • 优化建议:将循环内的单条查询改为循环外批量查询。在循环前先提取所有需要查询的 MerchantGoodsCode 集合,调用批量接口获取结果并组装为 Map,然后在循环内直接从 Map 中获取数据。

    2. 业务代码与配置工具强耦合及魔法字符串(重要性:中)

    • 问题描述:使用 OrePropertyConfigurer.getProperty("division.rule.test.open.real.goods", ...) 直接在业务方法内部获取配置。同时使用了硬编码的魔法字符串。另外,判断布尔值使用了 Objects.equals(true, ...),不够直观。
    • 影响分析:
      1. 业务逻辑与特定的静态配置工具类强耦合,导致该 Service 类难以进行脱离容器的单元测试(Mock 困难)。
      2. 魔法字符串容易拼写错误且难以统一维护。
    • 优化建议:推荐使用 Spring 的 @Value 注解将配置注入为类的成员变量,并使用 Boolean.TRUE.equals() 进行安全的拆箱判断。
    • 代码示例: ```java // 在类中注入 @Value("${division.rule.test.open.real.goods:true}") private Boolean openRealGoods;

    // 在方法中使用 if(Boolean.TRUE.equals(openRealGoods)) { goodsOrder.setTestMerchantGoodsCode(testMerchantGoodsCode); }

    
    **3. 循环内的变量覆盖风险(重要性:低)**
    - **问题描述**:`testUpgradeGoodsId` 和 `testMerchantGoodsCode` 变量在 `UPGRADE_GOODS` 分支中被赋值。如果外部存在循环,且列表中有多个 `UPGRADE_GOODS` 类型的元素,后一个元素的值会直接覆盖前一个元素的值。
    - **影响分析**:如果业务逻辑设计上 `UPGRADE_GOODS` 确实只有一条,则没有问题;但如果不止一条,最终赋给 `goodsOrder` 的将始终是最后一条的数据,这可能引发意料之外的逻辑错误。
    - **优化建议**:请确认业务上是否允许多个 `UPGRADE_GOODS` 存在。如果只取第一个有效值,建议在找到非空值后增加控制标记;如果需要取最后一个,建议添加注释说明此处“Last-wins”的设计意图。
    
    ---
    
    #### 二、评分明细
    
    *   **功能实现的正确性与健壮性:32/40分**
        *   **理由**:核心的 fallback 获取 ID 逻辑修复是正确的(先取关联表,为空再查商品表)。未触碰 MyBatis 子查询及 Dubbo 接口变更等红线。扣分项在于循环内变量覆盖的潜在风险以及对异常场景(如查询服务超时/异常)缺乏显式兜底处理。
    *   **安全性与潜在风险:30/30分**
        *   **理由**:本次变更仅涉及内部业务逻辑和配置开关,未引入用户输入直接参与 SQL 拼接或系统命令执行,不存在越权、XSS 或 SQL 注入等安全隐患。
    *   **最佳实践与可维护性:14/20分**
        *   **理由**:代码可读性尚可,但存在魔法字符串、依赖静态配置工具类等不符合 Spring 最佳实践的问题。布尔值判断方式略显生硬。
    *   **性能与资源利用:2/5分**
        *   **理由**:大概率在循环内部引入了外部服务调用(N+1 问题),这是非常典型的性能隐患,在数据量放大时会导致明显的性能瓶颈。
    *   **提交信息质量:5/5分**
        *   **理由**:Commit message 非常优秀。遵循了 `fix(scope): subject` 规范,并且分条列出了具体的改动点(优先级调整、配置开关、恢复远程查询),清晰、准确、高度可追溯。
    
    总分:83分
    
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!