EmailParseApiServiceImpl.java 13 KB

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