Commit 4852511c by 范富荣

修改门店/员工查询预警信息逻辑

1 parent d4eb35dd
......@@ -23,5 +23,4 @@ public class StaffAnalysisDTO implements Serializable {
@ApiModelProperty(notes = "警告信息列表")
private List<WarningInfo> warningInfoList;
}
......@@ -4,15 +4,110 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
@Data
public class WarningInfo implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(notes = "警告类型")
@ApiModelProperty(notes = "记录ID")
private Integer id;
@ApiModelProperty(notes = "项目组ID")
private Integer groupId;
@ApiModelProperty(notes = "配置ID")
private Integer configId;
@ApiModelProperty(notes = "预警类型")
private Integer warningType;
@ApiModelProperty(notes = "预警内容")
@ApiModelProperty(notes = "预警类型名称")
private String warningTypeName;
@ApiModelProperty(notes = "业务类型")
private Integer businessType;
@ApiModelProperty(notes = "监控对象: 1=员工 2=门店")
private Integer monitorTarget;
@ApiModelProperty(notes = "时间维度")
private String timeDimension;
@ApiModelProperty(notes = "周期开始")
private Date periodStart;
@ApiModelProperty(notes = "周期结束")
private Date periodEnd;
@ApiModelProperty(notes = "门店ID")
private Integer storeId;
@ApiModelProperty(notes = "门店名称")
private String storeName;
@ApiModelProperty(notes = "员工ID")
private String staffId;
@ApiModelProperty(notes = "员工姓名")
private String staffName;
@ApiModelProperty(notes = "员工手机号")
private String staffPhone;
@ApiModelProperty(notes = "指标字典ID")
private Integer metricId;
@ApiModelProperty(notes = "指标编码")
private String metricCode;
@ApiModelProperty(notes = "指标名称")
private String metricName;
@ApiModelProperty(notes = "指标值")
private BigDecimal metricValue;
@ApiModelProperty(notes = "指标展示值")
private BigDecimal metricDisplay;
@ApiModelProperty(notes = "阈值类型")
private Integer thresholdType;
@ApiModelProperty(notes = "阈值展示值")
private BigDecimal thresholdDisplay;
@ApiModelProperty(notes = "区间下界展示值")
private BigDecimal thresholdMinDisplay;
@ApiModelProperty(notes = "区间上界展示值")
private BigDecimal thresholdMaxDisplay;
@ApiModelProperty(notes = "生态系数")
private BigDecimal ecoCoefficient;
@ApiModelProperty(notes = "运算符")
private String operator;
@ApiModelProperty(notes = "预警描述文本")
private String warningText;
@ApiModelProperty(notes = "是否已下发策略")
private Integer isStrategySent;
@ApiModelProperty(notes = "状态")
private Integer status;
@ApiModelProperty(notes = "创建时间")
private Date createdTime;
@ApiModelProperty(notes = "指标数据类型: 1=比率 2=数值")
private Integer dataType;
@ApiModelProperty(notes = "指标单位")
private String unit;
@ApiModelProperty(notes = "指标扩展品类")
private String categoryExtension;
}
......@@ -4,9 +4,7 @@ package cn.com.glsx.dj.peanalysis.remote;
import cn.com.glsx.dj.peanalysis.dto.job.BiRecord;
import cn.com.glsx.dj.peanalysis.dto.req.output.SceneBaseReq;
import cn.com.glsx.dj.peanalysis.dto.res.output.*;
import cn.com.glsx.dj.peanalysis.dto.strategy.GoodsCategorySummaryWithWarn;
import cn.com.glsx.dj.peanalysis.dto.strategy.StaffAnalysisDTO;
import cn.com.glsx.dj.peanalysis.dto.strategy.StaffSummaryDTO;
import cn.com.glsx.framework.core.annotation.ApiMethod;
import cn.com.glsx.framework.core.annotation.ApiService;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
......
......@@ -3,9 +3,7 @@ package cn.com.glsx.dj.peanalysis.remote;
import cn.com.glsx.dj.peanalysis.dto.job.BiRecord;
import cn.com.glsx.dj.peanalysis.dto.req.output.SceneBaseReq;
import cn.com.glsx.dj.peanalysis.dto.res.output.*;
import cn.com.glsx.dj.peanalysis.dto.strategy.GoodsCategorySummaryWithWarn;
import cn.com.glsx.dj.peanalysis.dto.strategy.StaffAnalysisDTO;
import cn.com.glsx.dj.peanalysis.dto.strategy.StaffSummaryDTO;
import cn.com.glsx.dj.peanalysis.service.BusinessSceneAnalysisService;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.exception.RpcServiceException;
......
......@@ -20,7 +20,10 @@ import cn.com.glsx.dj.peanalysis.dto.strategy.WarningInfo;
import cn.com.glsx.dj.strategy.dto.projectgroup.ProjectGroupIdDTO;
import cn.com.glsx.dj.strategy.dto.projectgroup.ProjectGroupWarningMerchantQueryDTO;
import cn.com.glsx.dj.strategy.dto.projectgroup.ProjectGroupWarningRecordDTO;
import cn.com.glsx.dj.strategy.dto.warning.WarningRecordDTO;
import cn.com.glsx.dj.strategy.dto.warning.WarningRecordQueryRequest;
import cn.com.glsx.dj.strategy.remote.ProjectGroupRemote;
import cn.com.glsx.dj.strategy.remote.WarningEngineRemote;
import cn.com.glsx.dj.peanalysis.mapper.*;
import cn.com.glsx.dj.peanalysis.util.SortUtils;
import cn.com.glsx.dj.peanalysis.utils.DateUtils;
......@@ -150,6 +153,9 @@ public class BusinessSceneAnalysisService {
@Resource
private StrategyInfoRemote strategyInfoRemote;
@Resource
private WarningEngineRemote warningEngineRemote;
/**
* 总产值、目标产值、达成率
......@@ -2284,18 +2290,46 @@ public class BusinessSceneAnalysisService {
* @param staffIds 员工ID列表
*/
public void handlerStaffWarning(List<StaffAnalysisDTO> staffList, SceneBaseReq req, List<String> staffIds) {
//查询预警信息
List<MonitoringMsgDTO> warningMsgList = listWarningInfo(req, staffIds);
if (CollectionUtils.isEmpty(warningMsgList)) return;
//根据员工id,匹配预警信息
Map<String, List<MonitoringMsgDTO>> staffWarningMap = warningMsgList.stream().filter(x -> ObjectUtil.isNotNull(x.getStaffId()))
.collect(Collectors.groupingBy(MonitoringMsgDTO::getStaffId));
// 自定义周期直接跳过
if (req.getCycleType() != null && req.getCycleType() == 4) {
return;
}
WarningRecordQueryRequest queryRequest = new WarningRecordQueryRequest();
queryRequest.setBusinessType(req.getBizs());
// 时间维度映射:cycleType 2=周(W), 3=月(M)
Integer cycleType = req.getCycleType();
if (cycleType != null) {
if (cycleType == 2) {
queryRequest.setTimeDimension("W");
} else if (cycleType == 3) {
queryRequest.setTimeDimension("M");
}
}
queryRequest.setStartDate(req.getStartTime());
queryRequest.setEndDate(req.getEndTime());
// monitorTarget=1(员工)
queryRequest.setMonitorTarget(1);
// TODO 员工ID参数暂未定义,后续支持批量员工查询时补充 staffId 过滤
RpcResponse<List<WarningRecordDTO>> response = warningEngineRemote.findWarningDetailByStoreAndStaff(queryRequest);
if (response.hasError()) {
log.error("查询预警信息异常: {}", response.getMessage());
throw new RpcServiceException(response.getError(), response.getMessage());
}
List<WarningRecordDTO> warningList = response.getResult();
if (CollectionUtils.isEmpty(warningList)) return;
// 根据员工id匹配预警信息
Map<String, List<WarningRecordDTO>> staffWarningMap = warningList.stream()
.filter(x -> x.getStaffId() != null)
.collect(Collectors.groupingBy(WarningRecordDTO::getStaffId));
for (StaffAnalysisDTO staff : staffList) {
List<MonitoringMsgDTO> warningMsg = staffWarningMap.get(staff.getStaffId());
if (CollectionUtils.isEmpty(warningMsg)) continue;
List<WarningInfo> warningInfoList = warningMsg.stream().map(x -> {
List<WarningRecordDTO> staffWarnings = staffWarningMap.get(staff.getStaffId());
if (CollectionUtils.isEmpty(staffWarnings)) continue;
List<WarningInfo> warningInfoList = staffWarnings.stream().map(x -> {
WarningInfo warningInfo = new WarningInfo();
warningInfo.setWarningText(x.getWarningText());
BeanUtils.copyProperties(x, warningInfo);
return warningInfo;
}).collect(Collectors.toList());
staff.setWarningInfoList(warningInfoList);
......
  • @wangxw1
    commented
    Master

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

    1. 存在严重的性能瓶颈与OOM风险(RPC全量查询)

    • 问题描述:在 BusinessSceneAnalysisService 的 handlerStaffWarning 方法中,组装 WarningRecordQueryRequest 时,注释明确提到“TODO 员工ID参数暂未定义,后续支持批量员工查询时补充 staffId 过滤”。这意味着当前发起的 RPC 调用没有传入员工ID集合进行过滤。
    • 影响分析:由于缺少过滤条件,该 RPC 请求会拉取指定周期内全量员工的预警记录,然后再在本地内存中通过 Stream 匹配。如果数据量较大,将导致网络带宽被大量占用、RPC 极易超时(Timeout),且巨量数据加载到内存中极易引发 OOM(内存溢出)和 CPU 飙升,属于高危逻辑。
    • 优化建议:必须在 WarningRecordQueryRequest 中新增 List<String> staffIds 字段,并在被调用的预警引擎服务端实现基于 ID 集合的 SQL 过滤(如 IN 查询)。在完善此逻辑前,不建议将该代码发布到生产环境。

    2. DTO 字段过度膨胀,导致接口响应报文急剧增大

    • 问题描述:WarningInfo 类原本只有 2 个字段,本次修改后暴增至 30 多个字段(包含各种指标、阈值等明细)。在代码中通过 BeanUtils.copyProperties(x, warningInfo) 将 RPC 返回的实体全量拷贝到了 WarningInfo 中。
    • 影响分析:WarningInfo 作为列表被嵌套在 StaffAnalysisDTO 中。在批量查询员工数据的场景下,每个员工又可能包含多条预警信息,这会导致接口返回的 JSON 报文体积呈现指数级膨胀,造成严重的网络带宽浪费和序列化/反序列化性能损耗。
    • 优化建议:重新评估业务场景是否需要将如此详尽的预警明细带在列表接口中。建议精简 WarningInfo 的字段,仅保留前端展示必须的字段(如预警类型、内容、时间等);详情数据可通过单独的详情接口获取。

    3. RPC 异常处理缺乏降级策略,影响系统整体可用性

    • 问题描述:在请求 warningEngineRemote 服务时,如果 response.hasError() 为 true,代码直接抛出了 RpcServiceException,中断了当前的整个业务流程。
    • 影响分析:预警信息通常属于核心业务数据的“附加/旁路信息”。如果预警引擎服务出现短暂的不可用或响应超时,会导致核心的“员工分析”接口整体报错,违反了微服务架构中的“容错与降级”最佳实践,拉低了系统的 SLA。
    • 优化建议:建议对预警信息的获取进行容错降级处理。当发生异常时,记录错误日志并跳过预警数据的装配(返回空列表),保证主业务数据依然能正常响应。 java RpcResponse<List<WarningRecordDTO>> response = warningEngineRemote.findWarningDetailByStoreAndStaff(queryRequest); // 增加防空指针判断,并做降级处理 if (response == null || response.hasError()) { log.error("查询预警信息异常, 降级处理: {}", response != null ? response.getMessage() : "RPC无响应"); return; // 不抛出异常,让主业务流程继续 }

    二、评分明细

    1. 功能实现的正确性与健壮性(28 / 40分)
      • 理由:基本业务逻辑能够走通,且没有触犯“Dubbo服务提供者Remote接口参数变更”的红线;但强依赖 RPC 且缺乏降级机制降低了健壮性,且存在未完成的 TODO 逻辑直接提交的情况,扣除 12 分。
    2. 安全性与潜在风险(22 / 30分)
      • 理由:未发现 SQL 注入、越权等典型的安全漏洞;但由于缺失条件过滤,存在极大的内存溢出(OOM)及服务雪崩的稳定性风险,扣除 8 分。
    3. 最佳实践与可维护性(12 / 20分)
      • 理由:新增实体类使用了包装类型,符合规范。但遗留了明显的 TODO 未处理,且滥用 BeanUtils.copyProperties 导致 DTO 过于臃肿,违背了接口轻量化原则,扣除 8 分。
    4. 性能与资源利用(1 / 5分)
      • 理由:全量拉取数据并在内存中做 Grouping 和匹配,是典型且严重的性能反模式(Performance Anti-pattern),扣除 4 分。
    5. 提交信息质量(4 / 5分)
      • 理由:Commit 信息(修改门店/员工查询预警信息逻辑)描述了主要的变更动作,但若能稍微提及关联的需求或 Bug 编号会更具追溯性,扣除 1 分。

    总分:67分

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