|
@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
|
|
import cn.hutool.core.collection.ListUtil;
|
|
import cn.hutool.core.collection.ListUtil;
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.exceptions.ExceptionUtil;
|
|
import cn.hutool.core.exceptions.ExceptionUtil;
|
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.map.MapUtil;
|
|
import cn.hutool.core.map.MapUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import com.smppw.modaq.application.components.ReportParseUtils;
|
|
import com.smppw.modaq.application.components.ReportParseUtils;
|
|
@@ -31,7 +32,6 @@ import com.smppw.modaq.domain.entity.EmailParseInfoDO;
|
|
import com.smppw.modaq.domain.mapper.EmailFileInfoMapper;
|
|
import com.smppw.modaq.domain.mapper.EmailFileInfoMapper;
|
|
import com.smppw.modaq.domain.mapper.EmailParseInfoMapper;
|
|
import com.smppw.modaq.domain.mapper.EmailParseInfoMapper;
|
|
import com.smppw.modaq.infrastructure.util.ExcelUtil;
|
|
import com.smppw.modaq.infrastructure.util.ExcelUtil;
|
|
-import com.smppw.modaq.infrastructure.util.FileUtil;
|
|
|
|
import jakarta.mail.*;
|
|
import jakarta.mail.*;
|
|
import jakarta.mail.internet.MimeUtility;
|
|
import jakarta.mail.internet.MimeUtility;
|
|
import jakarta.mail.search.ComparisonTerm;
|
|
import jakarta.mail.search.ComparisonTerm;
|
|
@@ -44,8 +44,8 @@ import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.util.StopWatch;
|
|
import org.springframework.util.StopWatch;
|
|
|
|
|
|
-import java.io.File;
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
|
+import java.io.*;
|
|
|
|
+import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.nio.file.Paths;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
@@ -126,6 +126,50 @@ public class EmailParseService {
|
|
DateUtil.format(startDate, DateConst.YYYY_MM_DD_HH_MM_SS), DateUtil.format(endDate, DateConst.YYYY_MM_DD_HH_MM_SS));
|
|
DateUtil.format(startDate, DateConst.YYYY_MM_DD_HH_MM_SS), DateUtil.format(endDate, DateConst.YYYY_MM_DD_HH_MM_SS));
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // 有.z01的压缩包说明是单独发送的附件来提供的解密包
|
|
|
|
+ List<EmailContentInfoDTO> emailFileInfoList = emailContentMap.values().stream().flatMap(Collection::stream).toList();
|
|
|
|
+ for (Map.Entry<String, List<EmailContentInfoDTO>> next : emailContentMap.entrySet()) {
|
|
|
|
+ List<EmailContentInfoDTO> dtos = next.getValue();
|
|
|
|
+ if (CollUtil.isEmpty(dtos) || dtos.size() != 1) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ EmailContentInfoDTO dto = dtos.get(0);
|
|
|
|
+ if (!"z01".equals(FileUtil.extName(dto.getFileName()))) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ EmailContentInfoDTO zipDto = emailFileInfoList.stream()
|
|
|
|
+ .filter(e -> StrUtil.equals(e.getEmailTitle(), dto.getEmailTitle()))
|
|
|
|
+ .filter(e -> StrUtil.equals(e.getSenderEmail(), dto.getSenderEmail()))
|
|
|
|
+ .filter(e -> StrUtil.equals(e.getEmailAddress(), dto.getEmailAddress()))
|
|
|
|
+ .filter(e -> !e.getFileName().equals(dto.getFileName()))
|
|
|
|
+ .filter(e -> e.getFileName().startsWith(FileUtil.mainName(dto.getFileName()))).findFirst().orElse(null);
|
|
|
|
+ if (zipDto == null) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 分卷合并
|
|
|
|
+ List<String> volumePath = ListUtil.list(true);
|
|
|
|
+ volumePath.add(dto.getFilePath());
|
|
|
|
+ volumePath.add(zipDto.getFilePath());
|
|
|
|
+ String mergePath = StrUtil.subBefore(zipDto.getFilePath(), ".zip", true) + "_merge.zip";
|
|
|
|
+ try (OutputStream mergedOutput = new BufferedOutputStream(new FileOutputStream(mergePath))) {
|
|
|
|
+ for (String volume : volumePath) {
|
|
|
|
+ try (InputStream input = new BufferedInputStream(new FileInputStream(volume))) {
|
|
|
|
+ byte[] buffer = new byte[1024 * 1024]; // 使用更大的缓冲区
|
|
|
|
+ int bytesRead;
|
|
|
|
+ while ((bytesRead = input.read(buffer)) != -1) {
|
|
|
|
+ mergedOutput.write(buffer, 0, bytesRead);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.error("邮件{} 分卷合并异常:{}", dto.getEmailTitle(), e.getMessage());
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ FileUtil.rename(FileUtil.file(mergePath), zipDto.getFilePath(), true);
|
|
|
|
+ }
|
|
|
|
+
|
|
for (Map.Entry<String, List<EmailContentInfoDTO>> emailEntry : emailContentMap.entrySet()) {
|
|
for (Map.Entry<String, List<EmailContentInfoDTO>> emailEntry : emailContentMap.entrySet()) {
|
|
List<EmailContentInfoDTO> emailContentInfoDTOList = emailEntry.getValue();
|
|
List<EmailContentInfoDTO> emailContentInfoDTOList = emailEntry.getValue();
|
|
if (CollUtil.isEmpty(emailContentInfoDTOList)) {
|
|
if (CollUtil.isEmpty(emailContentInfoDTOList)) {
|
|
@@ -141,7 +185,7 @@ public class EmailParseService {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
try {
|
|
try {
|
|
- List<EmailZipFileDTO> fundNavDTOList = parseZipEmail(emailContentInfoDTO);
|
|
|
|
|
|
+ List<EmailZipFileDTO> fundNavDTOList = this.parseZipEmail(emailContentInfoDTO);
|
|
emailZipFileMap.put(emailContentInfoDTO, fundNavDTOList);
|
|
emailZipFileMap.put(emailContentInfoDTO, fundNavDTOList);
|
|
} catch (IOException | ArchiveException e) {
|
|
} catch (IOException | ArchiveException e) {
|
|
log.error("压缩包解压失败:{}", ExceptionUtil.stacktraceToString(e));
|
|
log.error("压缩包解压失败:{}", ExceptionUtil.stacktraceToString(e));
|
|
@@ -198,16 +242,16 @@ public class EmailParseService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public List<EmailZipFileDTO> parseZipEmail(EmailContentInfoDTO emailContentInfoDTO) throws Exception {
|
|
|
|
|
|
+ private List<EmailZipFileDTO> parseZipEmail(EmailContentInfoDTO emailContentInfoDTO) throws Exception {
|
|
List<EmailZipFileDTO> resultList = ListUtil.list(false);
|
|
List<EmailZipFileDTO> resultList = ListUtil.list(false);
|
|
Integer emailType = emailContentInfoDTO.getEmailType();
|
|
Integer emailType = emailContentInfoDTO.getEmailType();
|
|
String filepath = emailContentInfoDTO.getFilePath();
|
|
String filepath = emailContentInfoDTO.getFilePath();
|
|
String emailTitle = emailContentInfoDTO.getEmailTitle();
|
|
String emailTitle = emailContentInfoDTO.getEmailTitle();
|
|
|
|
|
|
if (ExcelUtil.isZip(filepath)) {
|
|
if (ExcelUtil.isZip(filepath)) {
|
|
- handleCompressedFiles(emailTitle, filepath, ".zip", emailType, resultList);
|
|
|
|
|
|
+ this.handleCompressedFiles(emailTitle, filepath, ".zip", emailType, resultList);
|
|
} else if (ExcelUtil.isRAR(filepath)) {
|
|
} else if (ExcelUtil.isRAR(filepath)) {
|
|
- handleCompressedFiles(emailTitle, filepath, ".rar", emailType, resultList);
|
|
|
|
|
|
+ this.handleCompressedFiles(emailTitle, filepath, ".rar", emailType, resultList);
|
|
}
|
|
}
|
|
|
|
|
|
// 文件中的类型判断
|
|
// 文件中的类型判断
|
|
@@ -701,7 +745,7 @@ public class EmailParseService {
|
|
String emailDateStr = DateUtil.format(sendDate, DateConst.YYYYMMDD);
|
|
String emailDateStr = DateUtil.format(sendDate, DateConst.YYYYMMDD);
|
|
String filePath = path + File.separator + account + File.separator + emailDateStr + File.separator;
|
|
String filePath = path + File.separator + account + File.separator + emailDateStr + File.separator;
|
|
String realPath = filePath + emailDate + fileName;
|
|
String realPath = filePath + emailDate + fileName;
|
|
- File saveFile = cn.hutool.core.io.FileUtil.file(realPath);
|
|
|
|
|
|
+ File saveFile = FileUtil.file(realPath);
|
|
if (!saveFile.exists()) {
|
|
if (!saveFile.exists()) {
|
|
if (!saveFile.getParentFile().exists()) {
|
|
if (!saveFile.getParentFile().exists()) {
|
|
boolean mkdirs = saveFile.getParentFile().mkdirs();
|
|
boolean mkdirs = saveFile.getParentFile().mkdirs();
|
|
@@ -709,10 +753,10 @@ public class EmailParseService {
|
|
log.warn("file path mkdir failed.");
|
|
log.warn("file path mkdir failed.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- FileUtil.saveFile(saveFile, part);
|
|
|
|
|
|
+ saveFile(saveFile, part);
|
|
} else {
|
|
} else {
|
|
- cn.hutool.core.io.FileUtil.del(saveFile);
|
|
|
|
- FileUtil.saveFile(saveFile, part);
|
|
|
|
|
|
+ FileUtil.del(saveFile);
|
|
|
|
+ saveFile(saveFile, part);
|
|
}
|
|
}
|
|
EmailContentInfoDTO emailContentInfoDTO = new EmailContentInfoDTO();
|
|
EmailContentInfoDTO emailContentInfoDTO = new EmailContentInfoDTO();
|
|
emailContentInfoDTO.setFileName(fileName);
|
|
emailContentInfoDTO.setFileName(fileName);
|
|
@@ -794,4 +838,10 @@ public class EmailParseService {
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ private static void saveFile(File saveFile, Part part) throws Exception {
|
|
|
|
+ try (InputStream is = part.getInputStream()) {
|
|
|
|
+ Files.copy(is, saveFile.toPath());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|