Commit c28d82fa by zhouhaibao

feat(supervisor): 添加员工手机号字段并完善业绩统计逻辑

- 在 StaffPerformanceDTO 中新增员工手机号字段
- 修改业绩统计服务逻辑,确保无指标数据的员工也能返回
- 先初始化所有驻店员工再填充指标数据,优化数据聚合流程
- 更新测试用例中的商户ID和统计维度参数
1 parent 76722bda
...@@ -20,6 +20,9 @@ public class StaffPerformanceDTO extends BasePerformanceDTO { ...@@ -20,6 +20,9 @@ public class StaffPerformanceDTO extends BasePerformanceDTO {
@ApiModelProperty(notes = "员工姓名") @ApiModelProperty(notes = "员工姓名")
private String employeeName; private String employeeName;
@ApiModelProperty(notes = "员工手机号")
private String employeePhone;
@ApiModelProperty(notes = "关联门店名称") @ApiModelProperty(notes = "关联门店名称")
private String merchantName; private String merchantName;
......
...@@ -2236,13 +2236,29 @@ public class SupervisorDashboardService { ...@@ -2236,13 +2236,29 @@ public class SupervisorDashboardService {
requestId, System.currentTimeMillis() - stepStartTime, indicatorConfig.size()); requestId, System.currentTimeMillis() - stepStartTime, indicatorConfig.size());
// 7. 聚合数据并构建结果(按手机号分组) // 7. 聚合数据并构建结果(按手机号分组)
// 以驻店员工为准,即使没有指标数据也需要返回
stepStartTime = System.currentTimeMillis(); stepStartTime = System.currentTimeMillis();
Map<String, StaffPerformanceDTO> phoneMap = new HashMap<>(allResults.size()); Map<String, StaffPerformanceDTO> phoneMap = new HashMap<>(employeeList.size());
int processedCount = 0; int processedCount = 0;
int skippedCount = 0; int skippedCount = 0;
int fillSuccessCount = 0; int fillSuccessCount = 0;
int fillFailCount = 0; int fillFailCount = 0;
// 先初始化所有驻店员工(确保没有指标数据的员工也能返回)
for (EmployeeRanking employee : employeeList) {
String phone = employee.getEmployeePhone();
if (phone != null && !phone.trim().isEmpty()) {
StaffPerformanceDTO staff = new StaffPerformanceDTO();
staff.setEmployeeName(employee.getEmployeeName());
staff.setEmployeePhone(phone);
staff.setBrandId(req.getStatDimension() == 1 ? IndicatorConfig.BRAND_ID_HARMONY : IndicatorConfig.BRAND_ID_GENERAL);
staff.setBrandName(req.getStatDimension() == 1 ? "问界鸿蒙" : "通用品牌");
staff.setStatDimension(req.getStatDimension());
phoneMap.put(phone, staff);
}
}
// 然后用指标数据填充有数据的员工
if (!allResults.isEmpty()) { if (!allResults.isEmpty()) {
for (DataBusinessDTO dto : allResults) { for (DataBusinessDTO dto : allResults) {
String phone = dto.getStaffPhone(); String phone = dto.getStaffPhone();
...@@ -2251,15 +2267,23 @@ public class SupervisorDashboardService { ...@@ -2251,15 +2267,23 @@ public class SupervisorDashboardService {
continue; continue;
} }
StaffPerformanceDTO staff = phoneMap.computeIfAbsent(phone, k -> new StaffPerformanceDTO()); StaffPerformanceDTO staff = phoneMap.get(phone);
if (staff == null) {
// 如果指标数据对应的员工不在员工列表中,也创建一个
staff = new StaffPerformanceDTO();
staff.setBrandId(req.getStatDimension() == 1 ? IndicatorConfig.BRAND_ID_HARMONY : IndicatorConfig.BRAND_ID_GENERAL);
staff.setBrandName(req.getStatDimension() == 1 ? "问界鸿蒙" : "通用品牌");
staff.setStatDimension(req.getStatDimension());
phoneMap.put(phone, staff);
}
// 填充基础信息 // 填充基础信息
if (staff.getEmployeeName() == null && dto.getStaffName() != null) { if (staff.getEmployeeName() == null && dto.getStaffName() != null) {
staff.setEmployeeName(dto.getStaffName()); staff.setEmployeeName(dto.getStaffName());
} }
staff.setBrandId(req.getStatDimension() == 1 ? IndicatorConfig.BRAND_ID_HARMONY : IndicatorConfig.BRAND_ID_GENERAL); if (staff.getEmployeePhone() == null) {
staff.setBrandName(req.getStatDimension() == 1 ? "问界鸿蒙" : "通用品牌"); staff.setEmployeePhone(phone);
staff.setStatDimension(req.getStatDimension()); }
// 根据指标配置映射填充指标数据 // 根据指标配置映射填充指标数据
String itemCode = dto.getItemCode(); String itemCode = dto.getItemCode();
...@@ -3756,13 +3780,29 @@ public class SupervisorDashboardService { ...@@ -3756,13 +3780,29 @@ public class SupervisorDashboardService {
requestId, System.currentTimeMillis() - stepStartTime, indicatorConfig.size()); requestId, System.currentTimeMillis() - stepStartTime, indicatorConfig.size());
// 7. 聚合数据并构建结果(按手机号分组) // 7. 聚合数据并构建结果(按手机号分组)
// 以驻店员工为准,即使没有指标数据也需要返回
stepStartTime = System.currentTimeMillis(); stepStartTime = System.currentTimeMillis();
Map<String, StaffPerformanceDTO> phoneMap = new HashMap<>(allResults.size() / 10); Map<String, StaffPerformanceDTO> phoneMap = new HashMap<>(employeeList.size());
int processedCount = 0; int processedCount = 0;
int skippedCount = 0; int skippedCount = 0;
int fillSuccessCount = 0; int fillSuccessCount = 0;
int fillFailCount = 0; int fillFailCount = 0;
// 先初始化所有驻店员工(确保没有指标数据的员工也能返回)
for (EmployeeRanking employee : employeeList) {
String phone = employee.getEmployeePhone();
if (phone != null && !phone.trim().isEmpty()) {
StaffPerformanceDTO staff = new StaffPerformanceDTO();
staff.setEmployeeName(employee.getEmployeeName());
staff.setEmployeePhone(phone);
staff.setBrandId(req.getStatDimension() == 1 ? IndicatorConfig.BRAND_ID_HARMONY : IndicatorConfig.BRAND_ID_GENERAL);
staff.setBrandName(req.getStatDimension() == 1 ? "问界鸿蒙" : "通用品牌");
staff.setStatDimension(req.getStatDimension());
phoneMap.put(phone, staff);
}
}
// 然后用指标数据填充有数据的员工
if (!allResults.isEmpty()) { if (!allResults.isEmpty()) {
for (DataBusinessDTO dto : allResults) { for (DataBusinessDTO dto : allResults) {
String phone = dto.getStaffPhone(); String phone = dto.getStaffPhone();
...@@ -3771,15 +3811,23 @@ public class SupervisorDashboardService { ...@@ -3771,15 +3811,23 @@ public class SupervisorDashboardService {
continue; continue;
} }
StaffPerformanceDTO staff = phoneMap.computeIfAbsent(phone, k -> new StaffPerformanceDTO()); StaffPerformanceDTO staff = phoneMap.get(phone);
if (staff == null) {
// 如果指标数据对应的员工不在员工列表中,也创建一个
staff = new StaffPerformanceDTO();
staff.setBrandId(req.getStatDimension() == 1 ? IndicatorConfig.BRAND_ID_HARMONY : IndicatorConfig.BRAND_ID_GENERAL);
staff.setBrandName(req.getStatDimension() == 1 ? "问界鸿蒙" : "通用品牌");
staff.setStatDimension(req.getStatDimension());
phoneMap.put(phone, staff);
}
// 填充基础信息 // 填充基础信息
if (staff.getEmployeeName() == null && dto.getStaffName() != null) { if (staff.getEmployeeName() == null && dto.getStaffName() != null) {
staff.setEmployeeName(dto.getStaffName()); staff.setEmployeeName(dto.getStaffName());
} }
staff.setBrandId(req.getStatDimension() == 1 ? IndicatorConfig.BRAND_ID_HARMONY : IndicatorConfig.BRAND_ID_GENERAL); if (staff.getEmployeePhone() == null) {
staff.setBrandName(req.getStatDimension() == 1 ? "问界鸿蒙" : "通用品牌"); staff.setEmployeePhone(phone);
staff.setStatDimension(req.getStatDimension()); }
// 根据指标配置映射填充指标数据 // 根据指标配置映射填充指标数据
String itemCode = dto.getItemCode(); String itemCode = dto.getItemCode();
......
...@@ -197,11 +197,11 @@ public class SupervisorDashboardServiceTest extends BaseServiceTest { ...@@ -197,11 +197,11 @@ public class SupervisorDashboardServiceTest extends BaseServiceTest {
@Test @Test
public void testGetStorePerformanceByMerchantGeneral() { public void testGetStorePerformanceByMerchantGeneral() {
MerchantQueryReq req = new MerchantQueryReq(); MerchantQueryReq req = new MerchantQueryReq();
req.setMerchantId(44210895); // 测试门店ID req.setMerchantId(44211227); // 测试门店ID
req.setStartDate("2026-01-01"); req.setStartDate("2026-01-01");
req.setEndDate("2026-05-31"); req.setEndDate("2026-05-31");
req.setDateType(2); // 月 req.setDateType(4); // 月
req.setStatDimension(2); // 通用品牌 req.setStatDimension(1); // 通用品牌
List<StaffPerformanceDTO> result = supervisorDashboardService.getStorePerformanceByMerchant(req); List<StaffPerformanceDTO> result = supervisorDashboardService.getStorePerformanceByMerchant(req);
log.info("通用品牌门店维度业绩查询成功,{}", JSONObject.toJSONString(result)); log.info("通用品牌门店维度业绩查询成功,{}", JSONObject.toJSONString(result));
......
  • @wangxw1
    commented
    Master

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

    1. 代码严重重复,违反 DRY 原则(最佳实践与可维护性)

    • 问题描述:在 SupervisorDashboardService.java 中,第 2236 行和 3756 行附近新增的“初始化驻店员工及填充指标数据”的逻辑完全一致。
    • 影响分析:相同的代码块被复制粘贴到类的不同方法(或不同分支)中。这不仅增加了代码的冗余度,还极大地提高了后续的维护成本。如果未来需要修改聚合逻辑,极易造成多处修改不同步,引发线上 Bug。
    • 优化建议:将这部分数据聚合与映射的逻辑提取为一个独立的私有方法(private Map<String, StaffPerformanceDTO> buildStaffPerformanceMap(...)),并在需要的地方统一调用。

    2. 循环内重复计算与魔法值硬编码(性能与可维护性)

    • 问题描述:在遍历 employeeList 和 allResults 的 for 循环内部,多次执行了 req.getStatDimension() == 1 ? IndicatorConfig.BRAND_ID_HARMONY : IndicatorConfig.BRAND_ID_GENERAL 和 req.getStatDimension() == 1 ? "问界鸿蒙" : "通用品牌"。
    • 影响分析:每次循环都进行相同的条件判断不仅会带来不必要的性能开销,而且将“问界鸿蒙”和“通用品牌”等魔法字符串硬编码在循环内,不利于统一管理和国际化扩展。
    • 优化建议:将不变的判断逻辑提前到循环外部,声明为局部变量;建议将中文字符串提取为常量。 ```java // 提取到循环外部 int statDimension = req.getStatDimension(); int brandId = (statDimension == 1) ? IndicatorConfig.BRAND_ID_HARMONY : IndicatorConfig.BRAND_ID_GENERAL; String brandName = (statDimension == 1) ? "问界鸿蒙" : "通用品牌";

    for (EmployeeRanking employee : employeeList) { // ... staff.setBrandId(brandId); staff.setBrandName(brandName); staff.setStatDimension(statDimension); // ... }

    
    **3. 潜在的 NPE 风险与 Map 初始容量设置不当(功能健壮性与性能)**
    - **问题描述**:代码直接使用 `for (EmployeeRanking employee : employeeList)` 进行遍历,未见对 `employeeList` 的非空判断;同时,`new HashMap<>(employeeList.size())` 初始化容量时未考虑 HashMap 默认 `0.75` 的负载因子。
    - **影响分析**:如果上游逻辑传递的 `employeeList` 为 `null`,此处将抛出 `NullPointerException`;当 `employeeList` 较大时,直接传入 `size` 作为容量会导致 HashMap 在 put 元素时发生不必要的扩容与哈希重排,影响性能。
    - **优化建议**:增加非空判断,并合理设置 Map 的初始容量。
      ```java
      // 增加判空处理与合理的容量初始化
      int expectedSize = employeeList == null ? 0 : employeeList.size();
      // 考虑 0.75 负载因子,避免扩容
      Map<String, StaffPerformanceDTO> phoneMap = new HashMap<>((int) (expectedSize / 0.75f) + 1);
    
      if (employeeList != null) {
          for (EmployeeRanking employee : employeeList) {
              // 业务逻辑...
          }
      }
    

    二、评分明细

    • 功能实现的正确性与健壮性(35/40分):核心业务逻辑正确,成功实现了“即使没有指标数据也返回驻店员工”的需求。扣分在于直接遍历集合存在潜在的 NullPointerException 风险。
    • 安全性与潜在风险(30/30分):代码变更主要为内存数据聚合与 DTO 扩展,未引入 SQL 注入、越权、敏感信息泄露等安全风险。未触发特殊规则中的危险实现。
    • 最佳实践与可维护性(12/20分):存在大段的重复代码(Copy-Paste),严重违反了 DRY(Don't Repeat Yourself)原则;代码中存在魔法字符串硬编码。
    • 性能与资源利用(4/5分):循环内部存在重复的条件判断,且 HashMap 初始化容量未考虑负载因子,存在轻微的性能损耗与扩容开销。
    • 提交信息质量(5/5分):Commit 信息清晰、准确,详细列出了修改点及业务目的,具有良好的可追溯性。

    总分:86分

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