Commit 0fe5e260 by 林永健

feat(service): 优化业务场景分析服务中的门店数据统计功能

- 创建独立的请求对象副本避免原始参数被修改
- 使用BeanUtils复制请求参数确保数据隔离
- 更新所有方法调用使用新的请求对象引用
- 添加门店流失统计功能包括流失门店数和流失率计算
- 实现流失门店查询逻辑基于近30天无产值且近60天存在产值的条件
- 添加历史总门店数统计用于流失率计算基准
- 移除重复的流失门店计算代码块优化代码结构
- 保持环比计算逻辑但清理了相关代码位置
1 parent 31414d7c
...@@ -773,24 +773,26 @@ public class BusinessSceneAnalysisService { ...@@ -773,24 +773,26 @@ public class BusinessSceneAnalysisService {
StoreSceneSummaryResponse res = new StoreSceneSummaryResponse(); StoreSceneSummaryResponse res = new StoreSceneSummaryResponse();
//计算上个周期 //计算上个周期
calculatePreDate(req); calculatePreDate(req);
log.info("存量门店入参[{}]", JSON.toJSONString(req)); SceneBaseReq curReq = new SceneBaseReq();
BeanUtils.copyProperties(req, curReq);
log.info("存量门店入参[{}]", JSON.toJSONString(curReq));
// 查询总产值 // 查询总产值
List<Integer> merchantIds; List<Integer> merchantIds;
try { try {
merchantIds = getMerchantIdPermission(req, false); merchantIds = getMerchantIdPermission(curReq, false);
req.setMerchantIds(merchantIds); curReq.setMerchantIds(merchantIds);
} catch (Exception e) { } catch (Exception e) {
// 无权限,返回空继续执行历史代码逻辑 // 无权限,返回空继续执行历史代码逻辑
merchantIds = Collections.emptyList(); merchantIds = Collections.emptyList();
} }
String startDate = req.getStartTime(); String startDate = curReq.getStartTime();
String endDate = req.getEndTime(); String endDate = curReq.getEndTime();
//设置总开单门店数 //设置总开单门店数
res.setTotalStores(merchantIds.size()); res.setTotalStores(merchantIds.size());
res.setTotalStoreIds(merchantIds); res.setTotalStoreIds(merchantIds);
Integer biBusinessType = req.convertToBiBusinessType(req.getBizs()); Integer biBusinessType = curReq.convertToBiBusinessType(curReq.getBizs());
// 新店 // 新店
int newStoreCount = 0; int newStoreCount = 0;
// 老店 // 老店
...@@ -801,10 +803,10 @@ public class BusinessSceneAnalysisService { ...@@ -801,10 +803,10 @@ public class BusinessSceneAnalysisService {
if (!CollectionUtils.isEmpty(biRecords)) { if (!CollectionUtils.isEmpty(biRecords)) {
// 按门店分组 // 按门店分组
Map<Integer, List<BiRecord>> groupedByMerchant = biRecords.stream().collect(Collectors.groupingBy(BiRecord::getMerchantId)); Map<Integer, List<BiRecord>> groupedByMerchant = biRecords.stream().collect(Collectors.groupingBy(BiRecord::getMerchantId));
List<Integer> newStoreIds = countNewStore(groupedByMerchant, req.getBizs()); List<Integer> newStoreIds = countNewStore(groupedByMerchant, curReq.getBizs());
res.setNewStoreIds(newStoreIds); res.setNewStoreIds(newStoreIds);
newStoreCount = newStoreIds.size(); newStoreCount = newStoreIds.size();
List<Integer> oldStoreIds = countOldStore(groupedByMerchant, req.getBizs()); List<Integer> oldStoreIds = countOldStore(groupedByMerchant, curReq.getBizs());
res.setOldStoreIds(oldStoreIds); res.setOldStoreIds(oldStoreIds);
oldStoreCount = oldStoreIds.size(); oldStoreCount = oldStoreIds.size();
//统计单品类别门店数 //统计单品类别门店数
...@@ -822,6 +824,17 @@ public class BusinessSceneAnalysisService { ...@@ -822,6 +824,17 @@ 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%
...@@ -831,16 +844,7 @@ public class BusinessSceneAnalysisService { ...@@ -831,16 +844,7 @@ public class BusinessSceneAnalysisService {
//计算老店占比:(老店数量/总开单门店数)*100% //计算老店占比:(老店数量/总开单门店数)*100%
BigDecimal oldStoreRatio = usedCarDataStatsService.calculateDiv(oldStoreCount, res.getTotalStores()); BigDecimal oldStoreRatio = usedCarDataStatsService.calculateDiv(oldStoreCount, res.getTotalStores());
res.setOldStoresRatio(oldStoreRatio); res.setOldStoresRatio(oldStoreRatio);
// 流失门店:以统计周期的开始日期为基准,近30天无产值且近60天存在产值的门店数
int lostStoreCount = businessIntelligenceMapper.countLostMerchantByBizType(req.getMerchantIds(), startDate, biBusinessType, req.getExcludeIds());
res.setChurnedStores(lostStoreCount);
// 历史总门店数:以统计周期的开始日期为基准,近30-60天间存在产值的门店数;
int totalStoreCount = businessIntelligenceMapper.countHistoryMerchantByBizType(req.getMerchantIds(), startDate, biBusinessType, req.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. 潜在的空指针异常(NPE)与魔数问题(代码健壮性与规范)

    • 问题描述:在代码末尾的环比计算逻辑中,使用了 req.getCycleType().equals(4)。如果前端或上游调用未传递 cycleType 导致其为 null,此处会直接抛出 NullPointerException。同时,4 是一个缺乏业务语义的魔数(Magic Number)。
    • 影响分析:可能导致核心业务接口在特定入参下直接崩溃,影响服务稳定性。
    • 优化建议:将常量放在前面进行比较,或使用 Objects.equals();并将 4 提取为具有明确业务含义的常量或枚举。 java // 建议修改为: if (!Objects.equals(CycleTypeEnum.CUSTOM.getCode(), req.getCycleType())) { // 非自定义类型,需要环比 }

    2. 请求参数隔离不彻底(违背 Dubbo 参数防篡改规则)

    • 问题描述:提交信息中提到“创建独立的请求对象副本避免原始参数被修改”,这符合避免修改 Dubbo 服务提供者入参的重要规则。但是,代码在第 775 行 calculatePreDate(req); 对原始 req 进行了操作(该方法大概率会修改 req 的内部时间属性),随后才在第 777 行使用 BeanUtils.copyProperties(req, curReq); 进行拷贝。此外,BeanUtils 是浅拷贝,对象内部的集合(如 bizs)如果在后续被修改,依然会影响原始对象。
    • 影响分析:未能真正实现参数隔离,上游的 Dubbo 请求参数仍可能被污染,导致不可预知的上下文数据混乱。
    • 优化建议:先进行对象拷贝,再对副本进行业务计算;对于包含集合的复杂 DTO,建议手动 Set 或使用 MapStruct 替代 BeanUtils 以保证性能和深拷贝安全。 java // 建议修改为: SceneBaseReq curReq = new SceneBaseReq(); BeanUtils.copyProperties(req, curReq); // 确保 calculatePreDate 内部只操作 curReq calculatePreDate(curReq); log.info("存量门店入参[{}]", JSON.toJSONString(curReq));

    3. 需严格排查新增 Mapper 方法的 SQL 实现(MyBatis 子查询规则)

    • 问题描述:本次变更中整合并使用了 countLostMerchantByBizType 和 countHistoryMerchantByBizType 两个 Mapper 方法进行流失门店和历史门店的统计。
    • 影响分析:根据重要规则要求,MyBatis 的 SQL 语句绝对不允许使用子查询。由于此处未展示 XML 文件,但此类复杂维度的业务统计(如“近30天无产值且近60天存在产值”)极易被开发者顺手写成 NOT IN (SELECT ...) 或嵌套子查询。
    • 优化建议:请务必检查 businessIntelligenceMapper.xml 中这两个方法的 SQL 实现。如果存在子查询,请强制重构为 LEFT JOIN ... WHERE ... IS NULL 或通过应用层分步查询(先查 60 天有产值集合,再在内存中剔除近 30 天有产值的集合)来实现。

    二、评分明细

    • 功能实现的正确性与健壮性(32/40分): 扣分原因:存在 req.getCycleType().equals(4) 的潜在空指针风险;参数隔离逻辑存在时序错误(先计算后拷贝),未能完全规避原始入参被修改的风险。
    • 安全性与潜在风险(30/30分): 代码逻辑不涉及直接的安全漏洞(如 SQL 注入、越权等),符合安全基线。
    • 最佳实践与可维护性(14/20分): 扣分原因:代码中存在硬编码魔数(4);使用了基于反射的 BeanUtils.copyProperties,在严谨的工程实践中,建议使用 MapStruct 或手动赋值以获得更好的类型安全和性能。
    • 性能与资源利用(4/5分): 扣分原因:BeanUtils 存在轻微的反射性能损耗,但在当前非高频循环场景下尚可接受。将流失门店的计算逻辑移入 if 分支内,避免了不必要的数据库查询,这一点值得肯定。
    • 提交信息质量(5/5分): 提交信息非常规范(遵循 Conventional Commits),清晰详尽地列出了修改点和业务目的,具有很强的可追溯性。

    总分:85分

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