Commit 7bea1e42 by zhouhaibao

refactor(dashboard): 优化督导数据查询逻辑并添加员工排名功能

- 添加 Stream 流处理导入用于数据合并
- 新增 getSupervisorMerchants 方法支持多业务类型督导查询
- 实现 1.1 飞行督导和 2.1 飞行督导的数据分离查询
- 使用 Stream.concat 合并不同业务类型的员工数据并去重
- 修复员工列表查询中的重复数据问题
- 在门店业绩查询中添加省份筛选日志记录
- 更新测试用例中的区域参数格式
- 修改任务信息服务中团队员工统计逻辑,使用新的员工排名数据源
1 parent 4d660837
......@@ -21,4 +21,5 @@ public interface StaffAccountAssignMapper {
*/
List<EmployeeRanking> selectShopStaffList(@Param("merchantIds") List<Integer> merchantIds,
@Param("sceneType") Byte sceneType);
}
......@@ -74,6 +74,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Resource;
import org.springframework.core.task.TaskExecutor;
import org.springframework.data.redis.core.RedisTemplate;
......@@ -871,6 +872,39 @@ public class SupervisorDashboardService {
return phoneMetricMap;
}
public List<Integer> getSupervisorMerchants(String supervisorId, Integer bizs) {
// 根据业务类型确定需要查询的督导类型
List<Byte> directorTypes = new ArrayList<>();
// 1.1飞行督导(type=6)对应轻改(biz=2),2.1飞行督导(type=13)对应延保+权益(biz=1,3)
if (bizs == null) {
// biz为空时,查询两种类型的门店
directorTypes.add(DirectorTypeEnum.FLIGHT_SUPERVISION.getValue());
directorTypes.add(DirectorTypeEnum.FLIGHT_SUPERVISION_2_1.getValue()); // FLIGHT_SUPERVISION_2_1
} else if (bizs == 2) {
// 轻改业务,只查询1.1飞行督导
directorTypes.add(DirectorTypeEnum.FLIGHT_SUPERVISION.getValue());
} else if (bizs == 1 || bizs == 3) {
// 延保或权益业务,只查询2.1飞行督导
directorTypes.add(DirectorTypeEnum.FLIGHT_SUPERVISION_2_1.getValue()); // FLIGHT_SUPERVISION_2_1
}
if (directorTypes.isEmpty()) {
log.warn("getSupervisorMerchants: 未匹配到有效的业务类型,bizs={}", bizs);
return new ArrayList<>();
}
List<ShopMerchantDirectorConfigDTO> list = shopMerchantDirectorConfigRemote.list(ShopMerchantDirectorConfigDTO.builder()
.directorTypes(directorTypes)
.directorUserId(supervisorId)
.build()).getResult();
return list.stream()
.map(ShopMerchantDirectorConfigDTO::getMerchantId)
.distinct()
.collect(Collectors.toList());
}
/**
* 查询门店的员工列表
*
......@@ -888,7 +922,7 @@ public class SupervisorDashboardService {
log.warn("督导 {} 没有管理的门店", supervisorId);
return new ArrayList<>();
}
// 根据业务类型确定场景类型
// biz=1或3(延保/权益)时,查询scene_type=0
// biz=2(轻改)时,查询scene_type=1
......@@ -900,15 +934,45 @@ public class SupervisorDashboardService {
sceneType = 1;
}
}
// 2. 使用本地Mapper获取门店的员工列表
List<EmployeeRanking> employeeList = staffAccountAssignMapper.selectShopStaffList(merchantIds, sceneType);
List<EmployeeRanking> employeeList = null;
if(supervisorId!=null&&bizs==null){
// 查询1.1飞行督导的门店和轻改业务数据
List<Integer> flightSupervisionMerchants = getMerchantsByDirectorType(supervisorId,
DirectorTypeEnum.FLIGHT_SUPERVISION.getValue());
// 查询2.1飞行督导的门店和延保/权益业务数据
List<Integer> flightSupervision21Merchants = getMerchantsByDirectorType(supervisorId,
DirectorTypeEnum.FLIGHT_SUPERVISION_2_1.getValue()); // FLIGHT_SUPERVISION_2_1
// 2. 使用本地Mapper获取门店的员工列表
List<EmployeeRanking> employeeList1 = staffAccountAssignMapper.selectShopStaffList(
flightSupervisionMerchants, (byte) 1);
List<EmployeeRanking> employeeList2 = staffAccountAssignMapper.selectShopStaffList(
flightSupervision21Merchants, (byte) 0);
// 按照这个去重组合
employeeList = Stream.concat(employeeList1.stream(), employeeList2.stream())
.collect(Collectors.toMap(
// 1. Key生成器:按三个字段组合作为唯一标识
e -> e.getEmployeePhone() + "_" + e.getEmployeeName() + "_" + e.getMerchantId(),
// 2. Value映射器:保持对象本身
Function.identity(),
// 3. 冲突解决策略:如果遇到重复key,保留第一个出现的元素
(existing, replacement) -> existing
))
.values()
.stream()
.collect(Collectors.toList());
}else {
// 2. 使用本地Mapper获取门店的员工列表
employeeList = staffAccountAssignMapper.selectShopStaffList(
merchantIds, sceneType);
}
if (employeeList == null || employeeList.isEmpty()) {
log.warn("获取门店员工列表失败,督导ID:{}", supervisorId);
return new ArrayList<>();
}
// 3. 按照手机号去重,每个手机号只保留一个员工信息,并合并门店ID
Map<String, EmployeeRanking> uniqueStaffMap = new HashMap<>();
for (EmployeeRanking employee : employeeList) {
......@@ -937,46 +1001,13 @@ public class SupervisorDashboardService {
}
}
}
return new ArrayList<>(uniqueStaffMap.values());
} catch (Exception e) {
log.error("getSupervisorStoreAndEmployeePhoneList error: {}", e);
}
return new ArrayList<>();
}
public List<Integer> getSupervisorMerchants(String supervisorId, Integer bizs) {
// 根据业务类型确定需要查询的督导类型
List<Byte> directorTypes = new ArrayList<>();
// 1.1飞行督导(type=6)对应轻改(biz=2),2.1飞行督导(type=13)对应延保+权益(biz=1,3)
if (bizs == null) {
// biz为空时,查询两种类型的门店
directorTypes.add(DirectorTypeEnum.FLIGHT_SUPERVISION.getValue());
directorTypes.add(DirectorTypeEnum.FLIGHT_SUPERVISION_2_1.getValue()); // FLIGHT_SUPERVISION_2_1
} else if (bizs == 2) {
// 轻改业务,只查询1.1飞行督导
directorTypes.add(DirectorTypeEnum.FLIGHT_SUPERVISION.getValue());
} else if (bizs == 1 || bizs == 3) {
// 延保或权益业务,只查询2.1飞行督导
directorTypes.add(DirectorTypeEnum.FLIGHT_SUPERVISION_2_1.getValue()); // FLIGHT_SUPERVISION_2_1
}
if (directorTypes.isEmpty()) {
log.warn("getSupervisorMerchants: 未匹配到有效的业务类型,bizs={}", bizs);
return new ArrayList<>();
}
List<ShopMerchantDirectorConfigDTO> list = shopMerchantDirectorConfigRemote.list(ShopMerchantDirectorConfigDTO.builder()
.directorTypes(directorTypes)
.directorUserId(supervisorId)
.build()).getResult();
return list.stream()
.map(ShopMerchantDirectorConfigDTO::getMerchantId)
.distinct()
.collect(Collectors.toList());
return new ArrayList<>();
}
/**
......@@ -3643,7 +3674,8 @@ public class SupervisorDashboardService {
.map(MerchantProvinceDTO::getMerchantId)
.filter(Objects::nonNull)
.collect(Collectors.toSet());
log.info("[门店业绩查询] [{}] 步骤4:使用远程接口查询省份商户完成,耗时 {}ms,数量:{}",provinceMerchantIds.toString());
// 基于查询到的省份商户ID进行过滤
result.removeIf(store -> store.getMerchantId() == null || !provinceMerchantIds.contains(store.getMerchantId()));
} else {
......
......@@ -758,16 +758,20 @@ public class SupervisorTaskInfoService {
// 计算团队员工个数(使用所有门店去重)
StaffBaseInfoDTO param = new StaffBaseInfoDTO();
param.setMerchantIdList(merchantIdList);
RpcResponse<List<StaffBaseInfoDTO>> response = staffBaseInfoRemote.listShopStaffInfoGroupByUUid(param);
List<EmployeeRanking> supervisorStoreAndEmployeePhoneList = supervisorDashboardService.getSupervisorStoreAndEmployeePhoneList(
request.getUpdatedBy(), merchantIdList, null);
if (CollectionUtils.isEmpty(supervisorStoreAndEmployeePhoneList)){
return overviewDTO;
}
/* RpcResponse<List<StaffBaseInfoDTO>> response = staffBaseInfoRemote.listShopStaffInfoGroupByUUid(param);
if (response.hasError()) {
log.error("查询团队员工个数失败,merchantIdList={}, error={}, message={}", merchantIdList, response.getError(), response.getMessage());
throw new RpcServiceException(DefaultErrorEnum.SERVICE_ERROR, response.getMessage());
}
List<StaffBaseInfoDTO> staffList = response.getResult();
if (CollectionUtils.isNotEmpty(staffList)) {
Set<String> phoneSet = staffList.stream()
.filter(x -> StringUtils.isNotBlank(x.getStaffPhone()))
.map(StaffBaseInfoDTO::getStaffPhone)
}*/
if (CollectionUtils.isNotEmpty(supervisorStoreAndEmployeePhoneList)) {
Set<String> phoneSet = supervisorStoreAndEmployeePhoneList.stream()
.filter(x -> StringUtils.isNotBlank(x.getEmployeePhone()))
.map(EmployeeRanking::getEmployeePhone)
.collect(Collectors.toSet());
overviewDTO.setStaffCount(phoneSet.size());
}
......
......@@ -26,4 +26,5 @@
group by t1.staff_phone,t1.merchant_id
</select>
</mapper>
......@@ -45,8 +45,6 @@ public class SupervisorDashboardRemoteImplTest extends BaseServiceTest {
req.setEndDate("2026-03-26");
req.setDateType(0);
req.setNeedAiAnalysis(true);
req.setSortField(1);
req.setSortDirection(0);
RpcResponse<SupervisorDashboardVO> response = supervisorDashboardRemote.getDashboardData(req);
System.out.println(JSON.toJSONString(response));
}
......
......@@ -150,6 +150,8 @@ public class SupervisorDashboardServiceTest extends BaseServiceTest {
req.setEndDate("2026-05-31");
req.setDateType(2); // 月
req.setStatDimension(2); // 通用品牌
req.setRegion("440000");
StorePerformanceResponse allStorePerformance = supervisorDashboardService.getAllStorePerformance(
req);
......@@ -166,7 +168,7 @@ public class SupervisorDashboardServiceTest extends BaseServiceTest {
req.setEndDate("2026-05-31");
req.setDateType(2); // 月
req.setStatDimension(2); // 通用品牌
req.setRegion("华东区域"); // 按区域筛选
req.setRegion("440000"); // 按区域筛选
StorePerformanceResponse allStorePerformance = supervisorDashboardService.getAllStorePerformance(
req);
......
  • @wangxw1
    commented
    Master

    这是一份针对您提交代码的专业审查报告。本次审查重点关注了代码的功能正确性、性能隐患以及工程最佳实践,并严格遵照了您设定的审查目标与特殊规则。

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

    1. 严重的功能逻辑缺陷:参数被忽略导致业务数据错误

    • 问题描述:在 SupervisorDashboardService.java 的 getSupervisorStoreAndEmployeePhoneList 方法中,新增的逻辑分支 if(supervisorId != null && bizs == null) 内部,完全忽略了传入的 merchantIds 参数,而是直接通过 getMerchantsByDirectorType 获取督导名下的所有门店进行查询。
    • 影响分析:在 SupervisorTaskInfoService.java 中调用此方法时,传入了特定的 merchantIdList 且 bizs 为 null。按照现有逻辑,系统将不会按指定的 merchantIdList 进行过滤,而是查出了该督导名下的所有员工。这将导致“团队员工个数”的统计数据严重偏大,产生严重的业务功能Bug。
    • 优化建议:在 if 分支内,需要将获取到的督导门店集合与传入的 merchantIds 求交集,或者在 Mapper 查询时增加对传入 merchantIds 的过滤。 java // 示例修复逻辑 if (supervisorId != null && bizs == null) { List<Integer> flightSupervisionMerchants = getMerchantsByDirectorType(supervisorId, DirectorTypeEnum.FLIGHT_SUPERVISION.getValue()); // 如果调用方传入了特定的 merchantIds,需进行交集过滤 if (CollectionUtils.isNotEmpty(merchantIds)) { flightSupervisionMerchants.retainAll(merchantIds); } // ... 同样处理 flightSupervision21Merchants ... }

    2. 日志占位符不匹配与严重的性能隐患

    • 问题描述:在 SupervisorDashboardService.java 第 3677 行,新增的日志打印语句存在两个问题:log.info("[门店业绩查询] [{}] 步骤4:使用远程接口查询省份商户完成,耗时 {}ms,数量:{}", provinceMerchantIds.toString());。
    • 影响分析:
      1. 占位符缺失:日志字符串中有 3 个 {} 占位符,但仅传入了 1 个参数。SLF4J 无法正确解析,会导致日志输出格式混乱(直接打印出 {})。
      2. 内存与性能浪费:provinceMerchantIds 是一个 Set 集合,直接调用 .toString() 会将整个集合的内容拼接成一个巨大的字符串。如果商户数量较多,会产生极大的 CPU 消耗、内存占用(String 拼接)以及无意义的日志刷屏。
    • 优化建议:补全缺失的日志参数,并将打印具体内容改为打印集合大小。 java // 优化示例(假设上下文中存在 traceId 和 costTime 变量,若无请移除多余占位符) log.info("[门店业绩查询] 步骤4:使用远程接口查询省份商户完成,数量:{}", provinceMerchantIds.size());

    3. 提交了废弃代码(Dead Code)且存在潜在的空指针拼接风险

    • 问题描述:
      1. SupervisorTaskInfoService.java 中大段注释掉的旧 RPC 调用代码(/* RpcResponse<List<StaffBaseInfoDTO>> response... */)被直接提交。
      2. SupervisorDashboardService.java 中使用 Stream API 生成 Map Key 时:e -> e.getEmployeePhone() + "_" + e.getEmployeeName() + "_" + e.getMerchantId(),未对 null 值进行处理。
    • 影响分析:保留大段废弃代码严重影响代码可读性和后续维护。Stream 拼接时,若对象属性存在 null,会被拼接成 "null_null_123" 这种脏数据;如果在极端情况下多个实体的属性均为 null,会导致 Key 冲突,从而错误地覆盖去重数据。
    • 优化建议:
      1. 删除 SupervisorTaskInfoService.java 中被注释掉的代码块(通过 Git 历史即可追溯,无需留在代码中)。
      2. 建议在拼接 Key 时使用防御性编程或安全的字符串构建方式。

    二、评分明细

    • 功能实现的正确性与健壮性(20/40分) 扣分理由:引入了严重的业务逻辑缺陷(参数 merchantIds 在特定分支被完全忽略),导致关联业务(任务信息统计)的数据计算错误。未触发“子查询”和“Dubbo参数变更”的红线规则。

    • 安全性与潜在风险(30/30分) 得分理由:本次变更主要集中在内部数据聚合与业务流转,未发现 SQL 注入、越权、XSS 等明显安全漏洞。

    • 最佳实践与可维护性(12/20分) 扣分理由:提交了大段被注释的废弃代码;代码中存在魔法值如 (byte) 1 和 (byte) 0(应使用枚举或常量);Stream 操作的 Key 拼接不够优雅且存在隐患。

    • 性能与资源利用(2/5分) 扣分理由:在日志中对大集合直接调用 .toString() 造成不必要的资源浪费;在业务循环/判断逻辑中,存在对 getMerchantsByDirectorType 等潜在耗时方法的重复或冗余调用。

    • 提交信息质量(4/5分) 得分理由:Commit message 格式规范,清晰地列出了多条修改点,可追溯性较好。微小扣分在于未提及测试用例中排序参数的移除。

    总分:68分

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