zelong.shao

task|文件清理

...@@ -39,5 +39,7 @@ public interface ParamConfigConstants { ...@@ -39,5 +39,7 @@ public interface ParamConfigConstants {
39 String SEND_FAIL_ALERT_INTERVAL = "send_fail_alert_interval"; 39 String SEND_FAIL_ALERT_INTERVAL = "send_fail_alert_interval";
40 //错误邮件预警任务显示运单数量 40 //错误邮件预警任务显示运单数量
41 String FAILED_EMAIL_CON_NUMBER = "failed_email_con_number"; 41 String FAILED_EMAIL_CON_NUMBER = "failed_email_con_number";
42 + //文件清理天数
43 + String FILE_CLEAN_DAYS = "file_clean_days";
42 } 44 }
43 } 45 }
......
...@@ -10,6 +10,7 @@ import java.time.Instant; ...@@ -10,6 +10,7 @@ import java.time.Instant;
10 import java.time.LocalDate; 10 import java.time.LocalDate;
11 import java.time.Period; 11 import java.time.Period;
12 import java.time.ZoneId; 12 import java.time.ZoneId;
13 +import java.time.format.DateTimeFormatter;
13 import java.util.*; 14 import java.util.*;
14 import java.util.stream.Collectors; 15 import java.util.stream.Collectors;
15 16
...@@ -643,4 +644,16 @@ public class DateUtil { ...@@ -643,4 +644,16 @@ public class DateUtil {
643 //获取到完整的时间 644 //获取到完整的时间
644 return calendar.getTime(); 645 return calendar.getTime();
645 } 646 }
647 +
648 + /**
649 + * 获取当前日期减去指定天数
650 + * @param days
651 + * @return
652 + */
653 + public static String getDateBeforeDays(int days) {
654 + // 获取当前日期减去指定天数
655 + LocalDate dateBefore = LocalDate.now().minusDays(days);
656 + // 格式化日期为字符串
657 + return dateBefore.format(DateTimeFormatter.ofPattern(PATTERN_DATE));
658 + }
646 } 659 }
......
...@@ -3,13 +3,10 @@ package com.fedex.connect.common.dependencies.util; ...@@ -3,13 +3,10 @@ package com.fedex.connect.common.dependencies.util;
3 import org.slf4j.Logger; 3 import org.slf4j.Logger;
4 import org.springframework.stereotype.Component; 4 import org.springframework.stereotype.Component;
5 import org.springframework.util.CollectionUtils; 5 import org.springframework.util.CollectionUtils;
6 -import org.springframework.web.multipart.MultipartFile;
7 6
8 import java.io.*; 7 import java.io.*;
9 import java.nio.file.*; 8 import java.nio.file.*;
10 import java.nio.file.attribute.BasicFileAttributes; 9 import java.nio.file.attribute.BasicFileAttributes;
11 -import java.time.LocalDateTime;
12 -import java.time.format.DateTimeFormatter;
13 import java.util.ArrayList; 10 import java.util.ArrayList;
14 import java.util.List; 11 import java.util.List;
15 import java.util.zip.ZipEntry; 12 import java.util.zip.ZipEntry;
...@@ -236,4 +233,33 @@ public class NIOFileUtils { ...@@ -236,4 +233,33 @@ public class NIOFileUtils {
236 // 移动文件 233 // 移动文件
237 Files.move(sPath, tPath, StandardCopyOption.REPLACE_EXISTING); 234 Files.move(sPath, tPath, StandardCopyOption.REPLACE_EXISTING);
238 } 235 }
236 +
237 +
238 + // 删除文件并检查其父目录是否为空
239 + public static boolean deleteFileAndCheckEmptyDirectory(String filePath) {
240 + File file = new File(filePath);
241 +
242 + // 删除文件
243 + if (file.exists() && file.isFile()) {
244 + boolean fileDeleted = file.delete();
245 + if (fileDeleted) {
246 + // 如果文件删除成功,检查文件所在的目录是否为空
247 + File parentDir = file.getParentFile();
248 + if (parentDir != null && parentDir.isDirectory()) {
249 + // 如果目录为空,则删除该目录
250 + return deleteDirectoryIfEmpty(parentDir);
251 + }
252 + }
253 + }
254 + return false;
255 + }
256 +
257 + // 删除空目录
258 + private static boolean deleteDirectoryIfEmpty(File directory) {
259 + // 如果目录为空,则删除
260 + if (directory.list().length == 0) {
261 + return directory.delete();
262 + }
263 + return false;
264 + }
239 } 265 }
......
1 +package com.fedex.connect.task.job;
2 +
3 +import com.fedex.connect.task.service.sys.IFileCleanupService;
4 +import org.springframework.beans.factory.annotation.Autowired;
5 +import org.springframework.scheduling.annotation.Scheduled;
6 +import org.springframework.stereotype.Component;
7 +
8 +@Component
9 +public class FileCleanupJob {
10 +
11 + @Autowired
12 + private IFileCleanupService fileCleanupService;
13 +
14 +
15 + /**
16 + * @Author Szl
17 + * @Description 功能说明 任务每天凌晨1点执行,清理90天前的文件数据
18 + * @Date 2024/11/29
19 + * @param
20 + * @return void
21 + */
22 + //@Scheduled(cron = "${export.task.allocation.fileClean}")
23 + @Scheduled(cron = "${export.task.allocation.sendOb}")
24 + public void FileCleanupTask () {
25 + fileCleanupService.fileCleanup();
26 + }
27 +}
1 package com.fedex.connect.task.repository.base; 1 package com.fedex.connect.task.repository.base;
2 2
3 +import com.fedex.connect.common.dao.biz.AttachmentMapper;
3 import com.fedex.connect.common.dao.biz.ConsignmentMapper; 4 import com.fedex.connect.common.dao.biz.ConsignmentMapper;
4 import com.fedex.connect.common.dao.biz.EmailMapper; 5 import com.fedex.connect.common.dao.biz.EmailMapper;
5 import com.fedex.connect.common.dao.biz.PushObMapper; 6 import com.fedex.connect.common.dao.biz.PushObMapper;
...@@ -41,4 +42,6 @@ public class AbstractDaoRepository { ...@@ -41,4 +42,6 @@ public class AbstractDaoRepository {
41 protected ConsignmentMapper consignmentMapper; 42 protected ConsignmentMapper consignmentMapper;
42 @Autowired 43 @Autowired
43 protected EmailHistoryExtMapper emailHistoryExtMapper; 44 protected EmailHistoryExtMapper emailHistoryExtMapper;
45 + @Autowired
46 + protected AttachmentMapper attachmentMapper;
44 } 47 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -19,4 +19,13 @@ public interface AttachmentExtMapper { ...@@ -19,4 +19,13 @@ public interface AttachmentExtMapper {
19 "(SELECT MAX(ID) FROM T_BIZ_UPLOAD_RECORD WHERE " + 19 "(SELECT MAX(ID) FROM T_BIZ_UPLOAD_RECORD WHERE " +
20 " CONSIGNMENT_ID = #{consignmentId}) AND STATUS = 1") 20 " CONSIGNMENT_ID = #{consignmentId}) AND STATUS = 1")
21 List<Attachment> queryAttachmentInfo(@Param("consignmentId") Long consignmentId); 21 List<Attachment> queryAttachmentInfo(@Param("consignmentId") Long consignmentId);
22 +
23 + /**
24 + * 根据修改日期查询数据
25 + * @param date
26 + * @return
27 + */
28 + @Select("SELECT * FROM T_BIZ_ATTACHMENT " +
29 + "WHERE MODIFY_TIME < TO_DATE(#{date}, 'YYYY-MM-DD') AND STATUS = 1")
30 + List<Attachment> findAttachmentsOlderThan(@Param("date") String date);
22 } 31 }
......
...@@ -11,4 +11,8 @@ public interface IAttachmentRepository { ...@@ -11,4 +11,8 @@ public interface IAttachmentRepository {
11 List<PushZipEmailDto> findAttachmentDetailsByBizId(@Param("bizId") Long bizId); 11 List<PushZipEmailDto> findAttachmentDetailsByBizId(@Param("bizId") Long bizId);
12 12
13 List<Attachment> queryAttachmentInfo(Long consignmentId); 13 List<Attachment> queryAttachmentInfo(Long consignmentId);
14 +
15 + List<Attachment> findAttachmentsOlderThan(String date);
16 +
17 + void saveOrUpdateAll(List<Attachment> list);
14 } 18 }
......
...@@ -4,7 +4,6 @@ import com.fedex.connect.common.model.biz.Attachment; ...@@ -4,7 +4,6 @@ import com.fedex.connect.common.model.biz.Attachment;
4 import com.fedex.connect.task.data.dto.PushZipEmailDto; 4 import com.fedex.connect.task.data.dto.PushZipEmailDto;
5 import com.fedex.connect.task.repository.base.AbstractDaoRepository; 5 import com.fedex.connect.task.repository.base.AbstractDaoRepository;
6 import com.fedex.connect.task.repository.repo.biz.IAttachmentRepository; 6 import com.fedex.connect.task.repository.repo.biz.IAttachmentRepository;
7 -import org.apache.ibatis.annotations.Param;
8 import org.springframework.stereotype.Repository; 7 import org.springframework.stereotype.Repository;
9 8
10 import java.util.List; 9 import java.util.List;
...@@ -21,4 +20,20 @@ public class AttachmentRepositoryImpl extends AbstractDaoRepository implements I ...@@ -21,4 +20,20 @@ public class AttachmentRepositoryImpl extends AbstractDaoRepository implements I
21 public List<Attachment> queryAttachmentInfo(Long consignmentId){ 20 public List<Attachment> queryAttachmentInfo(Long consignmentId){
22 return attachmentExtMapper.queryAttachmentInfo(consignmentId); 21 return attachmentExtMapper.queryAttachmentInfo(consignmentId);
23 } 22 }
23 +
24 + @Override
25 + public List<Attachment> findAttachmentsOlderThan(String date) {
26 + return attachmentExtMapper.findAttachmentsOlderThan(date);
27 + }
28 +
29 + @Override
30 + public void saveOrUpdateAll(List<Attachment> list) {
31 + list.stream().forEach(p->{
32 + if(p.getId() == null || p.getId().longValue() <=0){
33 + attachmentMapper.insertSelective(p);
34 + }else if(p.getId() != null && p.getId().longValue() >0){
35 + attachmentMapper.updateByPrimaryKeySelective(p);
36 + }
37 + });
38 + }
24 } 39 }
......
1 +package com.fedex.connect.task.service.sys;
2 +
3 +public interface IFileCleanupService {
4 + void fileCleanup();
5 +}
1 +package com.fedex.connect.task.service.sys.impl;
2 +
3 +import com.fedex.connect.common.dependencies.contants.ParamConfigConstants;
4 +import com.fedex.connect.common.model.sys.ParamConfig;
5 +import com.fedex.connect.task.service.base.BaseService;
6 +import com.fedex.connect.task.service.sys.IFileCleanupService;
7 +import com.fedex.connect.task.utils.sys.FileCleanupUtil;
8 +import org.springframework.beans.factory.annotation.Autowired;
9 +import org.springframework.stereotype.Service;
10 +
11 +@Service
12 +public class FileCleanupServiceImpl extends BaseService implements IFileCleanupService {
13 +
14 + @Autowired
15 + private FileCleanupUtil fileCleanupUtil;
16 +
17 + @Override
18 + public void fileCleanup(){
19 + /**
20 + * 清理运单文件
21 + */
22 + ParamConfig valueByCode = paramConfigRepository.findValueByCode(ParamConfigConstants.TASK_PARAM_KEYS.FILE_CLEAN_DAYS);
23 + fileCleanupUtil.consignmentPicCleanup(valueByCode);
24 + /**
25 + * 清理邮件zip包
26 + */
27 + fileCleanupUtil.emailZipCleanup(valueByCode.getValue());
28 + }
29 +}
1 +package com.fedex.connect.task.utils.sys;
2 +
3 +import com.fedex.connect.common.dependencies.contants.DigitConstants;
4 +import com.fedex.connect.common.dependencies.util.DateUtil;
5 +import com.fedex.connect.common.dependencies.util.NIOFileUtils;
6 +import com.fedex.connect.common.model.biz.Attachment;
7 +import com.fedex.connect.common.model.sys.ParamConfig;
8 +import com.fedex.connect.task.repository.repo.biz.IAttachmentRepository;
9 +import org.slf4j.Logger;
10 +import org.slf4j.LoggerFactory;
11 +import org.springframework.beans.factory.annotation.Autowired;
12 +import org.springframework.stereotype.Component;
13 +
14 +import java.io.File;
15 +import java.util.List;
16 +
17 +@Component
18 +public class FileCleanupUtil {
19 +
20 + private static Logger logger = LoggerFactory.getLogger(FileCleanupUtil.class);
21 + @Autowired
22 + protected IAttachmentRepository attachmentRepository;
23 +
24 + /**
25 + * @Author Szl
26 + * @Description 功能说明 清理运单图片
27 + * @Date 2024/11/29
28 + * @param valueByCode
29 + * @return void
30 + */
31 + public void consignmentPicCleanup(ParamConfig valueByCode){
32 + String dateBeforeDays = DateUtil.getDateBeforeDays(Integer.parseInt(valueByCode.getValue()));
33 + List<Attachment> attachmentsOlderThan = attachmentRepository.findAttachmentsOlderThan(dateBeforeDays);
34 + /**
35 + * 删除文件
36 + */
37 + for (Attachment attachment : attachmentsOlderThan) {
38 + NIOFileUtils.deleteFileAndCheckEmptyDirectory(attachment.getFilePath());
39 + attachment.setStatus(DigitConstants.DIGIT_ZERO_LONG);
40 + }
41 + /**
42 + * 更新数据库
43 + */
44 + attachmentRepository.saveOrUpdateAll(attachmentsOlderThan);
45 + }
46 +
47 + /**
48 + * @Author Szl
49 + * @Description 功能说明 清理邮件zip
50 + * @Date 2024/11/29
51 + * @param days
52 + * @return void
53 + */
54 + public void emailZipCleanup(String days){
55 + /**
56 + * todo 待确认目录
57 + */
58 + String path = "D:\\test";
59 + long specifiedTimesMillis = this.getSpecifiedTimesMillis(Long.parseLong(days));
60 + this.cleanFiles(path,specifiedTimesMillis);
61 + }
62 +
63 + /**
64 + * 删除指定天数之前的文件
65 + */
66 + private void cleanFiles(String folderPath, long specifiedTimeMillis) {
67 + File folder = new File(folderPath);
68 + deleteFilesByTimeMillis(folder, specifiedTimeMillis);
69 + deleteEmptyFolders(folder);
70 + }
71 +
72 + private void deleteFilesByTimeMillis(File folder, long specifiedTimeMillis) {
73 + File[] files = folder.listFiles();
74 + if (files != null) {
75 + for (File file : files) {
76 + if (file.isDirectory()) {
77 + deleteFilesByTimeMillis(file, specifiedTimeMillis);
78 + } else {
79 + if (file.lastModified() < specifiedTimeMillis) {
80 + try{
81 + file.delete();
82 + }catch(Exception ex){
83 + logger.error("delefile error filePath : " + file.getPath() + " fileName : " + file.getName() , ex);
84 + }
85 + }
86 + }
87 + }
88 + }
89 + }
90 +
91 + private void deleteEmptyFolders(File folder) {
92 + File[] files = folder.listFiles();
93 + if (files != null) {
94 + for (File file : files) {
95 + if (file.isDirectory()) {
96 + deleteEmptyFolders(file);
97 + if (file.listFiles().length == 0) {
98 + try{
99 + file.delete();
100 + }catch(Exception ex){
101 + logger.error("delefile error filePath : " + file.getPath() + " fileName : " + file.getName() , ex);
102 + }
103 + }
104 + }
105 + }
106 + }
107 + }
108 +
109 + /**
110 + * 获取规定时间毫秒值
111 + *
112 + * @param specifiedTime 规定天数
113 + */
114 + private long getSpecifiedTimesMillis(long specifiedTime) {
115 + long currentTimeMillis = System.currentTimeMillis();
116 + long oneDayInMillis = 24 * 60 * 60 * 1000;
117 + return currentTimeMillis - (specifiedTime * oneDayInMillis);
118 + }
119 +
120 +}
...@@ -20,6 +20,8 @@ export: ...@@ -20,6 +20,8 @@ export:
20 sendObRetry: 0 0/1 * * * ? 20 sendObRetry: 0 0/1 * * * ?
21 #发送邮件到预清关组 21 #发送邮件到预清关组
22 pushConFile: 0 0/1 * * * ? 22 pushConFile: 0 0/1 * * * ?
23 + #文件清理任务
24 + fileClean: 0 1 * * *
23 mail: 25 mail:
24 smtp: 26 smtp:
25 host: smtp.qiye.aliyun.com 27 host: smtp.qiye.aliyun.com
......