Commit b6252b6f by 杨卫

fix:选择不下发策略,需要排除掉已有的策略的门店

1 parent 1937911d
......@@ -22,6 +22,9 @@ public class ProjectGroupWarningRecordQueryDTO implements Serializable {
@ApiModelProperty(notes = "门店id列表")
private List<Integer> merchantIds;
@ApiModelProperty(notes = "需排除的门店ID列表")
private List<Integer> excludeMerchantIds;
@ApiModelProperty(notes = "预警类型")
private Integer warningType;
......
......@@ -35,11 +35,13 @@ public class WarningRecordService {
/**
* 按条件动态查询预警记录。
*
* @param request 查询条件
* @param merchantIds 门店交集筛选(可为空)
* @param request 查询条件
* @param merchantIds 门店交集筛选(可为空)
* @param excludeMerchantIds 需排除的门店ID(可为空)
* @return 预警记录列表
*/
public List<WarningRecordDTO> queryRecords(WarningRecordQueryRequest request, List<Integer> merchantIds) {
public List<WarningRecordDTO> queryRecords(WarningRecordQueryRequest request, List<Integer> merchantIds,
List<Integer> excludeMerchantIds) {
if (StrUtil.isBlank(request.getTimeDimension())) {
return new ArrayList<>();
}
......@@ -48,6 +50,9 @@ public class WarningRecordService {
if (CollectionUtils.isNotEmpty(merchantIds)) {
queryDTO.setMerchantIds(merchantIds);
}
if (CollectionUtils.isNotEmpty(excludeMerchantIds)) {
queryDTO.setExcludeMerchantIds(excludeMerchantIds);
}
List<ProjectGroupWarningRecordDTO> list = recordMapper.listWarningRecordsByCondition(queryDTO);
if (list == null) {
return new ArrayList<>();
......@@ -58,16 +63,21 @@ public class WarningRecordService {
/**
* 按门店汇总预警——返回门店预警卡片列表。
*
* @param request 查询条件
* @param merchantIds 限定门店列表
* @param request 查询条件
* @param merchantIds 限定门店列表(可为空)
* @param excludeMerchantIds 需排除的门店ID(可为空)
* @return 门店预警汇总列表
*/
public List<StoreWarningSummaryDTO> queryStoreSummary(WarningRecordQueryRequest request,
List<Integer> merchantIds) {
List<Integer> merchantIds,
List<Integer> excludeMerchantIds) {
ProjectGroupWarningRecordQueryDTO queryDTO = buildQueryDTO(request);
if (CollectionUtils.isNotEmpty(merchantIds)) {
queryDTO.setMerchantIds(merchantIds);
}
if (CollectionUtils.isNotEmpty(excludeMerchantIds)) {
queryDTO.setExcludeMerchantIds(excludeMerchantIds);
}
queryDTO.setMonitorTarget(MonitorTargetEnum.SHOP.getCode());
List<ProjectGroupWarningRecordDTO> records = recordMapper.listWarningRecordsByCondition(queryDTO);
......
......@@ -74,6 +74,12 @@
#{merchantId}
</foreach>
</if>
<if test="excludeMerchantIds != null and excludeMerchantIds.size() > 0">
AND r.store_id NOT IN
<foreach collection="excludeMerchantIds" item="excludeId" open="(" separator="," close=")">
#{excludeId}
</foreach>
</if>
<if test="storeId != null">
AND r.store_id = #{storeId}
</if>
......
......@@ -17,7 +17,7 @@ public class WarningEngineServiceTest extends BaseServiceTest {
@Test
public void testWarningComputeExecutor() {
executor.execute("2026-05-31");
executor.execute("2026-06-09");
}
@Test
......
  • @wangxw1
    commented
    Master

    这是一份针对您提交代码的专业审查报告。代码整体逻辑清晰,较好地完成了“不下发策略时排除已有策略门店”的业务需求,且严格遵守了“不使用 MyBatis 子查询”和“不变更 Dubbo Remote 现有参数”的红线规则。但在性能和最佳实践上仍有优化空间。

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

    1. 潜在的 SQL 性能与稳定性风险(NOT IN 集合过大)

    • 问题描述:在 PgWarningRecordMapper.xml 中使用了 <foreach> 拼接 NOT IN 语句。当选择“不下发策略”时,excludeMerchantIds 可能会包含大量已有策略的门店 ID。如果该列表过大(例如超过 1000 个),会导致 SQL 语句极其臃肿。
    • 影响分析:大范围的 NOT IN 会导致数据库无法有效使用索引,引发严重的性能瓶颈(慢查询);极端情况下可能超出数据库的 max_allowed_packet 或 IN 子句的数量限制,直接导致 SQL 执行异常。
    • 优化建议:在 Java 业务层对 excludeMerchantIds 的大小进行校验。如果排除数量过大,建议采用分批查询合并结果,或者在内存中进行结果过滤(如果数据量允许)。

    2. Stream 处理中的低效类型转换与异常控制流(性能与最佳实践)

    • 问题描述:在 WarningEngineRemoteImpl.java 的 buildSummaryFromBi 方法中,针对 BI 结果的黑名单过滤,在 stream().filter() 循环内部使用了 Integer.parseInt(item.getMerchantId()),并且通过捕获 NumberFormatException 来处理不可转换的情况。
    • 影响分析:在循环中进行字符串到数字的解析不仅消耗 CPU,而且利用异常捕获来做业务控制流是 Java 编程中的反模式,会导致不必要的性能开销。
    • 优化建议:在进入 Stream 处理前,将 excludeIds 转换为 String 类型的 Set,在循环中直接进行字符串比对。 代码示例:

      if (filterResult.hasExclude()) {
        // 提前转换为 String 类型的 Set,避免循环内频繁转换和异常控制流
        Set<String> excludeStringSet = filterResult.getExcludeIds().stream()
                .map(String::valueOf)
                .collect(Collectors.toSet());
      
        items = items.stream()
                .filter(item -> item.getMerchantId() == null 
                        || !excludeStringSet.contains(item.getMerchantId()))
                .collect(Collectors.toList());
      }
      

    3. 单元测试中存在硬编码的“魔法日期”(可维护性)

    • 问题描述:在 WarningEngineServiceTest.java 中,将测试执行日期从 "2026-05-31" 修改为了 "2026-06-09"。
    • 影响分析:单元测试依赖硬编码的未来日期会导致测试用例变得脆弱(Brittle Tests)。随着时间推移,这些硬编码日期一旦成为“过去式”,可能会导致测试莫名失败,增加后续维护成本。
    • 优化建议:建议使用动态时间计算(如 LocalDate.now().plusDays(X).toString())或通过 Mock 框架固定测试上下文的时间,保证测试用例的长期稳定性。

    二、评分明细

    • 功能实现的正确性与健壮性(40/40分): 业务逻辑正确。合理处理了交集与差集的逻辑;通过 new ArrayList<>() 包装不可变集合避免了 UnsupportedOperationException;严格遵守了“不使用 MyBatis 子查询”的规则;新增 DTO 字段扩展查询条件,未修改 Dubbo 现有参数,符合向后兼容要求。

    • 安全性与潜在风险(30/30分): 未发现越权、SQL注入(MyBatis 中使用了安全的 #{} 遍历)等明显的安全漏洞,代码安全性良好。

    • 最佳实践与可维护性(15/20分): 代码结构清晰,注释完备,较好地解释了复杂的集合运算逻辑。扣分项在于 Stream 循环内使用异常捕获作为控制流,以及单元测试中使用了硬编码的日期。

    • 性能与资源利用(2/5分): 扣分项主要集中在 NOT IN 可能引发的全表扫描及大 SQL 性能隐患,以及在循环内部频繁进行 Integer.parseInt 带来的额外 CPU 资源消耗。

    • 提交信息质量(4/5分): Commit 信息(fix:选择不下发策略,需要排除掉已有的策略的门店)清晰说明了修改动机和业务背景,如果能简要提及涉及的模块(如 BI查询、持久层)会更加完美。

    总分:91分

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