Commit 643c23b9 by zhouhaibao

refactor(BusinessSceneAnalysisService): 优化品类分析服务中的单车采购产值计算逻辑

- 新增带参数的goodsSummary重载方法,支持控制是否计算单车采购产值
- 为方法添加详细的JavaDoc注释说明参数含义和用途
- 将单车采购产值计算逻辑包装在条件判断中,根据参数决定是否执行
- 修改原有调用方式,在特定场景下调用不计算单车产值的版本
- 在后续处理中重新计算单车购买产出值,确保数据准确性
- 提升代码灵活性和可维护性,避免重复计算问题
1 parent c768eed2
......@@ -2074,6 +2074,17 @@ public class BusinessSceneAnalysisService {
* 查询品类分析
*/
public GoodsCategorySummaryResponse goodsSummary(SceneBaseReq req) {
return goodsSummary(req, true);
}
/**
* 获取品类分析汇总数据
*
* @param req 查询参数
* @param calculatePerCarOutput 是否计算单车采购产值(true-计算,false-不计算,由调用方后续计算)
* @return 品类分析汇总响应
*/
public GoodsCategorySummaryResponse goodsSummary(SceneBaseReq req, boolean calculatePerCarOutput) {
GoodsCategorySummaryResponse res = new GoodsCategorySummaryResponse();
try {
......@@ -2108,17 +2119,19 @@ public class BusinessSceneAnalysisService {
handlerBusinessData(code, data, res);
});
// 计算单车采购产值
// BigDecimal rate = aggregateType.equals("staffIds") ? BigDecimal.ONE : BigDecimal.valueOf(0.35);
BigDecimal carBodyFilmPerCarOutput = computeSingleVehiclePurchaseOutput(res.getCarBodyFilmPerCarOutput(), res.getNewCarDeliveryCount(), BigDecimal.ONE);
BigDecimal accessoriesPerCarOutput = computeSingleVehiclePurchaseOutput(res.getAccessoriesPerCarOutput(), res.getNewCarDeliveryCount(), BigDecimal.ONE);
BigDecimal windowFilmPerCarOutput = computeSingleVehiclePurchaseOutput(res.getWindowFilmPerCarOutput(), res.getNewCarDeliveryCount(), BigDecimal.valueOf(0.35));
res.setCarBodyFilmPerCarOutput(carBodyFilmPerCarOutput);
res.setAccessoriesPerCarOutput(accessoriesPerCarOutput);
res.setWindowFilmPerCarOutput(windowFilmPerCarOutput);
res.setWindowFilmEcoCoefficient(ecoCoefficientService.getEcoCoefficient(DataStatsConstants.ITEM_CODE_A0107, windowFilmPerCarOutput)); // 车窗膜-生态系数
res.setCarBodyFilmEcoCoefficient(ecoCoefficientService.getEcoCoefficient(DataStatsConstants.ITEM_CODE_A0112, carBodyFilmPerCarOutput)); // 车身膜-生态系数
res.setAccessoriesEcoCoefficient(ecoCoefficientService.getEcoCoefficient(DataStatsConstants.ITEM_CODE_A0117, accessoriesPerCarOutput)); // 精品附件-生态系数
// 计算单车采购产值(根据参数控制是否计算)
if (calculatePerCarOutput) {
// BigDecimal rate = aggregateType.equals("staffIds") ? BigDecimal.ONE : BigDecimal.valueOf(0.35);
BigDecimal carBodyFilmPerCarOutput = computeSingleVehiclePurchaseOutput(res.getCarBodyFilmPerCarOutput(), res.getNewCarDeliveryCount(), BigDecimal.ONE);
BigDecimal accessoriesPerCarOutput = computeSingleVehiclePurchaseOutput(res.getAccessoriesPerCarOutput(), res.getNewCarDeliveryCount(), BigDecimal.ONE);
BigDecimal windowFilmPerCarOutput = computeSingleVehiclePurchaseOutput(res.getWindowFilmPerCarOutput(), res.getNewCarDeliveryCount(), BigDecimal.valueOf(0.35));
res.setCarBodyFilmPerCarOutput(carBodyFilmPerCarOutput);
res.setAccessoriesPerCarOutput(accessoriesPerCarOutput);
res.setWindowFilmPerCarOutput(windowFilmPerCarOutput);
res.setWindowFilmEcoCoefficient(ecoCoefficientService.getEcoCoefficient(DataStatsConstants.ITEM_CODE_A0107, windowFilmPerCarOutput)); // 车窗膜-生态系数
res.setCarBodyFilmEcoCoefficient(ecoCoefficientService.getEcoCoefficient(DataStatsConstants.ITEM_CODE_A0112, carBodyFilmPerCarOutput)); // 车身膜-生态系数
res.setAccessoriesEcoCoefficient(ecoCoefficientService.getEcoCoefficient(DataStatsConstants.ITEM_CODE_A0117, accessoriesPerCarOutput)); // 精品附件-生态系数
}
// 查询兜底目标(非轻改场景)
if (req.getBizs() == 2) {
......@@ -2664,8 +2677,8 @@ public class BusinessSceneAnalysisService {
req.setStartTime(config.getPeriodStartDate());
req.setEndTime(config.getPeriodEndDate());
}
// 先调用原有方法获取基础数据
res = goodsSummary(req);
// 先调用原有方法获取基础数据(不计算单车采购产值,后续会根据新交付数重新计算)
res = goodsSummary(req, false);
// 如果门店列表为空,直接返回
if (CollectionUtils.isEmpty(req.getMerchantIds())) {
......@@ -2695,6 +2708,14 @@ public class BusinessSceneAnalysisService {
return;
}
// 重新计算单车购买产出值(使用新的交付数)
BigDecimal carBodyFilmPerCarOutput = computeSingleVehiclePurchaseOutput(res.getCarBodyFilmPerCarOutput(), newDeliveryCount, BigDecimal.ONE);
BigDecimal accessoriesPerCarOutput = computeSingleVehiclePurchaseOutput(res.getAccessoriesPerCarOutput(), newDeliveryCount, BigDecimal.ONE);
BigDecimal windowFilmPerCarOutput = computeSingleVehiclePurchaseOutput(res.getWindowFilmPerCarOutput(), newDeliveryCount, BigDecimal.valueOf(0.35));
res.setCarBodyFilmPerCarOutput(carBodyFilmPerCarOutput);
res.setAccessoriesPerCarOutput(accessoriesPerCarOutput);
res.setWindowFilmPerCarOutput(windowFilmPerCarOutput);
// 整车延保
recalculateCategory(res.getVehicleWarrantyDealCount(), newDeliveryCount,
(rate) -> {
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议,如果必要,给出代码示例(仅限 Top 3)

    1. 遗漏生态系数(EcoCoefficient)的计算逻辑(重要逻辑缺陷)

    • 问题描述:在 goodsSummary 方法中,当 calculatePerCarOutput 为 true 时,计算完单车产值(xxxPerCarOutput)后会紧接着调用 ecoCoefficientService 计算生态系数。但在外部重新计算单车产值的代码块中(第2708行之后),仅重新计算了单车产值,遗漏了对生态系数的重新计算。
    • 影响分析:由于调用了 goodsSummary(req, false),内部跳过了生态系数的计算。外部重新计算产值后也没有补充该逻辑,将导致最终返回结果中车窗膜、车身膜、精品附件的生态系数(EcoCoefficient)数据为空或为默认值,造成业务数据不完整。
    • 优化建议:在外部重新计算单车产值后,补充生态系数的计算逻辑。为避免代码重复,建议将这部分逻辑提取为私有方法。

    2. 核心计算逻辑重复(违反 DRY 原则)

    • 问题描述:计算单车购买产出值的三行核心代码(包含硬编码的 0.35 等系数)在 goodsSummary 内部和外部被完全复制粘贴了两次。
    • 影响分析:代码可维护性降低。如果未来车窗膜的计算系数(如 0.35)或计算规则发生变化,需要在多个地方同步修改,极易因遗漏导致线上 Bug。
    • 优化建议:将“计算单车产值并设置生态系数”的逻辑封装为一个私有辅助方法复用。 代码示例:

      private void calculateAndSetPerCarOutput(GoodsCategorySummaryResponse res, int deliveryCount) {
        BigDecimal carBodyFilm = computeSingleVehiclePurchaseOutput(res.getCarBodyFilmPerCarOutput(), deliveryCount, BigDecimal.ONE);
        BigDecimal accessories = computeSingleVehiclePurchaseOutput(res.getAccessoriesPerCarOutput(), deliveryCount, BigDecimal.ONE);
        BigDecimal windowFilm = computeSingleVehiclePurchaseOutput(res.getWindowFilmPerCarOutput(), deliveryCount, BigDecimal.valueOf(0.35));
      
        res.setCarBodyFilmPerCarOutput(carBodyFilm);
        res.setAccessoriesPerCarOutput(accessories);
        res.setWindowFilmPerCarOutput(windowFilm);
      
        res.setWindowFilmEcoCoefficient(ecoCoefficientService.getEcoCoefficient(DataStatsConstants.ITEM_CODE_A0107, windowFilm));
        res.setCarBodyFilmEcoCoefficient(ecoCoefficientService.getEcoCoefficient(DataStatsConstants.ITEM_CODE_A0112, carBodyFilm));
        res.setAccessoriesEcoCoefficient(ecoCoefficientService.getEcoCoefficient(DataStatsConstants.ITEM_CODE_A0117, accessories));
      }
      

    3. DTO 字段语义存在歧义与复用(可维护性隐患)

    • 问题描述:从代码逻辑可以看出,res.getCarBodyFilmPerCarOutput() 在传入计算方法前,实际存储的是“总产值”,计算完成后又将“单车产值”覆写回同一个字段。
    • 影响分析:这是原代码遗留的坏味道,但在本次重构中被放大了。当调用 goodsSummary(req, false) 时,返回的 Response 对象中名为 xxxPerCarOutput 的字段实际装载的是“总产值”,这打破了对象字段的语义契约,容易对后续接手代码的开发者造成严重误导。
    • 优化建议:最佳方案是在 Response 中区分“总产值”和“单车产值”字段。如果因接口兼容性无法修改 DTO,务必在 goodsSummary(SceneBaseReq req, boolean calculatePerCarOutput) 的方法注释中明确警告:当入参为 false 时,返回对象中的 PerCarOutput 字段代表总产值。

    二、评分明细

    • 功能实现的正确性与健壮性(32/40分):重构提取了方法并优化了性能,但遗漏了关联数据(生态系数)的计算,导致特定场景下数据返回不完整,扣除8分。
    • 安全性与潜在风险(30/30分):未发现 SQL 注入、越权等安全隐患。保留了原方法的重载,未破坏原有接口兼容性(符合 Dubbo 接口安全规范)。
    • 最佳实践与可维护性(15/20分):通过参数控制避免重复计算思路正确,但出现了代码复制粘贴(CV)现象,且放大了 DTO 字段语义不清的问题,扣除5分。
    • 性能与资源利用(5/5分):通过引入 calculatePerCarOutput 标志位,成功避免了在特定场景下的无效计算,对性能有正面优化。
    • 提交信息质量(5/5分):Commit 信息格式规范,清晰地描述了修改的背景、动作及目的,可追溯性良好。

    总分:87分

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