From 0ba782669328d38d28c26e112549988e4a83e33a Mon Sep 17 00:00:00 2001 From: hayu <1604366271@qq.com> Date: Wed, 21 Aug 2024 13:34:30 +0800 Subject: [PATCH] app --- pom.xml | 6 + .../controller/PrimaryDataAppController.java | 34 +----- .../impl/TestReportManageAppServiceImpl.java | 30 +---- .../com/bonus/aqgqj/task/DeleteFileTask.java | 112 ++++++++++++++++++ .../resources/mappers/app/QrCodeMapper.xml | 6 + 5 files changed, 133 insertions(+), 55 deletions(-) create mode 100644 src/main/java/com/bonus/aqgqj/task/DeleteFileTask.java diff --git a/pom.xml b/pom.xml index 26543b9..4491378 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,12 @@ 1.8 + + + net.javacrumbs.shedlock + shedlock-spring + 2.2.1 + org.springframework.boot spring-boot-starter-freemarker diff --git a/src/main/java/com/bonus/aqgqj/app/controller/PrimaryDataAppController.java b/src/main/java/com/bonus/aqgqj/app/controller/PrimaryDataAppController.java index 835b520..8506bf0 100644 --- a/src/main/java/com/bonus/aqgqj/app/controller/PrimaryDataAppController.java +++ b/src/main/java/com/bonus/aqgqj/app/controller/PrimaryDataAppController.java @@ -11,10 +11,7 @@ import com.bonus.aqgqj.app.service.PrimaryDataAppService; import com.bonus.aqgqj.basis.entity.dto.ParamsDto; import com.bonus.aqgqj.basis.entity.vo.*; import com.bonus.aqgqj.system.vo.EncryptedReq; -import com.bonus.aqgqj.utils.ExcelStyleUtil; -import com.bonus.aqgqj.utils.FileUtil; -import com.bonus.aqgqj.utils.ServerResponse; -import com.bonus.aqgqj.utils.SystemUtils; +import com.bonus.aqgqj.utils.*; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import io.swagger.annotations.Api; @@ -169,7 +166,7 @@ public class PrimaryDataAppController { public ServerResponse downLoadExcelZip(HttpServletRequest request, HttpServletResponse response, EncryptedReq dto) { List fileList = new ArrayList(); List fileNameList = new ArrayList(); - String fileNames="检测原始记录_" + System.currentTimeMillis()+".zip"; + String fileNames= DateTimeHelper.getNowDate()+ "/检测原始记录_" + System.currentTimeMillis()+".zip"; String outputZipFilePath = SystemUtils.getUploadPath()+ fileNames; try { Long[] ids = dto.getData().getIds(); @@ -181,7 +178,6 @@ public class PrimaryDataAppController { fileNameList.add(excelModelPath); } } -// downloadZip(response, fileList); zipFiles(fileList, outputZipFilePath); } return ServerResponse.createSuccess("statics/"+fileNames); @@ -252,31 +248,11 @@ public class PrimaryDataAppController { } return null; } - - public void downloadZip(HttpServletResponse response, List filesToZip) throws IOException { - // 设置响应头信息,‌指定下载的文件名为"files.zip" - response.setContentType("application/zip"); - response.setHeader("Content-Disposition", "attachment; filename=files.zip"); - response.setHeader("code","200"); - try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) { - for (File file : filesToZip) { - try (FileInputStream fis = new FileInputStream(file)) { - ZipEntry zipEntry = new ZipEntry(file.getName()); - zos.putNextEntry(zipEntry); - byte[] buffer = new byte[1024]; - int len; - while ((len = fis.read(buffer)) > 0) { - zos.write(buffer, 0, len); - } - } - } - } catch (IOException e) { - log.error("文件压缩异常"); - } - } - public void zipFiles(List filesToZip, String outputZipFilePath) throws IOException { File outputFile = new File(outputZipFilePath); + if (!outputFile.getParentFile().exists()) { + outputFile.getParentFile().mkdirs(); + } try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outputFile))) { for (File file : filesToZip) { try (FileInputStream fis = new FileInputStream(file)) { diff --git a/src/main/java/com/bonus/aqgqj/app/service/impl/TestReportManageAppServiceImpl.java b/src/main/java/com/bonus/aqgqj/app/service/impl/TestReportManageAppServiceImpl.java index 48df088..6b65d46 100644 --- a/src/main/java/com/bonus/aqgqj/app/service/impl/TestReportManageAppServiceImpl.java +++ b/src/main/java/com/bonus/aqgqj/app/service/impl/TestReportManageAppServiceImpl.java @@ -229,7 +229,7 @@ public class TestReportManageAppServiceImpl implements TestReportManageAppServic List fileList = new ArrayList(); JSONArray jsonArray = JSONObject.parseArray(data.getParamsList()); List list = jsonArray.toJavaList(TestReportManageAppVo.class); - String fileNames="检测报告_" + System.currentTimeMillis()+".zip"; + String fileNames=DateTimeHelper.getNowDate()+ "/检测报告_" + System.currentTimeMillis()+".zip"; String outputZipFilePath = SystemUtils.getUploadPath()+ fileNames; if (list != null && list.size() > 0) { try { @@ -247,7 +247,6 @@ public class TestReportManageAppServiceImpl implements TestReportManageAppServic } } } -// downloadZip(response, fileList); zipFiles(fileList, outputZipFilePath); return ServerResponse.createSuccess("statics/"+fileNames); }catch (Exception e){ @@ -691,32 +690,11 @@ public class TestReportManageAppServiceImpl implements TestReportManageAppServic String code = nowDate + customerCode; return code; } - - - public void downloadZip(HttpServletResponse response, List filesToZip) throws IOException { - // 设置响应头信息,‌指定下载的文件名为"files.zip" - response.setContentType("application/zip"); - response.setHeader("Content-Disposition", "attachment; filename=files.zip"); - try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) { - for (File file : filesToZip) { - try (FileInputStream fis = new FileInputStream(file)) { - ZipEntry zipEntry = new ZipEntry(file.getName()); - zos.putNextEntry(zipEntry); - byte[] buffer = new byte[1024]; - int len; - while ((len = fis.read(buffer)) > 0) { - zos.write(buffer, 0, len); - } - } - } - } catch (IOException e) { - log.error("文件压缩异常"); - } - } - - public void zipFiles(List filesToZip, String outputZipFilePath) throws IOException { File outputFile = new File(outputZipFilePath); + if (!outputFile.getParentFile().exists()) { + outputFile.getParentFile().mkdirs(); + } try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outputFile))) { for (File file : filesToZip) { try (FileInputStream fis = new FileInputStream(file)) { diff --git a/src/main/java/com/bonus/aqgqj/task/DeleteFileTask.java b/src/main/java/com/bonus/aqgqj/task/DeleteFileTask.java new file mode 100644 index 0000000..1be5235 --- /dev/null +++ b/src/main/java/com/bonus/aqgqj/task/DeleteFileTask.java @@ -0,0 +1,112 @@ +package com.bonus.aqgqj.task; + +import com.bonus.aqgqj.utils.DateTimeHelper; +import com.bonus.aqgqj.utils.FileUtil; +import com.bonus.aqgqj.utils.SystemUtils; +import lombok.extern.slf4j.Slf4j; +import net.javacrumbs.shedlock.core.SchedulerLock; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; + +import java.io.File; +import java.nio.file.Files; +import java.nio.file.attribute.BasicFileAttributes; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.YearMonth; +import java.util.Date; +import java.util.List; + +/** +* @description 删除原始记录、检测报告等文件定时任务 +* @author hay +* @date 2024/8/21 10:14 +*/ +@Configuration +@EnableScheduling +@Slf4j +@EnableAsync +public class DeleteFileTask { + private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + + /** + * lockAtMostFor属性: + * 设置锁的最大持有时间; + */ + private static final int SCHEDULER_LOCK_MAX = 10 * 60 * 1000; + /** + * lockAtLeastFor属性: + * 指定保留锁的最短时间。 + */ + private static final int SCHEDULER_LOCK_MIN = 5 * 60 * 1000; + + /** + * 删除原始记录、检测报告等文件定时任务 + * 使用 cron 表达式指定每天23点,执行一次 + */ + @Scheduled(cron = "0 0 23 * * *") + @SchedulerLock(name = "StationMonthTask", lockAtMostFor = SCHEDULER_LOCK_MAX, lockAtLeastFor = SCHEDULER_LOCK_MIN) + @Async + public void getHomeCacheTask() { + log.info("--------删除原始记录、检测报告等文件定时任务开启------"); + // 当前日期 + String nowDate = DateTimeHelper.getNowDate(); + // 删除该日期之前的文件夹 + deleteFoldersBeforeDate(nowDate); + log.info("--------删除原始记录、检测报告等文件定时任务执行完毕------"); + } + + // 删除指定目录下所有在当前日期之前的文件夹 + public static void deleteFoldersBeforeDate(String nowDate) { + File dir = new File(SystemUtils.getUploadPath()); + if (!dir.exists() || !dir.isDirectory()) { + log.warn("指定的目录不存在或不是一个目录:" + SystemUtils.getUploadPath()); + return; + } + + File[] files = dir.listFiles(); + if (files != null) { + for (File file : files) { + if (file.isDirectory()) { + try { + // 假设文件夹名符合日期格式(yyyy-MM-dd) + String folderName = file.getName(); + Date folderDate = sdf.parse(folderName); + Date currentDate = sdf.parse(nowDate); + + if (folderDate.before(currentDate)) { + deleteDirectory(file); + } + } catch (ParseException e) { + log.error("日期解析失败,文件夹名称可能不符合预期格式:" + file.getName(), e); + } catch (Exception e) { + log.error("处理文件夹时发生异常:" + file.getName(), e); + } + } + } + } + } + + // 递归删除文件夹 + public static void deleteDirectory(File dir) { + File[] files = dir.listFiles(); + if (files != null) { + for (File file : files) { + if (file.isDirectory()) { + deleteDirectory(file); + } else { + if (!file.delete()) { + log.warn("文件删除失败:" + file.getAbsolutePath()); + } + } + } + } + if (!dir.delete()) { + log.warn("目录删除失败:" + dir.getAbsolutePath()); + } + } +} diff --git a/src/main/resources/mappers/app/QrCodeMapper.xml b/src/main/resources/mappers/app/QrCodeMapper.xml index 3ba83c1..c459d81 100644 --- a/src/main/resources/mappers/app/QrCodeMapper.xml +++ b/src/main/resources/mappers/app/QrCodeMapper.xml @@ -47,7 +47,11 @@