Commit 40af7955 by 林永健

refactor(BusinessSceneAnalysisService): 重构门店过滤逻辑并修复流失门店统计

- 修改filterProjectGroupAndWarningMerchants方法中的变量命名和逻辑流程
- 将filteredMerchantIds改为直接操作merchantIds减少内存占用
- 调整预警类型过滤条件,只有在警告周期时才进行预警类型过滤
- 优化filterByProjectGroup方法,先获取项目组门店再进行交集运算
- 移除空的getMerchantByProjectGroup方法实现
- 修复流失门店统计逻辑,添加项目组过滤后再计算流失门店数据
- 调整流失门店和历史总门店的计算位置到方法末尾
1 parent 4534465e
...@@ -278,32 +278,29 @@ public class BusinessSceneAnalysisService { ...@@ -278,32 +278,29 @@ public class BusinessSceneAnalysisService {
* @return 过滤后的门店列表 * @return 过滤后的门店列表
*/ */
private List<Integer> filterProjectGroupAndWarningMerchants(List<Integer> merchantIds, SceneBaseReq req) { private List<Integer> filterProjectGroupAndWarningMerchants(List<Integer> merchantIds, SceneBaseReq req) {
if (CollectionUtils.isEmpty(merchantIds) || !isWarningCycle(req)) { if (CollectionUtils.isEmpty(merchantIds)) {
return merchantIds; return merchantIds;
} }
List<Integer> filteredMerchantIds = merchantIds.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList()); merchantIds = merchantIds.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList());
// 项目组过滤,是直接通过过滤项目组所管辖的门店 // 项目组过滤,是直接通过过滤项目组所管辖的门店
if (req.getProjectGroupId() != null) { if (req.getProjectGroupId() != null) {
filteredMerchantIds = filterByProjectGroup(filteredMerchantIds, req.getProjectGroupId()); merchantIds = filterByProjectGroup(merchantIds, req.getProjectGroupId());
log.info("项目组过滤后门店列表,projectGroupId: {}, merchantIds: {}", req.getProjectGroupId(), filteredMerchantIds); log.info("项目组过滤后门店列表,projectGroupId: {}, merchantIds: {}", req.getProjectGroupId(), merchantIds);
if (CollectionUtils.isEmpty(filteredMerchantIds)) { if (CollectionUtils.isEmpty(merchantIds)) {
return Collections.emptyList(); return Collections.emptyList();
} }
} }
// 预警类型过滤,是通过过滤预警结果落地的数据 // 预警类型过滤,是通过过滤预警结果落地的数据
if (req.getWarningType() != null) { if (isWarningCycle(req) && req.getWarningType() != null) {
// 过滤项目组+预警类型 // 过滤项目组+预警类型
List<ProjectGroupWarningRecordDTO> warningRecords = listWarningRecords(buildWarningQueryDTO(req, filteredMerchantIds)); List<ProjectGroupWarningRecordDTO> warningRecords = listWarningRecords(buildWarningQueryDTO(req, merchantIds));
filteredMerchantIds = warningRecords.stream() merchantIds = warningRecords.stream()
.map(ProjectGroupWarningRecordDTO::getMerchantId) .map(ProjectGroupWarningRecordDTO::getMerchantId)
.collect(Collectors.toList()); .collect(Collectors.toList());
log.info("预警类型过滤后门店列表,projectGroupId:{},warningType: {}, merchantIds: {}", log.info("预警类型过滤后门店列表,projectGroupId:{},warningType: {}, merchantIds: {}",
req.getProjectGroupId(), req.getWarningType(), filteredMerchantIds); req.getProjectGroupId(), req.getWarningType(), merchantIds);
if (CollectionUtils.isEmpty(filteredMerchantIds)) {
return Collections.emptyList();
}
} }
return filteredMerchantIds; return merchantIds;
} }
/** /**
...@@ -314,6 +311,14 @@ public class BusinessSceneAnalysisService { ...@@ -314,6 +311,14 @@ public class BusinessSceneAnalysisService {
* @return 过滤后的门店列表 * @return 过滤后的门店列表
*/ */
private List<Integer> filterByProjectGroup(List<Integer> merchantIds, Integer projectGroupId) { private List<Integer> filterByProjectGroup(List<Integer> merchantIds, Integer projectGroupId) {
List<Integer> groupMerchantIds = getMerchantByProjectGroup(projectGroupId);
if (CollectionUtils.isEmpty(groupMerchantIds) || CollectionUtils.isEmpty(merchantIds)) {
return Collections.emptyList();
}
return new ArrayList<>(CollectionUtils.intersection(merchantIds, groupMerchantIds));
}
private List<Integer> getMerchantByProjectGroup(Integer projectGroupId) {
ProjectGroupIdDTO queryDTO = new ProjectGroupIdDTO(); ProjectGroupIdDTO queryDTO = new ProjectGroupIdDTO();
queryDTO.setGroupId(projectGroupId); queryDTO.setGroupId(projectGroupId);
RpcResponse<List<Integer>> response = projectGroupRemote.listMerchantIdsByProjectGroup(queryDTO); RpcResponse<List<Integer>> response = projectGroupRemote.listMerchantIdsByProjectGroup(queryDTO);
...@@ -325,8 +330,7 @@ public class BusinessSceneAnalysisService { ...@@ -325,8 +330,7 @@ public class BusinessSceneAnalysisService {
if (CollectionUtils.isEmpty(groupMerchantIds)) { if (CollectionUtils.isEmpty(groupMerchantIds)) {
return Collections.emptyList(); return Collections.emptyList();
} }
return new ArrayList<>(CollectionUtils.intersection( return groupMerchantIds;
Optional.ofNullable(merchantIds).orElse(Collections.emptyList()), groupMerchantIds));
} }
/** /**
...@@ -824,17 +828,6 @@ public class BusinessSceneAnalysisService { ...@@ -824,17 +828,6 @@ public class BusinessSceneAnalysisService {
BigDecimal multiCategoryStoresRatio = usedCarDataStatsService.calculateDiv(multiCategoryStore.size(), res.getTotalStores()); BigDecimal multiCategoryStoresRatio = usedCarDataStatsService.calculateDiv(multiCategoryStore.size(), res.getTotalStores());
res.setMultiCategoryStoresRatio(multiCategoryStoresRatio); res.setMultiCategoryStoresRatio(multiCategoryStoresRatio);
} }
// 流失门店:以统计周期的开始日期为基准,近30天无产值且近60天存在产值的门店数
int lostStoreCount = businessIntelligenceMapper.countLostMerchantByBizType(curReq.getMerchantIds(), startDate, biBusinessType, curReq.getExcludeIds());
res.setChurnedStores(lostStoreCount);
// 历史总门店数:以统计周期的开始日期为基准,近30-60天间存在产值的门店数;
int totalStoreCount = businessIntelligenceMapper.countHistoryMerchantByBizType(curReq.getMerchantIds(), startDate, biBusinessType, curReq.getExcludeIds());
// 流失率:(流失门店/历史总门店数)*100%,保留百分比两位小数,其中历史总门店数:以统计周期的开始日期为基准,近30-60天间存在产值的门店数;
if (totalStoreCount != 0) {
BigDecimal churnRate = usedCarDataStatsService.calculateDiv(lostStoreCount, totalStoreCount);
res.setChurnRate(churnRate);
}
} }
res.setNewStores(newStoreCount); res.setNewStores(newStoreCount);
//计算新店占比:(新店数量/总开单门店数)*100% //计算新店占比:(新店数量/总开单门店数)*100%
...@@ -845,6 +838,21 @@ public class BusinessSceneAnalysisService { ...@@ -845,6 +838,21 @@ public class BusinessSceneAnalysisService {
BigDecimal oldStoreRatio = usedCarDataStatsService.calculateDiv(oldStoreCount, res.getTotalStores()); BigDecimal oldStoreRatio = usedCarDataStatsService.calculateDiv(oldStoreCount, res.getTotalStores());
res.setOldStoresRatio(oldStoreRatio); res.setOldStoresRatio(oldStoreRatio);
// 流失门店:以统计周期的开始日期为基准,近30天无产值且近60天存在产值的门店数
// 把权限门店过滤一下项目组的
List<Integer> byGroupMerchantId = getMerchantByProjectGroup(curReq.getProjectGroupId());
List<Integer> queryLostStoreIds = CollectionUtils.isEmpty(curReq.getMerchantIds())
? byGroupMerchantId : new ArrayList<>(CollectionUtils.intersection(curReq.getMerchantIds(), byGroupMerchantId));
int lostStoreCount = businessIntelligenceMapper.countLostMerchantByBizType(queryLostStoreIds, startDate, biBusinessType, curReq.getExcludeIds());
res.setChurnedStores(lostStoreCount);
// 历史总门店数:以统计周期的开始日期为基准,近30-60天间存在产值的门店数;
int totalStoreCount = businessIntelligenceMapper.countHistoryMerchantByBizType(queryLostStoreIds, startDate, biBusinessType, curReq.getExcludeIds());
// 流失率:(流失门店/历史总门店数)*100%,保留百分比两位小数,其中历史总门店数:以统计周期的开始日期为基准,近30-60天间存在产值的门店数;
if (totalStoreCount != 0) {
BigDecimal churnRate = usedCarDataStatsService.calculateDiv(lostStoreCount, totalStoreCount);
res.setChurnRate(churnRate);
}
//计算环比 //计算环比
if (!req.getCycleType().equals(4)) {//非自定义类型,需要环比 if (!req.getCycleType().equals(4)) {//非自定义类型,需要环比
......
  • @wangxw1
    commented
    Master

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

    1. 无条件的 RPC 调用与潜在的空指针/逻辑异常(重要)

    • 问题描述:在重构后的流失门店统计逻辑中(第843行),getMerchantByProjectGroup(curReq.getProjectGroupId()) 被无条件调用。如果当前请求中 ProjectGroupId 为 null,系统将向 Dubbo 服务提供者发起带有 null 参数的 RPC 请求。
    • 影响分析:
      1. 功能与稳定性:如果下游 projectGroupRemote 未妥善处理 groupId 为 null 的情况,可能导致 RPC 异常或返回非预期的全量数据。
      2. 性能损耗:当不需要按项目组过滤时,发起了一次毫无意义的跨进程网络调用。
    • 优化建议:在调用前增加对 ProjectGroupId 的判空逻辑。

    2. 存在将空集合传入 MyBatis Mapper 的高风险(重要)

    • 问题描述:第846行及849行,直接将 queryLostStoreIds 传入 businessIntelligenceMapper 进行数据库查询。如果经过过滤后 queryLostStoreIds 为空集合,代码并未做拦截。
    • 影响分析:在 MyBatis 中,如果使用 <foreach> 标签拼接 IN 条件且传入空集合,极易生成类似 merchant_id IN () 的非法 SQL,直接导致数据库抛出 SQL 语法异常(除非在 XML 中做了严格的特判,但这不符合防御性编程原则);同时也会造成不必要的数据库交互。
    • 优化建议:在执行数据库查询前,判断 queryLostStoreIds 是否为空,若为空则直接返回 0。 (针对问题 1 和问题 2 的综合代码示例): ```java // 1. 优化项目组门店获取逻辑,避免空参数 RPC 调用 List queryLostStoreIds = curReq.getMerchantIds(); if (curReq.getProjectGroupId() != null) { List byGroupMerchantId = getMerchantByProjectGroup(curReq.getProjectGroupId()); queryLostStoreIds = CollectionUtils.isEmpty(curReq.getMerchantIds()) ? byGroupMerchantId : new ArrayList<>(CollectionUtils.intersection(curReq.getMerchantIds(), byGroupMerchantId)); }

    // 2. 拦截空集合,避免 MyBatis 报错及无效 DB 查询 if (CollectionUtils.isEmpty(queryLostStoreIds)) { res.setChurnedStores(0); // 视业务逻辑,totalStoreCount 等也可直接赋 0 或跳过查询 } else { int lostStoreCount = businessIntelligenceMapper.countLostMerchantByBizType(queryLostStoreIds, startDate, biBusinessType, curReq.getExcludeIds()); res.setChurnedStores(lostStoreCount); int totalStoreCount = businessIntelligenceMapper.countHistoryMerchantByBizType(queryLostStoreIds, startDate, biBusinessType, curReq.getExcludeIds()); if (totalStoreCount != 0) { BigDecimal churnRate = usedCarDataStatsService.calculateDiv(lostStoreCount, totalStoreCount); res.setChurnRate(churnRate); } }

    
    **3. 方法参数被重新赋值,降低代码可维护性**
    - **问题描述**:在 `filterProjectGroupAndWarningMerchants` 方法中,原代码使用了局部变量 `filteredMerchantIds`,而修改后的代码直接对方法入参 `merchantIds` 进行了重新赋值(`merchantIds = merchantIds.stream()...`)。
    - **影响分析**:虽然在 Java 中对引用本身重新赋值不会影响调用方,但这违反了“方法参数应尽量保持只读(类似 final)”的 Clean Code 最佳实践。在长方法中,参数被修改会导致代码阅读者难以追踪数据的原始状态,增加后续维护和 Debug 的心智负担。
    - **优化建议**:撤销对入参的直接修改,恢复使用独立的局部变量(如 `List<Integer> filteredMerchantIds`)来承接过滤后的结果。
    
    ---
    
    #### 二、评分明细
    
    - **功能实现的正确性与健壮性(30/40分)**:
      扣分原因:新增的流失门店统计逻辑未处理 `ProjectGroupId` 为空的情况;未对传入 Mapper 的集合判空,存在引发 SQL 语法错误的隐患。(注:代码中未见直接修改 Dubbo 已有请求参数签名的行为,符合规范;MyBatis XML 虽不可见,但 Java 侧的空集合防御缺失是明确的健壮性问题)。
    - **安全性与潜在风险(30/30分)**:
      得分原因:代码主要为业务逻辑过滤与统计,未引入新的越权、SQL注入或敏感信息泄露等安全漏洞。
    - **最佳实践与可维护性(15/20分)**:
      扣分原因:重新赋值方法入参(`merchantIds`)降低了代码的严谨性和可读性;抽取了 `getMerchantByProjectGroup` 提升了复用性,值得肯定。
    - **性能与资源利用(2/5分)**:
      扣分原因:存在不必要的 RPC 调用(参数为 null 时)以及潜在的无效数据库查询(参数为空集合时),造成了计算和网络资源的浪费。
    - **提交信息质量(5/5分)**:
      得分原因:Commit message 格式标准,清晰地列出了重构的具体点和修复的逻辑,具有很好的可追溯性。
    
    总分:82分
    
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!