From fef01b692ad6ca92dd2c197e9613e101bf217469 Mon Sep 17 00:00:00 2001 From: caozehui <2427765068@qq.com> Date: Thu, 27 Mar 2025 13:55:04 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=8A=A5=E5=91=8A=E6=A8=A1?= =?UTF-8?q?=E6=9D=BF=E4=BF=AE=E6=94=B9=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plan/controller/AdPlanController.java | 2 +- .../report/pojo/enums/ReportResponseEnum.java | 3 +- .../service/impl/PqReportServiceImpl.java | 103 +++++++++++++++--- 3 files changed, 89 insertions(+), 19 deletions(-) diff --git a/detection/src/main/java/com/njcn/gather/plan/controller/AdPlanController.java b/detection/src/main/java/com/njcn/gather/plan/controller/AdPlanController.java index a073f92c..7c47388e 100644 --- a/detection/src/main/java/com/njcn/gather/plan/controller/AdPlanController.java +++ b/detection/src/main/java/com/njcn/gather/plan/controller/AdPlanController.java @@ -124,7 +124,7 @@ public class AdPlanController extends BaseController { @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DOWNLOAD) @PostMapping("/downloadTemplate") - @ApiOperation("下载二楼检测计划导出模板") + @ApiOperation("下载检测计划导出模板") public void downloadTemplate() { adPlanService.downloadTemplate(); } diff --git a/detection/src/main/java/com/njcn/gather/report/pojo/enums/ReportResponseEnum.java b/detection/src/main/java/com/njcn/gather/report/pojo/enums/ReportResponseEnum.java index 691bff7c..56482024 100644 --- a/detection/src/main/java/com/njcn/gather/report/pojo/enums/ReportResponseEnum.java +++ b/detection/src/main/java/com/njcn/gather/report/pojo/enums/ReportResponseEnum.java @@ -18,7 +18,8 @@ public enum ReportResponseEnum { DEVICE_NOT_EXIST("A012007", "请检查装置是否存在!"), DEVICE_TYPE_NOT_EXIST("A012008", "设备类型缺失,请联系管理员!"), REPORT_TEMPLATE_NOT_EXIST("A012009", "报告模板缺失,请联系管理员!"), - NO_CHECK_DATA("A012010", "没有检测数据,无法生成报告!"); + NO_CHECK_DATA("A012010", "没有检测数据,无法生成报告!"), + FILE_RENAME_FAILED("A012011", "文件重命名失败"); private String code; private String message; diff --git a/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java b/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java index b6181f75..637554e8 100644 --- a/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/report/service/impl/PqReportServiceImpl.java @@ -190,9 +190,9 @@ public class PqReportServiceImpl extends ServiceImpl i } @Override + @Transactional public boolean update(ReportParam.UpdateParam reportParam) { this.checkRepeat(reportParam, true); - this.deleteFile(Collections.singletonList(reportParam.getId())); PqReport pqReport = new PqReport(); BeanUtils.copyProperties(reportParam, pqReport); @@ -231,13 +231,14 @@ public class PqReportServiceImpl extends ServiceImpl i * @param pqReport * @param isAdd */ - private void uploadFile(ReportParam reportParam, PqReport pqReport, Boolean isAdd) { + private void uploadFile(ReportParam reportParam, PqReport pqReport, boolean isAdd) { String uploadDir = templatePath + File.separator + reportParam.getName() + File.separator + reportParam.getVersion() + File.separator; //校验文件后缀 MultipartFile baseFile = reportParam.getBaseFile(); MultipartFile detailFile = reportParam.getDetailFile(); if (ObjectUtil.isNotNull(baseFile) && ObjectUtil.isNotNull(detailFile) && !baseFile.isEmpty() && !detailFile.isEmpty()) { + this.deleteFile(Collections.singletonList(pqReport.getId())); String baseOriginalFilename = baseFile.getOriginalFilename(); String baseFileName = baseOriginalFilename.substring(0, baseOriginalFilename.lastIndexOf(".")); String baseSuffix = baseOriginalFilename.substring(baseOriginalFilename.lastIndexOf(".") + 1); @@ -252,23 +253,11 @@ public class PqReportServiceImpl extends ServiceImpl i throw new BusinessException(ReportResponseEnum.FILE_NAME_SAME_ERROR); } - pqReport.setBasePath(uploadDir + baseFileName + "_" + pqReport.getVersion() + ".docx"); - pqReport.setDetailPath(uploadDir + detailFileName + "_" + pqReport.getVersion() + ".docx"); + pqReport.setBasePath(uploadDir + baseFileName + ".docx"); + pqReport.setDetailPath(uploadDir + detailFileName + ".docx"); try { - // 创建上传目录(如果不存在) - Path uploadPath = Paths.get(uploadDir); - if (!Files.exists(uploadPath)) { - Files.createDirectories(uploadPath); - } - - //清空目录下的文件 - File[] files = uploadPath.toFile().listFiles(); - if (ArrayUtil.isNotEmpty(files)) { - for (int i = 0; i < files.length; i++) { - files[i].delete(); - } - } + this.createDirectory(uploadDir); // 保存文件 byte[] baseBytes = baseFile.getBytes(); @@ -290,6 +279,71 @@ public class PqReportServiceImpl extends ServiceImpl i } else { if (isAdd) { throw new BusinessException(ReportResponseEnum.FILE_NOT_NULL); + } else { + PqReport oldPqReport = this.getById(pqReport.getId()); + String newDir = templatePath + File.separator + reportParam.getName() + File.separator + reportParam.getVersion() + File.separator; + String oldDir = oldPqReport.getBasePath().substring(0, oldPqReport.getBasePath().lastIndexOf(File.separator) + 1); + + //若修改了文件名称、版本号,则需要重命名文件 + if (!oldDir.equals(newDir)) { + // 文件夹重命名 + String oldBasePathStr = oldPqReport.getBasePath(); + String baseName = oldBasePathStr.substring(oldBasePathStr.lastIndexOf(File.separator) + 1); + Path oldBasePath = Paths.get(oldBasePathStr); + Path newBasePath = Paths.get(newDir + baseName); + pqReport.setBasePath(newDir + baseName); + + String oldDetailPathStr = oldPqReport.getDetailPath(); + String detailName = oldDetailPathStr.substring(oldDetailPathStr.lastIndexOf(File.separator) + 1); + Path oldDetailPath = Paths.get(oldDetailPathStr); + Path newDetailPath = Paths.get(newDir + detailName); + pqReport.setDetailPath(newDir + detailName); + + try { + this.createDirectory(newDir); + Files.move(oldBasePath, newBasePath); + Files.move(oldDetailPath, newDetailPath); + if (!oldPqReport.getName().equals(reportParam.getName()) && !this.existSameName(pqReport.getId(), oldPqReport.getName())) { + this.recursionDeleteDirectory(templatePath + File.separator + oldPqReport.getName()); + } else { + Paths.get(oldDir).toFile().delete(); + } + } catch (IOException e) { + throw new BusinessException(ReportResponseEnum.FILE_RENAME_FAILED); + } + } + } + } + } + + /** + * 返回是否存在同名的报告 + * + * @param id + * @param name + * @return + */ + public boolean existSameName(String id, String name) { + return this.lambdaQuery().eq(PqReport::getName, name).ne(PqReport::getId, id).count() > 0; + } + + /** + * 创建目录并清空目录下的文件 + * + * @param dirPath + * @throws IOException + */ + private void createDirectory(String dirPath) throws IOException { + // 创建上传目录(如果不存在) + Path uploadPath = Paths.get(dirPath); + if (!Files.exists(uploadPath)) { + Files.createDirectories(uploadPath); + } + //清空目录下的文件 + File[] files = uploadPath.toFile().listFiles(); + if (ArrayUtil.isNotEmpty(files)) { + for (int i = 0; i < files.length; i++) { + files[i].delete(); } } } @@ -325,6 +379,21 @@ public class PqReportServiceImpl extends ServiceImpl i } } + /** + * 递归删除目录 + * + * @param dirPath + */ + private void recursionDeleteDirectory(String dirPath) throws IOException { + Path path = Paths.get(dirPath); + if (Files.exists(path)) { + Files.walk(path) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + } + } + @Override public void generateReport(DevReportParam devReportParam) { AdPlan plan = adPlanService.getById(devReportParam.getPlanId());