Commit e0c16bcd by 林永健

fix(business-scene): 解决商户流失统计查询为空时的异常

- 添加空集合检查避免查询条件为空导致的错误
- 当查询商户ID列表为空时设置默认值为[-1]
- 确保商户流失统计功能正常执行
1 parent b042f70f
......@@ -849,6 +849,9 @@ public class BusinessSceneAnalysisService {
List<Integer> byGroupMerchantId = getMerchantByProjectGroup(req.getProjectGroupId());
queryLostStoreIds = CollectionUtils.isEmpty(queryLostStoreIds)
? byGroupMerchantId : new ArrayList<>(CollectionUtils.intersection(queryLostStoreIds, byGroupMerchantId));
if (CollectionUtils.isEmpty(queryLostStoreIds)) {
queryLostStoreIds = Collections.singletonList(-1);
}
}
int lostStoreCount = businessIntelligenceMapper.countLostMerchantByBizType(queryLostStoreIds, startDate, biBusinessType, req.getExcludeIds());
res.setChurnedStores(lostStoreCount);
......
  • @wangxw1
    commented
    Master

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

    1. 性能与逻辑优化:避免无效的数据库查询(短路处理)

    • 问题描述:当前代码在发现 queryLostStoreIds 为空时,通过赋值 Collections.singletonList(-1) 来规避 MyBatis 在处理 IN 语句时遇到空集合抛出的 SQL 语法异常。这虽然解决了报错,但属于一种“补丁式”的妥协做法。
    • 影响分析:当商户 ID 列表为空时,业务逻辑上已经明确知道流失商户的数量必然为 0。使用 [-1] 依然会发起一次对数据库的真实访问,这会产生不必要的网络 I/O、数据库连接占用以及数据库执行开销,浪费系统资源。同时,代码中引入了 -1 这个魔法值(Magic Number),降低了代码的严谨性。
    • 优化建议:应当在应用层直接进行短路(Short-circuit)拦截。如果集合为空,直接跳过 mapper 查询,将结果置为 0 即可。

    代码示例:

    // 优化前的逻辑
    if (CollectionUtils.isEmpty(queryLostStoreIds)) {
        queryLostStoreIds = Collections.singletonList(-1);
    }
    int lostStoreCount = businessIntelligenceMapper.countLostMerchantByBizType(queryLostStoreIds, startDate, biBusinessType, req.getExcludeIds());
    
    // 建议优化为:
    int lostStoreCount = 0;
    if (CollectionUtils.isNotEmpty(queryLostStoreIds)) {
        lostStoreCount = businessIntelligenceMapper.countLostMerchantByBizType(queryLostStoreIds, startDate, biBusinessType, req.getExcludeIds());
    }
    

    二、评分明细

    • 功能实现的正确性与健壮性(38/40分): 理由:成功修复了空集合导致的 SQL 异常,边界情况得到了处理,功能逻辑上能够返回正确的结果。扣除 2 分是因为处理方式在逻辑严密性上并非最优解。未发现违反 MyBatis 子查询或 Dubbo 接口变更的红线规则。

    • 安全性与潜在风险(30/30分): 理由:本次修改仅涉及集合判空与默认值赋值,未引入 SQL 注入、越权等安全隐患。

    • 最佳实践与可维护性(16/20分): 理由:使用了 Collections.singletonList() 创建不可变单元素集合,符合 Java 规范。但使用 -1 作为“Dummy Data”来欺骗数据库查询属于反模式(Anti-pattern),不如直接通过 if/else 控制流进行拦截更符合工程最佳实践。

    • 性能与资源利用(2/5分): 理由:未能及时在应用层阻断无效查询,导致了本可以避免的数据库资源消耗。虽然单次查询开销可能不大,但在高并发场景下会积少成多。

    • 提交信息质量(5/5分): 理由:Commit 信息非常优秀。符合 Conventional Commits 规范(使用了 fix: 前缀),清晰地描述了问题背景、修改方式以及最终达成的目的,可追溯性强。

    总分:91分

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