Commit a7e06f42 by 林永健

fix(service): 优化商户员工列表和关注状态处理逻辑

- 将查询账户中心员工异常时返回null改为返回空列表,避免空指针风险
- 将员工关注项的集合类型由List改为Set,提高查找效率并简化代码
- 去除不必要的distinct操作,提升代码性能和可读性
1 parent 22d48f18
...@@ -202,7 +202,7 @@ public class MerchantBasicInfoService { ...@@ -202,7 +202,7 @@ public class MerchantBasicInfoService {
RpcResponse<List<MerchantStaffResponse>> staffListRemote = merchantStaffRemote.findMerchantStaffListRemote(req); RpcResponse<List<MerchantStaffResponse>> staffListRemote = merchantStaffRemote.findMerchantStaffListRemote(req);
if (staffListRemote.isFailed()) { if (staffListRemote.isFailed()) {
log.error("查询账户中心员工异常, req={}, res={}", req, staffListRemote); log.error("查询账户中心员工异常, req={}, res={}", req, staffListRemote);
return null; return Collections.emptyList();
} }
List<MerchantStaffResponse> staffList = staffListRemote.getResult(); List<MerchantStaffResponse> staffList = staffListRemote.getResult();
if (CollectionUtils.isEmpty(staffList)) { if (CollectionUtils.isEmpty(staffList)) {
......
...@@ -204,10 +204,9 @@ public class MerchantBiFollowService { ...@@ -204,10 +204,9 @@ public class MerchantBiFollowService {
// 维护当前员工的关注状态 // 维护当前员工的关注状态
List<MyFollowResponse> myFollowList = followMapper.findMyFollowsByStaffUuid(req.getLoginUserUuid(), req.getMerchantName()); List<MyFollowResponse> myFollowList = followMapper.findMyFollowsByStaffUuid(req.getLoginUserUuid(), req.getMerchantName());
if (CollectionUtils.isNotEmpty(myFollowList)) { if (CollectionUtils.isNotEmpty(myFollowList)) {
List<String> myFollowItems = myFollowList.stream() Set<String> myFollowItems = myFollowList.stream()
.map(p -> p.getMerchantId() + "_" + p.getBusinessType()) .map(p -> p.getMerchantId() + "_" + p.getBusinessType())
.distinct() .collect(Collectors.toSet());
.collect(Collectors.toList());
resList.forEach(p -> { resList.forEach(p -> {
String key = p.getMerchantId() + "_" + p.getBusinessType(); String key = p.getMerchantId() + "_" + p.getBusinessType();
if (myFollowItems.contains(key)) { if (myFollowItems.contains(key)) {
......
  • @wangxw1
    commented
    Master

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

    1. 【健壮性】RPC调用异常被静默吞没,可能导致业务语义混淆

    • 问题描述:在 MerchantBasicInfoService.java 中,当 merchantStaffRemote.findMerchantStaffListRemote(req) 返回失败时,代码记录了错误日志并返回了 Collections.emptyList()。
    • 影响分析:虽然这修复了原有的空指针(NPE)风险,但也掩盖了真实的系统异常。对于上层调用者而言,无法区分“RPC服务宕机/超时”与“该商户确实没有员工”。如果上层逻辑依赖此结果进行全量覆盖或删除操作,可能会因为误判为“无员工”而导致严重的数据错误。
    • 优化建议:除非业务明确要求在此场景下进行静默降级(Fallback),否则建议抛出自定义业务异常,或将错误状态继续向上层透传,让调用方决定如何处理。
    • 代码示例: java if (staffListRemote.isFailed()) { log.error("查询账户中心员工异常, req={}, res={}", req, staffListRemote); // 建议做法:抛出业务异常,中断错误流程 throw new BizException(ErrorCode.RPC_INVOKE_ERROR, "查询账户中心员工失败"); // 或按需返回带有错误码的统一响应对象 }

    2. 【最佳实践】复合键(String拼接)存在潜在的 Null 拼接与碰撞风险

    • 问题描述:在 MerchantBiFollowService.java 中,使用 p.getMerchantId() + "_" + p.getBusinessType() 拼接字符串作为 Set 的判断键。
    • 影响分析:
      1. 若 MerchantId 或 BusinessType 为 null,拼接结果会出现 "null_xxx",虽然不会报错,但属于脏数据处理。
      2. 若这两个字段的内容本身包含下划线 _,在极端情况下可能会产生哈希碰撞(例如 A_B + C 和 A + B_C 拼接结果相同)。
    • 优化建议:建议在 stream 处理时加入 null 值过滤;在更严谨的工程实践中,建议使用内部类(如 Java 14+ 的 Record 或自定义 Tuple 类)重写 equals 和 hashCode 作为 Set 的泛型类型,而非字符串拼接。
    • 代码示例: java Set<String> myFollowItems = myFollowList.stream() // 建议增加非空过滤,防止 null 拼接 .filter(p -> p.getMerchantId() != null && p.getBusinessType() != null) .map(p -> p.getMerchantId() + "_" + p.getBusinessType()) .collect(Collectors.toSet());

    二、评分明细

    • 功能实现的正确性与健壮性(36/40分)
      • 理由:修复了潜在的 NPE 风险,逻辑修改方向正确。扣分点在于 RPC 异常的直接吞没(返回空列表)可能引发上层业务逻辑的误判,需确认是否符合业务预期。
    • 安全性与潜在风险(30/30分)
      • 理由:本次变更不涉及 SQL 拼接、越权或敏感信息处理,未引入新的安全漏洞,表现良好。
    • 最佳实践与可维护性(18/20分)
      • 理由:将 List 转换为 Set 进行后续的 contains 判断是极其优秀的实践,去除了冗余的 distinct(),代码可读性提升。微小扣分在于字符串拼接作为联合 Key 的不够严谨。
    • 性能与资源利用(5/5分)
      • 理由:将 $O(N)$ 复杂度的 List.contains() 优化为 $O(1)$ 复杂度的 Set.contains(),在数据量较大时能显著提升循环内的匹配性能。
    • 提交信息质量(5/5分)
      • 理由:Commit message 采用了标准的 Angular 规范(fix(service): ...),并且清晰地分条列出了修改原因和带来的收益,非常具有可追溯性。

    总分:94分

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