|
@@ -1716,15 +1716,20 @@ public class MarketEmailsServiceImpl extends MyBaseServiceImpl<MarketEmailsMappe
|
|
|
log.info("marketEmailList: {}", marketEmailList);
|
|
log.info("marketEmailList: {}", marketEmailList);
|
|
|
|
|
|
|
|
List<MarketEmailsEntity> newMarketEmailList = new ArrayList<>();
|
|
List<MarketEmailsEntity> newMarketEmailList = new ArrayList<>();
|
|
|
|
|
+ // 进程内去重:防止同一封首邮件在本次循环中(或被多次加入)被加两次
|
|
|
|
|
+ Set<Long> pendingFirstSendIds = new HashSet<>();
|
|
|
//新增一条需要发送的邮件记录
|
|
//新增一条需要发送的邮件记录
|
|
|
for (MarketEmailsEntity marketEmail : marketEmailList){
|
|
for (MarketEmailsEntity marketEmail : marketEmailList){
|
|
|
- //如果marketEmail.getId()不在marketEmailHasFirstIdList中
|
|
|
|
|
- if (marketEmailHasFirstIdList.contains(marketEmail.getId())) {
|
|
|
|
|
|
|
+ Long firstId = marketEmail.getId();
|
|
|
|
|
+ // 跳过:① 运行开始时已存在 follow-up 的;② 本次批次内已经加过一遍的(防同批重复)
|
|
|
|
|
+ if (firstId == null
|
|
|
|
|
+ || marketEmailHasFirstIdList.contains(firstId)
|
|
|
|
|
+ || pendingFirstSendIds.contains(firstId)) {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
MarketEmailsEntity newMarketEmail = new MarketEmailsEntity();
|
|
MarketEmailsEntity newMarketEmail = new MarketEmailsEntity();
|
|
|
BeanUtils.copyProperties(marketEmail, newMarketEmail);
|
|
BeanUtils.copyProperties(marketEmail, newMarketEmail);
|
|
|
- newMarketEmail.setFirstSendId(marketEmail.getId());
|
|
|
|
|
|
|
+ newMarketEmail.setFirstSendId(firstId);
|
|
|
newMarketEmail.setId(null);
|
|
newMarketEmail.setId(null);
|
|
|
newMarketEmail.setEmailId(null);
|
|
newMarketEmail.setEmailId(null);
|
|
|
newMarketEmail.setStatus(-1);
|
|
newMarketEmail.setStatus(-1);
|
|
@@ -1737,9 +1742,41 @@ public class MarketEmailsServiceImpl extends MyBaseServiceImpl<MarketEmailsMappe
|
|
|
newMarketEmail.setHasReply(0);
|
|
newMarketEmail.setHasReply(0);
|
|
|
|
|
|
|
|
newMarketEmailList.add(newMarketEmail);
|
|
newMarketEmailList.add(newMarketEmail);
|
|
|
|
|
+ pendingFirstSendIds.add(firstId);
|
|
|
}
|
|
}
|
|
|
if (!CollectionUtils.isEmpty(newMarketEmailList)){
|
|
if (!CollectionUtils.isEmpty(newMarketEmailList)){
|
|
|
- this.saveBatch(newMarketEmailList);
|
|
|
|
|
|
|
+ // 二次去重(防并发 / 多实例):运行期间(含其它实例)可能已经插入了这些首邮件的 follow-up,
|
|
|
|
|
+ // 再查一次 DB,把已经存在的 first_send_id 过滤掉,彻底避免同一封首邮件生成两条 second。
|
|
|
|
|
+ // (之前 109 / 113 各被生成两条,就是两个 doSendAgain 并发、彼此没看到对方插入导致的)
|
|
|
|
|
+ List<Long> candidateFirstIds = newMarketEmailList.stream()
|
|
|
|
|
+ .map(MarketEmailsEntity::getFirstSendId)
|
|
|
|
|
+ .filter(Objects::nonNull)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ if (!candidateFirstIds.isEmpty()) {
|
|
|
|
|
+ LambdaQueryWrapper<MarketEmailsEntity> existQuery = new LambdaQueryWrapper<>();
|
|
|
|
|
+ existQuery.eq(MarketEmailsEntity::getIsDelete, CommonConstant.DEL_FLAG_0)
|
|
|
|
|
+ .eq(MarketEmailsEntity::getHasFirst, 2)
|
|
|
|
|
+ .in(MarketEmailsEntity::getFirstSendId, candidateFirstIds);
|
|
|
|
|
+ Set<Long> alreadyExistIds = this.list(existQuery).stream()
|
|
|
|
|
+ .map(MarketEmailsEntity::getFirstSendId)
|
|
|
|
|
+ .filter(Objects::nonNull)
|
|
|
|
|
+ .collect(Collectors.toSet());
|
|
|
|
|
+ if (!alreadyExistIds.isEmpty()) {
|
|
|
|
|
+ newMarketEmailList = newMarketEmailList.stream()
|
|
|
|
|
+ .filter(e -> !alreadyExistIds.contains(e.getFirstSendId()))
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!CollectionUtils.isEmpty(newMarketEmailList)){
|
|
|
|
|
+ try {
|
|
|
|
|
+ this.saveBatch(newMarketEmailList);
|
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
|
+ // 兜底:若并发下仍有极少量重复键(依赖 DB 唯一索引 uq_second_first_send 兜底),
|
|
|
|
|
+ // 只记录告警,不让整个定时任务中断。
|
|
|
|
|
+ log.warn("[sendAgain] 批量插入 follow-up 邮件部分失败(可能为并发重复键,已靠唯一索引拦截):{}",
|
|
|
|
|
+ ex.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|