| | |
| | | package org.springblade.mdm.program.service; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | 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.system.feign.IDictClient; |
| | | import org.springblade.system.pojo.entity.Dict; |
| | | import org.springblade.mdm.program.service.programannotation.AnnotationUtil; |
| | | 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; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Optional; |
| | |
| | | public class ProgramAnnotationService { |
| | | |
| | | @Autowired |
| | | private IDictClient dictClient; |
| | | private IDictBizClient bizDictClient; |
| | | |
| | | private static final String DEFAULT_ANNOTATION_SETTING = "(,)"; |
| | | |
| | | public static final String ANNOTATION_DICT = "machine_annotation"; |
| | | /** |
| | | * 发送目录行索引(第2行) 0 based |
| | | */ |
| | | public static final int SENDPATH_LINE_INDEX = 1; |
| | | /** |
| | | * 状态行索引(第三行) 0 based |
| | | */ |
| | | public static final int STATUS_LINE_INDEX = 2; |
| | | |
| | | public List<Dict> getAnnotionList(){ |
| | | List<Dict> annotationList; |
| | | R<List<Dict>> dictsResult = dictClient.getList(ANNOTATION_DICT); |
| | | if(dictsResult.isSuccess()) { |
| | | annotationList = dictsResult.getData(); |
| | | /** |
| | | * 偏离单注释行索引号 0 based |
| | | */ |
| | | public static final int STATUS_DEVIATION_INDEX = 3; |
| | | |
| | | public static final String SQ = "SQ"; |
| | | public static final String GH = "GH"; |
| | | public static final String PL = "PL"; |
| | | |
| | | public List<DictBiz> getAnnotionDictList(){ |
| | | List<DictBiz> annotationList; |
| | | |
| | | R<List<DictBiz>> dictBizResult = bizDictClient.getList(ANNOTATION_DICT); |
| | | if(dictBizResult.isSuccess()) { |
| | | annotationList = dictBizResult.getData(); |
| | | }else{ |
| | | annotationList = Collections.emptyList(); |
| | | } |
| | |
| | | * @param annotationList 注释注释字典项列表 |
| | | * @return |
| | | */ |
| | | public boolean isAnnotation(String line,String machineGroupCode,List<Dict> annotationList){ |
| | | boolean isAnno = false; |
| | | Optional<Dict> 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]; |
| | | } |
| | | isAnno = StringUtils.startsWith(line,begin) && StringUtils.endsWith(line,end); |
| | | return isAnno; |
| | | /* |
| | | public boolean isAnnotation(String line,String systemDictVal,List<DictBiz> annotationList){ |
| | | ProgramAnnotation progAnnotation = this.getProgramAnnotationFormat(systemDictVal,annotationList); |
| | | |
| | | line = StringUtils.trim(line);//去首尾空格 |
| | | return progAnnotation.isAnnotation(line); |
| | | } |
| | | |
| | | |
| | | |
| | | public String generateAnnotation(String oriTest,String machineGroupCode,List<Dict> annotationList) { |
| | | public String generateAnnotation(String oriTest,String systemDictVal,List<DictBiz> annotationList) { |
| | | ProgramAnnotation progAnnotation = this.getProgramAnnotationFormat(systemDictVal,annotationList); |
| | | return progAnnotation.addAnnotation(oriTest); |
| | | }*/ |
| | | |
| | | String annotation = oriTest; |
| | | String begin = "("; |
| | | String end = ")"; |
| | | /** |
| | | * 提取注释中的文本 |
| | | * @param systemDictVal 机床组代码 |
| | | * @param text 清除注释标记 |
| | | * @return 清除后的文本 |
| | | */ |
| | | |
| | | Optional<Dict> dictOpt = annotationList.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 = begin = arr[0]; |
| | | end = ""; |
| | | public String removeAnnotation(String systemDictVal,String text){ |
| | | List<DictBiz> annoDicts = getAnnotionDictList(); |
| | | return removeAnnotation(systemDictVal,text,annoDicts); |
| | | } |
| | | public String removeAnnotation(String systemDictVal,String text,List<DictBiz> annotatiionList){ |
| | | ProgramAnnotation panno = AnnotationUtil.getProgramAnnotationFormat(systemDictVal,annotatiionList); |
| | | return panno.cleanAnnotation(text); |
| | | } |
| | | /** |
| | | * 设置固化注释行 |
| | | * @param inputStream 输入框 |
| | | * @param systemDictVal 机床组编码 |
| | | * @param annoDictList 注释字典配置 |
| | | * @return 加好注释的 |
| | | * @throws IOException |
| | | */ |
| | | /* |
| | | public InputStream setGHAnnotation(InputStream inputStream, String systemDictVal,List<DictBiz> annoDictList) throws IOException { |
| | | return setAnnotationAndGetInputStream(inputStream, systemDictVal, annoDictList, GH, STATUS_LINE_INDEX); |
| | | }*/ |
| | | |
| | | /** |
| | | * 设置偏离单注释 |
| | | * @param inputStream 文本输入流 |
| | | * @param systemDictVal 控制系统字典值 |
| | | * @param annoDictList 注释字典配置 |
| | | * @return 加好注释的输入流 |
| | | * @throws IOException 访问流可能抛出异常 |
| | | */ |
| | | /* |
| | | public InputStream setPldAnnotation(String deviation,InputStream inputStream,String systemDictVal,List<DictBiz> annoDictList) throws IOException { |
| | | return setAnnotationAndGetInputStream(inputStream, systemDictVal, annoDictList, deviation, STATUS_DEVIATION_INDEX); |
| | | } |
| | | */ |
| | | /* |
| | | @NotNull |
| | | private InputStream setAnnotationAndGetInputStream(InputStream inputStream, String systemDictVal, List<DictBiz> annoDictList, String text, int lineIndex) throws IOException { |
| | | InputStream finishedStream; |
| | | try(inputStream){ |
| | | ByteArrayInputStream byteInputStream = new ByteArrayInputStream(IOUtils.toByteArray(inputStream)); |
| | | |
| | | String annoTxt = generateAnnotation(text,systemDictVal,annoDictList);//加了注释之后的文本 |
| | | |
| | | String lineText = FileContentUtil.readLineAt(byteInputStream, lineIndex); |
| | | byteInputStream.reset(); |
| | | |
| | | if(isAnnotation(lineText,systemDictVal,annoDictList)){ |
| | | finishedStream = FileContentUtil.replaceAtLine(byteInputStream, lineIndex,annoTxt); |
| | | }else{ |
| | | finishedStream = FileContentUtil.insertLine(byteInputStream, lineIndex,annoTxt); |
| | | } |
| | | finishedStream.reset(); |
| | | } |
| | | |
| | | annotation = begin + oriTest + end; |
| | | return annotation; |
| | | return finishedStream; |
| | | } |
| | | |
| | | public String[] getAnnotationStartAndEnd(String machineGroupCode,List<Dict> annotationList) { |
| | | */ |
| | | |
| | | String begin = "("; |
| | | String end = ")"; |
| | | /** |
| | | * 为输入流设置发送目录(第二行)和状态(第三行) |
| | | * @param sendPath 发送路径 |
| | | * @param statusText 状态文本 |
| | | * @param inputStream 输入流 |
| | | * @param systemDictVal 控制系统代码 |
| | | * @return 加好发送路径和状态文本的输入流 |
| | | * @throws IOException |
| | | */ |
| | | /* |
| | | public InputStream setSendDirAndStatusAnnotation(String sendPath,String statusText,InputStream inputStream, String systemDictVal) throws IOException { |
| | | List<DictBiz> annoDicts = getAnnotionDictList(); |
| | | |
| | | Optional<Dict> dictOpt = annotationList.stream().filter(dict -> {return dict.getDictKey().equals(machineGroupCode);}).findFirst(); |
| | | InputStream finishedStream; |
| | | try(inputStream){ |
| | | ByteArrayInputStream byteInputStream = new ByteArrayInputStream(IOUtils.toByteArray(inputStream)); |
| | | |
| | | //1加入发送路径的注释 |
| | | String sendPathAnnotation = generateAnnotation(sendPath,systemDictVal,annoDicts);//加了注释之后的文本 |
| | | |
| | | 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,systemDictVal,annoDicts)){ |
| | | insAfterSetSendDir = FileContentUtil.replaceAtLine(byteInputStream,SENDPATH_LINE_INDEX,sendPathAnnotation); |
| | | }else{ |
| | | insAfterSetSendDir = FileContentUtil.insertLine(byteInputStream,SENDPATH_LINE_INDEX,sendPathAnnotation); |
| | | } |
| | | |
| | | insAfterSetSendDir.reset(); |
| | | //加入状态注释行 |
| | | String statusAnnotation = generateAnnotation(statusText,systemDictVal,annoDicts);//注释后的状态文本 |
| | | if(isAnnotation(statusLine,systemDictVal,annoDicts)){ |
| | | finishedStream = FileContentUtil.replaceAtLine(insAfterSetSendDir,STATUS_LINE_INDEX,statusAnnotation); |
| | | }else{ |
| | | finishedStream = FileContentUtil.insertLine(insAfterSetSendDir,STATUS_LINE_INDEX,statusAnnotation); |
| | | } |
| | | } |
| | | |
| | | return finishedStream; |
| | | }*/ |
| | | |
| | | /** |
| | | * 解析出机床程序注释的格式 |
| | | * @param systemDictVal 数控系统字典值 |
| | | * @param annoDictList 配置的注释字典数据 |
| | | * @return |
| | | */ |
| | | /* |
| | | ProgramAnnotation getProgramAnnotationFormat(String systemDictVal,List<DictBiz> annoDictList){ |
| | | String begin; |
| | | String end; |
| | | |
| | | Optional<DictBiz> dictOpt = annoDictList.stream().filter(dict -> {return dict.getDictKey().equals(systemDictVal);}).findFirst(); |
| | | String annotationSetting = DEFAULT_ANNOTATION_SETTING; |
| | | if(dictOpt.isPresent()) { |
| | | annotationSetting = dictOpt.get().getRemark(); |
| | |
| | | }else if(arr.length == 1){ |
| | | begin = arr[0]; |
| | | end = ""; |
| | | }else{ |
| | | throw new ServiceException("注释格式配置错误,应该为1到2段,中间用逗号分隔"); |
| | | } |
| | | |
| | | |
| | | return new String[]{begin,end}; |
| | | } |
| | | |
| | | public String removeAnnotation(String machineGroupCode,String text,String begin,String end){ |
| | | List<Dict> 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; |
| | | } |
| | | ProgramAnnotation programAnnotation = new ProgramAnnotation(); |
| | | programAnnotation.setBegin(begin); |
| | | programAnnotation.setEnd(end); |
| | | return programAnnotation; |
| | | }*/ |
| | | } |
| | | |
| | | |
| | | |