yangys
2025-09-25 31ecdb8c700529a59dd762f0c47f0ed9c66d5092
blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/programannotation/AbstractProcessor.java
@@ -2,6 +2,7 @@
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springblade.mdm.program.service.ProgramAnnotationService;
import org.springblade.mdm.utils.FileContentUtil;
import org.springblade.system.pojo.entity.DictBiz;
@@ -18,24 +19,70 @@
   private ProgramAnnotationService programAnnotationService;
   protected AnnotationProperties annotationProperties;
   @Override
   public InputStream putSendPathAnnotation(String sendPath, InputStream inputStream,List<DictBiz> annoDicts) throws IOException{
      InputStream finishedStream;
      try(inputStream){
         ByteArrayInputStream byteInputStream =  new ByteArrayInputStream(IOUtils.toByteArray(inputStream));
         //1加入发送路径的注释
         String sendPathAnnotation = AnnotationUtil.generateAnnotation(sendPath,getControlSystem(),annoDicts);//加了注释之后的文本
         String sendDirLine = FileContentUtil.readLineAt(byteInputStream,annotationProperties.getSendPathLineIndex());//第2行是发送路径
         byteInputStream.reset();
         if(AnnotationUtil.isAnnotation(sendDirLine,getControlSystem(),annoDicts)){
            String planText = AnnotationUtil.removeAnnotation(getControlSystem(),sendDirLine,annoDicts);
            if(!planText.equals(sendPath)) {
               //非路径的注释,插入
               finishedStream = FileContentUtil.insertLine(byteInputStream,annotationProperties.getSendPathLineIndex(),sendPathAnnotation);
            }else{
               //是路径,不处理直接返回原输入流
               finishedStream = inputStream;
            }
            //finishedStream = FileContentUtil.replaceAtLine(byteInputStream,annotationProperties.getSendPathLineIndex(),sendPathAnnotation);
         }else{
            finishedStream = FileContentUtil.insertLine(byteInputStream,annotationProperties.getSendPathLineIndex(),sendPathAnnotation);
         }
         finishedStream.reset();
      }
      return finishedStream;
   }
   @Override
   public InputStream putFilenameAnnotation(String fileName, InputStream inputStream) throws IOException {
      List<DictBiz> annoDicts = programAnnotationService.getAnnotionDictList();
      ByteArrayInputStream bais = new ByteArrayInputStream(com.qiniu.util.IOUtils.toByteArray(inputStream));
      List<DictBiz> annoDicts = programAnnotationService.getAnnotionDictList();//TODO 这里应该不用加载,参数传过来就行
      ByteArrayInputStream byteStream = new ByteArrayInputStream(IOUtils.toByteArray(inputStream));
      String line = FileContentUtil.readLineAt(bais, annotationProperties.getProgramNameLineIndex());
      bais.reset();
      boolean isAnnotation = AnnotationUtil.isAnnotation(line,this.getControlSystem(),annoDicts);
      InputStream insAfter;
      String programNameLine = FileContentUtil.readLineAt(byteStream, annotationProperties.getProgramNameLineIndex());
      byteStream.reset();
      boolean isAnnotation = AnnotationUtil.isAnnotation(programNameLine,this.getControlSystem(),annoDicts);
      InputStream insAfterProgramName;
      //加入程序名注释行
      String proNameLine =  AnnotationUtil.generateAnnotation(FilenameUtils.removeExtension(fileName),getControlSystem(),annoDicts);
      String proNameAnnotation =  AnnotationUtil.generateAnnotation(FilenameUtils.removeExtension(fileName),getControlSystem(),annoDicts);
      if(isAnnotation){//是注释
         //提取注释内文字
         //String planText = AnnotationUtil.removeAnnotation(getControlSystem(),programNameline,annoDicts);
         if(!StringUtils.equals(programNameLine,proNameAnnotation)) {
            //注释不是程序名(工艺员自己写的注释),插入
            insAfterProgramName = FileContentUtil.insertLine(byteStream,annotationProperties.getProgramNameLineIndex(),proNameAnnotation);
         }else{
            //是程序名注释,不处理,返回原stream
            insAfterProgramName =  byteStream;
         }
      }else{
         //非注释,插入状态
         insAfterProgramName = FileContentUtil.insertLine(byteStream,annotationProperties.getProgramNameLineIndex(),proNameAnnotation);
      }
      /*
      if(isAnnotation){
         insAfter = FileContentUtil.replaceAtLine(bais, annotationProperties.getProgramNameLineIndex(),proNameLine);
      }else{
         insAfter = FileContentUtil.insertLine(bais, annotationProperties.getProgramNameLineIndex(),proNameLine);
      }
      return insAfter;
      }*/
      return insAfterProgramName;
   }
   @Override
