EmailParseApiServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package com.smppw.modaq.application.service;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.exceptions.ExceptionUtil;
  5. import cn.hutool.core.map.MapUtil;
  6. import cn.hutool.core.util.StrUtil;
  7. import com.smppw.modaq.application.util.EmailUtil;
  8. import com.smppw.modaq.common.conts.DateConst;
  9. import com.smppw.modaq.domain.dto.EmailContentInfoDTO;
  10. import com.smppw.modaq.domain.dto.EmailZipFileDTO;
  11. import com.smppw.modaq.domain.dto.MailboxInfoDTO;
  12. import com.smppw.modaq.domain.entity.EmailFileInfoDO;
  13. import com.smppw.modaq.domain.entity.EmailParseInfoDO;
  14. import com.smppw.modaq.domain.entity.MailboxInfoDO;
  15. import com.smppw.modaq.domain.mapper.EmailFileInfoMapper;
  16. import com.smppw.modaq.domain.mapper.EmailParseInfoMapper;
  17. import com.smppw.modaq.domain.mapper.MailboxInfoMapper;
  18. import com.smppw.modaq.domain.service.EmailParseService;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import org.springframework.beans.factory.annotation.Qualifier;
  22. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  23. import org.springframework.stereotype.Service;
  24. import java.io.BufferedReader;
  25. import java.io.FileReader;
  26. import java.io.IOException;
  27. import java.util.Date;
  28. import java.util.List;
  29. import java.util.Map;
  30. /**
  31. * @author mozuwen
  32. * @date 2024-09-12
  33. * @description 邮件解析服务对外接口实现层
  34. */
  35. @Service
  36. public class EmailParseApiServiceImpl implements EmailParseApiService {
  37. private static final Logger log = LoggerFactory.getLogger(EmailParseApiServiceImpl.class);
  38. private final MailboxInfoMapper mailboxInfoMapper;
  39. private final EmailParseService emailParseService;
  40. private final EmailParseInfoMapper emailParseInfoMapper;
  41. private final EmailFileInfoMapper emailFileInfoMapper;
  42. private final ThreadPoolTaskExecutor asyncExecutor;
  43. // private final EmailTaskInfoMapper emailTaskInfoMapper;
  44. public EmailParseApiServiceImpl(MailboxInfoMapper mailboxInfoMapper,
  45. EmailParseService emailParseService,
  46. EmailParseInfoMapper emailParseInfoMapper,
  47. EmailFileInfoMapper emailFileInfoMapper,
  48. @Qualifier("asyncExecutor") ThreadPoolTaskExecutor asyncExecutor) {
  49. this.mailboxInfoMapper = mailboxInfoMapper;
  50. this.emailParseService = emailParseService;
  51. this.emailParseInfoMapper = emailParseInfoMapper;
  52. this.emailFileInfoMapper = emailFileInfoMapper;
  53. this.asyncExecutor = asyncExecutor;
  54. }
  55. @Override
  56. public void parseEmail(Date startDate, Date endDate, List<String> folderNames, List<Integer> emailTypes) {
  57. List<MailboxInfoDO> mailboxList = this.mailboxInfoMapper.listMailboxInfo();
  58. for (MailboxInfoDO mailbox : mailboxList) {
  59. MailboxInfoDTO paramDTO = new MailboxInfoDTO();
  60. paramDTO.setAccount(mailbox.getEmail());
  61. paramDTO.setPassword(mailbox.getPassword());
  62. paramDTO.setPort(mailbox.getPort());
  63. paramDTO.setHost(mailbox.getServer());
  64. paramDTO.setProtocol(mailbox.getProtocol());
  65. this.parseEmail(paramDTO, startDate, endDate, folderNames, emailTypes);
  66. }
  67. }
  68. @Override
  69. public void parseEmail(MailboxInfoDTO mailboxInfoDTO, Date startDate, Date endDate, List<String> folderNames, List<Integer> emailTypes) {
  70. // Integer userId = null;
  71. // try{
  72. // if(UserUtils.getPrincipal() == null){
  73. // userId = 1;
  74. // }else{
  75. // userId = UserUtils.getLoginUser().getUserId();
  76. // }
  77. // }catch (Exception e){
  78. // log.error(e.getMessage());
  79. // }
  80. // Integer finalUserId = userId;
  81. // asyncExecutor.execute(() -> {
  82. //// EmailTaskInfoDO emailTaskInfoDO = startEmailTask(mailboxInfoDTO.getAccount(), 1, finalUserId);
  83. // try {
  84. //
  85. // } catch (Exception e) {
  86. // log.error(e.getMessage(), e);
  87. //// endEmailTask(emailTaskInfoDO.getId(),-1);
  88. //// return;
  89. // }
  90. //// endEmailTask(emailTaskInfoDO.getId(),2);
  91. // }
  92. // );
  93. emailParseService.parseEmail(mailboxInfoDTO, startDate, endDate, folderNames, emailTypes);
  94. }
  95. // private void endEmailTask(Integer id,Integer taskStatus) {
  96. // try{
  97. // EmailTaskInfoDO emailTaskInfoDO = new EmailTaskInfoDO();
  98. // emailTaskInfoDO.setId(id);
  99. // emailTaskInfoDO.setTaskStatus(taskStatus);
  100. // emailTaskInfoDO.setUpdateTime(DateUtils.getNowDate());
  101. // emailTaskInfoDO.setEndTime(DateUtils.getNowDate());
  102. // emailTaskInfoMapper.updateTaskStatusById(emailTaskInfoDO);
  103. // }catch (Exception e){
  104. // log.error(e.getMessage());
  105. // }
  106. // }
  107. //
  108. // private EmailTaskInfoDO startEmailTask(String email,Integer taskStatus,Integer userId) {
  109. // EmailTaskInfoDO emailTaskInfoDO = new EmailTaskInfoDO();
  110. // try{
  111. // emailTaskInfoDO.setTaskName(TaskType.EMAIL_PARSE.getInfo());
  112. // emailTaskInfoDO.setTaskType(TaskType.EMAIL_PARSE.getType());
  113. // emailTaskInfoDO.setTaskStatus(taskStatus);
  114. // emailTaskInfoDO.setStartTime(DateUtils.getNowDate());
  115. // emailTaskInfoDO.setIsvalid(1);
  116. // emailTaskInfoDO.setEmail(email);
  117. // emailTaskInfoDO.setCreateTime(DateUtils.getNowDate());
  118. // emailTaskInfoDO.setUpdateTime(DateUtils.getNowDate());
  119. // emailTaskInfoDO.setCreatorId(userId);
  120. // emailTaskInfoDO.setUpdaterId(userId);
  121. // emailTaskInfoMapper.insert(emailTaskInfoDO);
  122. // }catch (Exception e){
  123. // log.error(e.getMessage());
  124. // }
  125. // return emailTaskInfoDO;
  126. // }
  127. @Override
  128. public void reparseEmail(Integer emailId) {
  129. // 查询邮件信息
  130. EmailParseInfoDO emailParseInfoDO = emailParseInfoMapper.queryById(emailId);
  131. if (emailParseInfoDO == null) {
  132. log.info("邮件不存在 ->邮件id:{}", emailId);
  133. return;
  134. }
  135. //解析成功的邮件不再解析
  136. if (emailParseInfoDO.getParseStatus() == 1) {
  137. log.info("邮件解析状态为成功,不再解析 ->邮件id:{}", emailId);
  138. return;
  139. }
  140. List<EmailFileInfoDO> emailFileInfoDOList = emailFileInfoMapper.queryByEmailId(emailId);
  141. if (CollUtil.isEmpty(emailFileInfoDOList)) {
  142. log.info("该邮件不存在附件 -> 邮件id:{}", emailId);
  143. return;
  144. }
  145. // // 邮件字段识别映射表
  146. // Map<String, List<String>> emailFieldMap = emailParseService.getEmailFieldMapping();
  147. // // 邮件类型配置
  148. // Map<Integer, List<String>> emailTypeMap = emailParseService.getEmailType();
  149. // 解析流程
  150. List<EmailContentInfoDTO> emailContentInfoDTOList = buildEmailContentInfoDTO(emailId, emailParseInfoDO, emailFileInfoDOList);
  151. // List<EmailFundNavDTO> emailFundNavDTOList = CollUtil.newArrayList();
  152. Map<EmailContentInfoDTO, List<EmailZipFileDTO>> emailZipFileMap = MapUtil.newHashMap();
  153. asyncExecutor.execute(() -> {
  154. for (EmailContentInfoDTO emailContentInfoDTO : emailContentInfoDTOList) {
  155. try {
  156. List<EmailZipFileDTO> emailZipFiles = emailParseService.parseZipEmail(emailContentInfoDTO);
  157. emailZipFileMap.put(emailContentInfoDTO, emailZipFiles);
  158. // emailFundNavDTOList.addAll(fundNavDTOList);
  159. } catch (Exception e) {
  160. log.error("重新解析邮件失败,邮件id:{},堆栈信息:{}", emailId, ExceptionUtil.stacktraceToString(e));
  161. }
  162. }
  163. // 保存相关信息 -> 邮件信息表,邮件文件表,邮件净值表,邮件规模表,基金净值表
  164. emailParseService.saveRelatedTable(null, emailParseInfoDO.getEmail(), emailZipFileMap);
  165. });
  166. }
  167. //
  168. // @Override
  169. // public void reparseFile(List<Integer> fileIdList) {
  170. // if (CollUtil.isEmpty(fileIdList)) {
  171. // return;
  172. // }
  173. // List<EmailInfoDTO> emailParseInfoDOList = emailParseInfoMapper.queryValuationEmailByFileId(fileIdList);
  174. // if (CollUtil.isEmpty(emailParseInfoDOList)) {
  175. // return;
  176. // }
  177. // asyncExecutor.execute(() -> {
  178. // Map<Integer, List<EmailInfoDTO>> emailIdFileMap = emailParseInfoDOList.stream().collect(Collectors.groupingBy(EmailInfoDTO::getId));
  179. // for (Map.Entry<Integer, List<EmailInfoDTO>> entry : emailIdFileMap.entrySet()) {
  180. // Integer emailId = entry.getKey();
  181. // List<EmailInfoDTO> emailInfoDTOList = entry.getValue();
  182. // String emailAddress = emailInfoDTOList.get(0).getEmail();
  183. // List<EmailContentInfoDTO> emailContentInfoDTOList = emailInfoDTOList.stream().map(this::buildEmailContentInfoDTO).collect(Collectors.toList());
  184. //
  185. // List<EmailFundNavDTO> emailFundNavDTOList = CollUtil.newArrayList();
  186. // Map<EmailContentInfoDTO, List<EmailFundNavDTO>> fileNameNavMap = MapUtil.newHashMap();
  187. // for (EmailContentInfoDTO emailContentInfoDTO : emailContentInfoDTOList) {
  188. // try {
  189. // log.info("开始重新解析文件 -> 文件id:{}", emailContentInfoDTO.getFileId());
  190. // List<EmailFundNavDTO> fundNavDTOList = emailParseService.parseEmail(emailContentInfoDTO, MapUtil.newHashMap());
  191. // fileNameNavMap.put(emailContentInfoDTO, fundNavDTOList);
  192. // emailFundNavDTOList.addAll(fundNavDTOList);
  193. // } catch (Exception e) {
  194. // log.error("重新解析文件失败,邮件id:{},文件id:{},堆栈信息:{}", emailId, emailContentInfoDTO.getFileId(), ExceptionUtil.stacktraceToString(e));
  195. // }
  196. // }
  197. // // 保存相关信息 -> 邮件信息表,邮件文件表,邮件净值表,邮件规模表,基金净值表
  198. // emailParseService.saveRelatedTable(emailAddress, emailContentInfoDTOList, fileNameNavMap);
  199. // }
  200. // log.info("重新解析文件结束... -> 文件id:{}", fileIdList);
  201. // });
  202. // }
  203. // private EmailContentInfoDTO buildEmailContentInfoDTO(EmailInfoDTO emailInfoDTO) {
  204. // String emailDate = DateUtils.format(emailInfoDTO.getEmailDate(), DateConst.YYYY_MM_DD_HH_MM_SS);
  205. // String parseDate = DateUtils.format(new Date(), DateConst.YYYY_MM_DD_HH_MM_SS);
  206. // EmailContentInfoDTO contentInfoDTO = new EmailContentInfoDTO();
  207. // contentInfoDTO.setEmailId(emailInfoDTO.getId());
  208. // contentInfoDTO.setFileId(emailInfoDTO.getFileId());
  209. // contentInfoDTO.setEmailAddress(emailInfoDTO.getEmail());
  210. // contentInfoDTO.setEmailDate(emailDate);
  211. // contentInfoDTO.setEmailTitle(emailInfoDTO.getEmailTitle());
  212. // contentInfoDTO.setParseDate(parseDate);
  213. // contentInfoDTO.setFileName(emailInfoDTO.getFileName());
  214. // contentInfoDTO.setFilePath(emailInfoDTO.getFilePath());
  215. // contentInfoDTO.setEmailType(emailInfoDTO.getEmailType());
  216. // return contentInfoDTO;
  217. // }
  218. private List<EmailContentInfoDTO> buildEmailContentInfoDTO(Integer emailId,
  219. EmailParseInfoDO emailParseInfoDO,
  220. List<EmailFileInfoDO> emailFileInfoDOList) {
  221. List<EmailContentInfoDTO> emailContentInfoDTOList = CollUtil.newArrayList();
  222. String emailDate = DateUtil.format(emailParseInfoDO.getEmailDate(), DateConst.YYYY_MM_DD_HH_MM_SS);
  223. String parseDate = DateUtil.format(new Date(), DateConst.YYYY_MM_DD_HH_MM_SS);
  224. for (EmailFileInfoDO fileInfoDO : emailFileInfoDOList) {
  225. EmailContentInfoDTO contentInfoDTO = new EmailContentInfoDTO();
  226. contentInfoDTO.setEmailId(emailId);
  227. contentInfoDTO.setFileId(fileInfoDO.getId());
  228. contentInfoDTO.setSenderEmail(emailParseInfoDO.getSenderEmail());
  229. contentInfoDTO.setEmailAddress(emailParseInfoDO.getEmail());
  230. contentInfoDTO.setEmailDate(emailDate);
  231. contentInfoDTO.setEmailTitle(emailParseInfoDO.getEmailTitle());
  232. contentInfoDTO.setParseDate(parseDate);
  233. contentInfoDTO.setFileName(fileInfoDO.getFileName());
  234. contentInfoDTO.setFilePath(fileInfoDO.getFilePath());
  235. Integer emailType = EmailUtil.getEmailTypeBySubject(emailParseInfoDO.getEmailTitle());
  236. contentInfoDTO.setEmailType(emailType);
  237. String emailContent = readHtmlFileContent(fileInfoDO.getFilePath());
  238. contentInfoDTO.setEmailContent(emailContent);
  239. contentInfoDTO.setAiFileId(fileInfoDO.getAiFileId());
  240. emailContentInfoDTOList.add(contentInfoDTO);
  241. }
  242. return emailContentInfoDTOList;
  243. }
  244. public static String readHtmlFileContent(String filePath) {
  245. if (StrUtil.isNotBlank(filePath) && filePath.endsWith("html")) {
  246. StringBuilder content = new StringBuilder();
  247. try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
  248. String line;
  249. while ((line = reader.readLine()) != null) {
  250. // 追加每一行到StringBuilder中
  251. content.append(line).append("\n");
  252. }
  253. } catch (IOException e) {
  254. System.err.println("Error reading the file: " + e.getMessage());
  255. return null;
  256. }
  257. return content.toString();
  258. }
  259. return null;
  260. }
  261. }