Commit ed51167a by 章家宝

fixbug: 提成构成根据门店-子品牌去归纳汇总数据

1 parent 060f4897
...@@ -315,7 +315,10 @@ public class StaffProductPerformanceService { ...@@ -315,7 +315,10 @@ public class StaffProductPerformanceService {
List<StaffPerformanceGetDTO> staffPerformanceGetDTOList = new ArrayList<>(); List<StaffPerformanceGetDTO> staffPerformanceGetDTOList = new ArrayList<>();
for (List<StaffProductPerformanceDTO> value : map.values()) { for (List<StaffProductPerformanceDTO> v : map.values()) {
// 根据子品牌分组
Map<Integer, List<StaffProductPerformanceDTO>> collect = v.stream().collect(Collectors.groupingBy(StaffProductPerformanceDTO::getChildBrandId));
for (List<StaffProductPerformanceDTO> value : collect.values()) {
Set<Byte> businessList = new HashSet<>(); Set<Byte> businessList = new HashSet<>();
Set<String> childBrandList = new HashSet<>(); Set<String> childBrandList = new HashSet<>();
...@@ -331,7 +334,8 @@ public class StaffProductPerformanceService { ...@@ -331,7 +334,8 @@ public class StaffProductPerformanceService {
BigDecimal totalPerformanceAmount = BigDecimal.ZERO; BigDecimal totalPerformanceAmount = BigDecimal.ZERO;
for (StaffPerformanceDTO performanceDTO : staffPerformanceDTOList) { for (StaffPerformanceDTO performanceDTO : staffPerformanceDTOList) {
if (performanceDTO.getMerchantId() != null && performanceDTO.getMerchantId().equals(staffProductPerformanceDTO.getMerchantId())) { if (performanceDTO.getMerchantId() != null && performanceDTO.getMerchantId().equals(staffProductPerformanceDTO.getMerchantId())
&& performanceDTO.getChildBrandId().equals(staffProductPerformanceDTO.getChildBrandId())) {
effectiveReferralQuantity += performanceDTO.getEffectiveReferralQuantity(); effectiveReferralQuantity += performanceDTO.getEffectiveReferralQuantity();
touchNumber += performanceDTO.getTouchNumber(); touchNumber += performanceDTO.getTouchNumber();
if (performanceDTO.getBusinessType() != null) { if (performanceDTO.getBusinessType() != null) {
...@@ -356,6 +360,7 @@ public class StaffProductPerformanceService { ...@@ -356,6 +360,7 @@ public class StaffProductPerformanceService {
staffPerformanceGetDTO.setChildBrandList(childBrandList); staffPerformanceGetDTO.setChildBrandList(childBrandList);
staffPerformanceGetDTO.setTotalPerformanceAmount(totalPerformanceAmount); staffPerformanceGetDTO.setTotalPerformanceAmount(totalPerformanceAmount);
} }
}
return staffPerformanceGetDTOList; return staffPerformanceGetDTOList;
} }
......
  • @wangxw1
    commented
    Master

    你好,我是负责此次代码审查的资深软件开发工程师。你的代码实现了按“门店-子品牌”维度的归纳汇总,逻辑方向符合 Commit 描述,但在健壮性、性能和可读性上存在显著优化空间。

    以下是我的审查意见:

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

    1. 存在高风险的 NullPointerException(NPE)隐患

    • 问题描述:在内层循环判断条件中,新增了 performanceDTO.getChildBrandId().equals(...)。如果 staffPerformanceDTOList 中的元素 childBrandId 为 null,此处将直接抛出空指针异常,导致整个接口崩溃。此外,groupingBy 允许 null 键,若当前分组的 childBrandId 为 null,后续逻辑也需小心处理。
    • 影响分析:生产环境一旦出现脏数据(子品牌 ID 为空),服务将不可用(Severity: High)。
    • 优化建议:使用 Objects.equals() 进行空安全的比较,或者在调用 .equals() 前显式判空。

    2. 嵌套循环导致性能复杂度过高(O(N*M))

    • 问题描述:代码在双重循环(门店分组 -> 子品牌分组)内部,又完整遍历了一次 staffPerformanceDTOList。假设外层分组后有 M 个组,List 大小为 N,整体复杂度接近 O(M*N)。当数据量较大时,这将成为显著的性能瓶颈。
    • 影响分析:在大促或数据量增长场景下,接口响应时间将呈线性甚至指数级增长,占用 CPU 资源。
    • 优化建议:空间换时间。在循环开始前,将 staffPerformanceDTOList 预处理为 Map<String, List<StaffPerformanceDTO>>(key 可由 merchantId + childBrandId 组成),在循环内直接 O(1) 获取对应的数据列表。

    3. 变量命名随意,代码可读性与可维护性差

    • 问题描述:使用了 v、collect、value 等无明确业务含义的变量名。且 value 变量名在内层循环中遮蔽了通常理解的含义,极易造成混淆。
    • 影响分析:增加了后续维护人员理解代码逻辑的成本,违背了 Clean Code 原则。
    • 优化建议:使用具有业务含义的命名,如 merchantPerformanceList、brandGroupMap、brandPerformanceList。

    综合优化代码示例:

    // 优化建议:预处理 staffPerformanceDTOList,避免嵌套循环
    // Key 建议构造一个唯一键,或者使用嵌套 Map: Map<MerchantId, Map<BrandId, List<DTO>>>
    Map<String, List<StaffPerformanceDTO>> performanceIndexMap = staffPerformanceDTOList.stream()
        .filter(dto -> dto.getMerchantId() != null && dto.getChildBrandId() != null)
        .collect(Collectors.groupingBy(dto -> dto.getMerchantId() + "_" + dto.getChildBrandId()));
    
    for (List<StaffProductPerformanceDTO> merchantList : map.values()) { // 变量名 v -> merchantList
        // 变量名 collect -> brandGroupMap
        Map<Integer, List<StaffProductPerformanceDTO>> brandGroupMap = merchantList.stream()
                .collect(Collectors.groupingBy(StaffProductPerformanceDTO::getChildBrandId));
    
        for (List<StaffProductPerformanceDTO> brandList : brandGroupMap.values()) { // 变量名 value -> brandList
            // ... 初始化代码 ...
            StaffProductPerformanceDTO currentDto = brandList.get(0);
    
            // 优化:直接从 Map 获取,代替内层循环
            String key = currentDto.getMerchantId() + "_" + currentDto.getChildBrandId();
            List<StaffPerformanceDTO> matchedPerformances = performanceIndexMap.getOrDefault(key, Collections.emptyList());
    
            for (StaffPerformanceDTO performanceDTO : matchedPerformances) {
                 // 累加逻辑,移除 if 判断,因为 Map 已经保证了匹配
                 effectiveReferralQuantity += performanceDTO.getEffectiveReferralQuantity();
                 // ...
            }
            // ... 后续逻辑 ...
        }
    }
    

    二、评分明细

    1. 功能实现的正确性与健壮性(30/40分)

      • 扣分理由:存在明显的 NPE 风险(Issue #1),未对 childBrandId 可能为空的情况做防御性编程。逻辑大体符合需求,但健壮性不足。
    2. 安全性与潜在风险(30/30分)

      • 理由:未引入明显的 SQL 注入、越权或敏感信息泄露风险。
    3. 最佳实践与可维护性(12/20分)

      • 扣分理由:变量命名(v, collect, value)非常不规范,严重影响可读性。代码结构嵌套过深,缺乏模块化思维。
    4. 性能与资源利用(2/5分)

      • 扣分理由:在多层嵌套循环中遍历列表,算法复杂度高,存在明显的性能优化空间。
    5. 提交信息质量(5/5分)

      • 理由:Commit message 清晰地描述了修改目的(bugfix)和具体内容(维度归纳),符合规范。

    三、总分

    总分:79分

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