yangys
2025-09-29 4c7296d45efe849dc70a3b2e2240c905481a91c9
blade-service/blade-mdm/src/main/java/org/springblade/mdm/machinefile/service/MachineFileScanService.java
@@ -1,18 +1,20 @@
package org.springblade.mdm.machinefile.service;
import com.alibaba.excel.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.mdm.basesetting.machine.service.MachineService;
import org.springblade.mdm.basesetting.machine.entity.Machine;
import org.springblade.mdm.commons.service.MachineDirTranslator;
import org.springblade.mdm.commons.service.ParamService;
import org.springblade.mdm.gkw.programnode.vo.ProgramNameVO;
import org.springblade.mdm.machinefile.entity.MachineFile;
import org.springblade.mdm.machinefile.mapper.MachineFileMapper;
import org.springblade.mdm.program.service.ProgramAnnotationService;
import org.springblade.mdm.program.service.programannotation.AnnotationProcessor;
import org.springblade.mdm.program.service.programannotation.AnnotationUtil;
import org.springblade.mdm.program.service.programannotation.MachineAnnotationConfig;
import org.springblade.mdm.program.service.programannotation.ProcessorHelper;
import org.springblade.mdm.program.service.programannotation.*;
import org.springblade.mdm.utils.FileContentUtil;
import org.springblade.mdm.utils.ProgramFileNameParser;
import org.springblade.system.pojo.entity.DictBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
@@ -37,15 +39,11 @@
   @Autowired
   private MachineService machineService;
   @Autowired
   private ParamService paramService;
   private AnnotationProcessorHelper annotationProcessorHelper;
   @Autowired
   private ProgramAnnotationService programAnnotationService;
   private MachineDirTranslator machineDirTranslator;
   @Autowired
   private MachineAnnotationConfig annoConfig;
   /**
    * 扫描所有机床的配置目录
    */
   private MachineFileMapper machineFileMapper;
   @Async
   public void scanMachineFile() {
      List<Machine> machines = machineService.getEnableMachines();
@@ -86,17 +84,17 @@
         }
      }
      machineFileMapper.deleteNotUseRecords();
   }
   /**
    * 扫描目录
    * @param machine 机床信息
    * @param dirType 目录类型
    * @throws IOException
    * @throws IOException 文件操作可能导致的异常
    */
   public void scanDir(Machine machine,String dirType) throws IOException {
      String basePath = MachineFileService.getBasePath(machine,dirType);
      String basePath = machineDirTranslator.trans(MachineFileService.getBasePath(machine,dirType));
      if(basePath == null) {
         log.warn("目录类型不匹配:{}",dirType);
         return;
@@ -111,11 +109,11 @@
         .filter(Files::isRegularFile).toList();
      //byte[] buffer = new byte[2048];
      List<DictBiz> annotatiionList = programAnnotationService.getAnnotionDictList();
      //List<DictBiz> annotatiionList = programAnnotationService.getAnnotionDictList();
      //boolean exists;//文件是否存在于数据库中
      for (Path filePath : files) {
         try {
            MachineFile mf = readFileToMachineFile(filePath,machine,dirType,annotatiionList);
            MachineFile mf = readFileToMachineFile(filePath,machine,dirType);
            machineFileService.refreshFileData(mf);
@@ -131,18 +129,16 @@
    * @param filePath 文件路径
    * @param machine 机器
    * @param dirType 目录类型
    * @param annotatiionList 注解配置字典 用于读取程序状态
    * @return MachineFile 实例
    * @throws IOException
    */
   public MachineFile readFileToMachineFile(Path filePath,Machine machine,String dirType,List<DictBiz> annotatiionList) throws IOException {
   public MachineFile readFileToMachineFile(Path filePath,Machine machine,String dirType) throws IOException {
      MachineFile mf = new MachineFile();
      mf.setTenantId("000000");
      mf.setName(filePath.toFile().getName());
      mf.setDirType(dirType);
      mf.setMachineCode(machine.getCode());
      mf.setProgramStatus(readProgramStatus(filePath,machine.getMachineGroupCode(),annotatiionList));
      BasicFileAttributes attrs = Files.readAttributes(
         filePath,
@@ -166,56 +162,30 @@
         log.error("读取文件md5失败",e);
         throw new ServiceException("读取文件md5失败"+e.getMessage());
      }
      try (InputStream inputStream = Files.newInputStream(filePath)) {
         AnnotationProcessor annoProcessor = annotationProcessorHelper.getProcessor(machine.getMachineGroupCode());
         AnnotationData annoData = annoProcessor.readAnnotationData(inputStream);
         mf.setProgramStatus(annoData.getProgramStatus());
      return mf;
   }
   /**
    * 读取文件中的程序状态
    * @param filePath 文件路径
    * @param controlSystem
    * @param annotatiionList 注释配置字典
    * @return 状态的整数之和
    * @throws IOException
    */
   public int readProgramStatus(Path filePath,String controlSystem,List<DictBiz> annotatiionList) throws IOException {
      int programStatus = MachineFile.PROGRAM_STATUS_UNKNOWN;
      try(InputStream ins = Files.newInputStream(filePath);
         BufferedInputStream bis = new BufferedInputStream(ins)){
         boolean isText= FileContentUtil.isTextFile(bis);
         if(!isText){
            return MachineFile.PROGRAM_STATUS_UNKNOWN;
         }
         bis.reset();
         AnnotationProcessor annoProcessor = ProcessorHelper.getProcessor(controlSystem,annoConfig);
         String statusLine = FileContentUtil.readLineAt(ins, annoProcessor.getAnnotationProperties().getStatusLineIndex());
         if(AnnotationUtil.isAnnotation(statusLine,controlSystem,annotatiionList)){
            String progStatusTxt = AnnotationUtil.removeAnnotation(controlSystem,statusLine,annotatiionList);
            if(AnnotationUtil.SQ.equals(progStatusTxt)){
               programStatus = MachineFile.PROGRAM_STATUS_TRY;
            }else if(AnnotationUtil.GH.equals(progStatusTxt)){
               programStatus = MachineFile.PROGRAM_STATUS_CURED;
            }else if(AnnotationUtil.LG.equals(progStatusTxt)){
               programStatus = MachineFile.PROGRAM_STATUS_DIVIASTION;
            }
         ProgramNameVO progVO = ProgramFileNameParser.parseProgramName(mf.getName());
         if(StringUtils.isNotBlank(progVO.getLgPart())) {
            mf.setDeviation(annoData.getDeviation());
         }
      }
      return programStatus;
      return mf;
   }
   /**
    * 清理已经删除了文件的记录
    * @param machine
    * @param dirType
    * @param machine 机床
    * @param dirType 目录类型
    * @throws IOException
    */
   void clearDeletedRecords(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);
      String basePath = machineDirTranslator.trans(MachineFileService.getBasePath(machine,dirType));
      if(basePath == null) {
         log.warn("目录类型不匹配:{}",dirType);
         return;