yangys
2025-08-13 0c7b3a418906c2e5d63fc3960dfeee045af60edc
blade-service/blade-mdm/src/main/java/org/springblade/mdm/task/MachineFileScanTask.java
@@ -37,7 +37,6 @@
   //@Scheduled(cron = "0 1 0 * * ?") // 每天上午0点1分执行
   @Scheduled(cron = "0 15 19 * * ?") //test
   public void executeEvery5Seconds() {
      System.out.println("定时任务执行: " + System.currentTimeMillis());
      scanMachineFile();
   }
@@ -45,6 +44,8 @@
   public void scanMachineFile() {
      List<Machine> machines = machineService.lambdaQuery().eq(Machine::getStatus,Machine.STATUS_ENABLE).list();
      for (Machine machine : machines) {
         //扫描新闻界
         try {
            scanDir(machine,MachineFile.DIR_TYPE_REC);
         }catch(Exception e) {
@@ -60,10 +61,26 @@
         }catch(Exception e) {
            log.error("TEMP扫描文件异常,机床"+machine.getCode(),e);
         }
         //清理数据库,删除文件不存在的数据
         try {
            clearDeletedReccords(machine,MachineFile.DIR_TYPE_REC);
         }catch(Exception e) {
            log.error("REC扫描文件异常,机床"+machine.getCode(),e);
         }
         try {
            clearDeletedReccords(machine,MachineFile.DIR_TYPE_SEND);
         }catch(Exception e) {
            log.error("SEND扫描文件异常,机床"+machine.getCode(),e);
         }
         try {
            clearDeletedReccords(machine,MachineFile.DIR_TYPE_TEMP);
         }catch(Exception e) {
            log.error("TEMP扫描文件异常,机床"+machine.getCode(),e);
         }
      }
   }
   /**
@@ -73,8 +90,6 @@
    * @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);
@@ -138,6 +153,36 @@
      }
   }
   /**
    * 清理已经删除了文件的记录
    * @param machine
    * @param dirType
    * @throws IOException
    */
   void clearDeletedReccords(Machine machine,String dirType) throws IOException {
      List<MachineFile> machineFiles =  this.machineFileService.lambdaQuery().eq(MachineFile::getDirType,dirType)
         .eq(MachineFile::getMachineCode,machine.getCode()).list();
      String basePath = MachineFileService.getBasePath(machine,dirType);
      if(basePath == null) {
         log.warn("目录类型不匹配:{}",dirType);
         return;
      }
      Path dirPath = Paths.get(basePath);
      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();
         if(findCount == 0){
            //文件夹内没找到,需要删除记录
            this.machineFileService.save(mf);
         }
      }
   }
}