package org.springblade.mdm.task;
|
|
import lombok.extern.slf4j.Slf4j;
|
import org.springblade.mdm.basesetting.machine.service.MachineService;
|
import org.springblade.mdm.basesetting.machine.entity.Machine;
|
import org.springblade.mdm.commons.service.ParamService;
|
import org.springblade.mdm.machinefile.entity.MachineFile;
|
import org.springblade.mdm.machinefile.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.nio.file.StandardCopyOption;
|
import java.time.LocalDateTime;
|
import java.util.List;
|
|
/**
|
* 机床回传文件夹rec,目录文件超过机床设定的时间移动到temp文件夹
|
*/
|
@Slf4j
|
@Component
|
@EnableScheduling
|
public class ReceiveDirMoveTask {
|
@Autowired
|
private MachineFileService machineFileService;
|
@Autowired
|
private MachineService machineService;
|
@Autowired
|
private ParamService paramService;
|
/**
|
* 文件默认保存小时数,0不限制
|
*/
|
private static final int DEFAULT_HOUR = 0;
|
|
@Scheduled(cron = "${task.cron.machine_rec_move:0 3 * * * ?}")
|
public void execute() {
|
String networkType = paramService.getParamValue(ParamService.NETWORK_TYPE,ParamService.NETWORK_TYPE_SHEMI);
|
|
if(!ParamService.NETWORK_TYPE_SHEMI.equals(networkType)){
|
//非涉密网,才扫描目录文件
|
moveValidateFiles();
|
}
|
|
}
|
|
/**
|
* 扫描数据库记录,超时则移动文件
|
*/
|
public void moveValidateFiles() {
|
List<Machine> machines = machineService.getEnableMachines();
|
LocalDateTime now = LocalDateTime.now();
|
for (Machine machine : machines) {
|
int remainHours = machine.getReceiveDirExpiryHours() != null ?machine.getReceiveDirExpiryHours():DEFAULT_HOUR;
|
/*if(remainHours == 0){
|
continue;
|
}*/
|
//LocalDateTime earlyTime = now.minusHours(remainHours);
|
List<MachineFile> pendingFiles = this.machineFileService.lambdaQuery()
|
.eq(MachineFile::getDirType,MachineFile.DIR_TYPE_REC)
|
.eq(MachineFile::getExceptionType,MachineFile.EXCEPTION_OK)
|
.eq(MachineFile::getMachineCode,machine.getCode()).list();
|
//.lt(MachineFile::getFileCreateTime,earlyTime)
|
|
for(MachineFile overTimeFile : pendingFiles){
|
try {
|
moveFileToTemp(overTimeFile, machine);
|
}catch(Exception e){
|
log.error("移动rec文件失败:{}",overTimeFile.getName(),e);
|
}
|
}
|
|
}
|
|
}
|
|
/**
|
* 文件移动到temp
|
* @param overTimeFile
|
*/
|
private void moveFileToTemp(MachineFile overTimeFile,Machine machine) throws IOException {
|
String srcFilepath = MachineFileService.getBasePath(machine,overTimeFile.getDirType())+ File.separator+overTimeFile.getName();
|
Path source = Paths.get(srcFilepath);
|
if(!source.toFile().exists()){
|
return;
|
}
|
String targetFilepath = MachineFileService.getBasePath(machine,MachineFile.DIR_TYPE_TEMP)+ File.separator+overTimeFile.getName();
|
Path target = Paths.get(targetFilepath);
|
|
Path targetDir = target.getParent();
|
if (targetDir != null && !Files.exists(targetDir)) {
|
Files.createDirectories(targetDir);
|
}
|
|
// 使用 REPLACE_EXISTING 选项来覆盖已存在的文件
|
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
|
}
|
}
|