|
@@ -2,7 +2,10 @@ package com.fjhx.service.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.lang.Assert;
|
|
|
import cn.hutool.core.thread.ThreadUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
@@ -11,6 +14,7 @@ import com.fjhx.config.redis.RedisCache;
|
|
|
import com.fjhx.constants.RedisConstant;
|
|
|
import com.fjhx.constants.SendConstant;
|
|
|
import com.fjhx.entity.*;
|
|
|
+import com.fjhx.enums.SendEventEnum;
|
|
|
import com.fjhx.service.*;
|
|
|
import com.fjhx.utils.EmailEngineUtil;
|
|
|
import com.fjhx.utils.RetryUtil;
|
|
@@ -55,6 +59,9 @@ public class AccountServiceImpl implements IAccountService {
|
|
|
|
|
|
String email = bindingVo.getEmail();
|
|
|
|
|
|
+ ProgressVo progressVo = RedisCache.get(RedisConstant.PROGRESS_KEY + email);
|
|
|
+ Assert.isNull(progressVo, "邮箱正在同步中,请勿重复操作");
|
|
|
+
|
|
|
EmailInfo emailInfo = emailInfoService.getOne(Wrappers.<EmailInfo>lambdaQuery().eq(EmailInfo::getEmail, email));
|
|
|
// 如果存在,直接返回邮箱信息
|
|
|
if (emailInfo != null) {
|
|
@@ -69,16 +76,32 @@ public class AccountServiceImpl implements IAccountService {
|
|
|
List<EmailMailboxVo.MailboxesDTO> mailboxList = EmailEngineUtil.getMailboxList(email);
|
|
|
// 添加邮箱文件夹
|
|
|
List<EmailMailbox> emailMailboxList = this.saveEmailMailbox(mailboxList, emailInfo.getId(), email);
|
|
|
-
|
|
|
// redis添加同步进度
|
|
|
this.synchronizationProgressInitialization(email, mailboxList);
|
|
|
-
|
|
|
// 异步遍历文件夹下的所有邮件
|
|
|
this.asyncReadEmail(email, emailMailboxList);
|
|
|
|
|
|
return emailInfo;
|
|
|
}
|
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public void listener(ListenerVo listenerVo) {
|
|
|
+
|
|
|
+ log.info("监听到新消息:{}", JSON.toJSONString(listenerVo));
|
|
|
+
|
|
|
+ switch (SendEventEnum.get(listenerVo.getEvent())) {
|
|
|
+ case MESSAGE_NEW:
|
|
|
+ this.handleMessageNewEvent(listenerVo);
|
|
|
+ break;
|
|
|
+
|
|
|
+
|
|
|
+ default:
|
|
|
+ log.error("监听到未知事件:{}", JSONObject.toJSONString(listenerVo));
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 添加邮件信息
|
|
|
*/
|
|
@@ -119,10 +142,8 @@ public class AccountServiceImpl implements IAccountService {
|
|
|
|
|
|
// 遍历每个邮箱
|
|
|
for (EmailMailbox emailMailbox : emailMailboxList) {
|
|
|
-
|
|
|
int page = 0;
|
|
|
int pages;
|
|
|
-
|
|
|
do {
|
|
|
MessageVo result = EmailEngineUtil.handleMessageList(email, emailMailbox.getPath(), page);
|
|
|
List<MessageVo.MessagesDTO> messagesDTOList = result.getMessages();
|
|
@@ -130,13 +151,10 @@ public class AccountServiceImpl implements IAccountService {
|
|
|
this.saveBatchMessage(email, lastMessageIdList, emailMailbox, messagesDTOList);
|
|
|
this.synchronizationProgress(email, messagesDTOList, emailMailbox);
|
|
|
}
|
|
|
-
|
|
|
pages = result.getPages();
|
|
|
page++;
|
|
|
} while (page < pages);
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -296,7 +314,6 @@ public class AccountServiceImpl implements IAccountService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 同步邮件进度初始化
|
|
|
*/
|
|
@@ -344,4 +361,46 @@ public class AccountServiceImpl implements IAccountService {
|
|
|
|
|
|
RedisCache.set(RedisConstant.PROGRESS_KEY + email, progressVo);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理收到新邮件事件
|
|
|
+ */
|
|
|
+ private void handleMessageNewEvent(ListenerVo listenerVo) {
|
|
|
+ // 查询邮箱
|
|
|
+ String account = listenerVo.getAccount();
|
|
|
+ String path = listenerVo.getPath();
|
|
|
+
|
|
|
+ EmailMailbox emailMailbox;
|
|
|
+ try {
|
|
|
+ emailMailbox = RetryUtil.execute(() -> {
|
|
|
+ EmailMailbox tempEmailMailbox = emailMailboxService.getOne(Wrappers.<EmailMailbox>lambdaQuery()
|
|
|
+ .eq(EmailMailbox::getPath, path)
|
|
|
+ .eq(EmailMailbox::getEmail, account)
|
|
|
+ );
|
|
|
+ Assert.notNull(tempEmailMailbox, "未找到邮箱信息");
|
|
|
+ return tempEmailMailbox;
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("查找邮箱失败", e);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取附件详情
|
|
|
+ MessageVo.MessagesDTO messagesDTO = listenerVo.getData().toJavaObject(MessageVo.MessagesDTO.class);
|
|
|
+
|
|
|
+ // 保存邮件
|
|
|
+ EmailMessage emailMessage = this.createMessage(account, emailMailbox, messagesDTO);
|
|
|
+ emailMessageService.save(emailMessage);
|
|
|
+
|
|
|
+ // 保存推送人信息
|
|
|
+ List<EmailMessageSend> emailMessageSendList = new ArrayList<>();
|
|
|
+ this.addMessageSend(emailMessageSendList, messagesDTO, emailMessage.getId());
|
|
|
+ emailMessageSendService.saveBatch(emailMessageSendList);
|
|
|
+
|
|
|
+ // 保存附件信息
|
|
|
+ List<EmailMessageAttachment> emailMessageAttachmentList = new ArrayList<>();
|
|
|
+ this.addMessageAttachment(account, emailMessageAttachmentList, messagesDTO, emailMessage.getId());
|
|
|
+ emailMessageAttachmentService.saveBatch(emailMessageAttachmentList);
|
|
|
+ }
|
|
|
+
|
|
|
}
|