@@ -47,31 +94,82 @@
         ByteArrayInputStream byteInputStream =  new ByteArrayInputStream(IOUtils.toByteArray(inputStream));
         //1加入发送路径的注释
         String sendPathAnnotation = AnnotationUtil.generateAnnotation(annoData.getSendPath(),getControlSystem(),annoDicts);//加了注释之后的文本
         String sendDirLine = FileContentUtil.readLineAt(byteInputStream,annotationProperties.getSendPathLineIndex());//第2行是发送路径
         byteInputStream.reset();
         String statusLine = FileContentUtil.readLineAt(byteInputStream,annotationProperties.getStatusLineIndex());//状态注释
         byteInputStream.reset();
         InputStream insAfterSetSendDir ;
         if(AnnotationUtil.isAnnotation(sendDirLine,getControlSystem(),annoDicts)){
            insAfterSetSendDir = FileContentUtil.replaceAtLine(byteInputStream,annotationProperties.getSendPathLineIndex(),sendPathAnnotation);
         }else{
            insAfterSetSendDir = FileContentUtil.insertLine(byteInputStream,annotationProperties.getSendPathLineIndex(),sendPathAnnotation);
         }
         InputStream insAfterSetSendDir = putSendPathAnnotation(annoData.getSendPath(),byteInputStream,annoDicts);
         /*
         String statusLine = FileContentUtil.readLineAt(insAfterSetSendDir,annotationProperties.getStatusLineIndex());//状态注释
         insAfterSetSendDir.reset();
         //加入状态注释行
         InputStream insAfterStatus;
         String statusAnnotation = AnnotationUtil.generateAnnotation(annoData.getProgramStatus(),getControlSystem(),annoDicts);//注释后的状态文本
         if(AnnotationUtil.isAnnotation(statusLine,getControlSystem(),annoDicts)){
            finishedStream = FileContentUtil.replaceAtLine(insAfterSetSendDir,annotationProperties.getStatusLineIndex(),statusAnnotation);
            insAfterStatus = FileContentUtil.replaceAtLine(insAfterSetSendDir,annotationProperties.getStatusLineIndex(),statusAnnotation);
         }else{
            finishedStream = FileContentUtil.insertLine(insAfterSetSendDir,annotationProperties.getStatusLineIndex(),statusAnnotation);
            insAfterStatus = FileContentUtil.insertLine(insAfterSetSendDir,annotationProperties.getStatusLineIndex(),statusAnnotation);
         }*/
         InputStream insAfterStatus = putStatusAnnotation(annoData.getProgramStatus(),insAfterSetSendDir,annoDicts);
         if(StringUtils.isNotBlank(annoData.getDeviation())){
            finishedStream = putDeviationAnnotation(annoData.getDeviation(),insAfterStatus,annoDicts);
         }else{
            //是更改单号,不处理直接返回原输入流
            finishedStream = insAfterStatus;
         }
      }
      return finishedStream;
   }
   protected InputStream putStatusAnnotation(String status,InputStream inputStream,List<DictBiz> annoDicts) throws IOException {
      InputStream insAfterStatus;
      //加入lg单号
      String statusLine = FileContentUtil.readLineAt(inputStream,annotationProperties.getStatusLineIndex());//状态注释
      String stationAnnotation = AnnotationUtil.generateAnnotation(status,getControlSystem(),annoDicts);
      inputStream.reset();
      if(AnnotationUtil.isAnnotation(statusLine,getControlSystem(),annoDicts) ){
         //是注释
         //提取注释内文字
         String planText = AnnotationUtil.removeAnnotation(getControlSystem(),statusLine,annoDicts);
         if(!StringUtils.equalsAny(planText,AnnotationUtil.SQ,AnnotationUtil.GH,AnnotationUtil.LG)) {
            //注释不是状态(工艺员自己写的注释),插入
            insAfterStatus = FileContentUtil.insertLine(inputStream,annotationProperties.getStatusLineIndex(),stationAnnotation);
         }else{
            //是状态注释,替换
            insAfterStatus =  FileContentUtil.replaceAtLine(inputStream,annotationProperties.getStatusLineIndex(),stationAnnotation);
         }
      }else{
         //非注释,插入状态
         insAfterStatus = FileContentUtil.insertLine(inputStream,annotationProperties.getStatusLineIndex(),stationAnnotation);
      }
      return insAfterStatus;
   }
   protected InputStream putDeviationAnnotation(String deviation,InputStream inputStream,List<DictBiz> annoDicts) throws IOException {
      InputStream insAfter1;
      //加入lg单号
      String devLine = FileContentUtil.readLineAt(inputStream,annotationProperties.getDeviationLineIndex());//状态注释
      String devAnnotation = AnnotationUtil.generateAnnotation(deviation,getControlSystem(),annoDicts);
      inputStream.reset();
      if(AnnotationUtil.isAnnotation(devLine,getControlSystem(),annoDicts) ){
         //是注释
         //提取注释内文字
         String planText = AnnotationUtil.removeAnnotation(getControlSystem(),devLine,annoDicts);
         if(!planText.equals(deviation)) {
            //注释不是临时更改单号(工艺员自己写的注释),插入
            insAfter1 = FileContentUtil.insertLine(inputStream,annotationProperties.getDeviationLineIndex(),devAnnotation);
         }else{
            //是更改单号,不处理直接返回原输入流
            insAfter1 = inputStream;
         }
      }else{
         //非注释,怎插入lg号
         insAfter1 = FileContentUtil.insertLine(inputStream,annotationProperties.getDeviationLineIndex(),devAnnotation);
      }
      return insAfter1;
   }
   @Override
