Commit c869d461 by zhouhaibao

[+]新增类型参数转换

1 parent 1b8fe383
......@@ -2213,6 +2213,10 @@ public class BusinessAnalysisService {
// 获取兜底预警门店
SceneBaseReq sceneBaseReq = new SceneBaseReq();
sceneBaseReq.setMerchantIds(storeList);
sceneBaseReq.setCycleType(converToCycle(request.getCycleType()));
/*
sceneBaseReq.setCycleType(request.getCycleType());
*/
// 当日期是日的时候,获取的是本周的生态预警
if (request.getCycleType() == DATE_CYCLE_TYPE){
// 1. 定义格式化器 (根据实际入参格式调整,通常是 yyyy-MM-dd)
......@@ -2247,7 +2251,6 @@ public class BusinessAnalysisService {
log.info("兜底预警门店数:{}", backUpStores.size());
// 获取生态预警门店
sceneBaseReq.setBizs(1);
List<Integer> ybEcosystemWarningStores = businessSceneAnalysisService.getDelayWarningStore(sceneBaseReq);
sceneBaseReq.setBizs(2);
List<Integer> qgEcosystemWarningStores = businessSceneAnalysisService.getDelayWarningStore(sceneBaseReq);
......@@ -2320,6 +2323,19 @@ public class BusinessAnalysisService {
return resp;
}
private Integer converToCycle(Integer cycleType) {
switch (cycleType) {
case 0:
return 1;
case 1:
return 2;
case 2:
return 3;
default:
return 1;
}
}
/**
*
......
  • @wangxw1
    commented
    Master

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

    1. 潜在的空指针异常 (NPE) 风险

    • 问题描述:在 converToCycle(Integer cycleType) 方法中,参数 cycleType 是包装类型 Integer。在 switch 语句中,Java 会对 cycleType 进行自动拆箱以进行分支匹配。如果 request.getCycleType() 返回 null,此处将抛出 NullPointerException。
    • 影响分析:可能导致请求处理中断,线上环境出现 500 错误,影响系统稳定性。
    • 优化建议:在 switch 前增加判空处理,或利用 Objects.requireNonNullElse 提供默认值。

    2. 魔法数字 (Magic Numbers) 与硬编码逻辑

    • 问题描述:converToCycle 方法中使用了大量的硬编码数字(0, 1, 2, 3),且 default 分支直接返回 1。这些数字缺乏业务语义,且映射关系隐晦。
    • 影响分析:代码可读性差,维护困难。如果后续业务定义的周期类型发生变化,开发者很难在不查阅文档的情况下理解这些数字的含义,极易引入逻辑错误。
    • 优化建议:使用 Enum(枚举类)或静态常量来定义这些周期类型。

    3. 残留注释代码与命名规范问题

    • 问题描述:代码中保留了被注释掉的旧逻辑(/* sceneBaseReq.setCycleType(...) */),且新增方法 converToCycle 存在拼写错误(缺少字母 't',应为 convertToCycle)。
    • 影响分析:残留注释会干扰代码阅读,增加技术债。拼写错误降低了工程专业度,可能导致后续调用时产生混淆。
    • 优化建议:删除所有不再使用的注释代码;修正方法名为 convertToCycle。

    代码优化示例:

    // 建议使用枚举定义
    public enum CycleTypeEnum {
        DAY(0, 1), WEEK(1, 2), MONTH(2, 3);
        // ... getter & fromCode method
    }
    
    private Integer convertToCycle(Integer cycleType) {
        if (cycleType == null) {
            return 1; // 或抛出业务异常
        }
        switch (cycleType) {
            case 0: return 1;
            case 1: return 2;
            case 2: return 3;
            default: return 1;
        }
    }
    

    二、评分明细

    评分维度 分数 理由
    功能正确性与健壮性 30/40 实现了类型转换逻辑,但未处理 Integer 拆箱引发的 NPE 风险,边界防御不足。
    安全性与潜在风险 30/30 未发现 SQL 注入、越权等显著安全漏洞。
    最佳实践与可维护性 12/20 存在拼写错误、硬编码魔法数字以及残留注释代码,不符合整洁代码规范。
    性能与资源利用 5/5 逻辑简单,无性能瓶颈。
    提交信息质量 4/5 描述了新增功能,但过于简略,未说明转换的原因。

    总分:81分

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