Commit 1207ce1a by 杨卫

feat:预警引擎生态系数策略按照指标代码查询

1 parent 6b507a61
......@@ -520,11 +520,10 @@ public class WarningEngineRemoteImpl implements WarningEngineRemote {
return value;
}
try {
BigDecimal coefficient = ecoCoefficientQueryService.getCoefficient(metricCode, value);
return coefficient != null ? coefficient : value;
return ecoCoefficientQueryService.getCoefficient(metricCode, value);
} catch (Exception e) {
log.warn("生态系数转换失败,metricCode={}", metricCode, e);
return value;
return null;
}
}
}
......@@ -92,7 +92,7 @@ public class EcoCoefficientQueryService implements ApplicationListener<Applicati
return coefficient;
}
log.warn("生态系数规则未命中,indicatorCode:{},metricValue:{}", indicatorCode, metricValue);
return BigDecimal.ZERO;
return null;
}
private BigDecimal matchRules(List<EcoCoefficientRule> rules, BigDecimal metricValue) {
......
......@@ -168,7 +168,7 @@ public class WarningEngineService {
String itemCode = dict != null ? dict.getStoreIndexCode() : null;
BigDecimal rawValue = getStoreValueFromResult(metricValues, storeId, itemCode);
BigDecimal scaledValue = scaleMetricValue(rawValue, dict);
BigDecimal value = transformMetricValue(scaledValue, config.getWarningType(), dict);
BigDecimal value = transformMetricValue(scaledValue, config.getWarningType(), dict, true);
WarningOperatorEnum operator = WarningOperatorEnum.fromSymbol(cond.getOperator());
ThresholdValue threshold = resolveThresholdForCond(cond, preloadedThresholds, String.valueOf(storeId));
boolean met = operator.evaluate(value,
......@@ -264,7 +264,7 @@ public class WarningEngineService {
String itemCode = dict != null ? dict.getStaffIndexCode() : null;
BigDecimal rawValue = getStaffValueFromResult(metricValues, storeIdStr, staffId, itemCode);
BigDecimal scaledValue = scaleMetricValue(rawValue, dict);
BigDecimal value = transformMetricValue(scaledValue, config.getWarningType(), dict);
BigDecimal value = transformMetricValue(scaledValue, config.getWarningType(), dict, false);
WarningOperatorEnum operator = WarningOperatorEnum.fromSymbol(cond.getOperator());
ThresholdValue threshold = resolveThresholdForCond(cond, preloadedThresholds, storeIdStr);
boolean met = operator.evaluate(value,
......@@ -324,13 +324,14 @@ public class WarningEngineService {
* @since 2026/06/02 16:00
*/
private BigDecimal transformMetricValue(BigDecimal metricValue, Integer warningType,
PgWarningMetricDict dict) {
PgWarningMetricDict dict, boolean storeTarget) {
if (metricValue == null) {
return null;
}
TransformContext ctx = TransformContext.builder()
.metricValue(metricValue)
.metricDict(dict)
.storeTarget(storeTarget)
.build();
for (MetricValueTransformStrategy strategy : metricValueTransformStrategies) {
if (strategy.supportedWarningType() != null
......
......@@ -6,6 +6,7 @@ import cn.com.glsx.dj.strategy.dto.warning.StoreWarningSummaryDTO;
import cn.com.glsx.dj.strategy.dto.warning.WarningRecordDTO;
import cn.com.glsx.dj.strategy.dto.warning.WarningRecordQueryRequest;
import cn.com.glsx.dj.strategy.mapper.PgWarningRecordMapper;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
......@@ -38,6 +39,10 @@ public class WarningRecordService {
* @return 预警记录列表
*/
public List<WarningRecordDTO> queryRecords(WarningRecordQueryRequest request, List<Integer> merchantIds) {
if (StrUtil.isBlank(request.getTimeDimension())) {
return new ArrayList<>();
}
ProjectGroupWarningRecordQueryDTO queryDTO = buildQueryDTO(request);
if (CollectionUtils.isNotEmpty(merchantIds)) {
queryDTO.setMerchantIds(merchantIds);
......
......@@ -82,17 +82,22 @@ public class EcoMetricValueTransformStrategy implements MetricValueTransformStra
&& UNIT_PERCENT.equals(dict.getUnit())) {
originalValue = scaledValue.divide(PERCENT_SCALE, 10, RoundingMode.HALF_UP);
}
BigDecimal coefficient = ecoCoefficientQueryService.getCoefficient(
dict.getIndexName(), originalValue);
String metricCode = ctx.isStoreTarget() ? dict.getStoreIndexCode() : dict.getStaffIndexCode();
if (metricCode == null) {
log.warn("指标字典中无指标编码,跳过系数转换,metricCode={}", dict.getIndexName());
return null;
}
BigDecimal coefficient = ecoCoefficientQueryService.getCoefficient(metricCode, originalValue);
if (coefficient == null) {
return scaledValue;
log.warn("生态系数查询结果为空,跳过系数转换,metricCode={}", metricCode);
return null;
}
log.info("生态系数转换,metricCode={}, 原值={}, 系数={}",
dict.getIndexName(), originalValue, coefficient);
return coefficient;
} catch (Exception e) {
log.error("生态系数转换失败,使用原值", e);
return scaledValue;
return null;
}
}
}
......@@ -22,4 +22,7 @@ public class TransformContext {
/** 指标字典 */
private PgWarningMetricDict metricDict;
/** 门店维度(true=门店 false=员工) */
private boolean storeTarget;
}
  • @wangxw1
    commented
    Master

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

    1. 潜在的空指针异常(NPE)与业务逻辑中断风险(重要性:高)

    • 问题描述:在 WarningEngineRemoteImpl、EcoCoefficientQueryService 和 EcoMetricValueTransformStrategy 中,当生态系数未命中或发生异常时,返回值由原来的“返回原值(value / scaledValue)”或“返回 BigDecimal.ZERO”统一改为了返回 null。
    • 影响分析:BigDecimal 类型的 null 值极易在下游引发 NullPointerException。例如在 WarningEngineService 中,transformMetricValue 返回 null 后,直接传入了 operator.evaluate(value, threshold.getValue())。如果 evaluate 方法内部或 RPC 消费者没有对 value 进行严格的非空校验,直接进行 compareTo 或算术运算,将导致整个预警评估链路崩溃。此外,发生异常时返回 null 改变了原有的“降级使用原值”的容错逻辑。
    • 优化建议:
      1. 确认业务上是否允许在没有生态系数时直接跳过预警计算。如果是,必须确保下游(如 WarningOperatorEnum.evaluate 及所有 RPC 调用方)具备完善的 null 值处理逻辑。
      2. 如果属于代码遗漏,建议在异常 catch 块中恢复原有的降级逻辑(返回原值 value 或 scaledValue),保障系统的健壮性。

    2. 异常吞没掩盖真实错误(重要性:中)

    • 问题描述:在 WarningEngineRemoteImpl 和 EcoMetricValueTransformStrategy 的 catch (Exception e) 块中,捕获异常后仅打印日志并返回 null。
    • 影响分析:调用方无法区分返回 null 是因为“规则未命中(正常业务分支)”还是“数据库/网络异常(系统故障)”。这种异常吞没会导致系统发生故障时,上层业务无感知,且预警数据会静默丢失或计算错误,极大增加排查难度。
    • 优化建议:对于系统级异常,建议抛出自定义的业务异常(如 WarningEngineException),让上层感知并决定是重试还是告警;或者至少在异常时采用安全的降级策略(如返回计算前的原值),而不是简单地返回 null。

    3. “魔法布尔值”降低代码可读性(重要性:低)

    • 问题描述:WarningEngineService.java 中的 transformMetricValue 方法新增了 boolean storeTarget 参数,导致调用时出现 transformMetricValue(..., true) 和 transformMetricValue(..., false) 的硬编码。
    • 影响分析:布尔值参数在调用处缺乏语义,阅读代码的人如果不点进方法签名,无法知道 true 和 false 分别代表什么含义,不符合 Clean Code 最佳实践。
    • 优化建议:建议引入枚举类来代替布尔值,提升代码的自解释能力。 ```java // 定义枚举 public enum TargetDimensionEnum { STORE, STAFF }

    // 方法签名修改 private BigDecimal transformMetricValue(..., TargetDimensionEnum dimension)

    // 调用处修改 transformMetricValue(..., TargetDimensionEnum.STORE);

    
    ---
    
    #### 二、评分明细
    
    - **功能实现的正确性与健壮性(32/40分)**:
      扣分原因在于多处去除了原有的默认值/原值降级逻辑,改为返回 `null`,且未能看到下游对 `BigDecimal` 为 `null` 的配套防御性编程,存在较高的 NPE 风险和逻辑中断隐患。未触犯 MyBatis 子查询及 Dubbo 接口参数变更的红线。
    
    - **安全性与潜在风险(30/30分)**:
      代码变更主要集中在业务逻辑转换上,未发现 SQL 注入、越权、XSS 或敏感信息泄露等安全漏洞。`WarningRecordService` 中增加了针对空时间维度的前置拦截,增强了安全性。
    
    - **最佳实践与可维护性(16/20分)**:
      扣分原因:1. 引入了布尔值作为方法参数(魔法布尔值),降低了可读性;2. 异常处理过于简单,将异常状态与业务上的“无数据”状态混淆(均返回 null),不利于后续维护和排障。
    
    - **性能与资源利用(5/5分)**:
      在 `WarningRecordService` 中新增了 `StrUtil.isBlank(request.getTimeDimension())` 的快速失败校验,避免了无效的数据库查询,对资源利用有正面作用。
    
    - **提交信息质量(4/5分)**:
      Commit 信息(`feat:预警引擎生态系数策略按照指标代码查询`)说明了主要的功能变更点,但未能涵盖将默认返回值改为 `null` 这一重大逻辑变更的背景原因,略微欠缺追溯价值。
    
    总分:87分
    
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!