Commit a3ce1062 by 林永健

fix(project-group): 修复项目组服务中的错误处理和数据导入问题

- 将SERVICE_ERROR统一更改为SERVER_ERROR以保持错误处理一致性
- 重构项目组门店批量添加逻辑以支持导入商户映射
- 修复导入商户数据时的数据结构处理问题
- 优化商户ID列表构建逻辑以同时支持多种数据源
- 更新错误处理机制以提供更准确的错误分类
- 改进门店关系验证和错误提示信息
1 parent 13fb39c1
...@@ -48,7 +48,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote { ...@@ -48,7 +48,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote {
return RpcResponse.error(e.getError(), e.getMessage()); return RpcResponse.error(e.getError(), e.getMessage());
} catch (Exception e) { } catch (Exception e) {
log.error("分页查询项目组列表异常", e); log.error("分页查询项目组列表异常", e);
return RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, e.getMessage()); return RpcResponse.error(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
} }
} }
...@@ -61,7 +61,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote { ...@@ -61,7 +61,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote {
return RpcResponse.error(e.getError(), e.getMessage()); return RpcResponse.error(e.getError(), e.getMessage());
} catch (Exception e) { } catch (Exception e) {
log.error("查询项目组下拉列表异常", e); log.error("查询项目组下拉列表异常", e);
return RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, e.getMessage()); return RpcResponse.error(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
} }
} }
...@@ -74,7 +74,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote { ...@@ -74,7 +74,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote {
return RpcResponse.error(e.getError(), e.getMessage()); return RpcResponse.error(e.getError(), e.getMessage());
} catch (Exception e) { } catch (Exception e) {
log.error("查询项目组详情异常", e); log.error("查询项目组详情异常", e);
return RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, e.getMessage()); return RpcResponse.error(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
} }
} }
...@@ -87,7 +87,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote { ...@@ -87,7 +87,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote {
return RpcResponse.error(e.getError(), e.getMessage()); return RpcResponse.error(e.getError(), e.getMessage());
} catch (Exception e) { } catch (Exception e) {
log.error("保存项目组异常", e); log.error("保存项目组异常", e);
return RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, e.getMessage()); return RpcResponse.error(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
} }
} }
...@@ -100,7 +100,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote { ...@@ -100,7 +100,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote {
return RpcResponse.error(e.getError(), e.getMessage()); return RpcResponse.error(e.getError(), e.getMessage());
} catch (Exception e) { } catch (Exception e) {
log.error("删除项目组异常", e); log.error("删除项目组异常", e);
return RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, e.getMessage()); return RpcResponse.error(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
} }
} }
...@@ -114,7 +114,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote { ...@@ -114,7 +114,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote {
return RpcResponse.error(e.getError(), e.getMessage()); return RpcResponse.error(e.getError(), e.getMessage());
} catch (Exception e) { } catch (Exception e) {
log.error("分页查询项目组门店列表异常", e); log.error("分页查询项目组门店列表异常", e);
return RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, e.getMessage()); return RpcResponse.error(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
} }
} }
...@@ -127,7 +127,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote { ...@@ -127,7 +127,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote {
return RpcResponse.error(e.getError(), e.getMessage()); return RpcResponse.error(e.getError(), e.getMessage());
} catch (Exception e) { } catch (Exception e) {
log.error("批量新增项目组门店异常", e); log.error("批量新增项目组门店异常", e);
return RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, e.getMessage()); return RpcResponse.error(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
} }
} }
...@@ -140,7 +140,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote { ...@@ -140,7 +140,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote {
return RpcResponse.error(e.getError(), e.getMessage()); return RpcResponse.error(e.getError(), e.getMessage());
} catch (Exception e) { } catch (Exception e) {
log.error("删除项目组门店异常", e); log.error("删除项目组门店异常", e);
return RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, e.getMessage()); return RpcResponse.error(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
} }
} }
...@@ -153,7 +153,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote { ...@@ -153,7 +153,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote {
return RpcResponse.error(e.getError(), e.getMessage()); return RpcResponse.error(e.getError(), e.getMessage());
} catch (Exception e) { } catch (Exception e) {
log.error("查询预警类型列表异常", e); log.error("查询预警类型列表异常", e);
return RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, e.getMessage()); return RpcResponse.error(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
} }
} }
...@@ -166,7 +166,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote { ...@@ -166,7 +166,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote {
return RpcResponse.error(e.getError(), e.getMessage()); return RpcResponse.error(e.getError(), e.getMessage());
} catch (Exception e) { } catch (Exception e) {
log.error("查询应用业务列表异常", e); log.error("查询应用业务列表异常", e);
return RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, e.getMessage()); return RpcResponse.error(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
} }
} }
...@@ -179,7 +179,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote { ...@@ -179,7 +179,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote {
return RpcResponse.error(e.getError(), e.getMessage()); return RpcResponse.error(e.getError(), e.getMessage());
} catch (Exception e) { } catch (Exception e) {
log.error("查询指标列表异常", e); log.error("查询指标列表异常", e);
return RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, e.getMessage()); return RpcResponse.error(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
} }
} }
...@@ -192,7 +192,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote { ...@@ -192,7 +192,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote {
return RpcResponse.error(e.getError(), e.getMessage()); return RpcResponse.error(e.getError(), e.getMessage());
} catch (Exception e) { } catch (Exception e) {
log.error("查询项目组门店号列表异常", e); log.error("查询项目组门店号列表异常", e);
return RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, e.getMessage()); return RpcResponse.error(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
} }
} }
...@@ -205,7 +205,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote { ...@@ -205,7 +205,7 @@ public class ProjectGroupRemoteImpl implements ProjectGroupRemote {
return RpcResponse.error(e.getError(), e.getMessage()); return RpcResponse.error(e.getError(), e.getMessage());
} catch (Exception e) { } catch (Exception e) {
log.error("按条件查询预警结果列表异常", e); log.error("按条件查询预警结果列表异常", e);
return RpcResponse.error(DefaultErrorEnum.SERVICE_ERROR, e.getMessage()); return RpcResponse.error(DefaultErrorEnum.SERVER_ERROR, e.getMessage());
} }
} }
} }
...@@ -232,13 +232,21 @@ public class ProjectGroupService { ...@@ -232,13 +232,21 @@ public class ProjectGroupService {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "新增参数不能为空"); throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "新增参数不能为空");
} }
getActiveGroup(addDTO.getGroupId(), true); getActiveGroup(addDTO.getGroupId(), true);
if (CollectionUtils.isEmpty(addDTO.getMerchantIds())) {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "门店号不能为空"); List<Integer> merchantIds = new ArrayList<>();
if (CollectionUtils.isNotEmpty(addDTO.getMerchantIds())) {
merchantIds.addAll(addDTO.getMerchantIds().stream()
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList()));
} }
List<Integer> merchantIds = addDTO.getMerchantIds().stream() if (MapUtils.isNotEmpty(addDTO.getImportMerchants())) {
merchantIds.addAll(addDTO.getImportMerchants().keySet().stream()
.filter(Objects::nonNull) .filter(Objects::nonNull)
.distinct() .distinct()
.collect(Collectors.toList()); .collect(Collectors.toList()));
}
if (CollectionUtils.isEmpty(merchantIds)) { if (CollectionUtils.isEmpty(merchantIds)) {
throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "门店号不能为空"); throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "门店号不能为空");
} }
...@@ -697,7 +705,7 @@ public class ProjectGroupService { ...@@ -697,7 +705,7 @@ public class ProjectGroupService {
.filter(item -> !merchantNameMap.containsKey(item)) .filter(item -> !merchantNameMap.containsKey(item))
.collect(Collectors.toList()); .collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(missingMerchantIds)) { if (CollectionUtils.isNotEmpty(missingMerchantIds)) {
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR,
"存在无效门店号: " + StringUtils.join(missingMerchantIds, ",")); "存在无效门店号: " + StringUtils.join(missingMerchantIds, ","));
} }
// 导入场景还要验证门店名称,验证商户关系 // 导入场景还要验证门店名称,验证商户关系
...@@ -706,7 +714,7 @@ public class ProjectGroupService { ...@@ -706,7 +714,7 @@ public class ProjectGroupService {
importMerchants.forEach((merchantId, merchantName) -> { importMerchants.forEach((merchantId, merchantName) -> {
if (!StringUtils.equals(merchantNameMap.get(merchantId), merchantName)) { if (!StringUtils.equals(merchantNameMap.get(merchantId), merchantName)) {
log.warn("门店名称不一致: {} != {}", merchantName, merchantNameMap.get(merchantId)); log.warn("门店名称不一致: {} != {}", merchantName, merchantNameMap.get(merchantId));
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR,
"无效商户: " + merchantName + " != " + merchantNameMap.get(merchantId)); "无效商户: " + merchantName + " != " + merchantNameMap.get(merchantId));
} }
}); });
...@@ -723,7 +731,7 @@ public class ProjectGroupService { ...@@ -723,7 +731,7 @@ public class ProjectGroupService {
|| merchantRefResponse.getResult() == null || merchantRefResponse.getResult() == null
|| CollectionUtils.isEmpty(merchantRefResponse.getResult().getList())) { || CollectionUtils.isEmpty(merchantRefResponse.getResult().getList())) {
log.warn("门店关系查询失败: {}", merchantRefResponse.getMessage()); log.warn("门店关系查询失败: {}", merchantRefResponse.getMessage());
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, "门店未上架"); throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "门店未上架");
} }
List<MerchantSceneDTO> merchantRefs = merchantRefResponse.getResult().getList(); List<MerchantSceneDTO> merchantRefs = merchantRefResponse.getResult().getList();
if (merchantRefs.size() != merchantIds.size()) { if (merchantRefs.size() != merchantIds.size()) {
...@@ -733,7 +741,7 @@ public class ProjectGroupService { ...@@ -733,7 +741,7 @@ public class ProjectGroupService {
List<Integer> invalidIds = merchantIds.stream() List<Integer> invalidIds = merchantIds.stream()
.filter(id -> !validMerchantIds.contains(id)) .filter(id -> !validMerchantIds.contains(id))
.collect(Collectors.toList()); .collect(Collectors.toList());
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, "存在未上架的门店: " + invalidIds); throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "存在未上架的门店: " + invalidIds);
} }
log.info("门店关系: {}", merchantRefs.stream().map(p -> p.getMerchantId() + ":" + p.getBaseSceneCode()).collect(Collectors.toList())); log.info("门店关系: {}", merchantRefs.stream().map(p -> p.getMerchantId() + ":" + p.getBaseSceneCode()).collect(Collectors.toList()));
} }
......
  • @wangxw1
    commented
    Master

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

    1. 集合合并后缺失全局去重(功能正确性与健壮性)

    • 问题描述:在 ProjectGroupService.java 的 addProjectGroupMerchant 逻辑中,分别对 addDTO.getMerchantIds() 和 addDTO.getImportMerchants().keySet() 进行了内部去重,然后追加到同一个 merchantIds 列表中。如果这两个数据源中存在相同的门店 ID,merchantIds 列表将出现重复元素。
    • 影响分析:代码在后续第 734 行有严格的校验逻辑 if (merchantRefs.size() != merchantIds.size())。如果 merchantIds 存在重复元素,而底层查询返回的 merchantRefs 只有实际存在的记录数,将导致判断条件不成立,误抛出“存在未上架的门店”异常,从而阻断正常业务流程。
    • 优化建议:建议使用 Set 结构来收集所有合法的门店 ID 以保证全局唯一性,或者在合并后进行一次全局去重。
    • 代码示例:

          Set<Integer> merchantIdSet = new HashSet<>();
          if (CollectionUtils.isNotEmpty(addDTO.getMerchantIds())) {
              merchantIdSet.addAll(addDTO.getMerchantIds());
          }
          if (MapUtils.isNotEmpty(addDTO.getImportMerchants())) {
              merchantIdSet.addAll(addDTO.getImportMerchants().keySet());
          }
      
          List<Integer> merchantIds = merchantIdSet.stream()
                  .filter(Objects::nonNull)
                  .collect(Collectors.toList());
      
          if (CollectionUtils.isEmpty(merchantIds)) {
              throw new RpcServiceException(DefaultErrorEnum.SERVER_ERROR, "门店号不能为空");
          }
      

    2. 异常枚举类型的语义使用不当(最佳实践与可维护性)

    • 问题描述:本次提交将多处业务校验异常(如“存在无效门店号”、“无效商户”、“存在未上架的门店”)和参数校验异常(“门店号不能为空”)的错误码统一由 SERVICE_ERROR 修改为了 SERVER_ERROR。
    • 影响分析:SERVER_ERROR 通常代表系统内部未捕获的严重异常或故障(如 HTTP 500),而此类校验失败属于预期内的业务逻辑或参数校验拦截(通常对应 HTTP 400 或业务状态码)。滥用 SERVER_ERROR 会导致监控大盘告警误报,且调用方无法准确区分是服务端宕机还是自身传参有误。
    • 优化建议:对于参数校验和业务规则冲突,建议恢复使用 SERVICE_ERROR,或者使用更为准确的枚举(例如 PARAM_ERROR 或 BUSINESS_ERROR),以便于异常分类和监控。

    3. Stream 操作与集合处理逻辑冗长(最佳实践与可维护性)

    • 问题描述:在构建 merchantIds 时,对两个集合分别进行了非空判断、Stream 过滤、去重,然后再 addAll 添加到 List 中,代码显得有些冗余。
    • 影响分析:增加了代码的阅读负担和不必要的中间集合对象创建。
    • 优化建议:参考问题 1 的代码示例,统一收集元素后再做一次 Stream 过滤即可,使代码更加简洁优雅且易于维护。

    二、评分明细

    • 功能实现的正确性与健壮性(30/40分):核心的门店 ID 提取逻辑存在合并后未去重的漏洞,可能导致后续校验逻辑(Size比对)误判,引发严重业务阻断 bug,故扣除 10 分。
    • 安全性与潜在风险(30/30分):未发现 SQL 注入、越权、敏感信息泄露等安全隐患。
    • 最佳实践与可维护性(15/20分):将预期的业务校验异常强制更改为服务器内部错误(SERVER_ERROR)破坏了错误码语义规范;同时集合的流处理逻辑不够精简。扣除 5 分。
    • 性能与资源利用(4/5分):整体性能表现正常,仅存在轻微的中间集合对象创建开销,优化空间如上所述。
    • 提交信息质量(5/5分):Commit 信息非常清晰,采用了标准的格式(fix、refactor),详细列出了具体的改动点,具备良好的可追溯性。

    总分:84分

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