Commit 03aa0d2c by 林永健

refactor(BusinessSceneAnalysisService): 提取流失门店查询逻辑到独立方法

- 将流失门店ID查询逻辑提取为getLostStoreIdsWithProjectGroup私有方法
- 在countLostMerchantByBizType调用中使用新的封装方法
- 在findLostStore方法中也复用相同的查询逻辑
- 消除代码重复,提高可维护性
1 parent e0c16bcd
...@@ -843,16 +843,7 @@ public class BusinessSceneAnalysisService { ...@@ -843,16 +843,7 @@ public class BusinessSceneAnalysisService {
// 流失门店:以统计周期的开始日期为基准,近30天无产值且近60天存在产值的门店数 // 流失门店:以统计周期的开始日期为基准,近30天无产值且近60天存在产值的门店数
// 使用未经产值过滤的权限门店 req.merchantIds // 使用未经产值过滤的权限门店 req.merchantIds
List<Integer> queryLostStoreIds = req.getMerchantIds(); List<Integer> queryLostStoreIds = getLostStoreIdsWithProjectGroup(req);
// 把权限门店过滤一下项目组的
if (req.getProjectGroupId() != null) {
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()); int lostStoreCount = businessIntelligenceMapper.countLostMerchantByBizType(queryLostStoreIds, startDate, biBusinessType, req.getExcludeIds());
res.setChurnedStores(lostStoreCount); res.setChurnedStores(lostStoreCount);
// 历史总门店数:以统计周期的开始日期为基准,近30-60天间存在产值的门店数; // 历史总门店数:以统计周期的开始日期为基准,近30-60天间存在产值的门店数;
...@@ -930,6 +921,21 @@ public class BusinessSceneAnalysisService { ...@@ -930,6 +921,21 @@ public class BusinessSceneAnalysisService {
return res; return res;
} }
private List<Integer> getLostStoreIdsWithProjectGroup(SceneBaseReq req) {
// 使用未经产值过滤的权限门店 req.merchantIds
List<Integer> queryLostStoreIds = req.getMerchantIds();
// 把权限门店过滤一下项目组的
if (req.getProjectGroupId() != null) {
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);
}
}
return queryLostStoreIds;
}
public boolean filterHarmonyStore(SceneBaseReq req) { public boolean filterHarmonyStore(SceneBaseReq req) {
Integer harmonyBrandId = env.getProperty("dj.pe.analysis.business.scene.harmony.brandId", Integer.class, 2365); Integer harmonyBrandId = env.getProperty("dj.pe.analysis.business.scene.harmony.brandId", Integer.class, 2365);
log.info("处理生态系数,过滤鸿蒙执行门店,品牌id[{}]", harmonyBrandId); log.info("处理生态系数,过滤鸿蒙执行门店,品牌id[{}]", harmonyBrandId);
...@@ -2471,9 +2477,10 @@ public class BusinessSceneAnalysisService { ...@@ -2471,9 +2477,10 @@ public class BusinessSceneAnalysisService {
* @return 门店列表 * @return 门店列表
*/ */
public List<LostStoreDetailResponse> findLostStore(SceneBaseReq req) { public List<LostStoreDetailResponse> findLostStore(SceneBaseReq req) {
List<Integer> queryLostStoreIds = getLostStoreIdsWithProjectGroup(req);
Integer biBusinessType = req.convertToBiBusinessType(req.getBizs()); Integer biBusinessType = req.convertToBiBusinessType(req.getBizs());
List<LostStoreDetailResponse> collect = businessIntelligenceMapper.lostMerchantByBizType( List<LostStoreDetailResponse> collect = businessIntelligenceMapper.lostMerchantByBizType(
req.getMerchantIds(), req.getStartTime(), biBusinessType, req.getExcludeIds()); queryLostStoreIds, req.getStartTime(), biBusinessType, req.getExcludeIds());
if (CollectionUtils.isEmpty(collect)){ if (CollectionUtils.isEmpty(collect)){
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
......
  • @wangxw1
    commented
    Master

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

    1. 逻辑变更确认(重构引入了未声明的业务行为改变)

    • 问题描述:在 findLostStore 方法中,传递给 businessIntelligenceMapper.lostMerchantByBizType 的参数由原本未经项目组过滤的 req.getMerchantIds() 变更为经过过滤的 queryLostStoreIds。
    • 影响分析:虽然提交信息声称是“消除代码重复”的重构,但实际上这改变了 findLostStore 方法的业务逻辑(现在该方法也会受 projectGroupId 过滤的影响)。如果这是一个预期的 Bug 修复或逻辑统一,那是合理的;但如果仅仅是为了提取代码而未意识到逻辑发生了变化,则可能导致线上数据查询结果与预期不符。
    • 优化建议:请与产品或业务方确认 findLostStore 是否确实需要应用项目组(projectGroupId)的过滤逻辑。如果确认需要,建议在 Commit Message 中明确标明这是一次“逻辑修复/统一”而非单纯的重构。

    2. 返回集合的可变性不一致(潜在的 UnsupportedOperationException 风险)

    • 问题描述:提取的 getLostStoreIdsWithProjectGroup 方法返回的 List<Integer> 存在可变性不一致的问题。当 req.getProjectGroupId() == null 时,返回的是 req.getMerchantIds()(通常是可变的 ArrayList);当交集不为空时,返回的是 new ArrayList(...)(可变);但当交集为空时,返回的是 Collections.singletonList(-1)(不可变集合)。
    • 影响分析:如果调用方(当前或未来)尝试对该方法返回的 List 进行 add、remove 等修改操作,在特定条件(交集为空)下会触发 UnsupportedOperationException,导致系统运行时异常。此外,直接返回 req.getMerchantIds() 存在引用泄露风险,外部修改会直接影响 req 对象内部的状态。
    • 优化建议:保证方法返回集合的可变性一致。建议统一返回一个新的可变集合。 java // 建议修改: if (CollectionUtils.isEmpty(queryLostStoreIds)) { queryLostStoreIds = new ArrayList<>(Collections.singletonList(-1)); }

    3. 泛型类型安全与规范问题

    • 问题描述:在 findLostStore 方法的末尾,使用了 return Collections.EMPTY_LIST;。
    • 影响分析:Collections.EMPTY_LIST 是一个没有泛型类型的原生类型(Raw Type),会导致编译器产生 unchecked assignment 警告,不符合现代 Java 泛型最佳实践。
    • 优化建议:使用带有泛型推断的 Collections.emptyList() 替代。 java // 建议修改: if (CollectionUtils.isEmpty(collect)){ return Collections.emptyList(); }

    二、评分明细

    • 功能实现的正确性与健壮性(38/40分): 代码成功提取了重复逻辑,未发现违反 MyBatis 子查询或 Dubbo 接口变更的红线规则。扣除2分是因为 findLostStore 中的入参替换带来了潜在的业务逻辑变更风险,且返回集合的不可变性混合使用降低了代码的健壮性。
    • 安全性与潜在风险(30/30分): 本次变更主要为逻辑提取,不涉及外部输入直接拼接 SQL 等操作,无越权、XSS 或 SQL 注入等安全隐患。
    • 最佳实践与可维护性(16/20分): 提取私有方法复用逻辑极大地提升了可维护性,方向非常正确。扣分项在于 Collections.EMPTY_LIST 的使用不符合泛型规范,以及未对返回集合的引用进行防御性拷贝(直接返回了 req 内部的引用)。
    • 性能与资源利用(5/5分): 使用了 CollectionUtils.intersection 处理集合交集,性能开销在正常范围内,未见明显的性能瓶颈或资源浪费。
    • 提交信息质量(4/5分): Commit 信息结构清晰,准确描述了“提取方法”和“消除重复”的动作。扣除1分是因为未能准确体现 findLostStore 中实际发生的业务逻辑对齐(行为变更)。

    总分:93分

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