|
|
@@ -5,10 +5,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.storlead.framework.common.constant.CommonConstant;
|
|
|
import com.storlead.framework.common.constant.DSConstants;
|
|
|
-import com.storlead.framework.common.result.Result;
|
|
|
-import com.storlead.mail.webclient.client.MktMailRemoteClient;
|
|
|
-import com.storlead.mail.webclient.dto.MktMailStatusChangeRemoteDTO;
|
|
|
-import com.storlead.mail.webclient.dto.MktMailStatusSyncQueryRemoteDTO;
|
|
|
+import com.storlead.mail.pojo.entity.MktMailStatusEntity;
|
|
|
+import com.storlead.mail.service.MktMailStatusService;
|
|
|
import com.storlead.trade.entity.MarketEmailsEntity;
|
|
|
import com.storlead.trade.service.MarketEmailsService;
|
|
|
import com.storlead.trade.service.MarketEmailsStatusSyncService;
|
|
|
@@ -21,8 +19,9 @@ import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
- * mkt_mail_status → market_emails:
|
|
|
- * 查待同步(update_time > status_updated_at)→ 按 mail_id 改业务表 → 回写同步时间。
|
|
|
+ * 跨库同步:邮件库 mkt_mail_status → 业务库 market_emails,再回写 status_updated_at。
|
|
|
+ * <p>
|
|
|
+ * 邮件侧走 {@link MktMailStatusService}(@DS mail),业务侧走 trade 数据源;不做跨库事务。
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
@@ -37,7 +36,7 @@ public class MarketEmailsStatusSyncServiceImpl implements MarketEmailsStatusSync
|
|
|
private static final int TRADE_STATUS_FAIL = 2;
|
|
|
|
|
|
@Resource
|
|
|
- private MktMailRemoteClient mktMailRemoteClient;
|
|
|
+ private MktMailStatusService mktMailStatusService;
|
|
|
|
|
|
@Resource
|
|
|
private MarketEmailsService marketEmailsService;
|
|
|
@@ -48,56 +47,44 @@ public class MarketEmailsStatusSyncServiceImpl implements MarketEmailsStatusSync
|
|
|
Long lastId = null;
|
|
|
|
|
|
for (int page = 0; page < MAX_PAGES; page++) {
|
|
|
- // 1. 查 mkt_mail_status:有最新修改(update_time > status_updated_at)
|
|
|
- MktMailStatusSyncQueryRemoteDTO query = new MktMailStatusSyncQueryRemoteDTO();
|
|
|
- query.setLimit(BATCH_LIMIT);
|
|
|
- query.setLastId(lastId);
|
|
|
-
|
|
|
- Result<List<MktMailStatusChangeRemoteDTO>> result;
|
|
|
+ // 1. 邮件库:update_time > status_updated_at
|
|
|
+ List<MktMailStatusEntity> changes;
|
|
|
try {
|
|
|
- result = mktMailRemoteClient.statusChanges(query);
|
|
|
+ changes = mktMailStatusService.listPendingChanges(lastId, BATCH_LIMIT);
|
|
|
} catch (Exception e) {
|
|
|
log.error("查询 mkt_mail_status 待同步数据失败 lastId={}", lastId, e);
|
|
|
break;
|
|
|
}
|
|
|
- if (result == null || !result.isSuccess()) {
|
|
|
- log.warn("查询 mkt_mail_status 失败 message={}", result == null ? null : result.getMessage());
|
|
|
- break;
|
|
|
- }
|
|
|
- List<MktMailStatusChangeRemoteDTO> changes = result.getResult();
|
|
|
if (CollectionUtils.isEmpty(changes)) {
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
List<Long> syncedMailIds = new ArrayList<>();
|
|
|
Long batchLastId = lastId;
|
|
|
- for (MktMailStatusChangeRemoteDTO change : changes) {
|
|
|
+ for (MktMailStatusEntity change : changes) {
|
|
|
if (change == null || change.getMailId() == null) {
|
|
|
continue;
|
|
|
}
|
|
|
if (change.getId() != null) {
|
|
|
batchLastId = change.getId();
|
|
|
}
|
|
|
- // 2. 按邮件 id 修改 market_emails
|
|
|
+ // 2. 业务库:按 email_id = mail_id 更新 market_emails
|
|
|
ApplyResult applyResult = applyToMarketEmails(change);
|
|
|
if (applyResult == ApplyResult.UPDATED) {
|
|
|
totalUpdated++;
|
|
|
syncedMailIds.add(change.getMailId());
|
|
|
} else if (applyResult == ApplyResult.MATCHED_NO_CHANGE) {
|
|
|
- // 业务表已对齐,仍需回写同步时间,避免反复拉取
|
|
|
syncedMailIds.add(change.getMailId());
|
|
|
}
|
|
|
// UNMATCHED:尚无 email_id 关联,不 ack,下次继续等
|
|
|
}
|
|
|
|
|
|
- // 3. 同步后回写 mkt_mail_status.status_updated_at
|
|
|
+ // 3. 邮件库:回写 status_updated_at
|
|
|
if (!syncedMailIds.isEmpty()) {
|
|
|
try {
|
|
|
- Result<?> ack = mktMailRemoteClient.ackStatusSync(syncedMailIds);
|
|
|
- if (ack == null || !ack.isSuccess()) {
|
|
|
- log.warn("回写 mkt_mail_status.status_updated_at 失败 mailIds={} message={}",
|
|
|
- syncedMailIds, ack == null ? null : ack.getMessage());
|
|
|
- // ack 失败则不再继续翻页,避免漏确认
|
|
|
+ boolean ackOk = mktMailStatusService.ackStatusSync(syncedMailIds);
|
|
|
+ if (!ackOk) {
|
|
|
+ log.warn("回写 mkt_mail_status.status_updated_at 失败 mailIds={}", syncedMailIds);
|
|
|
break;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
@@ -117,7 +104,7 @@ public class MarketEmailsStatusSyncServiceImpl implements MarketEmailsStatusSync
|
|
|
return totalUpdated;
|
|
|
}
|
|
|
|
|
|
- private ApplyResult applyToMarketEmails(MktMailStatusChangeRemoteDTO change) {
|
|
|
+ private ApplyResult applyToMarketEmails(MktMailStatusEntity change) {
|
|
|
MarketEmailsEntity existing = marketEmailsService.getOne(new LambdaQueryWrapper<MarketEmailsEntity>()
|
|
|
.eq(MarketEmailsEntity::getEmailId, change.getMailId())
|
|
|
.eq(MarketEmailsEntity::getIsDelete, CommonConstant.DEL_FLAG_0)
|
|
|
@@ -188,7 +175,7 @@ public class MarketEmailsStatusSyncServiceImpl implements MarketEmailsStatusSync
|
|
|
return TRADE_STATUS_DRAFT;
|
|
|
}
|
|
|
|
|
|
- private static Integer resolveReader(MktMailStatusChangeRemoteDTO change) {
|
|
|
+ private static Integer resolveReader(MktMailStatusEntity change) {
|
|
|
if (Integer.valueOf(1).equals(change.getOpened()) || Integer.valueOf(1).equals(change.getIsRead())) {
|
|
|
return 1;
|
|
|
}
|