Commit c976781b by zhouhaibao

[+]兜底预警生态预警查询优化以及员工日报推送产值达成率问题修复

1 parent 061ba4bf
...@@ -2311,14 +2311,28 @@ public class BusinessAnalysisService { ...@@ -2311,14 +2311,28 @@ public class BusinessAnalysisService {
request.setCycleType(MONTH_CYCLE_TYPE); request.setCycleType(MONTH_CYCLE_TYPE);
List<DataBusinessDTO> currentMonthOutPut = getOutputGroupByBiz(request); List<DataBusinessDTO> currentMonthOutPut = getOutputGroupByBiz(request);
tagetOutput= currentMonthOutPut.stream() totalOutput= currentMonthOutPut.stream()
.map(DataBusinessDTO::getItemValue) .map(DataBusinessDTO::getItemValue)
.filter(Objects::nonNull) // 过滤 null 值 .filter(Objects::nonNull) // 过滤 null 值
.reduce(BigDecimal.ZERO, BigDecimal::add); .reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal newtagetOutput = new BigDecimal("0.0");
for (DataBusinessDTO dto : currentMonthOutPut) {
if (dto.getMerchantId() == null || dto.getBiz() == null) {
continue;
} }
String key = dto.getMerchantId() + "##" + convertToBusinessType(Integer.valueOf(dto.getBiz()));
newtagetOutput = newtagetOutput.add(stringBigDecimalMap.getOrDefault(key, BigDecimal.ZERO)); // ✅ 关键:重新赋值!
}
log.info("当月产值{},当月目标:{}",totalOutput,newtagetOutput);
resp.setCurrentAchievementRate(
calculatePercent(totalOutput, newtagetOutput));
}else {
resp.setCurrentAchievementRate( resp.setCurrentAchievementRate(
calculatePercent(totalOutput, tagetOutput)); calculatePercent(totalOutput, tagetOutput));
}
return resp; return resp;
} }
......
  • @wangxw1
    commented
    Master

    你好,我是代码审查员。正在对你的代码进行审查,以下是审查结果及建议。

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

    1. 业务逻辑缺陷:目标值计算依赖实际产出(严重)

    • 问题描述:在计算 newtagetOutput(当月目标)时,代码遍历的是 currentMonthOutPut(当月实际产出列表)。
    • 影响分析:这是一个严重的逻辑漏洞。如果某商户/业务线本月有目标值(Target),但实际产出为 0(即不在 currentMonthOutPut 列表中,或列表为空),那么该商户的目标值将不会被累加到 newtagetOutput 中。这会导致分母变小,计算出的“达成率”虚高,甚至出现实际上未开工但达成率为 100%(如果逻辑处理了 0/0)或异常的情况。
    • 优化建议:
      • 目标值的计算不应依赖“实际产出列表”。
      • 应该根据查询条件(如商户ID列表、时间周期)直接从 stringBigDecimalMap 或配置源中获取所有相关的目标值进行累加。
      • 如果必须依赖列表,请确保列表包含所有“有目标”的商户,而不仅仅是“有产出”的商户。

    2. 健壮性风险:数据类型转换缺乏保护

    • 问题描述:代码中直接使用了 Integer.valueOf(dto.getBiz()),仅判空了 dto.getBiz(),未判断字符串内容是否为数字。
    • 影响分析:若上游数据脏污,dto.getBiz() 返回了非数字字符串(如 "Unknown" 或空字符串 ""),此处会直接抛出 NumberFormatException,导致整个接口请求失败,系统稳定性受损。
    • 优化建议:
      • 在转换前增加格式校验(如 StringUtils.isNumeric)。
      • 或者使用 try-catch 捕获异常并记录错误日志,跳过异常数据,避免阻断主流程。

    3. 代码规范与可维护性:命名拼写错误与魔法值

    • 问题描述:
      • 拼写错误:变量名 tagetOutput 和 newtagetOutput 中 "taget" 拼写错误,应为 "target"。且 newtagetOutput 命名随意,不符合语义化标准。
      • 魔法值:"##" 硬编码在循环中作为 Key 的拼接符;new BigDecimal("0.0") 写法不够优雅。
    • 影响分析:拼写错误降低了代码的专业度和可读性,给后续维护(如搜索变量)带来困扰。硬编码的分隔符导致若 Key 生成规则变更,需修改多处代码。
    • 优化建议:
      • 修正变量名为 totalTargetOutput 或 currentTargetSum。
      • 使用 BigDecimal.ZERO 替代 new BigDecimal("0.0")。
      • 将 "##" 抽取为常量 KEY_SEPARATOR。

    代码优化示例(针对上述问题):

    // 建议优化后的代码片段
    BigDecimal totalActualOutput = currentMonthOutPut.stream()
        .map(DataBusinessDTO::getItemValue)
        .filter(Objects::nonNull)
        .reduce(BigDecimal.ZERO, BigDecimal::add);
    
    BigDecimal totalTargetOutput = BigDecimal.ZERO;
    
    // 【修正逻辑】此处假设你需要根据实际产出的商户去匹配目标(即使这在业务上可能不严谨,但在不改变原有数据源结构下的防御性写法)
    // 更好的做法是遍历所有应有目标的商户列表,而非 output 列表
    for (DataBusinessDTO dto : currentMonthOutPut) {
        if (dto.getMerchantId() == null || dto.getBiz() == null) {
            continue;
        }
        try {
            // 增加类型转换保护
            String bizType = convertToBusinessType(Integer.valueOf(dto.getBiz()));
            String key = dto.getMerchantId() + KEY_SEPARATOR + bizType; // 使用常量
            totalTargetOutput = totalTargetOutput.add(stringBigDecimalMap.getOrDefault(key, BigDecimal.ZERO));
        } catch (NumberFormatException e) {
            log.warn("业务类型数据异常, biz: {}", dto.getBiz());
        }
    }
    
    log.info("当月产值: {}, 当月目标: {}", totalActualOutput, totalTargetOutput);
    resp.setCurrentAchievementRate(calculatePercent(totalActualOutput, totalTargetOutput));
    

    二、评分明细

    1. 功能实现的正确性与健壮性(25/40分)
      • 扣分点:目标值计算逻辑严重依赖实际产出数据(Issue #1),这在报表类业务中是致命逻辑伤;缺乏数字转换的异常处理(Issue #2)。
    2. 安全性与潜在风险(28/30分)
      • 扣分点:无明显注入或越权风险,但 Map 的并发访问安全性在代码片段中无法确认(假设为只读配置则无碍)。
    3. 最佳实践与可维护性(12/20分)
      • 扣分点:存在明显的拼写错误 (taget),变量命名 newtagetOutput 质量较低,存在魔法字符串。
    4. 性能与资源利用(4/5分)
      • 扣分点:循环内字符串拼接对性能有微小影响,但在数据量不大时可忽略,BigDecimal 初始化方式可微调。
    5. 提交信息质量(4/5分)
      • 扣分点:提交信息涵盖了“优化”和“修复”,描述尚可,但建议将重构和Bug修复拆分提交。

    总分:73分

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