| | |
| | | package org.springblade.mdm.task; |
| | | |
| | | import com.alibaba.excel.util.StringUtils; |
| | | 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.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; |
| | |
| | | private MachineFileService machineFileService; |
| | | @Autowired |
| | | private MachineService machineService; |
| | | @Autowired |
| | | private ParamService paramService; |
| | | // 每5秒执行一次 |
| | | //@Scheduled(fixedRate = 1000000) |
| | | //@Scheduled(cron = "0 1 0 * * ?") // 每天上午0点1分执行 |
| | | @Scheduled(cron = "0 15 19 * * ?") //test |
| | | @Scheduled(cron = "0 */5 * * * ?") |
| | | //@Scheduled(cron = "0 15 19 * * ?") //test |
| | | public void executeEvery5Seconds() { |
| | | scanMachineFile(); |
| | | String networkType = paramService.getParamValue(ParamService.NETWORK_TYPE,ParamService.NETWORK_TYPE_SHEMI); |
| | | |
| | | if(!ParamService.NETWORK_TYPE_SHEMI.equals(networkType)){ |
| | | //非涉密网,才扫描目录文件 |
| | | scanMachineFile(); |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 扫描所有机床的配置之目录 |
| | | */ |
| | | public void scanMachineFile() { |
| | | List<Machine> machines = machineService.lambdaQuery().eq(Machine::getStatus,Machine.STATUS_ENABLE).list(); |
| | | for (Machine machine : machines) { |
| | |
| | | |
| | | byte[] buffer = new byte[2048]; |
| | | |
| | | boolean exists;//文件是否存在于数据库中 |
| | | MachineFile existFileInDb; |
| | | //boolean exists;//文件是否存在于数据库中 |
| | | for (Path filePath : files) { |
| | | |
| | | exists = false; |
| | | existFileInDb = null; |
| | | //exists = false; |
| | | |
| | | MachineFile mf = new MachineFile(); |
| | | |
| | |
| | | mf.setFileCreateTime(creationDate); |
| | | |
| | | FileTime modifyTime = attrs.lastModifiedTime(); |
| | | mf.setFileModifyDate(new Date(modifyTime.toMillis())); |
| | | mf.setFileModifyTime(new Date(modifyTime.toMillis())); |
| | | mf.setFileSize(Files.size(filePath)); |
| | | try (InputStream inputStream = Files.newInputStream(filePath)) { |
| | | // 使用输入流读取文件内容 |
| | |
| | | continue;//有错误,掠过 |
| | | } |
| | | |
| | | exists = machineFileService.fileExists(mf.getName(),mf.getMd5(),machine.getCode()); |
| | | //exists = machineFileService.fileExists(mf.getName(),dirType,machine.getCode()); |
| | | |
| | | existFileInDb = machineFileService.getExistsFile(mf.getName(),dirType,machine.getCode()); |
| | | System.out.println("文件创建时间: " + creationDate); |
| | | } catch (IOException e) { |
| | | log.error("读取文件信息失败",e); |
| | | } |
| | | if(!exists) { |
| | | if(existFileInDb == null) { |
| | | machineFileService.save(mf); |
| | | }else{ |
| | | log.info("文件已如果掠过:{}",filePath.getFileName()); |
| | | log.info("文件已存在掠过:{}",filePath.getFileName()); |
| | | //已存在咋整。 |
| | | existFileInDb.setFileSize(mf.getFileSize()); |
| | | |
| | | existFileInDb.setFileCreateTime(mf.getFileCreateTime()); |
| | | existFileInDb.setFileModifyTime(mf.getFileModifyTime()); |
| | | if(!StringUtils.equals(existFileInDb.getMd5(),mf.getMd5())){ |
| | | //文件内容发生变化了,设置状态为初始状态 |
| | | existFileInDb.setStatus(MachineFile.STATUS_NORMAL); |
| | | } |
| | | existFileInDb.setMd5(mf.getMd5()); |
| | | |
| | | |
| | | machineFileService.updateById(existFileInDb); |
| | | |
| | | } |
| | | |
| | | } |
| | |
| | | return; |
| | | } |
| | | Path dirPath = Paths.get(basePath); |
| | | if(!dirPath.toFile().exists()){ |
| | | log.warn("扫描目录:{} 不存在",dirPath); |
| | | return; |
| | | } |
| | | |
| | | List<Path> files = Files.list(dirPath) |
| | | .filter(Files::isRegularFile).toList(); |
| | | |
| | | for(MachineFile mf : machineFiles){ |
| | | |
| | | long findCount = files.stream().filter(filePath -> {return filePath.toFile().getName().equals(mf.getName());}).count(); |
| | | long findCount = files.stream().filter(filePath -> filePath.toFile().getName().equals(mf.getName())).count(); |
| | | if(findCount == 0){ |
| | | //文件夹内没找到,需要删除记录 |
| | | this.machineFileService.save(mf); |
| | | mf.markFileDeleted(); |
| | | this.machineFileService.updateById(mf); |
| | | } |
| | | } |
| | | } |