diff --git a/jero-boot-incoming-payment/src/main/java/com/jero/project/service/impl/PayWorkingGroupServiceImpl.java b/jero-boot-incoming-payment/src/main/java/com/jero/project/service/impl/PayWorkingGroupServiceImpl.java index f6a205f9..6a0a47fa 100644 --- a/jero-boot-incoming-payment/src/main/java/com/jero/project/service/impl/PayWorkingGroupServiceImpl.java +++ b/jero-boot-incoming-payment/src/main/java/com/jero/project/service/impl/PayWorkingGroupServiceImpl.java @@ -830,58 +830,53 @@ public class PayWorkingGroupServiceImpl extends ServiceImpl roleCodes = new ArrayList<>(Arrays.asList(sysUser.getRoleCode().split(","))); - //管理员和财务查询全部,其余角色查询自己的 - if(!roleCodes.contains(constant.ADMIN)&&!roleCodes.contains(constant.CWRY)){ + if (!roleCodes.contains(constant.ADMIN) && !roleCodes.contains(constant.CWRY)) { payWorkingGroup.setPrincipalA(sysUser.getId()); payWorkingGroup.setPrincipalB(sysUser.getId()); } } else { throw new JeroBootException("登录用户角色为空!"); } - // 创建一个不分页的 Page 对象 + Page page = new Page<>(-1, -1); - // 处理 workingGroupProject 字段 String workingGroupProject = payWorkingGroup.getWorkingGroupProject(); if (StringUtils.isNotBlank(workingGroupProject)) { payWorkingGroup.setWorkingGroupProject(oConvertUtils.replaceAllPercent(workingGroupProject)); } - // 查询分页列表 + List pageList = payWorkingGroupService.queryPageList(page, payWorkingGroup).getRecords(); - // 获取前端传递的 selections 参数 String selections = request.getParameter("selections"); - List ids = pageList.stream() - .map(PayWorkingGroupVO::getId) - .collect(Collectors.toList()); + List ids = pageList.stream().map(PayWorkingGroupVO::getId).collect(Collectors.toList()); if (oConvertUtils.isNotEmpty(selections)) { List selectionList = Arrays.asList(selections.split(",")); - ids.retainAll(selectionList); // 仅保留在 selections 中的 id + ids.retainAll(selectionList); } + if (oConvertUtils.isEmpty(ids)) { throw new JeroBootException("没有工作组文件可以导出!"); } - Set addedEntries = new HashSet<>(); // 用于记录已添加的条目路径 + + Set addedEntries = new HashSet<>(); + try (ZipOutputStream zipos = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()))) { response.reset(); response.setCharacterEncoding("utf-8"); - response.setContentType("multipart/form-data"); - // 设置压缩包的名字 + response.setContentType("application/zip"); + String downloadName = UUID.randomUUID().toString() + ".zip"; String agent = request.getHeader("USER-AGENT"); - try { + if (agent != null) { if (agent.contains("MSIE") || agent.contains("Trident")) { downloadName = java.net.URLEncoder.encode(downloadName, "UTF-8"); } else { downloadName = new String(downloadName.getBytes("UTF-8"), "ISO-8859-1"); } - } catch (Exception e) { - e.printStackTrace(); } response.setHeader("Content-Disposition", "attachment;fileName=\"" + downloadName + "\""); - // 处理工作组资料 for (String projectId : ids) { PayWorkingGroupDataFile payWorkingGroupDataFile = new PayWorkingGroupDataFile(); payWorkingGroupDataFile.setProjectId(projectId); @@ -891,22 +886,21 @@ public class PayWorkingGroupServiceImpl extends ServiceImpl queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(PayResearchProject::getWorkingGroupId, projectId); List payResearchProjectList = payResearchProjectService.list(queryWrapper); @@ -914,80 +908,23 @@ public class PayWorkingGroupServiceImpl extends ServiceImpl researchProjectFileIds = new ArrayList<>(); if (!StringUtils.isEmpty(payResearchProject.getFiles())) { researchProjectFileIds.addAll(Arrays.asList(payResearchProject.getFiles().split(","))); } - // 添加在研项目的文件到在研项目名文件夹 for (String fileId : researchProjectFileIds) { - OSSFile ossFile = iossFileService.getById(fileId.trim()); - if (ossFile == null) { - continue; - } - File file = new File(upLoadPath + File.separator + ossFile.getUrl()); - if (!file.exists()) { - continue; - } - String entryPath = projectFolderPath + ossFile.getFileName(); - // 检查是否已经添加过相同路径的条目 - if (addedEntries.contains(entryPath)) { - // 如果已经存在相同路径的条目,可以修改文件名或者跳过处理 - continue; - } - // 添加到压缩包中 - zipos.putNextEntry(new ZipEntry(entryPath)); - try (InputStream is = new FileInputStream(file)) { - byte[] b = new byte[1024]; - int length; - while ((length = is.read(b)) != -1) { - zipos.write(b, 0, length); - } - } catch (IOException e) { - e.printStackTrace(); - } - zipos.closeEntry(); // 关闭当前条目 - addedEntries.add(entryPath); // 记录已添加的条目路径 + addFileToZip(fileId.trim(), zipos, projectFolderPath, addedEntries); } } - // 添加工作组资料文件到分发资料文件夹 for (PayWorkingGroupDataFile dataFile : fileList) { - OSSFile ossFile = iossFileService.getById(dataFile.getFileId()); - if (ossFile == null) { - continue; - } - File file = new File(upLoadPath + File.separator + ossFile.getUrl()); - if (!file.exists()) { - continue; - } - String entryPath = otherFilesFolderPath + ossFile.getFileName(); - // 检查是否已经添加过相同路径的条目 - if (addedEntries.contains(entryPath)) { - // 如果已经存在相同路径的条目,可以修改文件名或者跳过处理 - continue; - } - // 添加到压缩包中 - zipos.putNextEntry(new ZipEntry(entryPath)); - try (InputStream is = new FileInputStream(file)) { - byte[] b = new byte[1024]; - int length; - while ((length = is.read(b)) != -1) { - zipos.write(b, 0, length); - } - } catch (IOException e) { - e.printStackTrace(); - } - zipos.closeEntry(); // 关闭当前条目 - addedEntries.add(entryPath); // 记录已添加的条目路径 + addFileToZip(dataFile.getFileId(), zipos, otherFilesFolderPath, addedEntries); } } - // 处理工作组会议资料 for (String projectId : ids) { LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(PayMeeting::getWorkingGroupId, projectId); @@ -998,16 +935,12 @@ public class PayWorkingGroupServiceImpl extends ServiceImpl fileIds = new ArrayList<>(); if (!StringUtils.isEmpty(payMeeting.getMeetingMinute())) { @@ -1016,6 +949,10 @@ public class PayWorkingGroupServiceImpl extends ServiceImpl addedEntries) throws IOException { + if (!addedEntries.contains(folderPath)) { + zipos.putNextEntry(new ZipEntry(folderPath)); + zipos.closeEntry(); + addedEntries.add(folderPath); + } + } + + private void addFileToZip(String fileId, ZipOutputStream zipos, String folderPath, Set addedEntries) throws IOException { + OSSFile ossFile = iossFileService.getById(fileId); + if (ossFile == null) { + return; + } + File file = new File(upLoadPath + File.separator + ossFile.getUrl()); + if (!file.exists()) { + return; + } + String entryPath = folderPath + ossFile.getFileName(); + if (addedEntries.contains(entryPath)) { + return; + } + + zipos.putNextEntry(new ZipEntry(entryPath)); + try (InputStream is = new FileInputStream(file)) { + byte[] b = new byte[1024]; + int length; + while ((length = is.read(b)) != -1) { + zipos.write(b, 0, length); + } + } catch (Exception e) { + e.printStackTrace(); + } + zipos.closeEntry(); + addedEntries.add(entryPath); + } + }