Commit efb52665 by 章家宝

fixbug:修复保存审批记录的时候业务线名称和品牌名称为空的bug

1 parent 13e20617
...@@ -135,13 +135,30 @@ public class PerformanceConfigApproveService { ...@@ -135,13 +135,30 @@ public class PerformanceConfigApproveService {
// 2 保存审批记录 // 2 保存审批记录
List<PerformanceConfigMerchantBrandRefDTO> merchantBrandRefs = performanceConfigMerchantBrandRefService List<PerformanceConfigMerchantBrandRefDTO> merchantBrandRefs = performanceConfigMerchantBrandRefService
.listByConfigId(dto.getId()); .listByConfigId(performanceConfigId);
String merchantNames = merchantBrandRefs.stream().map(PerformanceConfigMerchantBrandRefDTO::getMerchantName).collect(Collectors.joining(",")); String merchantNames = merchantBrandRefs.stream().map(PerformanceConfigMerchantBrandRefDTO::getMerchantName).collect(Collectors.joining(","));
Set<String> brandNames = new HashSet<>(); Map<String, Set<String>> nameMap = new HashMap<>();
for (PerformanceConfigMerchantBrandRefDTO merchantBrandRef : merchantBrandRefs) { for (PerformanceConfigMerchantBrandRefDTO merchantBrandRef : merchantBrandRefs) {
brandNames.add(String.format("%s-%s", merchantBrandRef.getBrandName(), merchantBrandRef.getChildName())); Set<String> childs = nameMap.computeIfAbsent(merchantBrandRef.getBrandName(), k -> new HashSet<>());
String child = merchantBrandRef.getChildName();
if (StringUtils.isNotBlank(child)) {
childs.add(child);
}
}
List<String> brandList = new ArrayList<>();
for (Map.Entry<String, Set<String>> entry : nameMap.entrySet()) {
String firstBrand = entry.getKey();
Set<String> secondBrands = entry.getValue();
if (secondBrands == null || secondBrands.isEmpty()) {
brandList.add(firstBrand);
}
else {
String secondBrandStr = String.join("/", secondBrands);
String brandItem = firstBrand + "-" + secondBrandStr;
brandList.add(brandItem);
}
} }
String brandsNames = String.join(",", brandNames); String brandsNames = String.join(",", brandList);
dto.setMerchantNames(merchantNames); dto.setMerchantNames(merchantNames);
dto.setBrandsNames(brandsNames); dto.setBrandsNames(brandsNames);
dto.setApproveStatus(PerformanceConfigApproveStatusEnum.APPROVEING.getCode()); dto.setApproveStatus(PerformanceConfigApproveStatusEnum.APPROVEING.getCode());
......
  • @wangxw1
    commented
    Master

    你好,我是负责此次代码审查的资深软件开发工程师。

    这段代码主要修改了 PerformanceConfigApproveService 中构建 brandsNames 字符串的逻辑,将原本简单的平铺结构改为按“品牌-子品牌/子品牌”聚合的格式。

    以下是我的审查结果:

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

    1. 数据一致性风险:集合遍历顺序不确定

    • 问题描述:代码中使用了 HashMap 存储品牌聚合信息,使用 HashSet 存储子品牌集合。由于 HashMap and HashSet 是无序的,这会导致生成的 brandsNames 字符串中,品牌出现的顺序以及同一品牌下子品牌的顺序是随机的。
    • 影响分析:
      • 数据不一致:对于相同的输入数据,多次执行可能生成不同的字符串(例如一次是 "A-a/b, B",另一次是 "B, A-b/a")。
      • 测试困难:导致单元测试断言难以编写,且在前端展示或日志排查时造成困扰。
    • 优化建议:
      • 使用 TreeMap 和 TreeSet 替代 Hash 实现,或者在进行 String.join 之前对 List/Set 进行排序,确保输出结果的确定性。

    2. 健壮性不足:潜在的 Null 拼接问题

    • 问题描述:在循环中直接使用 merchantBrandRef.getBrandName() 作为 Map 的 Key,且后续直接进行字符串拼接 firstBrand + "-" + ...。
    • 影响分析:如果数据库中存在脏数据导致 BrandName 为 null,代码不会报错,但生成的字符串会包含 "null-ChildName" 或单纯的 "null",这对业务展示是不友好的。
    • 优化建议:在处理数据前增加非空过滤,或者使用 Optional 处理。

    3. 代码可维护性:逻辑实现过于冗长

    • 问题描述:使用了两个 for 循环加上中间变量 nameMap 和 brandList 来完成分组和格式化,代码行数较多,且存在变量命名不规范(如 childs 应为 children)。
    • 影响分析:降低了代码的可读性,增加了维护成本。
    • 优化建议:利用 Java 8 Stream API 可以更简洁、清晰地完成分组和映射。

    优化代码示例(综合上述三点):

    // 2 保存审批记录
    List<PerformanceConfigMerchantBrandRefDTO> merchantBrandRefs = performanceConfigMerchantBrandRefService
            .listByConfigId(performanceConfigId);
    
    String merchantNames = merchantBrandRefs.stream()
            .map(PerformanceConfigMerchantBrandRefDTO::getMerchantName)
            .filter(StringUtils::isNotBlank) // 防御性编程
            .distinct()
            .collect(Collectors.joining(","));
    
    // 使用 TreeMap 保证品牌排序,Value 使用 TreeSet 保证子品牌排序
    Map<String, Set<String>> groupedMap = merchantBrandRefs.stream()
            .filter(dto -> StringUtils.isNotBlank(dto.getBrandName())) // 过滤空品牌
            .collect(Collectors.groupingBy(
                    PerformanceConfigMerchantBrandRefDTO::getBrandName,
                    TreeMap::new, // 使用 TreeMap 保证品牌有序
                    Collectors.mapping(
                            dto -> StringUtils.defaultString(dto.getChildName(), ""), 
                            Collectors.toCollection(TreeSet::new) // 使用 TreeSet 保证子品牌有序
                    )
            ));
    
    String brandsNames = groupedMap.entrySet().stream()
            .map(entry -> {
                String brand = entry.getKey();
                Set<String> children = entry.getValue();
                // 移除空字符串(处理 childName 为空的情况)
                children.removeIf(String::isEmpty);
    
                if (children.isEmpty()) {
                    return brand;
                } else {
                    return brand + "-" + String.join("/", children);
                }
            })
            .collect(Collectors.joining(","));
    
    dto.setMerchantNames(merchantNames);
    dto.setBrandsNames(brandsNames);
    dto.setApproveStatus(PerformanceConfigApproveStatusEnum.APPROVEING.getCode());
    

    二、评分明细

    1. 功能实现的正确性与健壮性(30/40分)
      • 扣分理由:使用了无序集合(HashMap/HashSet)导致生成的字符串顺序随机,缺乏确定性;对 BrandName 为空的情况缺乏防御处理。
    2. 安全性与潜在风险(30/30分)
      • 理由:逻辑主要涉及数据对象转换,未发现明显的 SQL 注入、XSS 或越权风险。
    3. 最佳实践与可维护性(12/20分)
      • 扣分理由:代码略显冗余,未使用 Stream API 简化逻辑;变量命名 childs 存在语法错误(应为 children);变量 firstBrand/secondBrands 命名语意不够直观(建议 brandName/childNames)。
    4. 性能与资源利用(5/5分)
      • 理由:数据量预期不大,当前的循环处理方式在性能上是可以接受的。
    5. 提交信息质量(5/5分)
      • 理由:Commit message 清晰描述了修复的 Bug 内容和意图。

    三、总分

    总分:82分

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