Commit fcc4ea19 by 谢海旭

策略中心:优化代码

1 parent aea26b44
......@@ -47,10 +47,13 @@ public class StrategyStaffRelService {
//根据手机号,查询北森人员userId
List<String> phoneList = staffList.stream().map(StrategyStaffRel::getUserPhone).collect(Collectors.toList());
List<BeiSenEmployeeListResonseDTO> employeeList = listEmployeeInfoByStaffPhone(phoneList);
Map<String, Integer> phoneUserIdMap = Collections.EMPTY_MAP;
Map<String, Integer> phoneUserIdMap = new HashMap<>();
if (CollectionUtils.isNotEmpty(employeeList)) {
//构建map
phoneUserIdMap = employeeList.stream().collect(Collectors.toMap(BeiSenEmployeeListResonseDTO::getMobilePhone, BeiSenEmployeeListResonseDTO::getUserID));
//构建map(相同手机号取最后一条)
phoneUserIdMap = employeeList.stream().collect(
Collectors.toMap(BeiSenEmployeeListResonseDTO::getMobilePhone,
BeiSenEmployeeListResonseDTO::getUserID,
(oldVal, newVal) -> newVal));
}
//根据员工id查询 员工信息
//Map<String, StaffInfoDTO> userMap = queryUserMap(strategyInfoList, staffList, sceneType);
......@@ -91,10 +94,13 @@ public class StrategyStaffRelService {
//根据手机号,查询北森人员userId
List<String> phoneList = staffList.stream().map(StrategyStaffRel::getUserPhone).collect(Collectors.toList());
List<BeiSenEmployeeListResonseDTO> employeeList = listEmployeeInfoByStaffPhone(phoneList);
Map<String, Integer> phoneUserIdMap = Collections.EMPTY_MAP;
Map<String, Integer> phoneUserIdMap = new HashMap<>();
if (CollectionUtils.isNotEmpty(employeeList)) {
//构建map
phoneUserIdMap = employeeList.stream().collect(Collectors.toMap(BeiSenEmployeeListResonseDTO::getMobilePhone, BeiSenEmployeeListResonseDTO::getUserID));
//构建map(相同手机号取最后一条)
phoneUserIdMap = employeeList.stream().collect(
Collectors.toMap(BeiSenEmployeeListResonseDTO::getMobilePhone,
BeiSenEmployeeListResonseDTO::getUserID,
(oldVal, newVal) -> newVal));
}
//根据员工id查询 员工信息
//构建对象,遍历2个对象创建新StrategyStaffRel
......
  • @wangxw1
    commented
    Master

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

    1. 代码重复(违反 DRY 原则)

    • 问题描述:从提交的 diff 可以明显看出,提取手机号、调用北森接口查询员工、并将结果转换为 phoneUserIdMap (包含解决重复 Key 的逻辑)的这整段代码,在同一个类(StrategyStaffRelService.java)中至少出现了两次。
    • 影响分析:重复的业务逻辑会显著降低代码的可维护性。如果未来北森接口的数据结构发生变更,或者合并策略需要调整,开发者必须在多处进行同步修改。一旦遗漏某处,就会引发线上 Bug。
    • 优化建议:建议将这段逻辑抽取为一个独立的私有方法。

      private Map<String, Integer> getPhoneUserIdMap(List<StrategyStaffRel> staffList) {
        if (CollectionUtils.isEmpty(staffList)) {
            return new HashMap<>(); // 或 Collections.emptyMap(),取决于下游是否需要修改此Map
        }
        List<String> phoneList = staffList.stream()
                .map(StrategyStaffRel::getUserPhone)
                .collect(Collectors.toList());
        List<BeiSenEmployeeListResonseDTO> employeeList = listEmployeeInfoByStaffPhone(phoneList);
      
        if (CollectionUtils.isEmpty(employeeList)) {
            return new HashMap<>();
        }
        return employeeList.stream().collect(
                Collectors.toMap(BeiSenEmployeeListResonseDTO::getMobilePhone,
                                 BeiSenEmployeeListResonseDTO::getUserID,
                                 (oldVal, newVal) -> newVal));
      }
      

    2. 变量初始化导致的不必要对象创建

    • 问题描述:代码中声明并初始化了 Map<String, Integer> phoneUserIdMap = new HashMap<>();,但在紧接着的 if (CollectionUtils.isNotEmpty(employeeList)) 块中,该变量被 Stream.collect() 产生的新 Map 直接重新赋值覆盖。
    • 影响分析:在绝大多数情况下(即 employeeList 不为空时),初始化的那个 new HashMap<>() 完全没有被使用,直接成为了需要被垃圾回收(GC)的废弃对象。虽然单个对象开销极小,但在高并发场景下属于不必要的资源浪费,不符合代码整洁之道。
    • 优化建议:仅声明变量,根据条件进行赋值;或者利用三目运算符/提前返回。 java Map<String, Integer> phoneUserIdMap; if (CollectionUtils.isNotEmpty(employeeList)) { phoneUserIdMap = employeeList.stream().collect(...); } else { phoneUserIdMap = new HashMap<>(); }

    3. 提交信息(Commit Message)缺乏实质内容

    • 问题描述:本次提交信息仅为“策略中心:优化代码 (by xiehx)”,描述过于宽泛和模糊。
    • 影响分析:本次代码变更实际上是非常有价值的 Bug Fix——它解决了原代码在使用 Collectors.toMap 时,若遇到相同手机号会抛出 IllegalStateException: Duplicate key 的严重隐患;同时移除了 Collections.EMPTY_MAP(不仅存在泛型警告,且返回不可变集合,容易引发 UnsupportedOperationException)。当前的提交信息掩盖了这些重要的修复背景,导致代码审查和未来的问题追溯(Git Blame)失去上下文。
    • 优化建议:提交信息应符合规范,清晰描述“修复了什么问题”或“实现了什么功能”。建议改为如:fix: 修复北森接口返回重复手机号导致 toMap 异常的问题,并修复不可变 Map 潜在风险。

    二、评分明细

    • 功能实现的正确性与健壮性 (40/40分) 理由:本次修改非常精准地解决了 Collectors.toMap 遇到重复 Key 导致程序崩溃的隐患,使用了 (oldVal, newVal) -> newVal 保留最新数据,提升了系统的健壮性。同时替换掉了可能引发不可变异常的 Collections.EMPTY_MAP。未违反 MyBatis 和 Dubbo 的特殊规则。

    • 安全性与潜在风险 (30/30分) 理由:本次变更属于纯内存数据处理逻辑的优化,不涉及外部输入直接拼装、权限校验等安全敏感操作,未引入新的安全隐患(如 SQL 注入、XSS 等)。

    • 最佳实践与可维护性 (15/20分) 理由:扣除 5 分。虽然解决了逻辑 Bug,但同样的代码块在同一个文件中直接复制粘贴修改,严重违反了 DRY(Don't Repeat Yourself)原则,降低了代码的可维护性。

    • 性能与资源利用 (4/5分) 理由:扣除 1 分。变量初始化逻辑略有瑕疵,导致在正常逻辑分支下会创建无用的 HashMap 对象,存在微小的优化空间。

    • 提交信息质量 (2/5分) 理由:扣除 3 分。“优化代码”属于典型的无效提交信息,未能体现出本次修改修复 Duplicate Key 异常的核心价值,不利于团队协作和历史追溯。

    总分:91分

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