|
@@ -92,17 +92,21 @@ public class EmailParseService {
|
|
|
* @param mailboxInfoDTO 邮箱配置信息
|
|
|
* @param startDate 邮件起始日期(yyyy-MM-dd HH:mm:ss)
|
|
|
* @param endDate 邮件截止日期(yyyy-MM-dd HH:mm:ss, 为null,将解析邮件日期小于等于startDate的当天邮件)
|
|
|
+ * @param emailTypes 当前任务支持的邮件类型,默认支持确认单
|
|
|
*/
|
|
|
- public void parseEmail(MailboxInfoDTO mailboxInfoDTO, Date startDate, Date endDate) {
|
|
|
+ public void parseEmail(MailboxInfoDTO mailboxInfoDTO,
|
|
|
+ Date startDate, Date endDate,
|
|
|
+ List<String> folderNames, List<Integer> emailTypes) {
|
|
|
+ if (CollUtil.isEmpty(emailTypes)) {
|
|
|
+ emailTypes = ListUtil.of(EmailTypeConst.REPORT_LETTER_EMAIL_TYPE);
|
|
|
+ }
|
|
|
log.info("开始邮件解析 -> 邮箱信息:{},开始时间:{},结束时间:{}", mailboxInfoDTO, DateUtil.format(startDate,
|
|
|
DateConst.YYYY_MM_DD_HH_MM_SS), DateUtil.format(endDate, DateConst.YYYY_MM_DD_HH_MM_SS));
|
|
|
// 邮件类型配置
|
|
|
Map<Integer, List<String>> emailTypeMap = getEmailType();
|
|
|
- // 邮件字段识别映射表
|
|
|
-// Map<String, List<String>> emailFieldMap = getEmailFieldMapping();
|
|
|
Map<String, List<EmailContentInfoDTO>> emailContentMap;
|
|
|
try {
|
|
|
- emailContentMap = realEmail(mailboxInfoDTO, emailTypeMap, startDate, endDate);
|
|
|
+ emailContentMap = realEmail(mailboxInfoDTO, emailTypeMap, startDate, endDate, folderNames);
|
|
|
} catch (Exception e) {
|
|
|
log.error("采集邮件失败 -> 邮箱配置信息:{},堆栈信息:{}", mailboxInfoDTO, ExceptionUtil.stacktraceToString(e));
|
|
|
return;
|
|
@@ -120,9 +124,7 @@ public class EmailParseService {
|
|
|
}
|
|
|
log.info("开始解析邮件数据 -> 邮件主题:{},邮件日期:{}", emailContentInfoDTOList.get(0).getEmailTitle(), emailContentInfoDTOList.get(0).getEmailDate());
|
|
|
Map<EmailContentInfoDTO, List<EmailZipFileDTO>> emailZipFileMap = MapUtil.newHashMap();
|
|
|
- Iterator<EmailContentInfoDTO> iterator = emailContentInfoDTOList.iterator();
|
|
|
- while (iterator.hasNext()) {
|
|
|
- EmailContentInfoDTO emailContentInfoDTO = iterator.next();
|
|
|
+ for (EmailContentInfoDTO emailContentInfoDTO : emailContentInfoDTOList) {
|
|
|
// 正文不用解压附件
|
|
|
if (emailContentInfoDTO.getFileName() != null && emailContentInfoDTO.getFileName().endsWith(".html")) {
|
|
|
emailZipFileMap.put(emailContentInfoDTO, ListUtil.empty());
|
|
@@ -138,11 +140,37 @@ public class EmailParseService {
|
|
|
fail.setParseStatus(EmailParseStatusConst.FAIL);
|
|
|
fail.setEmailKey(emailEntry.getKey());
|
|
|
this.emailParseInfoMapper.insert(fail);
|
|
|
- iterator.remove();
|
|
|
} catch (Exception e) {
|
|
|
log.error("堆栈信息:{}", ExceptionUtil.stacktraceToString(e));
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ Iterator<Map.Entry<EmailContentInfoDTO, List<EmailZipFileDTO>>> entryIterator = emailZipFileMap.entrySet().iterator();
|
|
|
+ while (entryIterator.hasNext()) {
|
|
|
+ Map.Entry<EmailContentInfoDTO, List<EmailZipFileDTO>> entry = entryIterator.next();
|
|
|
+ EmailContentInfoDTO key = entry.getKey();
|
|
|
+ List<EmailZipFileDTO> dtos = entry.getValue();
|
|
|
+
|
|
|
+ List<Integer> types = ListUtil.list(false);
|
|
|
+ types.add(key.getEmailType());
|
|
|
+ if (CollUtil.isNotEmpty(dtos)) {
|
|
|
+ List<Integer> list = dtos.stream().map(EmailZipFileDTO::getEmailType).distinct().toList();
|
|
|
+ types.addAll(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean flag = false;
|
|
|
+ for (Integer type : types) {
|
|
|
+ if (emailTypes.contains(type)) {
|
|
|
+ flag = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!flag) {
|
|
|
+ log.warn("当前邮件{} 的类型{} 不在支持的任务类型{} 中,不用执行解析逻辑。", key, types, emailTypes);
|
|
|
+ entryIterator.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// 保存相关信息 -> 邮件信息表,邮件文件表,邮件净值表,邮件规模表,基金净值表
|
|
|
saveRelatedTable(emailEntry.getKey(), mailboxInfoDTO.getAccount(), emailZipFileMap);
|
|
|
log.info("结束邮件解析 -> 邮箱信息:{},开始时间:{},结束时间:{}", mailboxInfoDTO,
|
|
@@ -485,17 +513,55 @@ public class EmailParseService {
|
|
|
* @throws Exception 异常信息
|
|
|
*/
|
|
|
private Map<String, List<EmailContentInfoDTO>> realEmail(MailboxInfoDTO mailboxInfoDTO,
|
|
|
- Map<Integer, List<String>> emailTypeMap, Date startDate, Date endDate) throws Exception {
|
|
|
+ Map<Integer, List<String>> emailTypeMap,
|
|
|
+ Date startDate, Date endDate,
|
|
|
+ List<String> folderNames) throws Exception {
|
|
|
+ if (CollUtil.isEmpty(folderNames)) {
|
|
|
+ folderNames.add("INBOX");
|
|
|
+ }
|
|
|
Store store = EmailUtil.getStoreNew(mailboxInfoDTO);
|
|
|
if (store == null) {
|
|
|
- return MapUtil.newHashMap();
|
|
|
+ return MapUtil.newHashMap(4);
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, List<EmailContentInfoDTO>> result = MapUtil.newHashMap(128);
|
|
|
+ try {
|
|
|
+ if (log.isInfoEnabled()) {
|
|
|
+ Folder[] list = store.getDefaultFolder().list("*");
|
|
|
+ List<String> names = Arrays.stream(list).map(Folder::getFullName).toList();
|
|
|
+ log.info("获取所有邮箱文件夹:{}", names);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (String folderName : folderNames) {
|
|
|
+ try {
|
|
|
+ Map<String, List<EmailContentInfoDTO>> temp = this.getFolderEmail(mailboxInfoDTO, emailTypeMap,
|
|
|
+ startDate, endDate, store, folderName);
|
|
|
+ if (MapUtil.isNotEmpty(temp)) {
|
|
|
+ result.putAll(temp);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("文件夹{} 邮件获取失败:{}", folderName, ExceptionUtil.stacktraceToString(e));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("邮件获取失败:{}", ExceptionUtil.stacktraceToString(e));
|
|
|
+ } finally {
|
|
|
+ store.close();
|
|
|
}
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, List<EmailContentInfoDTO>> getFolderEmail(MailboxInfoDTO mailboxInfoDTO,
|
|
|
+ Map<Integer, List<String>> emailTypeMap,
|
|
|
+ Date startDate, Date endDate,
|
|
|
+ Store store, String folderName) throws MessagingException {
|
|
|
// 默认读取收件箱的邮件
|
|
|
- Folder folder = store.getFolder("INBOX");
|
|
|
+ Folder folder = store.getFolder(folderName);
|
|
|
folder.open(Folder.READ_ONLY);
|
|
|
Message[] messages = getEmailMessage(folder, mailboxInfoDTO.getProtocol(), startDate);
|
|
|
if (messages == null || messages.length == 0) {
|
|
|
- log.warn("获取不到邮件 -> 邮箱信息:{},开始时间:{},结束时间:{}", mailboxInfoDTO, startDate, endDate);
|
|
|
+ log.warn("{} 获取不到邮件 -> 邮箱信息:{},开始时间:{},结束时间:{}", folderName, mailboxInfoDTO, startDate, endDate);
|
|
|
return MapUtil.newHashMap();
|
|
|
}
|
|
|
Map<String, List<EmailContentInfoDTO>> emailMessageMap = MapUtil.newHashMap();
|
|
@@ -511,23 +577,23 @@ public class EmailParseService {
|
|
|
Date emailDate = message.getSentDate();
|
|
|
String emailDateStr = DateUtil.format(emailDate, DateConst.YYYY_MM_DD_HH_MM_SS);
|
|
|
if (log.isInfoEnabled()) {
|
|
|
- log.info("邮件{} 数据获取中,邮件时间:{}", emailTitle, emailDateStr);
|
|
|
+ log.info("{} 邮件{} 数据获取中,邮件时间:{}", folderName, emailTitle, emailDateStr);
|
|
|
}
|
|
|
|
|
|
boolean isNotParseConditionSatisfied = emailDate == null
|
|
|
|| (endDate != null && emailDate.compareTo(endDate) > 0)
|
|
|
|| (startDate != null && emailDate.compareTo(startDate) < 0);
|
|
|
if (isNotParseConditionSatisfied) {
|
|
|
- log.warn("邮件{} 没有日期{} 或者 邮件日期不在区间内【{} ~ {}】", emailTitle, emailDate, startDate, endDate);
|
|
|
+ log.warn("{} 邮件{} 没有日期{} 或者 邮件日期不在区间内【{} ~ {}】", folderName, emailTitle, emailDate, startDate, endDate);
|
|
|
continue;
|
|
|
}
|
|
|
senderEmail = getSenderEmail(message);
|
|
|
emailType = EmailUtil.getEmailTypeBySubject(emailTitle, emailTypeMap);
|
|
|
if (emailType == null) {
|
|
|
- log.warn("邮件不满足解析条件 -> 邮件主题:{},邮件日期:{}", emailTitle, emailDateStr);
|
|
|
+ log.warn("{} 邮件不满足解析条件 -> 邮件主题:{},邮件日期:{}", folderName, emailTitle, emailDateStr);
|
|
|
continue;
|
|
|
}
|
|
|
- log.info("邮件{} 基本信息获取完成,开始下载附件!邮件日期:{}", emailTitle, emailDateStr);
|
|
|
+ log.info("{} 邮件{} 基本信息获取完成,开始下载附件!邮件日期:{}", folderName, emailTitle, emailDateStr);
|
|
|
Object content = message.getContent();
|
|
|
|
|
|
if (content instanceof Multipart multipart) {
|
|
@@ -535,7 +601,7 @@ public class EmailParseService {
|
|
|
} else if (content instanceof Part part) {
|
|
|
this.rePart(mailboxInfoDTO.getAccount(), emailTitle, emailDate, part, emailContentInfoDTOList);
|
|
|
} else {
|
|
|
- log.warn("不支持的邮件数据 {}", emailTitle);
|
|
|
+ log.warn("{} 不支持的邮件数据 {}", folderName, emailTitle);
|
|
|
}
|
|
|
|
|
|
if (CollUtil.isNotEmpty(emailContentInfoDTOList)) {
|
|
@@ -546,15 +612,14 @@ public class EmailParseService {
|
|
|
emailMessageMap.put(uuidKey, emailContentInfoDTOList);
|
|
|
}
|
|
|
if (log.isInfoEnabled() && emailTitle != null) {
|
|
|
- log.info("邮件{} 下载完成,总计耗时{} ms,文件内容如下\n {}",
|
|
|
+ log.info("{} 邮件{} 下载完成,总计耗时{} ms,文件内容如下\n {}", folderName,
|
|
|
emailTitle, System.currentTimeMillis() - start, emailContentInfoDTOList);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- log.error("获取邮箱的邮件{} 报错,堆栈信息:{}", emailTitle, ExceptionUtil.stacktraceToString(e));
|
|
|
+ log.error("{} 获取邮箱的邮件{} 报错,堆栈信息:{}", folderName, emailTitle, ExceptionUtil.stacktraceToString(e));
|
|
|
}
|
|
|
}
|
|
|
folder.close(false);
|
|
|
- store.close();
|
|
|
return emailMessageMap;
|
|
|
}
|
|
|
|