ValuationEmailParser.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package com.simuwang.daq.service;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.collection.ListUtil;
  4. import cn.hutool.core.map.MapUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import com.simuwang.base.common.conts.EmailTypeConst;
  7. import com.simuwang.base.common.util.ExcelUtil;
  8. import com.simuwang.base.pojo.dto.EmailContentInfoDTO;
  9. import com.simuwang.base.pojo.dto.EmailFundNavDTO;
  10. import com.simuwang.base.pojo.dto.ValuationPdfTransformToExcelDTO;
  11. import com.simuwang.base.pojo.valuation.AssetsValuationResult;
  12. import com.simuwang.base.pojo.valuation.ParseValuationInfo;
  13. import com.simuwang.base.pojo.valuation.ValuationNeedParseParam;
  14. import org.springframework.stereotype.Component;
  15. import java.io.File;
  16. import java.util.*;
  17. import java.util.stream.Collectors;
  18. /**
  19. * @author mozuwen
  20. * @date 2024-09-12
  21. * @description 估值表邮件解析器
  22. */
  23. @Component
  24. public class ValuationEmailParser extends AbstractEmailParser {
  25. private final ValuationParseService valuationParseService;
  26. private final PdfToExcelService pdfToExcelService;
  27. public ValuationEmailParser(ValuationParseService valuationParseService, PdfToExcelService pdfToExcelService) {
  28. this.valuationParseService = valuationParseService;
  29. this.pdfToExcelService = pdfToExcelService;
  30. }
  31. @Override
  32. public boolean isSupport(Integer emailType) {
  33. return EmailTypeConst.VALUATION_EMAIL_TYPE.equals(emailType);
  34. }
  35. @Override
  36. public List<EmailFundNavDTO> parse(EmailContentInfoDTO emailContentInfoDTO, Map<String, List<String>> emailFieldMap) {
  37. List<EmailFundNavDTO> emailFundNavDTOList = CollUtil.newArrayList();
  38. boolean isSatisfiedParse = emailContentInfoDTO != null && StrUtil.isNotBlank(emailContentInfoDTO.getFileName())
  39. && (ExcelUtil.isExcel(emailContentInfoDTO.getFileName()) || ExcelUtil.isPdf(emailContentInfoDTO.getFileName()) || ExcelUtil.isZip(emailContentInfoDTO.getFileName()));
  40. if (!isSatisfiedParse) {
  41. return emailFundNavDTOList;
  42. }
  43. List<ValuationNeedParseParam> valuationNeedParseParams = buildValuationNeedParseParam(emailContentInfoDTO);
  44. List<AssetsValuationResult.Record> recordList = valuationParseService.parseValuationExcel(valuationNeedParseParams);
  45. if (CollUtil.isNotEmpty(recordList)) {
  46. List<AssetsValuationResult.Record> parseSuccessList = recordList.stream()
  47. .filter(e -> e.getSuccess() == 1 || (StrUtil.isNotBlank(e.getMsg()) && "未匹配基金".equals(e.getMsg()))).collect(Collectors.toList());
  48. List<EmailFundNavDTO> fundNavDTOList = convertToFundNavDTO(parseSuccessList);
  49. Optional.ofNullable(fundNavDTOList).ifPresent(emailFundNavDTOList::addAll);
  50. }
  51. return emailFundNavDTOList;
  52. }
  53. private List<ValuationNeedParseParam> buildValuationNeedParseParam(EmailContentInfoDTO emailContentInfoDTO) {
  54. String fileName = emailContentInfoDTO.getFileName();
  55. String filePath = emailContentInfoDTO.getFilePath();
  56. if (StrUtil.isNotBlank(fileName) && ExcelUtil.isZip(fileName)) {
  57. List<String> filePathList = compressedFile(filePath, fileName);
  58. if (CollUtil.isEmpty(filePathList)) {
  59. return CollUtil.newArrayList();
  60. }
  61. List<ValuationNeedParseParam> parseParamList = CollUtil.newArrayList();
  62. for (String path : filePathList) {
  63. ValuationNeedParseParam parseParam = new ValuationNeedParseParam();
  64. parseParam.setFile(new File(path));
  65. parseParam.setFileUrl(path);
  66. parseParam.setOriginFileName(fileName);
  67. parseParam.setFundId(null);
  68. parseParam.setFromEmail(1);
  69. // pdf格式文件转成excel
  70. transformPdfToExcel(parseParam);
  71. parseParamList.add(parseParam);
  72. }
  73. return parseParamList;
  74. } else {
  75. File file = new File(filePath);
  76. ValuationNeedParseParam parseParam = new ValuationNeedParseParam();
  77. parseParam.setFile(file);
  78. parseParam.setFileUrl(filePath);
  79. parseParam.setOriginFileName(fileName);
  80. parseParam.setFundId(null);
  81. parseParam.setFromEmail(1);
  82. // pdf格式文件转成excel
  83. transformPdfToExcel(parseParam);
  84. return ListUtil.toList(parseParam);
  85. }
  86. }
  87. private List<String> compressedFile(String filePath, String fileName) {
  88. List<String> filePathList = CollUtil.newArrayList();
  89. String destPath = filePath.substring(0, filePath.indexOf(fileName)) + fileName.replaceAll(".zip", "").replaceAll(".ZIP", "");
  90. List<String> compressedFiles = ExcelUtil.extractCompressedFiles(filePath, destPath);
  91. for (String path : compressedFiles) {
  92. boolean isSatisfiedParse = ExcelUtil.isExcel(path) || ExcelUtil.isPdf(path) || ExcelUtil.isZip(path);
  93. if (!isSatisfiedParse) {
  94. continue;
  95. }
  96. if (ExcelUtil.isZip(path)) {
  97. filePathList.addAll(compressedFile(path, new File(path).getName()));
  98. }
  99. File file = new File(path);
  100. if (file.isDirectory()) {
  101. filePathList.addAll(Arrays.stream(Objects.requireNonNull(file.list())).toList());
  102. }
  103. filePathList.add(path);
  104. }
  105. return filePathList;
  106. }
  107. private void transformPdfToExcel(ValuationNeedParseParam valuationNeedParseParam) {
  108. String originFileName = valuationNeedParseParam.getOriginFileName();
  109. if (StrUtil.isNotBlank(originFileName) && !ExcelUtil.isPdf(originFileName)) {
  110. return;
  111. }
  112. File file = valuationNeedParseParam.getFile();
  113. valuationNeedParseParam.setFileUrl(file.getAbsolutePath());
  114. //将pdf文件转为excel
  115. List<ValuationPdfTransformToExcelDTO> toExcelDTOList = pdfToExcelService.pdfToExcel(ListUtil.toList(file));
  116. Map<String, ValuationPdfTransformToExcelDTO> pdfToExcelMap = MapUtil.newHashMap();
  117. if (CollUtil.isNotEmpty(toExcelDTOList)) {
  118. pdfToExcelMap = toExcelDTOList.stream().collect(Collectors.toMap(ValuationPdfTransformToExcelDTO::getOriginalFileName, v -> v));
  119. }
  120. ValuationPdfTransformToExcelDTO toExcelDTO = pdfToExcelMap.get(originFileName);
  121. valuationNeedParseParam.setFile(toExcelDTO != null ? toExcelDTO.getExcelFile() : null);
  122. }
  123. private List<EmailFundNavDTO> convertToFundNavDTO(List<AssetsValuationResult.Record> parseSuccessList) {
  124. if (CollUtil.isEmpty(parseSuccessList)) {
  125. return null;
  126. }
  127. List<EmailFundNavDTO> emailFundNavDTOList = CollUtil.newArrayList();
  128. // 考虑一个估值表匹配到多个基金的情况
  129. for (AssetsValuationResult.Record record : parseSuccessList) {
  130. EmailFundNavDTO fundNavDTO = new EmailFundNavDTO();
  131. fundNavDTO.setFundName(record.getParseValuationInfo() != null ? record.getParseValuationInfo().getFundName() : null);
  132. fundNavDTO.setRegisterNumber(record.getParseValuationInfo() != null ? record.getParseValuationInfo().getRegisterNumber() : null);
  133. fundNavDTO.setPriceDate(record.getDate());
  134. fundNavDTO.setNav(record.getNav());
  135. fundNavDTO.setCumulativeNavWithdrawal(record.getCumulativeNavWithdrawal());
  136. fundNavDTO.setAssetNet(record.getAssetNet());
  137. fundNavDTO.setAssetShare(record.getAssetShare());
  138. fundNavDTO.setFundIdList(StrUtil.isNotBlank(record.getFundId()) ? ListUtil.toList(record.getFundId()) : null);
  139. emailFundNavDTOList.add(fundNavDTO);
  140. }
  141. return emailFundNavDTOList;
  142. }
  143. }