Commit a6c60284 by 林永健

refactor(business-analysis): 优化预警门店统计与状态标记逻辑

- 将统计兜底门店数量方法改为查询门店ID列表并返回详细信息
- 修改代码逻辑使用门店ID列表计算兜底预警门店数量和状态标记
- 新增处理延保预警门店和轻改预警门店的独立方法,返回具体门店ID列表
- 在门店列表响应中增加追加门店状态方法支持状态维护和更新
- 优化各预警门店的查询逻辑,并增加日志输出便于调试跟踪
- 修改XML映射文件实现查询接口的对应调整,保证数据库操作正确解析
- 修正代码格式和空格,提高代码可读性和维护性
1 parent eab455c4
package cn.com.glsx.dj.peanalysis.dto.res.output; package cn.com.glsx.dj.peanalysis.dto.res.output;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;
import java.io.Serializable; import java.io.Serializable;
import java.math.BigDecimal; import java.math.BigDecimal;
...@@ -52,4 +54,11 @@ public class StoreListResponse implements Serializable { ...@@ -52,4 +54,11 @@ public class StoreListResponse implements Serializable {
this.storeId = storeId; this.storeId = storeId;
this.storeName = storeName; this.storeName = storeName;
} }
public void appendStoreStatus(String status) {
if (CollectionUtils.isEmpty(storeStatus)) {
this.storeStatus = Lists.newArrayList();
}
this.storeStatus.add(status);
}
} }
...@@ -131,6 +131,6 @@ public interface BusinessIntelligenceMapper { ...@@ -131,6 +131,6 @@ public interface BusinessIntelligenceMapper {
List<Integer> getZyMerchantIds(@Param("property") String property); List<Integer> getZyMerchantIds(@Param("property") String property);
//统计兜底门店数量 //统计兜底门店数量
Integer countBackUpStores(@Param("merchantIds") List<Integer> merchantIds, @Param("businessType") Integer businessType List<Integer> findBackUpStores(@Param("merchantIds") List<Integer> merchantIds, @Param("businessType") Integer businessType
, @Param("startDate") String startDate, @Param("endDate") String endDate); , @Param("startDate") String startDate, @Param("endDate") String endDate);
} }
\ No newline at end of file \ No newline at end of file
...@@ -244,7 +244,9 @@ public class BusinessSceneAnalysisService { ...@@ -244,7 +244,9 @@ public class BusinessSceneAnalysisService {
BigDecimal targetOutputConfig = monthTargetOutputMap.get(key); BigDecimal targetOutputConfig = monthTargetOutputMap.get(key);
item.setRatio(computeOutputRatio(item.getOutput(), targetOutputConfig)); item.setRatio(computeOutputRatio(item.getOutput(), targetOutputConfig));
} }
}else { } else {
item.setOutput(BigDecimal.ZERO);
item.setRatio(BigDecimal.ZERO);
item.setStartTime(DateUtil.formatDate(begin)); item.setStartTime(DateUtil.formatDate(begin));
item.setEndTime(DateUtil.formatDate(DateUtils.getEndTimeWithCycleType(begin, req.getCycleType()))); item.setEndTime(DateUtil.formatDate(DateUtils.getEndTimeWithCycleType(begin, req.getCycleType())));
} }
...@@ -401,8 +403,9 @@ public class BusinessSceneAnalysisService { ...@@ -401,8 +403,9 @@ public class BusinessSceneAnalysisService {
} }
} }
// 兜底预警门店:触发兜底预警的门店数; // 兜底预警门店:触发兜底预警的门店数;
int backUpStoreCount = businessIntelligenceMapper.countBackUpStores(req.getMerchantIds(), req.getBizs(), startDate, endDate); List<Integer> backUpStoreIds = businessIntelligenceMapper.findBackUpStores(req.getMerchantIds(), req.getBizs(), startDate, endDate);
res.setBottomLineWarningStores(backUpStoreCount); res.setBottomLineWarningStoreIds(backUpStoreIds);
res.setBottomLineWarningStores(backUpStoreIds.size());
// 生态指标预警门店:触发生态指标预警的门店数,即生态指标低于1.0即属于生态指标预警; // 生态指标预警门店:触发生态指标预警的门店数,即生态指标低于1.0即属于生态指标预警;
switch (queryParams.getBizs()) { switch (queryParams.getBizs()) {
case 1: case 1:
...@@ -426,6 +429,14 @@ public class BusinessSceneAnalysisService { ...@@ -426,6 +429,14 @@ public class BusinessSceneAnalysisService {
* 处理延保预警门店 * 处理延保预警门店
*/ */
public void handleDelayWarningStore(SceneBaseReq req, String startDate, String endDate, StoreSceneSummaryResponse res) { public void handleDelayWarningStore(SceneBaseReq req, String startDate, String endDate, StoreSceneSummaryResponse res) {
List<Integer> storeIds = handleDelayWarningStore(req, startDate, endDate);
if (CollectionUtils.isEmpty(storeIds)) {
return;
}
res.setEcosystemWarningStoreIds(storeIds);
res.setEcosystemWarningStores(storeIds.size());
}
public List<Integer> handleDelayWarningStore(SceneBaseReq req, String startDate, String endDate) {
Set<Integer> warningSet = new HashSet<>(); Set<Integer> warningSet = new HashSet<>();
DataBizQueryParamsDTO queryParams = req.convertQueryParam(); DataBizQueryParamsDTO queryParams = req.convertQueryParam();
queryParams.setStartDate(startDate); queryParams.setStartDate(startDate);
...@@ -464,8 +475,7 @@ public class BusinessSceneAnalysisService { ...@@ -464,8 +475,7 @@ public class BusinessSceneAnalysisService {
break; break;
} }
} }
res.setEcosystemWarningStores(warningSet.size()); return new ArrayList<>(warningSet);
res.setEcosystemWarningStoreIds(new ArrayList<>(warningSet));
} }
public void compareRatio(List<DataBusinessDTO> penetrationList, Set<Integer> warningSet, BigDecimal limit) { public void compareRatio(List<DataBusinessDTO> penetrationList, Set<Integer> warningSet, BigDecimal limit) {
...@@ -498,6 +508,7 @@ public class BusinessSceneAnalysisService { ...@@ -498,6 +508,7 @@ public class BusinessSceneAnalysisService {
/** /**
* 自定义时间,需要累计每家门店的数据 * 自定义时间,需要累计每家门店的数据
*
* @param dataBusinessDTOList * @param dataBusinessDTOList
* @param cycleType * @param cycleType
* @return * @return
...@@ -551,6 +562,14 @@ public class BusinessSceneAnalysisService { ...@@ -551,6 +562,14 @@ public class BusinessSceneAnalysisService {
* @param res * @param res
*/ */
public void handleLightChangeWarningStore(SceneBaseReq req, String startDate, String endDate, StoreSceneSummaryResponse res) { public void handleLightChangeWarningStore(SceneBaseReq req, String startDate, String endDate, StoreSceneSummaryResponse res) {
List<Integer> storeIds = handleLightChangeWarningStore(req, startDate, endDate);
if (CollectionUtils.isEmpty(storeIds)) {
return;
}
res.setEcosystemWarningStoreIds(storeIds);
res.setEcosystemWarningStores(storeIds.size());
}
public List<Integer> handleLightChangeWarningStore(SceneBaseReq req, String startDate, String endDate) {
DataBizQueryParamsDTO queryParams = req.convertQueryParam(); DataBizQueryParamsDTO queryParams = req.convertQueryParam();
queryParams.setStartDate(startDate); queryParams.setStartDate(startDate);
queryParams.setEndDate(endDate); queryParams.setEndDate(endDate);
...@@ -1123,7 +1142,36 @@ public class BusinessSceneAnalysisService { ...@@ -1123,7 +1142,36 @@ public class BusinessSceneAnalysisService {
List<StoreListResponse> res = new ArrayList<>(resMap.values()); List<StoreListResponse> res = new ArrayList<>(resMap.values());
// 计算兜底预警、生态预警标识 // 计算兜底预警、生态预警标识
// todo // 兜底预警门店:触发兜底预警的门店数;
String startDate = queryParams.getStartDate();
String endDate = queryParams.getEndDate();
List<Integer> bottomLineWarningStoresIds = Optional.ofNullable(businessIntelligenceMapper
.findBackUpStores(req.getMerchantIds(), req.getBizs(), startDate, endDate)).orElse(Collections.emptyList());
log.info("查询兜底预警结果:{}", bottomLineWarningStoresIds);
res.stream()
.filter(item -> bottomLineWarningStoresIds.contains(item.getStoreId()))
.forEach(item -> item.appendStoreStatus("2"));
// 生态指标预警门店:触发生态指标预警的门店数,即生态指标低于1.0即属于生态指标预警;
List<Integer> ecosystemWarningStoreIds = new ArrayList<>();
switch (queryParams.getBizs()) {
case 1:
//延保
ecosystemWarningStoreIds.addAll(handleDelayWarningStore(req, startDate, endDate));
break;
case 3:
//权益
break;
case 2:
//轻改
ecosystemWarningStoreIds.addAll(handleLightChangeWarningStore(req, startDate, endDate));
break;
default:
break;
}
log.info("查询生态预警结果:{}", ecosystemWarningStoreIds);
res.stream()
.filter(item -> ecosystemWarningStoreIds.contains(item.getStoreId()))
.forEach(item -> item.appendStoreStatus("3"));
// 排序: 支持选择「按产值」「按达成率」「按触客率」「按转化率」「按渗透率」「按升级客单价」「按平均客单价」及排序顺序「从大到小」「从小到大」切换排序规则;默认=「按产值」+「从大到小」 // 排序: 支持选择「按产值」「按达成率」「按触客率」「按转化率」「按渗透率」「按升级客单价」「按平均客单价」及排序顺序「从大到小」「从小到大」切换排序规则;默认=「按产值」+「从大到小」
SortUtils.sortList(res, req); SortUtils.sortList(res, req);
......
...@@ -787,8 +787,8 @@ ...@@ -787,8 +787,8 @@
AND t1.deleted_flag = 'N' AND t1.deleted_flag = 'N'
</select> </select>
<select id="countBackUpStores" resultType="java.lang.Integer"> <select id="findBackUpStores" resultType="java.lang.Integer">
SELECT count(distinct merchant_id4s) SELECT distinct merchant_id4s
FROM early_warning_msg t1 FROM early_warning_msg t1
left join early_warning_rule t2 on t1.rule_id =t2.id left join early_warning_rule t2 on t1.rule_id =t2.id
WHERE t1.deleted_flag = 'N' and t2.rule_type ='兜底指标' WHERE t1.deleted_flag = 'N' and t2.rule_type ='兜底指标'
......
  • @wangxw1
    commented
    Master

    Auto Review Result:

    代码审查报告

    问题描述和优化建议

    1. 功能实现的正确性与健壮性

    • 问题: appendStoreStatus 方法在 storeStatus 为空时初始化列表,但未考虑并发场景下的线程安全性。如果多个线程同时调用该方法,可能会导致 NullPointerException 或其他并发问题。

      • 建议: 使用 Collections.synchronizedList 或者 ConcurrentHashMap 来处理多线程环境下的并发问题。或者,如果该对象不被多线程共享,可以忽略此建议。
    • 问题: handleDelayWarningStore 和 handleLightChangeWarningStore 方法返回 ArrayList,而调用者直接使用了该列表。如果返回的列表为空,调用者需要手动检查是否为空,这增加了调用者的负担。

      • 建议: 考虑在方法内部进行空值检查,确保返回的列表永远不会为 null,而是返回一个空列表。例如: java public List<Integer> handleDelayWarningStore(SceneBaseReq req, String startDate, String endDate) { Set<Integer> warningSet = new HashSet<>(); // ... other logic ... return new ArrayList<>(warningSet); // or Collections.emptyList() if warningSet is empty }

    2. 安全性与潜在风险

    • 问题: 没有明显的安全漏洞,但需注意 SQL 查询的参数化和输入验证。虽然代码中没有直接看到 SQL 查询,但在实际应用中,确保所有数据库操作都使用参数化查询是非常重要的。
      • 建议: 确保所有数据库操作都使用参数化查询,避免 SQL 注入。此外,对输入数据进行必要的验证和清理。

    3. 是否符合最佳实践

    • 问题: appendStoreStatus 方法中的 Lists.newArrayList() 使用了 Guava 库,而项目中已经使用了 Spring 的 CollectionUtils,可以考虑统一使用 Spring 提供的工具类。

      • 建议: 统一使用 Spring 的工具类,减少对外部库的依赖。例如,使用 new ArrayList<>() 替代 Lists.newArrayList()。
    • 问题: handleDelayWarningStore 和 handleLightChangeWarningStore 方法的命名不够清晰,容易引起混淆。特别是两个方法签名相同,仅返回类型不同,这违反了 Java 的编码规范。

      • 建议: 重命名方法以提高可读性和避免混淆。例如,可以将 handleDelayWarningStore(SceneBaseReq req, String startDate, String endDate) 重命名为 getDelayWarningStoreIds(SceneBaseReq req, String startDate, String endDate)。

    4. 性能与资源利用效率

    • 问题: 在 BusinessSceneAnalysisService 类中,多次调用 contains 方法来检查 storeIds 列表中的元素。由于 contains 方法的时间复杂度为 O(n),当 storeIds 列表较大时,性能可能受到影响。
      • 建议: 使用 HashSet 来存储 storeIds,以提高查找效率。例如: java Set<Integer> bottomLineWarningStoreIdsSet = new HashSet<>(bottomLineWarningStoresIds); res.stream() .filter(item -> bottomLineWarningStoreIdsSet.contains(item.getStoreId())) .forEach(item -> item.appendStoreStatus("2"));

    5. Commits信息的清晰性与准确性

    • 问题: 提交信息描述详细且准确,涵盖了代码的主要改动点。
      • 建议: 保持现有的提交信息格式,继续提供清晰、详细的提交信息。

    评分明细

    • 功能实现的正确性与健壮性: 35分
    • 安全性与潜在风险: 28分
    • 是否符合最佳实践: 18分
    • 性能与资源利用效率: 4分
    • Commits信息的清晰性与准确性: 5分

    总分: 90分


    通过以上审查,代码整体上是正确的,但存在一些可以改进的地方。希望这些建议能够帮助提升代码的质量和可维护性。

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