Commit cf416a12 by 林永健

refactor(job): 优化每日推送任务的策略处理逻辑

- 按业务类型分组处理昨日动态策略数据,避免一次性加载大数据结构
- 批量查询门店昨日动态数和门店配置,减少数据库调用次数
- 针对无关注记录的门店业务组合,构造临时关注记录实现推送
- 优化关注详情查询及内存分组,简化推送数据准备
- 增强日志输出,便于追踪门店和业务类型的处理情况
- 统一异常处理,确保单条数据处理失败不影响整体任务执行
1 parent 56d990d8
...@@ -15,6 +15,7 @@ import com.dangdang.ddframe.job.api.ShardingContext; ...@@ -15,6 +15,7 @@ import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob; import com.dangdang.ddframe.job.api.simple.SimpleJob;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.oreframework.boot.autoconfigure.elasticjob.annotation.ElasticJobConf; import org.oreframework.boot.autoconfigure.elasticjob.annotation.ElasticJobConf;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -62,62 +63,52 @@ public class MerchantBiDailyPushJob implements SimpleJob { ...@@ -62,62 +63,52 @@ public class MerchantBiDailyPushJob implements SimpleJob {
log.info("昨日无新增策略/留言,无需推送"); log.info("昨日无新增策略/留言,无需推送");
return; return;
} }
Map<Integer, Map<Integer, List<StrategyInfo>>> yesterdayDataMap = yesterdayStrategy.stream() List<Integer> merchantIds = yesterdayStrategy.stream().map(StrategyInfo::getMerchantId).distinct().collect(Collectors.toList());
.collect(Collectors.groupingBy(StrategyInfo::getMerchantId, Collectors.groupingBy(StrategyInfo::getBiz, Collectors.toList()))); List<String> merchantNames = yesterdayStrategy.stream().map(StrategyInfo::getMerchantName).distinct().collect(Collectors.toList());
List<Integer> activeMerchantIds = new ArrayList<>(yesterdayDataMap.keySet()); List<Byte> actualBusinessTypes = yesterdayStrategy.stream().map(p -> p.getBiz().byteValue()).distinct().collect(Collectors.toList());
log.info("昨日有动态的门店数:{}", activeMerchantIds.size()); log.info("昨日有动态的门店号:{}", merchantIds);
log.info("昨日有动态的门店名:{}", merchantNames);
// 2. 获取这些门店的所有业务类型(1-延保,2-轻改,3-权益) // 2. 批量查询昨日动态数 (返回 Map<key:merchantId_businessType, count>)
// List<Byte> businessTypes = Arrays.asList((byte)1, (byte)2, (byte)3); Map<String, Integer> countMap = commentService.batchCountYesterdayCommentsByBizType(merchantIds, actualBusinessTypes);
// 3. 批量查询门店配置 (按业务类型对应的 scene 查询)
Map<String, StoreSettingDTO> settingMap = new HashMap<>();
cacheMerchantSettingMap(settingMap, merchantIds, actualBusinessTypes);
Map<Integer, List<StrategyInfo>> groupByBiz = yesterdayStrategy.stream().collect(Collectors.groupingBy(StrategyInfo::getBiz));
for (Map.Entry<Integer, List<StrategyInfo>> entry : groupByBiz.entrySet()) {
Byte biz = entry.getKey().byteValue();
List<StrategyInfo> itemList = entry.getValue();
if (CollectionUtils.isEmpty(itemList)) {
log.error("该场景下无策略数据, biz:{}", biz);
continue;
}
// 3. 批量查询这些门店 + 业务的关注记录(只查询存在关注的组合) List<Integer> subMerchantIds = itemList.stream().map(StrategyInfo::getMerchantId).collect(Collectors.toList());
List<MerchantBiFollow> follows = followMapper.findByMerchantIdsAndBiz(activeMerchantIds, null); // 4. 批量查询这些门店 + 业务的关注记录
List<MerchantBiFollow> follows = followMapper.findByMerchantIdsAndBiz(subMerchantIds, biz);
if (CollectionUtils.isEmpty(follows)) { if (CollectionUtils.isEmpty(follows)) {
log.info("这些门店均无关注记录,但仍需推送给区域运营和线上运营"); log.info("这些门店均无关注记录,但仍需推送给区域运营和线上运营");
// 即使没有关注记录,也需要为每个门店 + 业务生成推送(场景 3) // 即使没有关注记录,也需要为每个门店 + 业务生成推送(场景 3)
// 构造临时的 follow 记录用于推送 // 构造临时的 follow 记录用于推送
List<MerchantBiFollow> tempFollows = new ArrayList<>(); follows = itemList.stream().map(p -> {
yesterdayDataMap.forEach((merchantId, data) -> data.forEach((biz, list) -> list.forEach(item -> {
MerchantBiFollow tempFollow = new MerchantBiFollow(); MerchantBiFollow tempFollow = new MerchantBiFollow();
tempFollow.setMerchantId(item.getMerchantId()); tempFollow.setMerchantId(p.getMerchantId());
tempFollow.setBusinessType(item.getBiz().byteValue()); tempFollow.setBusinessType(p.getBiz().byteValue());
tempFollow.setMerchantName(item.getMerchantName()); tempFollow.setMerchantName(p.getMerchantName());
tempFollows.add(tempFollow); return tempFollow;
}))); }).collect(Collectors.toList());
follows = tempFollows;
} }
log.info("需要处理的关注记录数:{}", follows.size()); log.info("需要处理的关注记录数:{}", follows.size());
Map<String, StoreSettingDTO> settingMap = new HashMap<>();
// 4. 提取所有 merchantId 和 followId
List<Integer> merchantIds = follows.stream()
.map(MerchantBiFollow::getMerchantId)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList());
List<Integer> followIds = follows.stream() List<Integer> followIds = follows.stream()
.map(MerchantBiFollow::getId) .map(MerchantBiFollow::getId)
.filter(Objects::nonNull) .filter(Objects::nonNull)
.distinct() .distinct()
.collect(Collectors.toList()); .collect(Collectors.toList());
// 提取所有业务类型 // 5. 批量查询关注明细 (一次 SQL 查询,内存中分组)
List<Byte> actualBusinessTypes = follows.stream()
.map(MerchantBiFollow::getBusinessType)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList());
// 5. 批量查询昨日动态数 (返回 Map<key:merchantId_businessType, count>)
Map<String, Integer> countMap = commentService.batchCountYesterdayCommentsByBizType(merchantIds, actualBusinessTypes);
// 6. 批量查询门店配置 (按业务类型对应的 scene 查询)
cacheMerchantSettingMap(settingMap, merchantIds, actualBusinessTypes);
// 7. 批量查询关注明细 (一次 SQL 查询,内存中分组)
Map<Integer, List<MerchantBiFollowDetail>> followersMap = new HashMap<>(); Map<Integer, List<MerchantBiFollowDetail>> followersMap = new HashMap<>();
if (CollectionUtils.isNotEmpty(followIds)) { if (CollectionUtils.isNotEmpty(followIds)) {
List<MerchantBiFollowDetail> allDetails = followDetailMapper.batchFindByFollowId(followIds); List<MerchantBiFollowDetail> allDetails = followDetailMapper.batchFindByFollowId(followIds);
...@@ -129,7 +120,7 @@ public class MerchantBiDailyPushJob implements SimpleJob { ...@@ -129,7 +120,7 @@ public class MerchantBiDailyPushJob implements SimpleJob {
log.info("followersMap:{}", followersMap); log.info("followersMap:{}", followersMap);
} }
// 8. 内存匹配并推送 // 6. 内存匹配并推送
for (MerchantBiFollow follow : follows) { for (MerchantBiFollow follow : follows) {
try { try {
Integer merchantId = follow.getMerchantId(); Integer merchantId = follow.getMerchantId();
...@@ -171,6 +162,7 @@ public class MerchantBiDailyPushJob implements SimpleJob { ...@@ -171,6 +162,7 @@ public class MerchantBiDailyPushJob implements SimpleJob {
follow.getMerchantId(), follow.getBusinessType(), e.getMessage(), e); follow.getMerchantId(), follow.getBusinessType(), e.getMessage(), e);
} }
} }
}
log.info("每日 BI 动态汇总推送任务执行完成"); log.info("每日 BI 动态汇总推送任务执行完成");
} }
......
  • @wangxw1
    commented
    Master

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

    1. 逻辑漏洞:部分门店无关注记录时会被遗漏推送(功能正确性)

    • 问题描述:在查询门店关注记录后,代码仅在 CollectionUtils.isEmpty(follows)(即该业务下所有门店均无关注记录)时,才会统一构造临时的推送记录。如果查询到的 follows 不为空,但只包含了部分门店的关注记录,那么那些没有关注记录的门店将被直接忽略,不会为其构造临时记录。
    • 影响分析:未能完全满足代码注释中“场景 3:无论是否有关注人都要推送给区域运营和线上运营”的业务需求,导致部分无关注记录的门店动态被遗漏推送,造成业务数据通知缺失。
    • 优化建议:对比该业务下产生动态的门店集合(subMerchantIds)和查询出的 follows 中的门店集合,找出缺失的门店并专门为其补充构建临时记录。
    • 代码示例: ```java List followedMerchantIds = follows.stream() .map(MerchantBiFollow::getMerchantId) .collect(Collectors.toList());

    // 找出没有关注记录的门店,补充构造临时记录 List tempFollows = itemList.stream() .filter(p -> !followedMerchantIds.contains(p.getMerchantId())) // 需配合去重逻辑(见问题3) .map(p -> { MerchantBiFollow tempFollow = new MerchantBiFollow(); tempFollow.setMerchantId(p.getMerchantId()); tempFollow.setBusinessType(p.getBiz().byteValue()); tempFollow.setMerchantName(p.getMerchantName()); return tempFollow; }).collect(Collectors.toList());

    follows.addAll(tempFollows);

    
    #### 2. 健壮性隐患:未判空可能导致 NullPointerException 任务中断(功能正确性)
    - **问题描述**:在提取 `actualBusinessTypes` 和按 `biz` 进行数据分组时,直接调用了 `p.getBiz().byteValue()` 和 `Collectors.groupingBy(StrategyInfo::getBiz)`,未对 `biz` 字段进行 `null` 值防御处理。
    - **影响分析**:如果上游数据存在异常导致 `biz` 字段为 `null`,自动拆箱或 `groupingBy`(不允许 key 为 null)会直接抛出 `NullPointerException`。由于这是在定时任务的主流程中,会导致整个任务崩溃,所有门店的推送全部失败。
    - **优化建议**:在进行 `map` 转换或 `groupingBy` 分组前,先使用 `filter` 过滤掉 `biz` 为 `null` 的脏数据。
    - **代码示例**:
      ```java
      List<Byte> actualBusinessTypes = yesterdayStrategy.stream()
              .map(StrategyInfo::getBiz)
              .filter(Objects::nonNull)
              .map(Integer::byteValue)
              .distinct()
              .collect(Collectors.toList());
    
      Map<Integer, List<StrategyInfo>> groupByBiz = yesterdayStrategy.stream()
              .filter(p -> p.getBiz() != null)
              .collect(Collectors.groupingBy(StrategyInfo::getBiz));
    

    3. 数据重复处理:构造临时关注记录时未进行去重(功能正确性)

    • 问题描述:当 follows 为空时,代码直接通过 itemList.stream().map(...) 遍历构造临时记录。此时 itemList 并未按 merchantId 去重,如果同一个门店在同一业务下昨天产生了多条策略动态,就会生成多条完全相同的临时关注记录。
    • 影响分析:会导致同一个门店的动态被重复推送到区域运营和线上运营,不仅影响用户体验(收到重复消息),还会造成系统资源的浪费。
    • 优化建议:在构造临时记录前,按照 merchantId 对 itemList 进行去重处理。
    • 代码示例: java follows = itemList.stream() .filter(p -> p.getMerchantId() != null) // 按照 merchantId 去重 .collect(Collectors.collectingAndThen( Collectors.toMap(StrategyInfo::getMerchantId, p -> p, (a, b) -> a), map -> map.values().stream().map(p -> { MerchantBiFollow tempFollow = new MerchantBiFollow(); tempFollow.setMerchantId(p.getMerchantId()); tempFollow.setBusinessType(p.getBiz().byteValue()); tempFollow.setMerchantName(p.getMerchantName()); return tempFollow; }).collect(Collectors.toList()) ));

    二、评分明细

    • 功能实现的正确性与健壮性(25 / 40分) 理由:核心业务逻辑存在遗漏推送的缺陷;集合操作中存在潜在的 NPE 风险;临时记录构造缺乏去重机制。未见 MyBatis 子查询违规(仅 Java 代码变更)。

    • 安全性与潜在风险(30 / 30分) 理由:代码主要涉及内部数据的流转与处理,未发现 SQL 注入、XSS、越权等明显的安全隐患。

    • 最佳实践与可维护性(15 / 20分) 理由:重构后日志输出更加清晰,异常捕获粒度更细,有助于维护。但扣分点在于:将原先的批量数据库查询(findByMerchantIdsAndBiz 和 batchFindByFollowId)移入了 for 循环内部。虽然 biz 的种类有限(循环次数少),但这违背了避免在循环中查询数据库的工程最佳实践。

    • 性能与资源利用(3 / 5分) 理由:循环内查询数据库略微增加了网络 I/O 开销;临时记录未去重会导致无意义的重复推送计算及下游资源消耗。

    • 提交信息质量(5 / 5分) 理由:Commit 信息非常清晰且格式规范,按条目准确描述了重构点,具备良好的可追溯性。

    总分:78分

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