Commit 4bd0566b authored by huyuchen's avatar huyuchen

huych-电子档案批量导入第五次提交

parent 9a7278aa
...@@ -207,10 +207,10 @@ public class TElectronicArchiveDetailServiceImpl extends ServiceImpl<TElectronic ...@@ -207,10 +207,10 @@ public class TElectronicArchiveDetailServiceImpl extends ServiceImpl<TElectronic
*/ */
@Override @Override
public void batchDownloadArchives(List<ArchiveBatchDownloadRequestVO> requestList, HttpServletResponse response) { public void batchDownloadArchives(List<ArchiveBatchDownloadRequestVO> requestList, HttpServletResponse response) {
// 反馈列表 // 反馈列表
List<ArchiveDownloadFeedbackVO> feedbackList = new ArrayList<>(); List<ArchiveDownloadFeedbackVO> feedbackList = new ArrayList<>();
// 用于存储需要下载的档案及其附件 // 用于存储需要下载的档案及其附件
Map<String, ArchiveDownloadInfo> downloadMap = new LinkedHashMap<>(); Map<String, ArchiveDownloadInfo> downloadMap = new LinkedHashMap<>();
...@@ -220,89 +220,66 @@ public class TElectronicArchiveDetailServiceImpl extends ServiceImpl<TElectronic ...@@ -220,89 +220,66 @@ public class TElectronicArchiveDetailServiceImpl extends ServiceImpl<TElectronic
List<Long> roleList = user.getClientRoleMap().get(ClientNameConstants.CLIENT_MVP); List<Long> roleList = user.getClientRoleMap().get(ClientNameConstants.CLIENT_MVP);
if (CommonConstants.ZERO_STRING.equals(user.getSystemFlag()) || (Common.isNotNull(roleList) if (CommonConstants.ZERO_STRING.equals(user.getSystemFlag()) || (Common.isNotNull(roleList)
&& (roleList.stream().anyMatch(role -> role == 1569585030423158786L) && (roleList.stream().anyMatch(role -> role == 1569585030423158786L)
|| roleList.stream().anyMatch(role -> role == 2003347621962379266L)))) { || roleList.stream().anyMatch(role -> role == 2003347621962379266L)))) {
isCheck = false; isCheck = false;
} }
if (Common.isNotNull(user) && isCheck) { if (Common.isNotNull(user) && isCheck) {
deptNoList = getDeptNoList(user.getId()); deptNoList = getDeptNoList(user.getId());
} }
try { try {
// 1. 校验请求并查询档案信息 // 1. 校验请求并查询档案信息
for (ArchiveBatchDownloadRequestVO request : requestList) { for (ArchiveBatchDownloadRequestVO request : requestList) {
try { try {
validateAndPrepareDownload(request, downloadMap, feedbackList, isCheck, deptNoList); validateAndPrepareDownload(request, downloadMap, feedbackList, isCheck, deptNoList);
} catch (Exception e) { } catch (Exception e) {
log.error("处理下载请求失败,姓名:{},身份证:{},项目编码:{}", log.error("处理下载请求失败,姓名:{},身份证:{},项目编码:{}",
request.getEmpName(), request.getEmpIdcard(), request.getDeptNo(), e); request.getEmpName(), request.getEmpIdcard(), request.getDeptNo(), e);
feedbackList.add(createFeedbackVO(request, "系统异常:" + e.getMessage(),null)); feedbackList.add(createFeedbackVO(request, "系统异常:" + e.getMessage(),null));
} }
} }
// 2. 校验文件总大小是否超过500MB // 2. 校验文件总大小是否超过500MB
long totalSize = calculateTotalFileSize(downloadMap); long totalSize = calculateTotalFileSize(downloadMap);
long maxSize = 500 * 1024 * 1024L; // 500MB long maxSize = 500 * 1024 * 1024L; // 500MB
// #region agent log if (totalSize > maxSize) {
try{java.nio.file.Files.write(java.nio.file.Paths.get("e:\\IdeaProjects\\yifu_mvp\\.cursor\\debug.log"),("{\"location\":\"TElectronicArchiveDetailServiceImpl.java:244\",\"message\":\"开始批量下载\",\"data\":{\"totalSizeMB\":"+(totalSize/1024/1024)+",\"fileCount\":"+downloadMap.size()+"},\"timestamp\":"+System.currentTimeMillis()+",\"sessionId\":\"debug-session\",\"runId\":\"run1\",\"hypothesisId\":\"H1,H2\"}\n").getBytes(java.nio.charset.StandardCharsets.UTF_8),java.nio.file.StandardOpenOption.CREATE,java.nio.file.StandardOpenOption.APPEND);}catch(Exception e){} log.warn("批量下载文件总大小超过限制,总大小:{}MB,限制:500MB", totalSize / 1024 / 1024);
// #endregion response.setContentType("application/json;charset=UTF-8");
if (totalSize > maxSize) { response.setCharacterEncoding("UTF-8");
log.warn("批量下载文件总大小超过限制,总大小:{}MB,限制:500MB", totalSize / 1024 / 1024); String errorMsg = String.format("{\"code\":1,\"msg\":\"下载文件总大小超过限制,当前总大小:%.2fMB,最大允许:500MB\"}",
response.setContentType("application/json;charset=UTF-8"); totalSize / 1024.0 / 1024.0);
response.getWriter().write(errorMsg);
return;
}
// 3. 设置响应头
String fileName = "电子档案批量下载_" +
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + ".zip";
response.setContentType("application/zip");
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
String errorMsg = String.format("{\"code\":1,\"msg\":\"下载文件总大小超过限制,当前总大小:%.2fMB,最大允许:500MB\"}", response.setHeader(CommonConstants.CONTENT_DISPOSITION,
totalSize / 1024.0 / 1024.0); "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.getWriter().write(errorMsg);
return; // 4. 创建ZIP输出流
} try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
// 3. 设置响应头
String fileName = "电子档案批量下载_" +
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + ".zip";
response.setContentType("application/zip");
response.setCharacterEncoding("UTF-8");
response.setHeader(CommonConstants.CONTENT_DISPOSITION,
"attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
// #region agent log
try{java.nio.file.Files.write(java.nio.file.Paths.get("e:\\IdeaProjects\\yifu_mvp\\.cursor\\debug.log"),("{\"location\":\"TElectronicArchiveDetailServiceImpl.java:263\",\"message\":\"响应头已设置\",\"data\":{\"fileName\":\""+fileName+"\"},\"timestamp\":"+System.currentTimeMillis()+",\"sessionId\":\"debug-session\",\"runId\":\"run1\",\"hypothesisId\":\"H1,H2\"}\n").getBytes(java.nio.charset.StandardCharsets.UTF_8),java.nio.file.StandardOpenOption.CREATE,java.nio.file.StandardOpenOption.APPEND);}catch(Exception e){}
// #endregion
// 4. 创建ZIP输出流
try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
// 5. 下载并打包档案文件 // 5. 下载并打包档案文件
// #region agent log
long totalBytesWritten = 0;
int folderIndex = 0;
// #endregion
for (Map.Entry<String, ArchiveDownloadInfo> entry : downloadMap.entrySet()) { for (Map.Entry<String, ArchiveDownloadInfo> entry : downloadMap.entrySet()) {
String folderName = entry.getKey(); String folderName = entry.getKey();
ArchiveDownloadInfo downloadInfo = entry.getValue(); ArchiveDownloadInfo downloadInfo = entry.getValue();
// #region agent log
folderIndex++;
long beforeWrite = totalBytesWritten;
try{java.nio.file.Files.write(java.nio.file.Paths.get("e:\\IdeaProjects\\yifu_mvp\\.cursor\\debug.log"),("{\"location\":\"TElectronicArchiveDetailServiceImpl.java:280\",\"message\":\"开始处理文件夹\",\"data\":{\"folderIndex\":"+folderIndex+",\"folderName\":\""+folderName.replace("\"","\\\"").replace("\\","/")+"\",\"totalBytesWritten\":"+totalBytesWritten+"},\"timestamp\":"+System.currentTimeMillis()+",\"sessionId\":\"debug-session\",\"runId\":\"run1\",\"hypothesisId\":\"H3,H4\"}\n").getBytes(java.nio.charset.StandardCharsets.UTF_8),java.nio.file.StandardOpenOption.CREATE,java.nio.file.StandardOpenOption.APPEND);}catch(Exception ex){}
// #endregion
try { try {
downloadArchiveFiles(zipOut, folderName, downloadInfo); downloadArchiveFiles(zipOut, folderName, downloadInfo);
// #region agent log
try{zipOut.flush();response.getOutputStream().flush();}catch(Exception ex){}
totalBytesWritten += (downloadInfo.getMaterialTypeAttaMap().values().stream().mapToLong(list->list.stream().mapToLong(atta->atta.getAttaSize()!=null?atta.getAttaSize():0).sum()).sum());
try{java.nio.file.Files.write(java.nio.file.Paths.get("e:\\IdeaProjects\\yifu_mvp\\.cursor\\debug.log"),("{\"location\":\"TElectronicArchiveDetailServiceImpl.java:285\",\"message\":\"文件夹处理完成\",\"data\":{\"folderIndex\":"+folderIndex+",\"folderName\":\""+folderName.replace("\"","\\\"").replace("\\","/")+"\",\"totalBytesWritten\":"+totalBytesWritten+",\"bytesInFolder\":"+(totalBytesWritten-beforeWrite)+"},\"timestamp\":"+System.currentTimeMillis()+",\"sessionId\":\"debug-session\",\"runId\":\"run1\",\"hypothesisId\":\"H1,H3,H4\"}\n").getBytes(java.nio.charset.StandardCharsets.UTF_8),java.nio.file.StandardOpenOption.CREATE,java.nio.file.StandardOpenOption.APPEND);}catch(Exception ex){}
// #endregion
// 更新反馈状态为成功 // 更新反馈状态为成功
// 使用materialTypeAttaMap的所有key来匹配,确保所有资料类型的反馈都被更新 // 使用materialTypeAttaMap的所有key来匹配,确保所有资料类型的反馈都被更新
Set<String> materialTypes = downloadInfo.getMaterialTypeAttaMap().keySet(); Set<String> materialTypes = downloadInfo.getMaterialTypeAttaMap().keySet();
for (ArchiveDownloadFeedbackVO feedback : feedbackList) { for (ArchiveDownloadFeedbackVO feedback : feedbackList) {
if (feedback.getEmpName().equals(downloadInfo.getDetail().getEmpName()) && if (feedback.getEmpName().equals(downloadInfo.getDetail().getEmpName()) &&
feedback.getEmpIdcard().equals(downloadInfo.getDetail().getEmpIdcard()) && feedback.getEmpIdcard().equals(downloadInfo.getDetail().getEmpIdcard()) &&
feedback.getDeptNo().equals(downloadInfo.getDetail().getDeptNo()) && feedback.getDeptNo().equals(downloadInfo.getDetail().getDeptNo()) &&
(materialTypes.contains(feedback.getMaterialType()) || "全部资料类型".equals(feedback.getMaterialType()))) { (materialTypes.contains(feedback.getMaterialType()) || "全部资料类型".equals(feedback.getMaterialType()))) {
feedback.setDownloadStatus("成功"); feedback.setDownloadStatus("成功");
feedback.setFailReason(""); feedback.setFailReason("");
} }
...@@ -313,36 +290,24 @@ public class TElectronicArchiveDetailServiceImpl extends ServiceImpl<TElectronic ...@@ -313,36 +290,24 @@ public class TElectronicArchiveDetailServiceImpl extends ServiceImpl<TElectronic
Set<String> materialTypes = downloadInfo.getMaterialTypeAttaMap().keySet(); Set<String> materialTypes = downloadInfo.getMaterialTypeAttaMap().keySet();
for (ArchiveDownloadFeedbackVO feedback : feedbackList) { for (ArchiveDownloadFeedbackVO feedback : feedbackList) {
if (feedback.getEmpName().equals(downloadInfo.getDetail().getEmpName()) && if (feedback.getEmpName().equals(downloadInfo.getDetail().getEmpName()) &&
feedback.getEmpIdcard().equals(downloadInfo.getDetail().getEmpIdcard()) && feedback.getEmpIdcard().equals(downloadInfo.getDetail().getEmpIdcard()) &&
feedback.getDeptNo().equals(downloadInfo.getDetail().getDeptNo()) && feedback.getDeptNo().equals(downloadInfo.getDetail().getDeptNo()) &&
(materialTypes.contains(feedback.getMaterialType()) || "全部资料类型".equals(feedback.getMaterialType()))) { (materialTypes.contains(feedback.getMaterialType()) || "全部资料类型".equals(feedback.getMaterialType()))) {
feedback.setDownloadStatus("失败"); feedback.setDownloadStatus("失败");
feedback.setFailReason("下载文件失败:" + e.getMessage()); feedback.setFailReason("下载文件失败:" + e.getMessage());
} }
} }
} }
} }
// 6. 生成并添加反馈Excel表 // 6. 生成并添加反馈Excel表
// #region agent log
try{java.nio.file.Files.write(java.nio.file.Paths.get("e:\\IdeaProjects\\yifu_mvp\\.cursor\\debug.log"),("{\"location\":\"TElectronicArchiveDetailServiceImpl.java:311\",\"message\":\"开始添加反馈Excel\",\"data\":{\"totalBytesWritten\":"+totalBytesWritten+"},\"timestamp\":"+System.currentTimeMillis()+",\"sessionId\":\"debug-session\",\"runId\":\"run1\",\"hypothesisId\":\"H4\"}\n").getBytes(java.nio.charset.StandardCharsets.UTF_8),java.nio.file.StandardOpenOption.CREATE,java.nio.file.StandardOpenOption.APPEND);}catch(Exception ex){}
// #endregion
addFeedbackExcel(zipOut, feedbackList); addFeedbackExcel(zipOut, feedbackList);
// #region agent log
try{java.nio.file.Files.write(java.nio.file.Paths.get("e:\\IdeaProjects\\yifu_mvp\\.cursor\\debug.log"),("{\"location\":\"TElectronicArchiveDetailServiceImpl.java:313\",\"message\":\"开始finish zip\",\"data\":{\"totalBytesWritten\":"+totalBytesWritten+"},\"timestamp\":"+System.currentTimeMillis()+",\"sessionId\":\"debug-session\",\"runId\":\"run1\",\"hypothesisId\":\"H1,H4\"}\n").getBytes(java.nio.charset.StandardCharsets.UTF_8),java.nio.file.StandardOpenOption.CREATE,java.nio.file.StandardOpenOption.APPEND);}catch(Exception ex){}
// #endregion
zipOut.finish(); zipOut.finish();
// #region agent log
try{java.nio.file.Files.write(java.nio.file.Paths.get("e:\\IdeaProjects\\yifu_mvp\\.cursor\\debug.log"),("{\"location\":\"TElectronicArchiveDetailServiceImpl.java:315\",\"message\":\"zip finish完成\",\"data\":{\"totalBytesWritten\":"+totalBytesWritten+"},\"timestamp\":"+System.currentTimeMillis()+",\"sessionId\":\"debug-session\",\"runId\":\"run1\",\"hypothesisId\":\"H1,H4\"}\n").getBytes(java.nio.charset.StandardCharsets.UTF_8),java.nio.file.StandardOpenOption.CREATE,java.nio.file.StandardOpenOption.APPEND);}catch(Exception ex){}
// #endregion
} }
} catch (Exception e) { } catch (Exception e) {
log.error("批量下载电子档案失败", e); log.error("批量下载电子档案失败", e);
// #region agent log
try{java.nio.file.Files.write(java.nio.file.Paths.get("e:\\IdeaProjects\\yifu_mvp\\.cursor\\debug.log"),("{\"location\":\"TElectronicArchiveDetailServiceImpl.java:319\",\"message\":\"批量下载异常\",\"data\":{\"error\":\""+e.getMessage().replace("\"","\\\"").replace("\\","/").substring(0,Math.min(100,e.getMessage().length()))+"\",\"exceptionClass\":\""+e.getClass().getName()+"\"},\"timestamp\":"+System.currentTimeMillis()+",\"sessionId\":\"debug-session\",\"runId\":\"run1\",\"hypothesisId\":\"H2,H3,H4,H5\"}\n").getBytes(java.nio.charset.StandardCharsets.UTF_8),java.nio.file.StandardOpenOption.CREATE,java.nio.file.StandardOpenOption.APPEND);}catch(Exception ex){}
// #endregion
} }
} }
......
...@@ -245,59 +245,37 @@ public class OSSUtil { ...@@ -245,59 +245,37 @@ public class OSSUtil {
int fileIndex = 0; int fileIndex = 0;
long totalBytesRead = 0; long totalBytesRead = 0;
// #endregion // #endregion
for(OSSObjectBean key:srcList){ for(OSSObjectBean key:srcList){
// #region agent log // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
fileIndex++; ossObject = client.getObject(bucketName, key.getKey());
long fileBytesRead = 0; if(null != ossObject){
String fileName = key.getName()!=null?key.getName().replace("\"","\\\"").replace("\\","/"):"null"; // 读取文件内容。
try{java.nio.file.Files.write(java.nio.file.Paths.get("e:\\IdeaProjects\\yifu_mvp\\.cursor\\debug.log"),("{\"location\":\"OSSUtil.java:246\",\"message\":\"开始读取OSS文件\",\"data\":{\"fileIndex\":"+fileIndex+",\"fileName\":\""+fileName+"\",\"key\":\""+key.getKey().replace("\"","\\\"").replace("\\","/")+"\"},\"timestamp\":"+System.currentTimeMillis()+",\"sessionId\":\"debug-session\",\"runId\":\"run1\",\"hypothesisId\":\"H3,H5\"}\n").getBytes(java.nio.charset.StandardCharsets.UTF_8),java.nio.file.StandardOpenOption.CREATE,java.nio.file.StandardOpenOption.APPEND);}catch(Exception ex){} log.info("Object content:");
// #endregion inputStream = ossObject.getObjectContent();
// ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。 //添加到zip
ossObject = client.getObject(bucketName, key.getKey()); zip.putNextEntry(new ZipEntry(key.getName()));
if(null != ossObject){ byte[] buf = new byte[1024];
// 读取文件内容。 int len;
log.info("Object content:"); while ((len = inputStream.read(buf)) != -1){
inputStream = ossObject.getObjectContent(); zip.write(buf, 0, len);
//添加到zip }
zip.putNextEntry(new ZipEntry(key.getName())); }
byte[] buf = new byte[1024]; // 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
int len; IoUtil.close(inputStream);
while ((len = inputStream.read(buf)) != -1){ zip.closeEntry();
zip.write(buf, 0, len); }
// #region agent log }
fileBytesRead += len; // 关闭OSSClient。
totalBytesRead += len; } catch (OSSException oe) {
// #endregion ossexceptionlog(oe);
} } catch (ClientException ce) {
} clientExceptionLog(ce);
// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。 } catch (IOException e) {
IoUtil.close(inputStream); e.printStackTrace();
zip.closeEntry(); } finally {
// #region agent log IoUtil.close(inputStream);
try{java.nio.file.Files.write(java.nio.file.Paths.get("e:\\IdeaProjects\\yifu_mvp\\.cursor\\debug.log"),("{\"location\":\"OSSUtil.java:262\",\"message\":\"OSS文件读取完成\",\"data\":{\"fileIndex\":"+fileIndex+",\"fileName\":\""+fileName+"\",\"fileBytesRead\":"+fileBytesRead+",\"totalBytesRead\":"+totalBytesRead+"},\"timestamp\":"+System.currentTimeMillis()+",\"sessionId\":\"debug-session\",\"runId\":\"run1\",\"hypothesisId\":\"H3,H5\"}\n").getBytes(java.nio.charset.StandardCharsets.UTF_8),java.nio.file.StandardOpenOption.CREATE,java.nio.file.StandardOpenOption.APPEND);}catch(Exception ex){} }
// #endregion }
}
}
// 关闭OSSClient。
} catch (OSSException oe) {
ossexceptionlog(oe);
// #region agent log
try{java.nio.file.Files.write(java.nio.file.Paths.get("e:\\IdeaProjects\\yifu_mvp\\.cursor\\debug.log"),("{\"location\":\"OSSUtil.java:266\",\"message\":\"OSS异常\",\"data\":{\"error\":\""+oe.getMessage().replace("\"","\\\"").replace("\\","/").substring(0,Math.min(100,oe.getMessage().length()))+"\",\"errorCode\":\""+oe.getErrorCode()+"\"},\"timestamp\":"+System.currentTimeMillis()+",\"sessionId\":\"debug-session\",\"runId\":\"run1\",\"hypothesisId\":\"H3,H5\"}\n").getBytes(java.nio.charset.StandardCharsets.UTF_8),java.nio.file.StandardOpenOption.CREATE,java.nio.file.StandardOpenOption.APPEND);}catch(Exception ex){}
// #endregion
} catch (ClientException ce) {
clientExceptionLog(ce);
// #region agent log
try{java.nio.file.Files.write(java.nio.file.Paths.get("e:\\IdeaProjects\\yifu_mvp\\.cursor\\debug.log"),("{\"location\":\"OSSUtil.java:268\",\"message\":\"OSS Client异常\",\"data\":{\"error\":\""+ce.getMessage().replace("\"","\\\"").replace("\\","/").substring(0,Math.min(100,ce.getMessage().length()))+"\",\"errorCode\":\""+ce.getErrorCode()+"\"},\"timestamp\":"+System.currentTimeMillis()+",\"sessionId\":\"debug-session\",\"runId\":\"run1\",\"hypothesisId\":\"H3,H5\"}\n").getBytes(java.nio.charset.StandardCharsets.UTF_8),java.nio.file.StandardOpenOption.CREATE,java.nio.file.StandardOpenOption.APPEND);}catch(Exception ex){}
// #endregion
} catch (IOException e) {
e.printStackTrace();
// #region agent log
try{java.nio.file.Files.write(java.nio.file.Paths.get("e:\\IdeaProjects\\yifu_mvp\\.cursor\\debug.log"),("{\"location\":\"OSSUtil.java:270\",\"message\":\"IO异常\",\"data\":{\"error\":\""+e.getMessage().replace("\"","\\\"").replace("\\","/").substring(0,Math.min(100,e.getMessage().length()))+"\",\"exceptionClass\":\""+e.getClass().getName()+"\"},\"timestamp\":"+System.currentTimeMillis()+",\"sessionId\":\"debug-session\",\"runId\":\"run1\",\"hypothesisId\":\"H1,H3,H4,H5\"}\n").getBytes(java.nio.charset.StandardCharsets.UTF_8),java.nio.file.StandardOpenOption.CREATE,java.nio.file.StandardOpenOption.APPEND);}catch(Exception ex){}
// #endregion
} finally {
IoUtil.close(inputStream);
}
}
/** /**
* @author fang * @author fang
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment