Commit d4f1e816 by 范富荣

优化查询督导团队概况逻辑

1 parent c697ea55
...@@ -691,8 +691,7 @@ public class SupervisorTaskInfoService { ...@@ -691,8 +691,7 @@ public class SupervisorTaskInfoService {
* @return 督导团队概况信息 * @return 督导团队概况信息
*/ */
public SupervisorTeamOverviewDTO getTeamOverview(SupervisorTaskQueryDTO request) { public SupervisorTeamOverviewDTO getTeamOverview(SupervisorTaskQueryDTO request) {
log.info("查询督导团队概况信息,查询条件: {}", request); log.info("查询督导团队概况信息,查询条件: {}", JSON.toJSONString(request));
SupervisorTeamOverviewDTO overviewDTO = new SupervisorTeamOverviewDTO(); SupervisorTeamOverviewDTO overviewDTO = new SupervisorTeamOverviewDTO();
List<Integer> merchantIdList = request.getMerchantIdList(); List<Integer> merchantIdList = request.getMerchantIdList();
// 计算团队员工个数(使用所有门店去重) // 计算团队员工个数(使用所有门店去重)
......
...@@ -22,10 +22,7 @@ import org.apache.commons.collections.CollectionUtils; ...@@ -22,10 +22,7 @@ import org.apache.commons.collections.CollectionUtils;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
...@@ -275,25 +272,29 @@ public class SupervisorTaskInfoServiceTest extends BaseServiceTest { ...@@ -275,25 +272,29 @@ public class SupervisorTaskInfoServiceTest extends BaseServiceTest {
@Test @Test
public void testGetTeamOverview() { public void testGetTeamOverview() {
ShopMerchantDirectorConfigDTO dto = new ShopMerchantDirectorConfigDTO(); ShopMerchantDirectorConfigDTO dto = ShopMerchantDirectorConfigDTO.builder()
dto.setDirectorUserId("63c121922cba4956b2aac3f21dacd92f"); .directorUserId("63c121922cba4956b2aac3f21dacd92f")
dto.setDirectorTypes(Arrays.asList(DirectorTypeEnum.FLIGHT_SUPERVISION.getValue())); .directorTypes(Arrays.asList(
RpcResponse<List<ShopMerchantDirectorConfigDTO>> merchantResponse = shopMerchantDirectorConfigRemote.list(dto); DirectorTypeEnum.FLIGHT_SUPERVISION.getValue(),
List<Integer> merchantIdList = new ArrayList<>(); DirectorTypeEnum.FLIGHT_SUPERVISION_2_1.getValue()))
if (merchantResponse.isSuccess() && merchantResponse.getResult() != null) { .build();
List<ShopMerchantDirectorConfigDTO> merchantList = merchantResponse.getResult(); RpcResponse<List<ShopMerchantDirectorConfigDTO>> response = shopMerchantDirectorConfigRemote.list(dto);
if (CollectionUtils.isNotEmpty(merchantList)) { List<ShopMerchantDirectorConfigDTO> list = response.getResult();
merchantIdList = merchantList.stream() Map<Integer, List<Integer>> map = new HashMap<>();
.map(ShopMerchantDirectorConfigDTO::getMerchantId) if (CollectionUtils.isNotEmpty(list)) {
.collect(Collectors.toList()); for (ShopMerchantDirectorConfigDTO item : list) {
map.computeIfAbsent(Integer.valueOf(item.getDirectorType()), k -> new ArrayList<>())
.add(item.getMerchantId());
}
}
SupervisorTaskQueryDTO request = new SupervisorTaskQueryDTO(); SupervisorTaskQueryDTO request = new SupervisorTaskQueryDTO();
request.setMerchantIdList(merchantIdList); request.setDirectorTypeMerchantMap(map);
List<Integer> allMerchantIds = map.values().stream()
.flatMap(List::stream).distinct().collect(Collectors.toList());
request.setMerchantIdList(allMerchantIds);
SupervisorTeamOverviewDTO overview = supervisorTaskInfoService.getTeamOverview(request); SupervisorTeamOverviewDTO overview = supervisorTaskInfoService.getTeamOverview(request);
System.out.println(JSON.toJSONString(overview)); System.out.println(JSON.toJSONString(overview));
} }
}
}
/** /**
* 测试分页查询督导列表(不带筛选条件,查询第1页) * 测试分页查询督导列表(不带筛选条件,查询第1页)
......
  • @wangxw1
    commented
    Master

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

    1. 性能与日志最佳实践:日志记录中存在不必要的对象序列化开销

    • 问题描述:在 SupervisorTaskInfoService.java 中,将日志打印修改为了 log.info("...", JSON.toJSONString(request));。
    • 影响分析:JSON.toJSONString() 方法会在每次执行到该行代码时被调用(即时求值)。如果生产环境动态调整日志级别(如调整为 WARN 或 ERROR),该 JSON 序列化动作依然会执行,造成不必要的 CPU 和内存资源浪费。对于高频调用的接口,这种无谓的反射和字符串拼接会成为性能隐患。
    • 优化建议:推荐依赖 DTO 自身的 toString() 方法(通常由 Lombok 的 @Data 生成)。如果确实需要 JSON 格式输出,应使用 log.isInfoEnabled() 进行前置判断,避免无效的序列化操作。 ```java // 建议方式 1(推荐,依赖对象的 toString): log.info("查询督导团队概况信息,查询条件: {}", request);

    // 建议方式 2(如果强制要求 JSON 格式): if (log.isInfoEnabled()) { log.info("查询督导团队概况信息,查询条件: {}", JSON.toJSONString(request)); }

    
    #### 2. 代码健壮性:测试用例中移除了必要的防御性空指针检查
    - **问题描述**:在 `SupervisorTaskInfoServiceTest.java` 中,重构测试逻辑时,移除了对 RPC 响应结果的 `isSuccess()` 和 `!= null` 的判断,直接调用 `response.getResult()`。
    - **影响分析**:虽然这是测试代码,但如果 `shopMerchantDirectorConfigRemote.list(dto)` 模拟返回失败或为空,`response.getResult()` 将直接抛出 `NullPointerException`。脆弱的测试代码会导致 CI/CD 流水线不稳定,增加排查成本。
    - **优化建议**:恢复防御性编程习惯,确保在获取 RPC 结果前进行有效性校验。
      ```java
      RpcResponse<List<ShopMerchantDirectorConfigDTO>> response = shopMerchantDirectorConfigRemote.list(dto);
      if (response != null && response.isSuccess() && response.getResult() != null) {
          List<ShopMerchantDirectorConfigDTO> list = response.getResult();
          // ... 后续逻辑
      }
    

    3. 编码规范:使用了通配符导入(Wildcard Imports)

    • 问题描述:在 SupervisorTaskInfoServiceTest.java 中,将具体的类导入替换为了 import java.util.*;。
    • 影响分析:使用通配符导入(.*)违反了主流的 Java 工程规范(如阿里 Java 开发手册)。这会导致命名空间污染,增加类名冲突的风险(例如 java.util.Date 与 java.sql.Date),并降低代码的可读性,让阅读者难以直观判断当前类依赖了哪些具体组件。
    • 优化建议:将 IDE 的自动导入配置修改为明确导入具体的类,避免使用 *。 java import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map;

    二、评分明细

    1. 功能实现的正确性与健壮性(35/40分)
      • 理由:业务主流程未见明显错误。但在测试代码重构中移除了对 RPC 结果的非空和成功状态校验,降低了代码的健壮性,扣除 5 分。未发现对 MyBatis 子查询和 Dubbo 接口参数变更的违规行为。
    2. 安全性与潜在风险(30/30分)
      • 理由:本次提交主要涉及日志打印格式和测试用例的修改,未引入 SQL 注入、越权、敏感信息泄露等安全风险。
    3. 最佳实践与可维护性(15/20分)
      • 理由:引入了通配符导入(.*),违反了代码整洁规范;测试用例的重构略显随意,降低了代码的可维护性,扣除 5 分。
    4. 性能与资源利用(3/5分)
      • 理由:在日志中直接使用 JSON.toJSONString() 而未进行日志级别判断,存在潜在的无用序列化性能损耗,扣除 2 分。
    5. 提交信息质量(3/5分)
      • 理由:提交信息“优化查询督导团队概况逻辑”过于笼统。实际上测试代码中体现了对 directorType 分组逻辑的增加,但 Commit Message 未能准确描述“优化”的具体内容,不利于后续追溯,扣除 2 分。

    总分:86分

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