Commit 109cddb1 by zhouhaibao

[+]小数保留两位,新增轻改的产值转换

1 parent 148bbd3b
......@@ -38,6 +38,7 @@ import cn.com.glsx.merchants.remote.MerchantUserRemote;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
......@@ -869,7 +870,7 @@ public class SupervisorDashboardService {
dailyReport.setRest(rest); // 调休
// 计算日报完成率
String completionRate = totalDays > 0 ? String.format("%.0f%%", (double) submitted / totalDays * 100) : "0%";
String completionRate = totalDays > 0 ? String.format("%.2f%%", (double) submitted / totalDays * 100) : "0%";
dailyReport.setCompletionRate(completionRate); // 日报完成率
behaviorData.setDailyReport(dailyReport);
......@@ -935,7 +936,7 @@ public class SupervisorDashboardService {
// 计算成交率
int recordingCount = getIntegerValue(recordingAnalysisData, "recordingCount");
int dealCount = getIntegerValue(recordingAnalysisData, "dealCount");
String dealRate = recordingCount > 0 ? String.format("%.0f%%", (double) dealCount / recordingCount * 100) : "0%";
String dealRate = recordingCount > 0 ? String.format("%.2f%%", (double) dealCount / recordingCount * 100) : "0%";
quantitativeIndicators.setDealRate(dealRate); // 成交率
quantitativeIndicators.setQualityScore(formatPercentage2(getBigValue(recordingAnalysisData, "qualityScore"))); // 质检平均分
......@@ -1285,7 +1286,7 @@ public class SupervisorDashboardService {
// 一次请求所有指标,用逗号隔开
Map<String, BigDecimal> storeMetrics = new HashMap<>();
Map<String, String> storeRates = new HashMap<>(); // 环比数据
String storeItemCodes = "A036,A076,A084,A075";
String storeItemCodes = "A036,A076,A084,A075,A062";
// 根据业务类型调整指标编码
if (req.getBizs() != null && req.getBizs() == 2) { // 2. 轻改
......@@ -1441,7 +1442,7 @@ public class SupervisorDashboardService {
// 轻改品类数据
// 车窗膜产值(指标A0107)
employeePerformanceData.setWindowFilmCount(employeeMetrics.get("A0106") != null ? employeeMetrics.get("A0106").intValue() : 0);
employeePerformanceData.setWindowFilmValue(employeeMetrics.get("A0107"));
employeePerformanceData.setWindowFilmValue(computeSingleVehiclePurchaseOutput(employeeMetrics.get("A0107"), storeMetrics.get("A062"), BigDecimal.valueOf(0.35)));
employeePerformanceData.setWindowFilmValueRate(formatPercentage(employeeRates.get("A0107"))); // 车窗膜产值环比
// 车窗膜渗透率(指标A0108)
employeePerformanceData.setWindowFilmPenetration(formatPercentage(employeeMetrics.get("A0108")));
......@@ -1456,7 +1457,7 @@ public class SupervisorDashboardService {
employeePerformanceData.setBodyFilmDealCount(employeeMetrics.get("A0111") != null ? employeeMetrics.get("A0111").intValue() : 0);
employeePerformanceData.setBodyFilmDealCountRate(formatPercentage(employeeRates.get("A0111"))); // 车身膜成交数环比
// 车身膜产值(指标A0112)
employeePerformanceData.setBodyFilmValue(employeeMetrics.get("A0112"));
employeePerformanceData.setBodyFilmValue(computeSingleVehiclePurchaseOutput(employeeMetrics.get("A0112"), storeMetrics.get("A062"), BigDecimal.ONE));
employeePerformanceData.setBodyFilmValueRate(formatPercentage(employeeRates.get("A0112"))); // 车身膜产值环比
// 车身膜渗透率(指标A0113)
employeePerformanceData.setBodyFilmPenetration(formatPercentage(employeeMetrics.get("A0113")));
......@@ -1471,7 +1472,7 @@ public class SupervisorDashboardService {
employeePerformanceData.setAccessoriesDealCount(employeeMetrics.get("A0116") != null ? employeeMetrics.get("A0116").intValue() : 0);
employeePerformanceData.setAccessoriesDealCountRate(formatPercentage(employeeRates.get("A0116"))); // 精品附件成交数环比
// 精品附件产值(指标A0117)
employeePerformanceData.setAccessoriesValue(employeeMetrics.get("A0117"));
employeePerformanceData.setAccessoriesValue(computeSingleVehiclePurchaseOutput(employeeMetrics.get("A0117"), storeMetrics.get("A062"), BigDecimal.ONE));
employeePerformanceData.setAccessoriesValueRate(formatPercentage(employeeRates.get("A0117"))); // 精品附件产值环比
// 精品附件渗透率(指标A0118)
employeePerformanceData.setAccessoriesPenetration(formatPercentage(employeeMetrics.get("A0118")));
......@@ -1549,6 +1550,22 @@ public class SupervisorDashboardService {
}
/**
* 计算单车采购产值
* 产值*比例/新车交付数
*/
private BigDecimal computeSingleVehiclePurchaseOutput(BigDecimal value, BigDecimal count, BigDecimal rate) {
if (value == null || count == null || rate == null || BigDecimal.ZERO.compareTo(count) == 0) {
return null;
}
try {
return value.multiply(rate).divide(count, 2, RoundingMode.HALF_UP);
} catch (ArithmeticException e) {
log.error("计算单车采购产值时发生除法异常,value={}, count={}, rate={}", value, count, rate);
return null;
}
}
/**
* 获取员工调店记录
* <p>
* 根据员工手机号、门店ID列表和业务类型查询员工调店记录
......
  • @wangxw1
    commented
    Master

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

    1. 健壮性风险:计算方法返回 null 可能导致前端显示异常或后续 NPE(功能正确性与健壮性)

    • 问题描述:在新增的 computeSingleVehiclePurchaseOutput 方法中,当入参为 null 或分母 count 为 0 时,方法返回了 null。在统计类看板的数据封装中,直接将 null 赋值给实体属性(如 setWindowFilmValue),如果前端没有做好判空,或者后续逻辑有对该字段的二次计算(如累加),极易引发 NullPointerException 或页面显示 null。
    • 优化建议:建议在遇到无效入参或除数为 0 时,返回 BigDecimal.ZERO 而不是 null,以保证业务数据的连续性和类型的统一。 java if (value == null || count == null || rate == null || BigDecimal.ZERO.compareTo(count) == 0) { return BigDecimal.ZERO; }

    2. 规范性问题:代码中存在魔法值(最佳实践与可维护性)

    • 问题描述:在第 1441 行,调用计算方法时直接传入了 BigDecimal.valueOf(0.35)。根据《阿里巴巴 Java 开发手册》规定,不允许任何魔法值(未经预先定义的常量)直接出现在代码中。硬编码会降低代码的可读性,且后续如果比例调整,不利于统一维护。
    • 优化建议:将 0.35 提取为具有明确业务含义的常量。

      // 在类顶部定义常量
      private static final BigDecimal WINDOW_FILM_CONVERSION_RATE = new BigDecimal("0.35");
      
      // 替换原有调用
      employeePerformanceData.setWindowFilmValue(computeSingleVehiclePurchaseOutput(employeeMetrics.get("A0107"), storeMetrics.get("A062"), WINDOW_FILM_CONVERSION_RATE));
      

    3. 代码细节:字符串格式化输出的精度不一致(功能正确性与健壮性)

    • 问题描述:在第 869 行和 935 行,三目运算符的真值分支将格式化精度改为了 %.2f%%(如 85.23% 或 0.00%),但假值分支(即 totalDays <= 0 时)依然保留了原来的 "0%"。这会导致前端在不同情况下接收到的数据格式不一致(有时是带有两位小数的字符串,有时是没有小数的字符串)。
    • 优化建议:将默认的兜底值也统一修改为保留两位小数的格式,保持前后端数据契约的一致性。 java // 修改前 String completionRate = totalDays > 0 ? String.format("%.2f%%", (double) submitted / totalDays * 100) : "0%"; // 修改后 String completionRate = totalDays > 0 ? String.format("%.2f%%", (double) submitted / totalDays * 100) : "0.00%";

    二、评分明细

    1. 功能实现的正确性与健壮性(40分):33分。除数判零逻辑严谨(使用了 compareTo),异常捕获处理得当。但返回 null 存在潜在的 NPE 风险,且百分比格式化存在微小的展示不一致。
    2. 安全性与潜在风险(30分):30分。本次变更仅涉及数学计算与数据组装,未引入新的安全漏洞(如 SQL 注入、XSS 等),日志打印也未包含敏感信息。
    3. 最佳实践与可维护性(20分):15分。引入了 0.35 这样的魔法值,违反了阿里规约;且异常捕获 ArithmeticException 在已经判零并指定了 RoundingMode 的情况下实际上是冗余的,不过作为防御性编程可以接受。
    4. 性能与资源利用(5分):5分。BigDecimal 的运算虽然有微小性能损耗,但在当前计费/产值计算场景下是必须且合理的。
    5. 提交信息质量(5分):5分。Commit 信息 "[+]小数保留两位,新增轻改的产值转换" 简洁明了,说清了变更的目的。

    总分:88分

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