|
|
@@ -4,8 +4,11 @@ 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.mybatis.service.impl.MyBaseServiceImpl;
|
|
|
+import com.storlead.sales.mail.common.entity.SmtpPopSettingsEntity;
|
|
|
+import com.storlead.sales.mail.common.service.SmtpPopSettingsService;
|
|
|
import com.storlead.sales.mail.mkt.entity.MktMailMessageEntity;
|
|
|
import com.storlead.sales.mail.mkt.entity.MktMailStatusEntity;
|
|
|
+import com.storlead.sales.mail.mkt.mapper.MktMailMessageMapper;
|
|
|
import com.storlead.sales.mail.mkt.mapper.MktMailStatusMapper;
|
|
|
import com.storlead.sales.mail.mkt.service.MktMailStatusService;
|
|
|
import lombok.extern.log4j.Log4j2;
|
|
|
@@ -14,6 +17,7 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.util.Collection;
|
|
|
import java.util.Date;
|
|
|
|
|
|
@@ -25,15 +29,43 @@ import java.util.Date;
|
|
|
public class MktMailStatusServiceImpl extends MyBaseServiceImpl<MktMailStatusMapper, MktMailStatusEntity>
|
|
|
implements MktMailStatusService {
|
|
|
|
|
|
+ @Resource
|
|
|
+ private MktMailMessageMapper mktMailMessageMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private SmtpPopSettingsService smtpPopSettingsService;
|
|
|
+
|
|
|
@Override
|
|
|
public void ensureStatus(MktMailMessageEntity mail) {
|
|
|
if (mail == null || mail.getId() == null) {
|
|
|
return;
|
|
|
}
|
|
|
+ Long outboxId = mail.getAccountId();
|
|
|
+ String outbox = resolveOutboxAddr(mail.getFromAddr(), outboxId);
|
|
|
+
|
|
|
MktMailStatusEntity existing = getByMailId(mail.getId());
|
|
|
if (existing != null) {
|
|
|
+ LambdaUpdateWrapper<MktMailStatusEntity> patch = new LambdaUpdateWrapper<>();
|
|
|
+ patch.eq(MktMailStatusEntity::getMailId, mail.getId());
|
|
|
+ patch.eq(MktMailStatusEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
|
|
|
+ boolean needPatch = false;
|
|
|
if (mail.getStatus() != null && !mail.getStatus().equals(existing.getSendStatus())) {
|
|
|
- markSendStatus(mail.getId(), mail.getStatus());
|
|
|
+ patch.set(MktMailStatusEntity::getSendStatus, mail.getStatus());
|
|
|
+ needPatch = true;
|
|
|
+ }
|
|
|
+ if (existing.getOutboxId() == null && outboxId != null) {
|
|
|
+ patch.set(MktMailStatusEntity::getOutboxId, outboxId);
|
|
|
+ needPatch = true;
|
|
|
+ }
|
|
|
+ if (!StringUtils.hasText(existing.getOutbox()) && StringUtils.hasText(outbox)) {
|
|
|
+ patch.set(MktMailStatusEntity::getOutbox, outbox);
|
|
|
+ needPatch = true;
|
|
|
+ }
|
|
|
+ if (needPatch) {
|
|
|
+ Date now = new Date();
|
|
|
+ patch.set(MktMailStatusEntity::getStatusUpdatedAt, now);
|
|
|
+ patch.set(MktMailStatusEntity::getUpdateTime, now);
|
|
|
+ this.update(patch);
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
@@ -41,6 +73,9 @@ public class MktMailStatusServiceImpl extends MyBaseServiceImpl<MktMailStatusMap
|
|
|
Date now = new Date();
|
|
|
MktMailStatusEntity row = new MktMailStatusEntity();
|
|
|
row.setMailId(mail.getId());
|
|
|
+ row.setOutboxId(outboxId);
|
|
|
+ row.setOutbox(outbox);
|
|
|
+ row.setHasRefuse(0);
|
|
|
row.setSendStatus(mail.getStatus() == null ? 0 : mail.getStatus());
|
|
|
row.setOpened(0);
|
|
|
row.setOpenCount(0);
|
|
|
@@ -141,6 +176,23 @@ public class MktMailStatusServiceImpl extends MyBaseServiceImpl<MktMailStatusMap
|
|
|
this.update(update);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void markRefused(Long mailId) {
|
|
|
+ if (mailId == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ ensureMinimalRow(mailId);
|
|
|
+ Date now = new Date();
|
|
|
+ LambdaUpdateWrapper<MktMailStatusEntity> update = new LambdaUpdateWrapper<>();
|
|
|
+ update.eq(MktMailStatusEntity::getMailId, mailId);
|
|
|
+ update.eq(MktMailStatusEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
|
|
|
+ update.set(MktMailStatusEntity::getHasRefuse, 1);
|
|
|
+ update.set(MktMailStatusEntity::getStatusUpdatedAt, now);
|
|
|
+ update.set(MktMailStatusEntity::getUpdateTime, now);
|
|
|
+ this.update(update);
|
|
|
+ log.info("mkt_mail_status markRefused, mailId={}", mailId);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 将回复邮件 ID 追加到逗号串;已存在则原样返回;超过 200 字符则不再追加。
|
|
|
*/
|
|
|
@@ -200,18 +252,31 @@ public class MktMailStatusServiceImpl extends MyBaseServiceImpl<MktMailStatusMap
|
|
|
.last("limit 1"));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 兜底建行:从原信补齐 outbox_id / outbox。
|
|
|
+ */
|
|
|
private void ensureMinimalRow(Long mailId) {
|
|
|
if (getByMailId(mailId) != null) {
|
|
|
return;
|
|
|
}
|
|
|
+ MktMailMessageEntity mail = mktMailMessageMapper.selectById(mailId);
|
|
|
Date now = new Date();
|
|
|
MktMailStatusEntity row = new MktMailStatusEntity();
|
|
|
row.setMailId(mailId);
|
|
|
- row.setSendStatus(0);
|
|
|
+ if (mail != null) {
|
|
|
+ row.setOutboxId(mail.getAccountId());
|
|
|
+ row.setOutbox(resolveOutboxAddr(mail.getFromAddr(), mail.getAccountId()));
|
|
|
+ row.setOwnerBy(mail.getOwnerBy());
|
|
|
+ row.setCreateBy(mail.getCreateBy() == null ? mail.getOwnerBy() : mail.getCreateBy());
|
|
|
+ row.setSendStatus(mail.getStatus() == null ? 0 : mail.getStatus());
|
|
|
+ } else {
|
|
|
+ row.setSendStatus(0);
|
|
|
+ }
|
|
|
row.setOpened(0);
|
|
|
row.setOpenCount(0);
|
|
|
row.setIsRead(0);
|
|
|
row.setReplied(0);
|
|
|
+ row.setHasRefuse(0);
|
|
|
row.setStatusUpdatedAt(now);
|
|
|
row.setSyncedAt(null);
|
|
|
row.setCreateTime(now);
|
|
|
@@ -226,4 +291,21 @@ public class MktMailStatusServiceImpl extends MyBaseServiceImpl<MktMailStatusMap
|
|
|
log.error("ensureMinimalRow error, mailId={}", mailId, e);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /** 优先用邮件 from_addr;为空则按账户 ID 查邮箱地址。 */
|
|
|
+ private String resolveOutboxAddr(String fromAddr, Long accountId) {
|
|
|
+ if (StringUtils.hasText(fromAddr)) {
|
|
|
+ return fromAddr.trim();
|
|
|
+ }
|
|
|
+ if (accountId == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ SmtpPopSettingsEntity account = smtpPopSettingsService.getById(accountId);
|
|
|
+ return account == null ? null : account.getEmailAddress();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("resolve outbox by accountId failed, accountId={}", accountId, e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|