yangys
2025-08-22 ddafa0375398053baaafdee8612f68c1b75ff7cb
blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/NcProgramExportDNCService.java
@@ -2,14 +2,10 @@
package org.springblade.mdm.program.service;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BizEntity;
import org.springblade.core.mp.base.BizServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
@@ -18,7 +14,6 @@
import org.springblade.core.tool.utils.Func;
import org.springblade.mdm.basesetting.machine.MachineService;
import org.springblade.mdm.basesetting.machine.entity.Machine;
import org.springblade.mdm.flow.entity.ApproveRecord;
import org.springblade.mdm.flow.entity.FlowProgramFile;
import org.springblade.mdm.flow.service.ApproveRecordService;
import org.springblade.mdm.flow.service.FlowProgramFileService;
@@ -27,13 +22,10 @@
import org.springblade.mdm.program.entity.NcProgramExchange;
import org.springblade.mdm.program.mapper.NcProgramExchangeMapper;
import org.springblade.mdm.program.vo.DncSendBackData;
import org.springblade.mdm.utils.FileContentUtil;
import org.springblade.system.feign.IDictClient;
import org.springblade.system.pojo.entity.Dict;
import org.springframework.stereotype.Service;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@@ -52,14 +44,10 @@
   private final IDictClient dictClient;
   private final MachineService machineService;
   private final NcProgramApprovedService approvedService;
   private final ApproveRecordService approveRecordService;
   private final NcNodeService ncNodeService;
   private final OssTemplate ossTemplate;
   private final FlowProgramFileService flowProgramFileService;
   private final ProgramAnnotationService programAnnotationService;
   //public static final String PROGRAM_JSON_FILE = "exp_mdm_nc_program.json";
   //public static final String NODE_JSON_FILE = "exp_mdm_nc_node.json";
   //public static final String APPROVE_RECORD_JSON_FILE = "exp_mdm_approve_record.json";
   public static final String ANNOTATION_DICT = "machine_annotation";
@@ -88,15 +76,10 @@
            approvedService.updateById(approved);
         }
         //状态修改为已导出
         /*
         approvedService.lambdaUpdate().in(NcProgramApproved::getId, Arrays.asList(approvedIdArray))
            .set(NcProgramApproved::getStatus,NcProgramApproved.STATUS_EXPORTED).update();*/
      }catch(Exception e){
         Throwable [] err = e.getSuppressed();
         throw new ServiceException("导出工控网错误"+e.getMessage());
      }
   }
   /**
@@ -105,12 +88,14 @@
    * @param approved
    */
   private void addProgramPackageToZip(ZipOutputStream zipOut, NcProgramApproved approved) throws IOException{
      String packageFolder = approved.getProgramName()+"/";
      NcNode packageNode = ncNodeService.getById(approved.getNcNodeId());
      String packageFolder = packageNode.getName()+"-"+packageNode.getProcessEdition()+"/";
      ZipEntry zipEntry = new ZipEntry(packageFolder);// "/"结尾表示文件夹
      zipOut.putNextEntry(zipEntry);
      zipOut.closeEntry();
      NcNode packageNode = ncNodeService.getById(approved.getNcNodeId());
      Machine machine = machineService.getByCode(packageNode.getMachineCode());//程序包节点,获取注释用
      List<NcNode> programNodes = ncNodeService.lambdaQuery().eq(NcNode::getIsLastEdition,1).eq(NcNode::getParentId, approved.getNcNodeId()).list();
@@ -123,14 +108,14 @@
      }else if(programPackageNode.hasCured()){
         status = "GH";
      }
      List<Dict> annotationList = programAnnotationService.getAnnotionList();
      for (NcNode node : programNodes) {
         String filePathInZip = packageFolder + node.getName();
         programFile = this.flowProgramFileService.getById(node.getFlowProgramFileId());
         if(programFile.isProgram()) {//程序文件,才会加入压缩包
            InputStream inputStream = ossTemplate.statFileStream(programFile.getOssName());
            InputStream addedIns1 = addSendDirAnnotation(inputStream, machine, annotationList);
            InputStream addedIns2 = addProgramStatusAnnotation(addedIns1, status, machine, annotationList);
            String sendDir = machine.getProgSendDir()==null? StringUtil.EMPTY:machine.getProgSendDir();
            InputStream addedIns2 = programAnnotationService.setSendDirAndStatusAnnotation(sendDir,status,inputStream,machine.getMachineGroupCode());
            this.addInputStreamToZip(zipOut, addedIns2, filePathInZip);
         }
      }
