Commit 772335a9 by zhouhaibao

[+]新增交集处理

1 parent 3bc63b46
...@@ -266,10 +266,16 @@ public class EmployeeBadgeService { ...@@ -266,10 +266,16 @@ public class EmployeeBadgeService {
if (CollectionUtils.isNotEmpty(condition.getPermissMerchantNoList())) { if (CollectionUtils.isNotEmpty(condition.getPermissMerchantNoList())) {
if (CollectionUtils.isNotEmpty(integerList)) { if (CollectionUtils.isNotEmpty(integerList)) {
// 取并集 // 取并集
integerList = Stream.concat(condition.getPermissMerchantNoList().stream(), integerList.stream()) integerList = integerList.stream()
.distinct() // 过滤:只保留存在于 permissMerchantNoList 中的元素
.filter(condition.getPermissMerchantNoList()::contains)
.distinct() // 如果需要去重
.collect(Collectors.toList()); .collect(Collectors.toList());
if (integerList.isEmpty()){
integerList = Arrays.asList(-1);
}
} else { } else {
integerList = condition.getPermissMerchantNoList(); integerList = condition.getPermissMerchantNoList();
} }
......
  • @wangxw1
    commented
    Master

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

    1. 健壮性风险:Arrays.asList(-1) 返回的是定长集合

    • 问题描述:当交集为空时,代码使用了 integerList = Arrays.asList(-1);。Arrays.asList() 返回的是一个由数组支持的定长列表(Fixed-size List),它不支持 add() 或 remove() 操作。
    • 影响分析:如果该方法后续的代码逻辑(或调用此方法的上层逻辑)尝试对 integerList 进行元素的添加或删除,将会抛出 java.lang.UnsupportedOperationException 运行时异常,导致核心业务流程中断。
    • 优化建议:明确集合的预期行为。如果后续不需要修改该集合,建议使用 Collections.singletonList(-1) 以明确其不可变语义;如果后续需要修改,应将其包装在 ArrayList 中。 java // 推荐做法(如果后续需要修改集合) integerList = new ArrayList<>(Collections.singletonList(-1)); // 推荐做法(如果确定后续仅为只读) // integerList = Collections.singletonList(-1);

    2. 性能隐患:Stream 过滤中直接使用 List 的 contains 方法

    • 问题描述:代码在 Stream 的 filter 操作中直接调用了 condition.getPermissMerchantNoList()::contains。
    • 影响分析:如果 permissMerchantNoList 是一个普通的 ArrayList,其 contains 方法的时间复杂度为 $O(N)$。在 Stream 中遍历 integerList 时,整体时间复杂度将劣化为 $O(M \times N)$。如果这两个集合的数据量较大,会产生明显的性能瓶颈,消耗过多的 CPU 资源。
    • 优化建议:在进行交集操作前,先将作为判定条件的 List 转换为 HashSet,将 contains 的时间复杂度降为 $O(1)$,整体复杂度优化为 $O(M+N)$。 java Set<Integer> permissSet = new HashSet<>(condition.getPermissMerchantNoList()); integerList = integerList.stream() .filter(permissSet::contains) .distinct() .collect(Collectors.toList());

    3. 可维护性问题:魔法值 -1 与语义不明确的注释

    • 问题描述:代码中硬编码了 -1 作为空集时的默认值(通常是为了防止 MyBatis 在处理 IN () 时抛出 SQL 语法错误),且注释 // 如果需要去重 显得开发者对业务逻辑不确定。
    • 影响分析:硬编码的魔法值降低了代码的可读性,如果其他地方不知道 -1 的特殊含义,可能会引起误解;不确定的注释会给后续维护的工程师带来困扰,不知道到底是否应该保留 distinct()。
    • 优化建议:将 -1 提取为具有明确业务含义的常量(如 INVALID_MERCHANT_NO),并确认业务是否真需要去重,删除模棱两可的注释。 java private static final Integer INVALID_MERCHANT_NO = -1; // ... if (integerList.isEmpty()) { integerList = new ArrayList<>(Collections.singletonList(INVALID_MERCHANT_NO)); }

    二、评分明细

    1. 功能实现的正确性与健壮性(35/40分)
      • 理由:业务逻辑从“并集”改为“交集”,符合提交信息的描述。未触发 MyBatis 子查询和 Dubbo 接口变更的红线。但 Arrays.asList(-1) 存在潜在的 UnsupportedOperationException 异常风险,扣除 5 分。
    2. 安全性与潜在风险(30/30分)
      • 理由:通过交集逻辑收紧了商户权限范围,符合最小权限原则,未发现 SQL 注入、越权等安全漏洞。
    3. 最佳实践与可维护性(17/20分)
      • 理由:存在魔法值硬编码(-1)以及不确定的注释(// 如果需要去重),代码规范性有待提升,扣除 3 分。
    4. 性能与资源利用(3/5分)
      • 理由:在 Stream 过滤中直接使用 List 的 contains 方法进行交集计算,存在 $O(N2)$ 的性能劣化风险,扣除 2 分。
    5. 提交信息质量(4/5分)
      • 理由:[+]新增交集处理 清楚地说明了“做了什么(What)”,但缺少了“为什么这么做(Why)”的背景描述。整体尚可,扣除 1 分。

    总分:89分

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