package org.springblade.mdm.task;
|
|
import lombok.extern.slf4j.Slf4j;
|
import org.springblade.mdm.basesetting.machine.MachineService;
|
import org.springblade.mdm.basesetting.machine.entity.Machine;
|
import org.springblade.mdm.commons.service.ParamService;
|
import org.springblade.mdm.gkw.programnode.entity.MachineFile;
|
import org.springblade.mdm.gkw.programnode.service.MachineFileService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.nio.file.Files;
|
import java.nio.file.Path;
|
import java.nio.file.Paths;
|
import java.time.LocalDateTime;
|
import java.util.List;
|
|
/**
|
* 机床回传文件夹send,目录文件超过机床设定的时间清除
|
*/
|
@Slf4j
|
@Component
|
@EnableScheduling
|
public class SendDirCleanTask {
|
@Autowired
|
private MachineFileService machineFileService;
|
@Autowired
|
private MachineService machineService;
|
@Autowired
|
private ParamService paramService;
|
/**
|
* 文件默认保存小时数
|
*/
|
private static final int DEFAULT_HOUR = 0;
|
// 每5秒执行一次
|
//@Scheduled(fixedRate = 1000000)
|
//@Scheduled(cron = "0 1 0 * * ?") // 每天上午0点1分执行
|
//@Scheduled(cron = "0 */3 * * * ?")
|
//@Scheduled(cron = "0 15 19 * * ?") //test
|
//@Scheduled(cron = "${task.cron.machine_file_scan}")
|
@Scheduled(cron = "${task.cron.machine_send_clean:0 0 * * * ?}")
|
public void execute() {
|
String networkType = paramService.getParamValue(ParamService.NETWORK_TYPE,ParamService.NETWORK_TYPE_SHEMI);
|
|
if(!ParamService.NETWORK_TYPE_SHEMI.equals(networkType)){
|
//非涉密网,才扫描目录文件
|
cleanOverTimeFiles();
|
}
|
|
}
|
|
/**
|
* 扫描数据库记录,超时则移动文件
|
*/
|
public void cleanOverTimeFiles() {
|
List<Machine> machines = machineService.getEnableMachines();
|
LocalDateTime now = LocalDateTime.now();
|
for (Machine machine : machines) {
|
int remainHours = machine.getReceiveDirExpiryHours() != null ?machine.getSendDirExpiryHours():DEFAULT_HOUR;
|
if(remainHours == 0){
|
continue;
|
}
|
LocalDateTime earlyTime = now.minusHours(remainHours);
|
List<MachineFile> overTimeFiles = this.machineFileService.lambdaQuery().lt(MachineFile::getFileCreateTime,earlyTime)
|
.eq(MachineFile::getDirType,MachineFile.DIR_TYPE_SEND)
|
.eq(MachineFile::getMachineCode,machine.getCode()).list();
|
|
for(MachineFile overTimeFile : overTimeFiles){
|
try {
|
cleanFile(overTimeFile, machine);
|
}catch(Exception e){
|
log.error("移动rec文件失败:"+overTimeFile.getName(),e);
|
}
|
}
|
|
}
|
|
}
|
|
/**
|
* 文件删除
|
* @param overTimeFile
|
* @machine 所属机床
|
*/
|
private void cleanFile(MachineFile overTimeFile,Machine machine) throws IOException {
|
String targetFilepath = MachineFileService.getBasePath(machine,overTimeFile.getDirType())+ File.separator+overTimeFile.getName();
|
Path filepath = Paths.get(targetFilepath);
|
if(!filepath.toFile().exists()){
|
return;
|
}
|
Files.delete(filepath);
|
}
|
|
|
}
|