yangys
2025-09-19 b0d0191a88912b352385349461b500a4964d693b
blade-service/blade-mdm/src/main/java/org/springblade/mdm/machinefile/service/ReceiveFileCheckService.java
@@ -11,6 +11,7 @@
import org.springblade.mdm.machinefile.entity.FileSendRecord;
import org.springblade.mdm.machinefile.entity.MachineFile;
import org.springblade.mdm.program.service.ProgramAnnotationService;
import org.springblade.mdm.program.service.ProgramOnMachineService;
import org.springblade.mdm.program.service.programannotation.AnnotationUtil;
import org.springblade.mdm.utils.FileContentUtil;
import org.springblade.mdm.utils.ProgramFileNameParser;
@@ -42,7 +43,7 @@
   @Autowired
   private MachineService machineService;
   @Autowired
   private ParamService paramService;
   private ProgramOnMachineService programOnMachineService;
   @Autowired
   private FileSendRecordService fileSendRecordService;
   /**
@@ -51,27 +52,30 @@
    */
   @Transactional(rollbackFor = Exception.class)
   public void check(MachineFile machineFile,List<MachineFile> allFilesInDir,Machine machine) throws IOException {
      String srcFilepath = MachineFileService.getBasePath(machine,machineFile.getDirType())+ File.separator+machineFile.getName();
      Path source = Paths.get(srcFilepath);
      if(!source.toFile().exists()){
      log.info("开始检查文件{}",srcFilepath);
      Path checkFilePath = Paths.get(srcFilepath);
      if(!checkFilePath.toFile().exists()){
         return;
      }
      try(InputStream fileIns = Files.newInputStream(Paths.get(srcFilepath));) {
      try(InputStream fileIns = Files.newInputStream(checkFilePath);) {
         int excepType = MachineFile.EXCEPTION_OK;
         if(!FileContentUtil.isTextFile(fileIns)){
            //非文本
            machineFile.setExceptionType(MachineFile.EXCEPTION_NOT_TEXT);
            excepType = MachineFile.EXCEPTION_NOT_TEXT;
         }else{
            Matcher matcher = RegExpConstants.PROGRAM_FILE_PATTERN.matcher(machineFile.getName());
            boolean filenameValid = matcher.find();
            if(!filenameValid){
               machineFile.setExceptionType(MachineFile.EXCEPTION_BAD_FILENAME);
               excepType = MachineFile.EXCEPTION_BAD_FILENAME;
            }else {
               ProgramNameVO progNameVO =  ProgramFileNameParser.parseProgramName(machineFile.getName());
               String prefix = progNameVO.logicProgramName()+"-";
               long matchCount = allFilesInDir.stream().filter(file -> file.getName().startsWith(prefix)).count();
               if(matchCount != progNameVO.getSegmentCount()){//文件段数缺失
                  machineFile.setExceptionType(MachineFile.EXCEPTION_LOST_FILES);
                  excepType = MachineFile.EXCEPTION_LOST_FILES;
               }else{
                  //检查是否匹配下发记录的段数
                  //正负3秒作为查询时间
@@ -86,16 +90,15 @@
                     FileSendRecord sendFile = optFile.get();
                     ProgramNameVO sendProgNameVO =  ProgramFileNameParser.parseProgramName(sendFile.getName());
                     if(progNameVO.getSegmentCount() != sendProgNameVO.getSegmentCount()){
                        //段数不匹配
                        machineFile.setExceptionType(MachineFile.EXCEPTION_NOT_MATCH_SEND);
                     }else{
                        machineFile.setExceptionType(MachineFile.EXCEPTION_OK);
                        //段数不匹配下发记录
                        excepType = MachineFile.EXCEPTION_NOT_MATCH_SEND;
                     }
                  }
               }
            }
         }
         machineFile.setExceptionType(excepType);
         log.info("检查文件结束{}",srcFilepath);
         machineFileService.updateById(machineFile);
      }
   }