yangys
2025-08-30 86c8efdb19a2f4be79a947650ef62ed79382011b
blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/ProgramAnnotationService.java
@@ -5,7 +5,9 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springblade.mdm.program.vo.ProgramAnnotation;
import org.springblade.mdm.utils.FileContentUtil;
import org.springblade.system.feign.IDictBizClient;
import org.springblade.system.pojo.entity.DictBiz;
@@ -52,12 +54,10 @@
   public static final String GH = "GH";
   public static final String PL = "PL";
   public List<DictBiz> getAnnotionList(){
   public List<DictBiz> getAnnotionDictList(){
      List<DictBiz> annotationList;
      //R<List<Dict>> dictsResult = dictClient.getList(ANNOTATION_DICT);
      R<List<DictBiz>> dictBizResult = bizDictClient.getList(ANNOTATION_DICT);
      R<List<DictBiz>> gr = bizDictClient.getList("machine_group");
      R<List<DictBiz>> gcate = bizDictClient.getList("org_category");
      if(dictBizResult.isSuccess()) {
         annotationList = dictBizResult.getData();
      }else{
@@ -76,30 +76,20 @@
    */
   public boolean isAnnotation(String line,String machineGroupCode,List<DictBiz> annotationList){
      boolean isAnno = false;
      Optional<DictBiz> dictOpt = annotationList.stream().filter(dict -> {return dict.getDictKey().equals(machineGroupCode);}).findFirst();
      String annotationSetting = DEFAULT_ANNOTATION_SETTING;
      if(dictOpt.isPresent()) {
         annotationSetting = dictOpt.get().getRemark();
      }
      String begin = "";
      String end = "";
      String[] arr = annotationSetting.split(",");
      if(arr.length == 2){
         begin = arr[0];
         end = arr[1];
      }
      ProgramAnnotation progAnnotation = this.getProgramAnnotationFormat(machineGroupCode,annotationList);
      line = StringUtils.trim(line);//去首尾空格
      isAnno = StringUtils.startsWith(line,begin) && StringUtils.endsWith(line,end);
      return isAnno;
      return progAnnotation.isAnnotation(line);
   }
   public String generateAnnotation(String oriTest,String machineGroupCode,List<DictBiz> annotationList) {
      /*
      String annotation = oriTest;
      String begin = "(";
      String end = ")";
      Optional<DictBiz> dictOpt = annotationList.stream().filter(dict -> {return dict.getDictKey().equals(machineGroupCode);}).findFirst();
      String annotationSetting = DEFAULT_ANNOTATION_SETTING;
      if(dictOpt.isPresent()) {
@@ -110,14 +100,14 @@
         begin = arr[0];
         end = arr[1];
      }else if(arr.length == 1){
         begin = begin = arr[0];
         begin = arr[0];
         end = "";
      }
      annotation = begin + oriTest + end;
      return annotation;
      */
      ProgramAnnotation progAnnotation = this.getProgramAnnotationFormat(machineGroupCode,annotationList);
      return progAnnotation.addAnnotation(oriTest);
   }
   /*
   public String[] getAnnotationStartAndEnd(String machineGroupCode,List<DictBiz> annotationList) {
      String begin = "(";
@@ -136,47 +126,47 @@
         begin = arr[0];
         end = "";
      }
      ProgramAnnotation progAnno = this.getProgramAnnotationFormat(machineGroupCode,annotationList);
      return new String[]{begin,end};
   }
   public String removeAnnotation(String machineGroupCode,String text,String begin,String end){
      List<DictBiz> annoDicts = getAnnotionList();
      String[] arr = getAnnotationStartAndEnd(machineGroupCode,annoDicts);
      return removeAnnotationInner(text,begin,end);
   }
   public static String removeAnnotationInner(String text,String begin,String end){
      String temp = StringUtils.removeEnd(StringUtils.removeStart(text,begin),end);
      return temp;
   }
*/
   /**
    * 设置固化注释行
    * @param inputStream
    * @param machineGroup
    * @param annoDictList
    * @return
    * @throws IOException
    * 提取注释中的文本
    * @param machineGroupCode 机床组代码
    * @param text 清除注释标记
    * @return 清除后的文本
    */
   public InputStream setGHAnnotation(InputStream inputStream, String machineGroup,List<DictBiz> annoDictList) throws IOException {
   public String removeAnnotation(String machineGroupCode,String text){
      List<DictBiz> annoDicts = getAnnotionDictList();
      return removeAnnotation(machineGroupCode,text,annoDicts);
   }
   public String removeAnnotation(String machineGroupCode,String text,List<DictBiz> annotatiionList){
      ProgramAnnotation panno = this.getProgramAnnotationFormat(machineGroupCode,annotatiionList);
      return panno.cleanAnnotation(text);
   }
      /**
       * 设置固化注释行
       * @param inputStream 输入框
       * @param machineGroup 机床组编码
       * @param annoDictList 注释字典配置
       * @return 加好注释的
       * @throws IOException
       */
   public InputStream setGHAnnotation(InputStream inputStream, String machineGroup,List<DictBiz> annoDictList) throws IOException {
      return setAnnotationAndGetInputStream(inputStream, machineGroup, annoDictList, GH, STATUS_LINE_INDEX);
   }
   /**
    * 设置偏离单注释
    * @param inputStream
    * @param machineGroup
    * @param annoDictList
    * @return
    * @throws IOException
    * @param inputStream 文本输入流
    * @param machineGroup 机床组代码
    * @param annoDictList 注释字典配置
    * @return 加好注释的输入流
    * @throws IOException 访问流可能抛出异常
    */
   public InputStream setPldAnnotation(String deviation,InputStream inputStream,String machineGroup,List<DictBiz> annoDictList) throws IOException {
      //
      return setAnnotationAndGetInputStream(inputStream, machineGroup, annoDictList, deviation, STATUS_DEVIATION_INDEX);
   }
@@ -213,7 +203,7 @@
    * @throws IOException
    */
   public InputStream setSendDirAndStatusAnnotation(String sendPath,String statusText,InputStream inputStream, String machineGroup) throws IOException {
      List<DictBiz> annoDicts = getAnnotionList();
      List<DictBiz> annoDicts = getAnnotionDictList();
      InputStream finishedStream;
      try(inputStream){
@@ -222,12 +212,13 @@
         //1加入发送路径的注释
         String sendPathAnnotation = generateAnnotation(sendPath,machineGroup,annoDicts);//加了注释之后的文本
         InputStream insAfterSetSendDir ;
         String sendDirLine = FileContentUtil.readLineAt(byteInputStream,SENDPATH_LINE_INDEX);//第2行是发送路径
         byteInputStream.reset();
         String statusLine = FileContentUtil.readLineAt(byteInputStream,STATUS_LINE_INDEX);//第三行是状态注释
         byteInputStream.reset();
         InputStream insAfterSetSendDir ;
         if(isAnnotation(sendDirLine,machineGroup,annoDicts)){
            insAfterSetSendDir = FileContentUtil.replaceAtLine(byteInputStream,SENDPATH_LINE_INDEX,sendPathAnnotation);
         }else{
@@ -246,5 +237,39 @@
      return finishedStream;
   }
   /**
    * 解析出机床程序注释的格式
    * @param machineGroupCode 机床组
    * @param annoDictList 配置的注释字典数据
    * @return
    */
   ProgramAnnotation getProgramAnnotationFormat(String machineGroupCode,List<DictBiz> annoDictList){
      String begin;
      String end;
      Optional<DictBiz> dictOpt = annoDictList.stream().filter(dict -> {return dict.getDictKey().equals(machineGroupCode);}).findFirst();
      String annotationSetting = DEFAULT_ANNOTATION_SETTING;
      if(dictOpt.isPresent()) {
         annotationSetting = dictOpt.get().getRemark();
      }
      String[] arr = StringUtils.split(annotationSetting,",");
      if(arr.length == 2){
         begin = arr[0];
         end = arr[1];
      }else if(arr.length == 1){
         begin = arr[0];
         end = "";
      }else{
         throw new ServiceException("注释格式配置错误,应该为1到2段,中间用逗号分隔");
      }
      ProgramAnnotation programAnnotation = new ProgramAnnotation();
      programAnnotation.setBegin(begin);
      programAnnotation.setEnd(end);
      return programAnnotation;
   }
}