zelong.shao

task|文件清理

......@@ -39,5 +39,7 @@ public interface ParamConfigConstants {
String SEND_FAIL_ALERT_INTERVAL = "send_fail_alert_interval";
//错误邮件预警任务显示运单数量
String FAILED_EMAIL_CON_NUMBER = "failed_email_con_number";
//文件清理天数
String FILE_CLEAN_DAYS = "file_clean_days";
}
}
......
......@@ -10,6 +10,7 @@ import java.time.Instant;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
......@@ -643,4 +644,16 @@ public class DateUtil {
//获取到完整的时间
return calendar.getTime();
}
/**
* 获取当前日期减去指定天数
* @param days
* @return
*/
public static String getDateBeforeDays(int days) {
// 获取当前日期减去指定天数
LocalDate dateBefore = LocalDate.now().minusDays(days);
// 格式化日期为字符串
return dateBefore.format(DateTimeFormatter.ofPattern(PATTERN_DATE));
}
}
......
......@@ -3,13 +3,10 @@ package com.fedex.connect.common.dependencies.util;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
......@@ -236,4 +233,33 @@ public class NIOFileUtils {
// 移动文件
Files.move(sPath, tPath, StandardCopyOption.REPLACE_EXISTING);
}
// 删除文件并检查其父目录是否为空
public static boolean deleteFileAndCheckEmptyDirectory(String filePath) {
File file = new File(filePath);
// 删除文件
if (file.exists() && file.isFile()) {
boolean fileDeleted = file.delete();
if (fileDeleted) {
// 如果文件删除成功,检查文件所在的目录是否为空
File parentDir = file.getParentFile();
if (parentDir != null && parentDir.isDirectory()) {
// 如果目录为空,则删除该目录
return deleteDirectoryIfEmpty(parentDir);
}
}
}
return false;
}
// 删除空目录
private static boolean deleteDirectoryIfEmpty(File directory) {
// 如果目录为空,则删除
if (directory.list().length == 0) {
return directory.delete();
}
return false;
}
}
......
package com.fedex.connect.task.job;
import com.fedex.connect.task.service.sys.IFileCleanupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class FileCleanupJob {
@Autowired
private IFileCleanupService fileCleanupService;
/**
* @Author Szl
* @Description 功能说明 任务每天凌晨1点执行,清理90天前的文件数据
* @Date 2024/11/29
* @param
* @return void
*/
//@Scheduled(cron = "${export.task.allocation.fileClean}")
@Scheduled(cron = "${export.task.allocation.sendOb}")
public void FileCleanupTask () {
fileCleanupService.fileCleanup();
}
}
package com.fedex.connect.task.repository.base;
import com.fedex.connect.common.dao.biz.AttachmentMapper;
import com.fedex.connect.common.dao.biz.ConsignmentMapper;
import com.fedex.connect.common.dao.biz.EmailMapper;
import com.fedex.connect.common.dao.biz.PushObMapper;
......@@ -41,4 +42,6 @@ public class AbstractDaoRepository {
protected ConsignmentMapper consignmentMapper;
@Autowired
protected EmailHistoryExtMapper emailHistoryExtMapper;
@Autowired
protected AttachmentMapper attachmentMapper;
}
\ No newline at end of file
......
......@@ -19,4 +19,13 @@ public interface AttachmentExtMapper {
"(SELECT MAX(ID) FROM T_BIZ_UPLOAD_RECORD WHERE " +
" CONSIGNMENT_ID = #{consignmentId}) AND STATUS = 1")
List<Attachment> queryAttachmentInfo(@Param("consignmentId") Long consignmentId);
/**
* 根据修改日期查询数据
* @param date
* @return
*/
@Select("SELECT * FROM T_BIZ_ATTACHMENT " +
"WHERE MODIFY_TIME < TO_DATE(#{date}, 'YYYY-MM-DD') AND STATUS = 1")
List<Attachment> findAttachmentsOlderThan(@Param("date") String date);
}
......
......@@ -11,4 +11,8 @@ public interface IAttachmentRepository {
List<PushZipEmailDto> findAttachmentDetailsByBizId(@Param("bizId") Long bizId);
List<Attachment> queryAttachmentInfo(Long consignmentId);
List<Attachment> findAttachmentsOlderThan(String date);
void saveOrUpdateAll(List<Attachment> list);
}
......
......@@ -4,7 +4,6 @@ import com.fedex.connect.common.model.biz.Attachment;
import com.fedex.connect.task.data.dto.PushZipEmailDto;
import com.fedex.connect.task.repository.base.AbstractDaoRepository;
import com.fedex.connect.task.repository.repo.biz.IAttachmentRepository;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
......@@ -21,4 +20,20 @@ public class AttachmentRepositoryImpl extends AbstractDaoRepository implements I
public List<Attachment> queryAttachmentInfo(Long consignmentId){
return attachmentExtMapper.queryAttachmentInfo(consignmentId);
}
@Override
public List<Attachment> findAttachmentsOlderThan(String date) {
return attachmentExtMapper.findAttachmentsOlderThan(date);
}
@Override
public void saveOrUpdateAll(List<Attachment> list) {
list.stream().forEach(p->{
if(p.getId() == null || p.getId().longValue() <=0){
attachmentMapper.insertSelective(p);
}else if(p.getId() != null && p.getId().longValue() >0){
attachmentMapper.updateByPrimaryKeySelective(p);
}
});
}
}
......
package com.fedex.connect.task.service.sys;
public interface IFileCleanupService {
void fileCleanup();
}
package com.fedex.connect.task.service.sys.impl;
import com.fedex.connect.common.dependencies.contants.ParamConfigConstants;
import com.fedex.connect.common.model.sys.ParamConfig;
import com.fedex.connect.task.service.base.BaseService;
import com.fedex.connect.task.service.sys.IFileCleanupService;
import com.fedex.connect.task.utils.sys.FileCleanupUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class FileCleanupServiceImpl extends BaseService implements IFileCleanupService {
@Autowired
private FileCleanupUtil fileCleanupUtil;
@Override
public void fileCleanup(){
/**
* 清理运单文件
*/
ParamConfig valueByCode = paramConfigRepository.findValueByCode(ParamConfigConstants.TASK_PARAM_KEYS.FILE_CLEAN_DAYS);
fileCleanupUtil.consignmentPicCleanup(valueByCode);
/**
* 清理邮件zip包
*/
fileCleanupUtil.emailZipCleanup(valueByCode.getValue());
}
}
package com.fedex.connect.task.utils.sys;
import com.fedex.connect.common.dependencies.contants.DigitConstants;
import com.fedex.connect.common.dependencies.util.DateUtil;
import com.fedex.connect.common.dependencies.util.NIOFileUtils;
import com.fedex.connect.common.model.biz.Attachment;
import com.fedex.connect.common.model.sys.ParamConfig;
import com.fedex.connect.task.repository.repo.biz.IAttachmentRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.List;
@Component
public class FileCleanupUtil {
private static Logger logger = LoggerFactory.getLogger(FileCleanupUtil.class);
@Autowired
protected IAttachmentRepository attachmentRepository;
/**
* @Author Szl
* @Description 功能说明 清理运单图片
* @Date 2024/11/29
* @param valueByCode
* @return void
*/
public void consignmentPicCleanup(ParamConfig valueByCode){
String dateBeforeDays = DateUtil.getDateBeforeDays(Integer.parseInt(valueByCode.getValue()));
List<Attachment> attachmentsOlderThan = attachmentRepository.findAttachmentsOlderThan(dateBeforeDays);
/**
* 删除文件
*/
for (Attachment attachment : attachmentsOlderThan) {
NIOFileUtils.deleteFileAndCheckEmptyDirectory(attachment.getFilePath());
attachment.setStatus(DigitConstants.DIGIT_ZERO_LONG);
}
/**
* 更新数据库
*/
attachmentRepository.saveOrUpdateAll(attachmentsOlderThan);
}
/**
* @Author Szl
* @Description 功能说明 清理邮件zip
* @Date 2024/11/29
* @param days
* @return void
*/
public void emailZipCleanup(String days){
/**
* todo 待确认目录
*/
String path = "D:\\test";
long specifiedTimesMillis = this.getSpecifiedTimesMillis(Long.parseLong(days));
this.cleanFiles(path,specifiedTimesMillis);
}
/**
* 删除指定天数之前的文件
*/
private void cleanFiles(String folderPath, long specifiedTimeMillis) {
File folder = new File(folderPath);
deleteFilesByTimeMillis(folder, specifiedTimeMillis);
deleteEmptyFolders(folder);
}
private void deleteFilesByTimeMillis(File folder, long specifiedTimeMillis) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
deleteFilesByTimeMillis(file, specifiedTimeMillis);
} else {
if (file.lastModified() < specifiedTimeMillis) {
try{
file.delete();
}catch(Exception ex){
logger.error("delefile error filePath : " + file.getPath() + " fileName : " + file.getName() , ex);
}
}
}
}
}
}
private void deleteEmptyFolders(File folder) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
deleteEmptyFolders(file);
if (file.listFiles().length == 0) {
try{
file.delete();
}catch(Exception ex){
logger.error("delefile error filePath : " + file.getPath() + " fileName : " + file.getName() , ex);
}
}
}
}
}
}
/**
* 获取规定时间毫秒值
*
* @param specifiedTime 规定天数
*/
private long getSpecifiedTimesMillis(long specifiedTime) {
long currentTimeMillis = System.currentTimeMillis();
long oneDayInMillis = 24 * 60 * 60 * 1000;
return currentTimeMillis - (specifiedTime * oneDayInMillis);
}
}
......@@ -20,6 +20,8 @@ export:
sendObRetry: 0 0/1 * * * ?
#发送邮件到预清关组
pushConFile: 0 0/1 * * * ?
#文件清理任务
fileClean: 0 1 * * *
mail:
smtp:
host: smtp.qiye.aliyun.com
......