@@ -88,4 +186,65 @@
   }
   public abstract String getControlSystem();
   @Override
   public AnnotationData readAnnotationData(InputStream inputStream) {
      AnnotationData data = new AnnotationData();
      int maxLineIndex = maxAnnotationLineIndex();
      try {
         int lineCount = maxLineIndex+1;
         List<String> lines = FileContentUtil.readFirstNLines(inputStream,lineCount);
         fixLine(lines,lineCount);
         String programNameLine = lines.get(this.getAnnotationProperties().getProgramNameLineIndex());
         String sendPathLine = lines.get(this.getAnnotationProperties().getSendPathLineIndex());
         String statusLine = lines.get(this.getAnnotationProperties().getStatusLineIndex());
         String devLine =  lines.get(this.getAnnotationProperties().getDeviationLineIndex());
         List<DictBiz> annoDictList= this.programAnnotationService.getAnnotionDictList();
         data.setFilename(AnnotationUtil.removeAnnotation(this.getControlSystem(),programNameLine,annoDictList));
         data.setSendPath(AnnotationUtil.removeAnnotation(this.getControlSystem(),sendPathLine,annoDictList));
         String statusText = AnnotationUtil.removeAnnotation(this.getControlSystem(),statusLine,annoDictList);
         if(AnnotationUtil.isStatusContent(statusText)){
            data.setProgramStatus(statusText);
         }
         data.setDeviation(AnnotationUtil.removeAnnotation(this.getControlSystem(),devLine,annoDictList));
      } catch (IOException e) {
         throw new RuntimeException(e);
      }
      return data;
   }
   /**
    * 没有的行填充未空串
    * @param lines
    * @return
    */
   void fixLine(List<String> lines,int fixLineCount){
      int needFix = fixLineCount - lines.size();
      if(needFix > 0){
         for(int i = 0 ; i < needFix ; i++){
            lines.add(StringUtils.EMPTY);
         }
      }
   }
   private int maxAnnotationLineIndex(){
      int max = this.getAnnotationProperties().getDeviationLineIndex();
      if(max < this.getAnnotationProperties().getStatusLineIndex()){
         max = this.getAnnotationProperties().getStatusLineIndex();
      }
      if(max < this.getAnnotationProperties().getSendPathLineIndex()){
         max = this.getAnnotationProperties().getSendPathLineIndex();
      }
      if(max < this.getAnnotationProperties().getProgramNameLineIndex()){
         max = this.getAnnotationProperties().getProgramNameLineIndex();
      }
      return max;
   }
}