yangys
2025-09-23 3baca21e0e6563f8379359ef2ba78c224eb4bc80
blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/DNCSendBackService.java
@@ -1,10 +1,10 @@
package org.springblade.mdm.program.service;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BizServiceImpl;
@@ -15,23 +15,28 @@
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.FileUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.mdm.basesetting.machine.entity.Machine;
import org.springblade.mdm.basesetting.machine.service.MachineService;
import org.springblade.mdm.commons.contants.ZipConstants;
import org.springblade.mdm.flow.entity.FlowProgramFile;
import org.springblade.mdm.flow.service.CureFlowService;
import org.springblade.mdm.flow.service.FlowCommonService;
import org.springblade.mdm.flow.service.FlowProgramFileService;
import org.springblade.mdm.program.entity.DncBackFile;
import org.springblade.mdm.program.entity.NcNode;
import org.springblade.mdm.program.entity.NcProgramExchange;
import org.springblade.mdm.program.mapper.NcProgramExchangeMapper;
import org.springblade.mdm.program.service.programannotation.*;
import org.springblade.mdm.program.vo.DncSendBackData;
import org.springblade.mdm.program.vo.DncSendBackFile;
import org.springblade.mdm.utils.FileContentUtil;
import org.springblade.mdm.utils.ZipTextFileContentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
@@ -47,22 +52,27 @@
 */
