This commit is contained in:
hayu 2024-08-21 13:34:30 +08:00
parent be007a8d52
commit 0ba7826693
5 changed files with 133 additions and 55 deletions

View File

@ -17,6 +17,12 @@
<java.version>1.8</java.version> <java.version>1.8</java.version>
</properties> </properties>
<dependencies> <dependencies>
<!-- 负载均衡定时任务执行一次 -->
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-spring</artifactId>
<version>2.2.1</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId> <artifactId>spring-boot-starter-freemarker</artifactId>

View File

@ -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.dto.ParamsDto;
import com.bonus.aqgqj.basis.entity.vo.*; import com.bonus.aqgqj.basis.entity.vo.*;
import com.bonus.aqgqj.system.vo.EncryptedReq; import com.bonus.aqgqj.system.vo.EncryptedReq;
import com.bonus.aqgqj.utils.ExcelStyleUtil; import com.bonus.aqgqj.utils.*;
import com.bonus.aqgqj.utils.FileUtil;
import com.bonus.aqgqj.utils.ServerResponse;
import com.bonus.aqgqj.utils.SystemUtils;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
@ -169,7 +166,7 @@ public class PrimaryDataAppController {
public ServerResponse downLoadExcelZip(HttpServletRequest request, HttpServletResponse response, EncryptedReq<ParamsDto> dto) { public ServerResponse downLoadExcelZip(HttpServletRequest request, HttpServletResponse response, EncryptedReq<ParamsDto> dto) {
List<File> fileList = new ArrayList(); List<File> fileList = new ArrayList();
List<String> fileNameList = new ArrayList(); List<String> fileNameList = new ArrayList();
String fileNames="检测原始记录_" + System.currentTimeMillis()+".zip"; String fileNames= DateTimeHelper.getNowDate()+ "/检测原始记录_" + System.currentTimeMillis()+".zip";
String outputZipFilePath = SystemUtils.getUploadPath()+ fileNames; String outputZipFilePath = SystemUtils.getUploadPath()+ fileNames;
try { try {
Long[] ids = dto.getData().getIds(); Long[] ids = dto.getData().getIds();
@ -181,7 +178,6 @@ public class PrimaryDataAppController {
fileNameList.add(excelModelPath); fileNameList.add(excelModelPath);
} }
} }
// downloadZip(response, fileList);
zipFiles(fileList, outputZipFilePath); zipFiles(fileList, outputZipFilePath);
} }
return ServerResponse.createSuccess("statics/"+fileNames); return ServerResponse.createSuccess("statics/"+fileNames);
@ -252,31 +248,11 @@ public class PrimaryDataAppController {
} }
return null; return null;
} }
public void downloadZip(HttpServletResponse response, List<File> 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<File> filesToZip, String outputZipFilePath) throws IOException { public void zipFiles(List<File> filesToZip, String outputZipFilePath) throws IOException {
File outputFile = new File(outputZipFilePath); File outputFile = new File(outputZipFilePath);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outputFile))) { try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outputFile))) {
for (File file : filesToZip) { for (File file : filesToZip) {
try (FileInputStream fis = new FileInputStream(file)) { try (FileInputStream fis = new FileInputStream(file)) {

View File

@ -229,7 +229,7 @@ public class TestReportManageAppServiceImpl implements TestReportManageAppServic
List<File> fileList = new ArrayList(); List<File> fileList = new ArrayList();
JSONArray jsonArray = JSONObject.parseArray(data.getParamsList()); JSONArray jsonArray = JSONObject.parseArray(data.getParamsList());
List<TestReportManageAppVo> list = jsonArray.toJavaList(TestReportManageAppVo.class); List<TestReportManageAppVo> list = jsonArray.toJavaList(TestReportManageAppVo.class);
String fileNames="检测报告_" + System.currentTimeMillis()+".zip"; String fileNames=DateTimeHelper.getNowDate()+ "/检测报告_" + System.currentTimeMillis()+".zip";
String outputZipFilePath = SystemUtils.getUploadPath()+ fileNames; String outputZipFilePath = SystemUtils.getUploadPath()+ fileNames;
if (list != null && list.size() > 0) { if (list != null && list.size() > 0) {
try { try {
@ -247,7 +247,6 @@ public class TestReportManageAppServiceImpl implements TestReportManageAppServic
} }
} }
} }
// downloadZip(response, fileList);
zipFiles(fileList, outputZipFilePath); zipFiles(fileList, outputZipFilePath);
return ServerResponse.createSuccess("statics/"+fileNames); return ServerResponse.createSuccess("statics/"+fileNames);
}catch (Exception e){ }catch (Exception e){
@ -691,32 +690,11 @@ public class TestReportManageAppServiceImpl implements TestReportManageAppServic
String code = nowDate + customerCode; String code = nowDate + customerCode;
return code; return code;
} }
public void downloadZip(HttpServletResponse response, List<File> 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<File> filesToZip, String outputZipFilePath) throws IOException { public void zipFiles(List<File> filesToZip, String outputZipFilePath) throws IOException {
File outputFile = new File(outputZipFilePath); File outputFile = new File(outputZipFilePath);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outputFile))) { try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outputFile))) {
for (File file : filesToZip) { for (File file : filesToZip) {
try (FileInputStream fis = new FileInputStream(file)) { try (FileInputStream fis = new FileInputStream(file)) {

View File

@ -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());
}
}
}

View File

@ -47,7 +47,11 @@
<select id="getCertificateData" resultType="com.bonus.aqgqj.app.entity.SamplesManageAppVo"> <select id="getCertificateData" resultType="com.bonus.aqgqj.app.entity.SamplesManageAppVo">
SELECT SELECT
ted.id, ted.id,
ts.custom_id as customId,
tsd.department_id as departmentId,
tcs.custom_name as sampleDepartment,
te.submit_location as submitLocation, te.submit_location as submitLocation,
sd.id as sampleToolsId,
tsd.dev_type_name as sampleTools, tsd.dev_type_name as sampleTools,
tsd.dev_module as devModule, tsd.dev_module as devModule,
ted.dev_code as devCode, ted.dev_code as devCode,
@ -64,8 +68,10 @@
LEFT JOIN tb_sample_device tsd on ted.dev_id=tsd.id and tsd.del_falg=0 LEFT JOIN tb_sample_device tsd on ted.dev_id=tsd.id and tsd.del_falg=0
LEFT JOIN tb_sample ts on ts.id=tsd.sample_id and ts.del_flag=0 LEFT JOIN tb_sample ts on ts.id=tsd.sample_id and ts.del_flag=0
LEFT JOIN tb_custom tc on tc.id=ts.custom_id and tc.del_flag=0 LEFT JOIN tb_custom tc on tc.id=ts.custom_id and tc.del_flag=0
LEFT JOIN tb_custom tcs on tcs.id=tsd.department_id and tcs.del_flag=0
LEFT JOIN tb_exper te on te.id=ted.exper_id LEFT JOIN tb_exper te on te.id=ted.exper_id
LEFT JOIN sys_user su on su.id=te.create_user and su.del_flag=0 LEFT JOIN sys_user su on su.id=te.create_user and su.del_flag=0
LEFT JOIN sys_distinct sd on sd.dict_name=tsd.dev_type_name and sd.del_flag=0
WHERE WHERE
tsd.dev_type_name is not null tsd.dev_type_name is not null
and te.`status`=4 and te.`status`=4