package org.springblade.mdm.task; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.tool.utils.FileUtil; import org.springblade.mdm.basesetting.machine.MachineService; import org.springblade.mdm.basesetting.machine.entity.Machine; 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.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.FileTime; import java.util.Date; import java.util.List; @Slf4j @Component @EnableScheduling public class MachineFileScanTask { @Autowired private MachineFileService machineFileService; @Autowired private MachineService machineService; // 每5秒执行一次 //@Scheduled(fixedRate = 1000000) //@Scheduled(cron = "0 1 0 * * ?") // 每天上午0点1分执行 @Scheduled(cron = "0 15 19 * * ?") //test public void executeEvery5Seconds() { System.out.println("定时任务执行: " + System.currentTimeMillis()); scanMachineFile(); } public void scanMachineFile() { List machines = machineService.lambdaQuery().eq(Machine::getStatus,Machine.STATUS_ENABLE).list(); for (Machine machine : machines) { try { scanDir(machine,MachineFile.DIR_TYPE_REC); }catch(Exception e) { log.error("REC扫描文件异常,机床"+machine.getCode(),e); } try { scanDir(machine,MachineFile.DIR_TYPE_SEND); }catch(Exception e) { log.error("SEND扫描文件异常,机床"+machine.getCode(),e); } try { scanDir(machine,MachineFile.DIR_TYPE_TEMP); }catch(Exception e) { log.error("TEMP扫描文件异常,机床"+machine.getCode(),e); } } } /** * 扫描目录 * @param machine 机床信息 * @param dirType 目录类型 * @throws IOException */ public void scanDir(Machine machine,String dirType) throws IOException { //MachineFile.DIR_TYPE_REC String basePath = MachineFileService.getBasePath(machine,dirType); if(basePath == null) { log.warn("目录类型不匹配:{}",dirType); return; } Path dirPath = Paths.get(basePath); if(!dirPath.toFile().exists()){ log.warn("扫描目录:{} 不存在",dirPath); return; } // 只包含普通文件 List files = Files.list(dirPath) .filter(Files::isRegularFile).toList(); byte[] buffer = new byte[2048]; boolean exists;//文件是否存在于数据库中 for (Path filePath : files) { exists = false; MachineFile mf = new MachineFile(); mf.setTenantId("000000"); mf.setName(filePath.toFile().getName()); mf.setDirType(dirType); mf.setMachineCode(machine.getCode()); try { BasicFileAttributes attrs = Files.readAttributes( filePath, BasicFileAttributes.class ); FileTime creationTime = attrs.creationTime(); Date creationDate = new Date(creationTime.toMillis()); mf.setFileCreateTime(creationDate); FileTime modifyTime = attrs.lastModifiedTime(); mf.setFileModifyDate(new Date(modifyTime.toMillis())); mf.setFileSize(Files.size(filePath)); try (InputStream inputStream = Files.newInputStream(filePath)) { // 使用输入流读取文件内容 int bytesRead = inputStream.read(buffer); mf.setMd5(DigestUtils.md5Hex(buffer)); } catch (IOException e) { log.error("读取文件md5失败",e); continue;//有错误,掠过 } exists = machineFileService.fileExists(mf.getName(),mf.getMd5(),machine.getCode()); System.out.println("文件创建时间: " + creationDate); } catch (IOException e) { log.error("读取文件信息失败",e); } if(!exists) { machineFileService.save(mf); }else{ log.info("文件已如果掠过:{}",filePath.getFileName()); } } } }