yangys
2025-09-23 3baca21e0e6563f8379359ef2ba78c224eb4bc80
blade-service/blade-mdm/src/main/java/org/springblade/mdm/machinefile/service/ReceiveFileCheckService.java
@@ -3,6 +3,7 @@
import com.qiniu.util.IOUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.Func;
import org.springblade.mdm.basesetting.machine.entity.Machine;
@@ -65,7 +66,6 @@
         return;
      }
      AnnotationProperties annoProps = AnnotationProperties.getDefault();
      try(InputStream fileIns = Files.newInputStream(checkFilePath);) {
         int excepType = MachineFile.EXCEPTION_OK;
         ByteArrayInputStream byteStream = new ByteArrayInputStream(IOUtils.toByteArray(fileIns));
@@ -73,13 +73,13 @@
            //非文本
            excepType = MachineFile.EXCEPTION_NOT_TEXT;
         }else{
            Matcher matcher = RegExpConstants.PROGRAM_FILE_PATTERN.matcher(machineFile.getName());
            boolean filenameValid = matcher.find();
            //Matcher matcher = RegExpConstants.PROGRAM_FILE_PATTERN.matcher(machineFile.getName());
            ProgramNameVO progNameVO =  ProgramFileNameParser.parseProgramName(machineFile.getName());
            boolean filenameValid = progNameVO.isValidFilename();
            if(!filenameValid){
               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()){//文件段数缺失
@@ -87,11 +87,18 @@
               }else{
                  //检查是否匹配下发记录的段数
                  //正负3秒作为查询时间
                  Date beginTime = new Date(machineFile.getFileCreateTime().getTime()-3000);
                  Date endTime = new Date(machineFile.getFileCreateTime().getTime()+3000);
                  //Date beginTime = new Date(machineFile.getFileCreateTime().getTime()-3000);
                  //Date endTime = new Date(machineFile.getFileCreateTime().getTime()+3000);
                  String likeStr;
                  if(StringUtils.isNotBlank(progNameVO.getLgPart())){
                     likeStr = prefix +"%P%";
                  }else{
                     likeStr = prefix;
                  }
                  Optional<FileSendRecord> optFile = fileSendRecordService.lambdaQuery()
                     .eq(FileSendRecord::getMachineCode,machineFile.getMachineCode())
                     .likeRight(FileSendRecord::getName,prefix).between(FileSendRecord::getCreateTime,beginTime,endTime).oneOpt();
                     .likeRight(FileSendRecord::getName,likeStr).last("limit 1")
                     .orderByDesc(FileSendRecord::getCreateTime).oneOpt();
                  boolean matchSendRecord;//文件是否有下发记录
                  if(optFile.isPresent()){
@@ -102,6 +109,8 @@
                     if(progNameVO.getSegmentCount() != sendProgNameVO.getSegmentCount()){
                        //段数不匹配下发记录
                        excepType = MachineFile.EXCEPTION_NOT_MATCH_SEND_SEG_COUNT;
                     }else if(!StringUtils.equals(sendProgNameVO.getLgPart(),progNameVO.getLgPart())){
                        excepType = MachineFile.EXCEPTION_NOT_MATCH_SEND_LG;
                     }
                  }else{
                     matchSendRecord = false;
@@ -109,6 +118,8 @@
                  if(!matchSendRecord){//如果文件未下发过,查看是否有现场编程记录
                     byteStream.reset();
                     /*
                     String planStatusLine = AnnotationUtil.removeAnnotation(machine.getControlSystem(),FileContentUtil.readLineAt(byteStream,annoProps.getStatusLineIndex()),programAnnotationService.getAnnotionDictList());
                     String lgNo;//更改单号
@@ -117,14 +128,17 @@
                        lgNo = AnnotationUtil.removeAnnotation(machine.getControlSystem(), lgLine, programAnnotationService.getAnnotionDictList());
                     }else{
                        lgNo = null;
                     }
                     }*/
                     String devSerial = null;
                     if(StringUtils.isNotBlank(progNameVO.getLgPart())){
                        devSerial = progNameVO.getLgPart().substring(1);
                     }
                     long progOnMachineCount = programOnMachineService.lambdaQuery()
                        .eq(ProgramOnMachine::getDrawingNo,progNameVO.getDrawingNo())
                        .eq(ProgramOnMachine::getProcessNo,progNameVO.getProcessNo())
                        .eq(ProgramOnMachine::getProcessEdition,progNameVO.getProcessEdition())
                        .eq(lgNo != null,ProgramOnMachine::getDeviation,lgNo)//临时更改单匹配
                        .eq(devSerial != null,ProgramOnMachine::getDeviationSerial,devSerial)
                        .eq(ProgramOnMachine::getMachineCode,machineFile.getMachineCode()).count();
                     if(progOnMachineCount == 0){//无现场编程记录