|
@@ -0,0 +1,125 @@
|
|
|
|
|
+package com.storlead.sales.mail.mkt.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.storlead.framework.common.constant.CommonConstant;
|
|
|
|
|
+import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
|
|
|
|
|
+import com.storlead.sales.mail.mkt.entity.MktMailMessageEntity;
|
|
|
|
|
+import com.storlead.sales.mail.mkt.entity.MktMailReplyRecordEntity;
|
|
|
|
|
+import com.storlead.sales.mail.mkt.mapper.MktMailMessageMapper;
|
|
|
|
|
+import com.storlead.sales.mail.mkt.mapper.MktMailReplyRecordMapper;
|
|
|
|
|
+import com.storlead.sales.mail.mkt.service.MktMailReplyRecordService;
|
|
|
|
|
+import lombok.extern.log4j.Log4j2;
|
|
|
|
|
+import org.springframework.dao.DuplicateKeyException;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.Collections;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+@Log4j2
|
|
|
|
|
+@Service
|
|
|
|
|
+public class MktMailReplyRecordServiceImpl
|
|
|
|
|
+ extends MyBaseServiceImpl<MktMailReplyRecordMapper, MktMailReplyRecordEntity>
|
|
|
|
|
+ implements MktMailReplyRecordService {
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private MktMailMessageMapper mktMailMessageMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void recordReply(MktMailMessageEntity original, Long replyMailId, String replyTheme, boolean firstReply) {
|
|
|
|
|
+ if (original == null || original.getId() == null || replyMailId == null) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 已存在则跳过(幂等)
|
|
|
|
|
+ Integer exist = this.count(new LambdaQueryWrapper<MktMailReplyRecordEntity>()
|
|
|
|
|
+ .eq(MktMailReplyRecordEntity::getMailId, original.getId())
|
|
|
|
|
+ .eq(MktMailReplyRecordEntity::getReplyMailId, replyMailId)
|
|
|
|
|
+ .eq(MktMailReplyRecordEntity::getIsDelete, CommonConstant.DEL_FLAG_0));
|
|
|
|
|
+ if (exist != null && exist > 0) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ MktMailMessageEntity reply = mktMailMessageMapper.selectOne(new LambdaQueryWrapper<MktMailMessageEntity>()
|
|
|
|
|
+ .select(MktMailMessageEntity::getId,
|
|
|
|
|
+ MktMailMessageEntity::getSubject,
|
|
|
|
|
+ MktMailMessageEntity::getFromAddr,
|
|
|
|
|
+ MktMailMessageEntity::getFromName,
|
|
|
|
|
+ MktMailMessageEntity::getRecipientDate,
|
|
|
|
|
+ MktMailMessageEntity::getSentDate,
|
|
|
|
|
+ MktMailMessageEntity::getCreateTime)
|
|
|
|
|
+ .eq(MktMailMessageEntity::getId, replyMailId)
|
|
|
|
|
+ .eq(MktMailMessageEntity::getIsDelete, CommonConstant.DEL_FLAG_0)
|
|
|
|
|
+ .last("limit 1"));
|
|
|
|
|
+
|
|
|
|
|
+ Date now = new Date();
|
|
|
|
|
+ MktMailReplyRecordEntity row = new MktMailReplyRecordEntity();
|
|
|
|
|
+ row.setMailId(original.getId());
|
|
|
|
|
+ row.setReplyMailId(replyMailId);
|
|
|
|
|
+ row.setAccountId(original.getAccountId());
|
|
|
|
|
+ row.setOrgId(original.getOrgId());
|
|
|
|
|
+ row.setBizRef(original.getBizRef());
|
|
|
|
|
+ row.setBizBatchNo(original.getBizBatchNo());
|
|
|
|
|
+ row.setOriginalSubject(original.getSubject());
|
|
|
|
|
+ if (reply != null && StringUtils.hasText(reply.getSubject())) {
|
|
|
|
|
+ row.setReplySubject(truncate(reply.getSubject(), 512));
|
|
|
|
|
+ } else if (StringUtils.hasText(replyTheme)) {
|
|
|
|
|
+ row.setReplySubject(truncate(replyTheme, 512));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (reply != null) {
|
|
|
|
|
+ row.setReplyFrom(reply.getFromAddr());
|
|
|
|
|
+ row.setReplyFromName(reply.getFromName());
|
|
|
|
|
+ if (reply.getRecipientDate() != null) {
|
|
|
|
|
+ row.setReplyTime(reply.getRecipientDate());
|
|
|
|
|
+ } else if (reply.getSentDate() != null) {
|
|
|
|
|
+ row.setReplyTime(reply.getSentDate());
|
|
|
|
|
+ } else if (reply.getCreateTime() != null) {
|
|
|
|
|
+ row.setReplyTime(reply.getCreateTime());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ row.setReplyTime(now);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ row.setReplySubject(truncate(replyTheme, 512));
|
|
|
|
|
+ row.setReplyTime(now);
|
|
|
|
|
+ }
|
|
|
|
|
+ row.setIsFirstReply(firstReply ? 1 : 0);
|
|
|
|
|
+ row.setOwnerBy(original.getOwnerBy());
|
|
|
|
|
+ row.setCreateBy(original.getOwnerBy());
|
|
|
|
|
+ row.setCreateTime(now);
|
|
|
|
|
+ row.setUpdateTime(now);
|
|
|
|
|
+ row.setIsDelete(CommonConstant.DEL_FLAG_0);
|
|
|
|
|
+ row.setEnabled(Boolean.TRUE);
|
|
|
|
|
+ try {
|
|
|
|
|
+ this.save(row);
|
|
|
|
|
+ log.info("mkt reply record saved, mailId={}, replyMailId={}, firstReply={}",
|
|
|
|
|
+ original.getId(), replyMailId, firstReply);
|
|
|
|
|
+ } catch (DuplicateKeyException e) {
|
|
|
|
|
+ log.debug("mkt reply record already exists, mailId={}, replyMailId={}",
|
|
|
|
|
+ original.getId(), replyMailId);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("mkt reply record save error, mailId={}, replyMailId={}",
|
|
|
|
|
+ original.getId(), replyMailId, e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<MktMailReplyRecordEntity> listByMailId(Long mailId) {
|
|
|
|
|
+ if (mailId == null) {
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+ return this.list(new LambdaQueryWrapper<MktMailReplyRecordEntity>()
|
|
|
|
|
+ .eq(MktMailReplyRecordEntity::getMailId, mailId)
|
|
|
|
|
+ .eq(MktMailReplyRecordEntity::getIsDelete, CommonConstant.DEL_FLAG_0)
|
|
|
|
|
+ .orderByAsc(MktMailReplyRecordEntity::getReplyTime)
|
|
|
|
|
+ .orderByAsc(MktMailReplyRecordEntity::getId));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static String truncate(String text, int max) {
|
|
|
|
|
+ if (!StringUtils.hasText(text)) {
|
|
|
|
|
+ return text;
|
|
|
|
|
+ }
|
|
|
|
|
+ String s = text.trim();
|
|
|
|
|
+ return s.length() <= max ? s : s.substring(0, max);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|