@Slf4j
@Service
@AllArgsConstructor
public class DNCSendBackService extends BizServiceImpl<NcProgramExchangeMapper, NcProgramExchange> {
   private final CureFlowService cureFlowService;
   private final FlowProgramFileService flowProgramFileService;
   private final NcNodeService ncNodeService;
   private final OssTemplate ossTemplate;
   private final BladeRedis bladeRedis;
   private final FlowCommonService flowCommonService;
   private final DncBackFileService dncBackFileService;
   private final ProgramFlowStatusQueryService programFlowStatusQueryService;
   @Autowired
   private CureFlowService cureFlowService;
   @Autowired
   private NcNodeService ncNodeService;
   @Autowired
   private OssTemplate ossTemplate;
   @Autowired
   private BladeRedis bladeRedis;
   @Autowired
   private FlowCommonService flowCommonService;
   @Autowired
   private DncBackFileService dncBackFileService;
   @Autowired
   private MachineService machineService;
   /**
    * 偏离单文件末尾的模式:P+数字
    */
   private static final String P_NUMBER_PATTERN = "(?i)P\\d+";
   //static String[] encodings = {"UTF-8","GBK", "GB2312", "ISO-8859-1"};
   private String getFileKey(){
      return "dncimpfile-"+ AuthUtil.getUserId();
   }
@@ -99,19 +109,47 @@
   /**
    * 从压缩包 解析回传程序列表,这里解析目录即可,目录就是程序包名
    * @param inputStream 压缩包输入流
    * @return 文件操作异常
    */
   List<DncSendBackData> parseProgramListFromZip(InputStream inputStream) throws IOException {
      List<DncSendBackData> result = null;
      ByteArrayInputStream byteInsStream = new ByteArrayInputStream(FileUtil.copyToByteArray(inputStream));
      for (String encoding : ZipConstants.TRY_ENCODINGS) {
         try {
            result = parseProgramListByCharset(byteInsStream,Charset.forName(encoding));
            log.error("使用编码 {} 解析成功 ",encoding);
            break;
         } catch (Exception e) {
            byteInsStream.reset();
            log.error("使用编码 {} 解析失败: ",encoding,e);
         }
      }
      if(result != null) {
         return result;
      }else{
         return Collections.emptyList();
      }
   }
   /**
    * 从压缩包 解析回传程序列表,这里解析目录即可,目录就是程序包名
    * @param inputStream 压缩包输入流
    * @return 回传程序列表
    * @throws IOException 文件操作异常
    */
   List<DncSendBackData> parseProgramListFromZip(InputStream inputStream) throws IOException {
   List<DncSendBackData> parseProgramListByCharset(InputStream inputStream,Charset charset) throws IOException {
      List<DncSendBackData> list = new ArrayList<>();
      Path tempZipFile = createTempFile(inputStream);
      List<String> fileEntryNameList = new ArrayList<>();
      List<String> dirEntryNameList = new ArrayList<>();//程序包名+工序版次 作为文件夹名
      try (ZipFile zipFile = new ZipFile(tempZipFile.toFile())) {
      ZipEntry entry;
      try (ZipFile zipFile = new ZipFile(tempZipFile.toFile(),charset)) {
         Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
         ZipEntry entry;
         //获取所有的entry名称
         while (zipEntries.hasMoreElements()) {
            entry = zipEntries.nextElement();
@@ -127,35 +165,46 @@
         //目录列表,即程序包列表
         for(String entryName : dirEntryNameList){
            DncSendBackData progData = new DncSendBackData();
            String folderName = StringUtils.removeEnd(entryName,"/");
            String packageName = StringUtils.removeEnd(entryName,"/");
            PackageAndProcessEdition pkgAndEdition = parseProgramPackageFromFolderName(folderName);
            String packageName = pkgAndEdition.getProgramPackageName();
            String processEdition = pkgAndEdition.getProcessEdition();
            if(StringUtils.isBlank(packageName) || StringUtils.isBlank(processEdition)){
               throw new ServiceException("包内文件夹名格式错误应该为[零组件号-工序号-工序版次]:"+folderName);
            }
            progData.setProgramName(packageName);
            String statusLine = "";
            Optional<String> optFilename = fileEntryNameList.stream().filter(n -> n.startsWith(entryName)).findFirst();
            if(optFilename.isPresent()){
               entry = zipFile.getEntry(optFilename.get());
               InputStream ins = zipFile.getInputStream(entry);
               ByteArrayInputStream byteArrayIns = new ByteArrayInputStream(IOUtils.toByteArray(ins));
               //解析机床
               progData.setFileBackTime(DateUtil.fromInstant(entry.getLastModifiedTime().toInstant()));
               statusLine = FileContentUtil.readLineAt(ins,2);
               if(statusLine.contains("SQ")){
                  //试切
                  programPackageNode = ncNodeService.getLastEditionTryingProgramPackage(packageName,processEdition);
               }else if(statusLine.contains("GH")){
                  //固化
                  programPackageNode = ncNodeService.getLastEditionCuredProgramPackage(packageName,processEdition);
               }else if(statusLine.contains("PL")){
                  //偏离
                  programPackageNode =ncNodeService.getLastEditionDeviationProgramPackage(packageName,processEdition);
               }else{
                  throw new ServiceException("未找到程序文件中的状态注释");
               AnnotationProperties defAnnoProperties =AnnotationProperties.getDefault();
               String statusLine = FileContentUtil.readLineAt(byteArrayIns,defAnnoProperties.getStatusLineIndex());//状态注释行
               byteArrayIns.reset();
               String sendPathLine = FileContentUtil.readLineAt(byteArrayIns,defAnnoProperties.getSendPathLineIndex());//状态注释行
               byteArrayIns.reset();
               if(statusLine.contains(AnnotationUtil.GH)){
                  //固化,不应回传,忽略
                  log.warn("状态{},不应回传,忽略",statusLine);
                  continue;
               }
               Machine machine = this.machineService.getMachineBySendPathAnnotation(sendPathLine);
               if(machine == null){
                  throw new ServiceException("根据下发路径未找到程序对应的机床:"+sendPathLine);
               }
               progData.setProgramName(packageName);
               if(statusLine.contains(AnnotationUtil.SQ)){
                  //试切
                  programPackageNode = ncNodeService.getLastEditionTryingProgramPackage(packageName);//TODO 还需根据机床组(如何获取?根据下发路径获取机床,进而获取),,processEdition
               }else if(statusLine.contains(AnnotationUtil.LG)){
                  //临时更改单
                  programPackageNode =ncNodeService.getLastEditionDeviationProgramPackage(packageName);
               }else{
                  throw new ServiceException("状态注释不在范围内:"+statusLine+",仅试切、临时更改单可以回传");
               }
               if(programPackageNode != null) {
                  progData.setId(programPackageNode.getId());
                  progData.setProgramNo(programPackageNode.getProgramNo());
@@ -172,10 +221,10 @@
                  progData.setFiles(programFiles);
                  list.add(progData);
               }else{
                  throw new ServiceException("找不到程序包名:"+packageName+statusLine);
                  throw new ServiceException("找不到程序包名:"+packageName);
               }
            }else{
               throw new ServiceException(entryName+"包下未找到文件"+statusLine);
               throw new ServiceException(entryName+"包下未找到文件");
            }
         }
@@ -184,26 +233,7 @@
      return list;
   }
   /**
    * 从文件夹名解析出程序包名和和工序版次
    * @param folderName 文件夹名
    * @return 结构数据
    */
   PackageAndProcessEdition parseProgramPackageFromFolderName(String folderName){
      int index = StringUtils.lastIndexOf(folderName,'-');
      String processEditon = "";
      String temp;
      String packageName = "";
      if(index != -1){
         processEditon = folderName.substring(index+1);
         packageName = folderName.substring(0,index);
      }
      PackageAndProcessEdition result = new PackageAndProcessEdition();;
      result.setProgramPackageName(packageName);
      result.setProcessEdition(processEditon);
      return result;
   }
   /**
    * 入库回传文件,并启动固化流程
@@ -223,7 +253,6 @@
      cureFlowService.startCureNew(pkgIdFileMap);
   }
   /**
    * 处理回传文件
    * @param ossFileName
@@ -234,12 +263,36 @@
   private Map<Long, List<FlowProgramFile>> dealWithBackFile(String ossFileName, List<Long> acceptIdList) throws IOException{
      Map<Long, List<FlowProgramFile>> pkgIdFileMap = new HashMap<>();
      //ByteArrayInputStream byteInsStream = new ByteArrayInputStream(FileUtil.copyToByteArray(inputStream));
      for (String encoding : ZipConstants.TRY_ENCODINGS) {
         try {
            pkgIdFileMap = dealWithBackFileWithCharset(ossFileName,acceptIdList,Charset.forName(encoding));
            log.error("使用编码 {} 解析成功 ",encoding);
            break;
         } catch (Exception e) {
            //byteInsStream.reset();
            log.error("使用编码 {} 解析失败: ",encoding,e);
         }
      }
      return pkgIdFileMap;
   }
   /**
    * 处理回传文件
    * @param ossFileName
    * @param acceptIdList
    * @return
    * @throws IOException
    */
   private Map<Long, List<FlowProgramFile>> dealWithBackFileWithCharset(String ossFileName, List<Long> acceptIdList,Charset charset) throws IOException{
      Map<Long, List<FlowProgramFile>> pkgIdFileMap = new HashMap<>();
      InputStream inputStream = this.ossTemplate.statFileStream(ossFileName);
      Path tempZipFile = createTempFile(inputStream);
      List<String> entryNameList = new ArrayList<>();
      ZipEntry entry;
      try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(tempZipFile.toFile())) {
      try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(tempZipFile.toFile(),charset)) {
         Enumeration<? extends ZipEntry> entries = zipFile.entries();
         while(entries.hasMoreElements()) {
            entry = entries.nextElement();
@@ -251,11 +304,7 @@
         //根据内部文件,读取和分析程序包和程序文件数据
         List<String> dirList = entryNameList.stream().filter(s -> s.endsWith("/")).toList();
         for(String dir : dirList){
            //String programPackageName1 = StringUtils.removeEnd(dir,"/");
            String folderName = StringUtils.removeEnd(dir,"/");
            PackageAndProcessEdition pkgAndEdition = parseProgramPackageFromFolderName(folderName);
            String programPackageName = pkgAndEdition.getProgramPackageName();
            String programPackageName = StringUtils.removeEnd(dir,"/");
            Optional<NcNode> optPackageNode = allAcceptPackages.stream().filter(node -> StringUtils.equals(node.getName(),programPackageName)).findFirst();
@@ -295,7 +344,7 @@
                     FlowProgramFile newFlowFile = new FlowProgramFile();
                     newFlowFile.setProgramName(packageNode.getName());
                     newFlowFile.setProcessInstanceId(null);//先置为空,启动流程后设置该值
                     newFlowFile.setFileType("program");
                     newFlowFile.setFileType(FlowProgramFile.TYPE_PROGRAM);
                     newFlowFile.setName(fileName);
                     InputStream ins = zipFile.getInputStream(zipFile.getEntry(entryName));
                     BladeFile newOssFile = ossTemplate.putFile("mdm",fileName,ins);
@@ -358,38 +407,7 @@
      String result  = "";
      String zipFileName = bladeRedis.get(getFileKey());
      return ZipTextFileContentUtil.getTextContent(this.ossTemplate.statFileStream(zipFileName),entryName);
      /*
      try(InputStream inputStream = this.ossTemplate.statFileStream(zipFileName);){
         Path tempZipFile = createTempFile(inputStream);
         ZipEntry entry;
         try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(tempZipFile.toFile())) {
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
               entry = entries.nextElement();
               if (!entryName.equals(entry.getName())) {
                  continue;
               }
               try (InputStream fileIns = zipFile.getInputStream(zipFile.getEntry(entryName))) {
                  ByteArrayInputStream bos = new ByteArrayInputStream(fileIns.readAllBytes());
                  boolean isText = StringUtils.endsWithIgnoreCase(entryName,".txt") || StringUtils.endsWithIgnoreCase(entryName,".nc")|| StringUtils.endsWithIgnoreCase(entryName,".xml");
                  if(!isText) {
                     isText = FileContentUtil.isTextFile(bos);
                  }
                  if (isText) {
                     bos.reset();
                     result = FileContentUtil.getContentFromStream(bos);
                  } else {
                     result = "<非文本文件>";
                  }
               }
            }
         }
      }
      return result;*/
   }
}
@@ -399,4 +417,8 @@
   private String programPackageName;
   private String processEdition;
   public String programName(){
      return programPackageName+"-"+processEdition;
   }
}