@@ -143,13 +128,14 @@
    * @param inputStream
    * @return
    */
   /*
   InputStream addSendDirAnnotation(InputStream inputStream,Machine machine,List<Dict> annotationList) throws IOException {
      //检测第二行是否是路径
      String annotationText = programAnnotationService.generateAnnotation(machine.getProgSendDir()==null? StringUtil.EMPTY:machine.getProgSendDir(),machine.getMachineGroupCode(),annotationList);
      //TODO 改判断行内容
      return FileContentUtil.insertLine(inputStream,1,annotationText);
   }
   }*/
   /**
    * 添加程序专改的注释
@@ -160,11 +146,12 @@
    * @return
    * @throws IOException
    */
   /*
   InputStream addProgramStatusAnnotation(InputStream inputStream,String status,Machine machine,List<Dict> annotationList) throws IOException {
      String annoText= programAnnotationService.generateAnnotation(status,machine.getMachineGroupCode(),annotationList);
      return FileContentUtil.insertLine(inputStream,2,annoText);
   }
   }*/
   /**
    * 判断一行文本是否是注释
@@ -172,14 +159,15 @@
    * @param annotationList
    * @return
    */
   /*
   boolean isAnnotation(String line,List<Dict> annotationList){
      boolean isAnno = false;
      return isAnno;
   }
   }*/
   /*
   String generateAnnotation(String oriTest,String machineGroupCode,List<Dict> annotationList) {
      R<List<Dict>> dictsResult = dictClient.getList(ANNOTATION_DICT);
      String annotation = oriTest;
@@ -201,114 +189,7 @@
      return annotation;
   }
   /**
    * 导入数据文件
    * @param zipOut
    */
   /*
   void addDataJson(ZipOutputStream zipOut, List<Long> programPackageNodeIdList) throws IOException {
      addNodeDataJson(zipOut, programPackageNodeIdList);
      addApproveRecordDataJson(zipOut, programPackageNodeIdList);
   }
   */
   /**
    * zip保重加入节点的数据
    * @param zipOut
    * @param pkgNodeIdList
    * @throws IOException
    */
   /*
   void  addNodeDataJson(ZipOutputStream zipOut,List<Long>  pkgNodeIdList) throws IOException {
      List<NcNode> pkgNodes =  this.ncNodeService.lambdaQuery().in(NcNode::getId, pkgNodeIdList).list();
      List<Long> allNodeIds = new ArrayList<>();
      for(NcNode node : pkgNodes){
         allNodeIds.addAll(Func.toLongList(node.getParentIds()));
         allNodeIds.add(node.getId());
      }
      //加入程序包下级的程序节点
      List<NcNode> programNodes = ncNodeService.lambdaQuery().in(NcNode::getParentId, pkgNodes.stream().map(NcNode::getId).toList()).list();
      allNodeIds.addAll(programNodes.stream().map(NcNode::getId).toList());
      List<Long>  distinctIds = allNodeIds.stream().distinct().toList();
      List<NcNode> allNodes = this.ncNodeService.lambdaQuery().in(NcNode::getId, distinctIds).list();
      JSONArray jsonArray = new JSONArray();
      for(NcNode node : allNodes){
         JSONObject recObj = getNodeJsonObject(node);
         addSuperProperties(recObj,node);
         jsonArray.add(recObj);
      }
      addInputStreamToZip(zipOut,new ByteArrayInputStream(jsonArray.toJSONString().getBytes(StandardCharsets.UTF_8)),NODE_JSON_FILE);
   }
   */
   /*
   @NotNull
   private static JSONObject getNodeJsonObject(NcNode node) {
      JSONObject recObj = new JSONObject();
      recObj.put("id", node.getId());
      recObj.put("name", node.getName());
      recObj.put("drawingNo", node.getDrawingNo());
      recObj.put("drawingNoEdition", node.getDrawingNoEdition());
      recObj.put("productModel", node.getProductModel());
      recObj.put("processName", node.getProcessName());
      recObj.put("processEdition", node.getProcessEdition());
      recObj.put("craftEdition", node.getCraftEdition());
      recObj.put("parentIds", node.getParentIds());
      recObj.put("processNo", node.getProcessNo());
      recObj.put("isCured", node.getIsCured());
      recObj.put("isLocked", node.getIsLocked());
      recObj.put("nodeType", node.getNodeType());
      recObj.put("machineCode", node.getMachineCode());
      recObj.put("flowProgramFileId", node.getFlowProgramFileId());
      recObj.put("processInstanceId", node.getProcessInstanceId());
      return recObj;
   }
   */
   /**
    * 导入审批记录
    * @param zipOut
    * @param programPackageNodeIdList 程序包名的id列表
    */
   /*
   void addApproveRecordDataJson(ZipOutputStream zipOut, List<Long> programPackageNodeIdList) throws IOException {
      //List<ApproveRecord> records = approveRecordService.lambdaQuery().in(ApproveRecord::getProcessInstanceId, instanceIds).list();
      List<ApproveRecord> records = approveRecordService.lambdaQuery().in(ApproveRecord::getNcNodeId, programPackageNodeIdList).list();
      JSONArray jsonArray = new JSONArray();
      for(ApproveRecord record : records){
         JSONObject recObj = new JSONObject();
         recObj.put("id", record.getId());
         recObj.put("comment", record.getComment());
         recObj.put("userId",record.getUserId());
         recObj.put("userNickname",record.getUserNickname());
         recObj.put("operateTime",record.getOperateTime());
         recObj.put("operateResult",record.getOperateResult());
         recObj.put("taskName",record.getTaskName());
         recObj.put("processInstanceId",record.getProcessInstanceId());
         recObj.put("ncNodeId",record.getNcNodeId());
         addSuperProperties(recObj,record);
         jsonArray.add(recObj);
      }
      addInputStreamToZip(zipOut,new ByteArrayInputStream(jsonArray.toJSONString().getBytes(StandardCharsets.UTF_8)),"exp_mdm_approve_record.json");
   }*/
   void addSuperProperties(JSONObject recObj, BizEntity entity){
      recObj.put("tenantId",entity.getTenantId());
      recObj.put("createTime",entity.getCreateTime());
      recObj.put("updateTime",entity.getUpdateTime());
      recObj.put("createUser",entity.getCreateUser());
      recObj.put("updateUser",entity.getUpdateUser());
      recObj.put("status",entity.getStatus());
      recObj.put("createDept",entity.getCreateDept());
   }
   /**
    * 将 输入流 中的内容写入zip