yangys
2025-08-04 95b1f7b2b35872444acf8e85cc236682d0c4e47e
blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/NcProgramExportDNCService.java
@@ -8,6 +8,7 @@
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springblade.core.mp.base.BizEntity;
import org.springblade.core.mp.base.BizServiceImpl;
import org.springblade.core.mp.support.Condition;
@@ -15,7 +16,9 @@
import org.springblade.core.oss.OssTemplate;
import org.springblade.core.tool.utils.Func;
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;
import org.springblade.mdm.program.entity.NcNode;
import org.springblade.mdm.program.entity.NcProgram;
import org.springblade.mdm.program.entity.NcProgramApproved;
@@ -46,19 +49,11 @@
   private final ApproveRecordService approveRecordService;
   private final NcNodeService ncNodeService;
   private final OssTemplate ossTemplate;
   public static final String PROGRAM_JSON_FILE = "exp_mdm_nc_program.json";
   private final FlowProgramFileService flowProgramFileService;
   //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";
   /**
    * 是否压缩包内的数据文件
    * @param filename 文件名称
    * @return
    */
   public static boolean isDataFile(String filename){
      return StringUtils.equals(filename, PROGRAM_JSON_FILE) || StringUtils.equals(filename, NODE_JSON_FILE) || StringUtils.equals(filename, APPROVE_RECORD_JSON_FILE);
   }
   /**
    * 分页查询
    * @param query 查询参数
@@ -81,87 +76,140 @@
      //FileOutputStream fos = new FileOutputStream("d:/exportDnc.zip");
      try (ZipOutputStream zipOut = new ZipOutputStream(os);) {//os
         ArrayList<Long> programIdList = new ArrayList<Long>();
         ArrayList<Long> programPackageNodeIdList = new ArrayList<Long>();
         for (Long approvedId : approvedIdArr) {
            NcProgramApproved approved = approvedService.getById(approvedId);
            //programIdList.add(approved.getNcProgramId());
            programPackageNodeIdList.add(approved.getNcNodeId());
            //NcProgram prog = progService.getById(approved.getNcProgramId());
            //String filename = prog.getOssName();
            //InputStream inputStream = ossTemplate.statFileStream(filename);
            //addInputStreamToZip(zipOut, inputStream, prog.getName());
            addProgramPackageToZip(zipOut,approved);
         }
         addDataJson(zipOut, programIdList);
         addDataJson(zipOut, programPackageNodeIdList);
      }
      os.close();
   }
   /**
    * 将程序包和下属文件加入压缩包
    * @param zipOut
    * @param approved
    */
   private void addProgramPackageToZip(ZipOutputStream zipOut, NcProgramApproved approved) throws IOException{
      String packageFolder = approved.getProgramName()+"/";
      ZipEntry zipEntry = new ZipEntry(packageFolder);// "/"结尾表示文件夹
      zipOut.putNextEntry(zipEntry);
      zipOut.closeEntry();
      List<NcNode> programNodes = ncNodeService.lambdaQuery().eq(NcNode::getParentId, approved.getNcNodeId()).list();
      FlowProgramFile programFile;
      for (NcNode node : programNodes) {
         String filePathInZip = packageFolder + node.getName();
         programFile = this.flowProgramFileService.getById(node.getFlowProgramFileId());
         InputStream inputStream = ossTemplate.statFileStream(programFile.getOssName());
         /*
         ZipEntry fileEntry = new ZipEntry(filePathInZip);
         zipOut.putNextEntry(fileEntry);
         programFile = this.flowProgramFileService.getById(node.getFlowProgramFileId());
         InputStream inputStream = ossTemplate.statFileStream(programFile.getOssName());
         byte[] buffer = new byte[1024];
         int length;
         while ((length = inputStream.read(buffer)) >= 0) {
            zipOut.write(buffer, 0, length);
         }
         zipOut.closeEntry();
          */
         this.addInputStreamToZip(zipOut,inputStream,filePathInZip);
      }
   }
   /**
    * 导入数据文件
    * @param zipOut
    */
   void addDataJson(ZipOutputStream zipOut, List<Long> programIdList) throws IOException {
      addProgramDataJson(zipOut, programIdList);
      addApproveRecordDataJson(zipOut, programIdList);
      addNcNodeDataJson(zipOut, programIdList);
   void addDataJson(ZipOutputStream zipOut, List<Long> programPackageNodeIdList) throws IOException {
      addNodeDataJson(zipOut, programPackageNodeIdList);
      addApproveRecordDataJson(zipOut, programPackageNodeIdList);
   }
   /**
    * 导入程序记录
    * zip保重加入节点的数据
    * @param zipOut
    * @param programIdList
    * @param pkgNodeIdList
    * @throws IOException
    */
   void addProgramDataJson(ZipOutputStream zipOut, List<Long> programIdList) throws IOException {
      LambdaQueryWrapper<NcProgram> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.in(NcProgram::getId, programIdList);
      List<NcProgram> programList = progService.list(queryWrapper);
   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(NcProgram program : programList){
         JSONObject recObj = new JSONObject();
         recObj.put("id", program.getId());
         recObj.put("code", program.getCode());
         recObj.put("ossName",program.getOssName());
         recObj.put("isTextFile",program.getIsTextFile());
         recObj.put("isLastEdition",program.getIsLastEdition());
         recObj.put("category",program.getCategory());
         recObj.put("description",program.getDescription());
         recObj.put("name",program.getName());
         recObj.put("drawingNo",program.getDrawingNo());
         recObj.put("ncNodeId",program.getNcNodeId());
         recObj.put("bindNcNodeId",program.getBindNcNodeId());
         recObj.put("url",program.getUrl());
         recObj.put("isCured",program.getIsCured());
         recObj.put("isLocked",program.getIsLocked());
         recObj.put("isTest",program.getIsTest());
         recObj.put("machineCode",program.getMachineCode());
         recObj.put("processEdition",program.getProcessEdition());
         recObj.put("taskAssignTime",program.getTaskAssignTime());
         addSuperProperties(recObj,program);
      for(NcNode node : allNodes){
         JSONObject recObj = getNodeJsonObject(node);
         addSuperProperties(recObj,node);
         jsonArray.add(recObj);
      }
      addInputStreamToZip(zipOut,new ByteArrayInputStream(jsonArray.toJSONString().getBytes(StandardCharsets.UTF_8)),PROGRAM_JSON_FILE);
      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 programIdList
    * @param programPackageNodeIdList 程序包名的id列表
    */
   void addApproveRecordDataJson(ZipOutputStream zipOut, List<Long> programIdList) throws IOException {
      LambdaQueryWrapper<ApproveRecord> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.in(ApproveRecord::getNcProgramId, programIdList);
      List<ApproveRecord> records = approveRecordService.list(queryWrapper);
   void addApproveRecordDataJson(ZipOutputStream zipOut, List<Long> programPackageNodeIdList) throws IOException {
      /*
      List<String> instanceIds = this.ncNodeService.lambdaQuery().in(NcNode::getId, programPackageNodeIdList)
         .list().stream().map(NcNode::getProcessInstanceId).toList();
*/
      //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();
@@ -172,71 +220,14 @@
         recObj.put("operateTime",record.getOperateTime());
         recObj.put("operateResult",record.getOperateResult());
         recObj.put("taskName",record.getTaskName());
         recObj.put("ncProgramId",record.getNcProgramId());
         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");
   }
   /**
    * 导入节点
    * @param zipOut
    * @param programIdList
    */
   void addNcNodeDataJson(ZipOutputStream zipOut, List<Long> programIdList) throws IOException {
      LambdaQueryWrapper<NcProgram> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.in(NcProgram::getId, programIdList);
      List<NcProgram> programs = progService.list(queryWrapper);
      JSONArray jsonArray = new JSONArray();
      ArrayList<Long> exportNodeIdList    = new ArrayList<>();
      for(NcProgram program : programs){
         //JSONObject recObj = new JSONObject();
         if(!exportNodeIdList.contains(program.getBindNcNodeId())){
            exportNodeIdList.add(program.getBindNcNodeId());
         }
         NcNode ncNode = ncNodeService.getById(program.getBindNcNodeId());//从绑定的节点本身开始导出
         if(StringUtils.isNotEmpty(ncNode.getParentIds())){
            List<Long> pids = Func.toLongList(ncNode.getParentIds());
            for(Long nodeId : pids){
               if(!exportNodeIdList.contains(nodeId)){
                  exportNodeIdList.add(nodeId);
               }
            }
         }
      }
      LambdaQueryWrapper<NcNode> nodeQueryWrapper = new LambdaQueryWrapper<>();
      nodeQueryWrapper.in(NcNode::getId, exportNodeIdList);
      List<NcNode> nodeList =ncNodeService.list(nodeQueryWrapper);
      for(NcNode node : nodeList){
         JSONObject recObj = new JSONObject();
         recObj.put("id", node.getId());
         recObj.put("nodeType", node.getNodeType());
         recObj.put("machineCode",node.getMachineCode());
         recObj.put("parentId",node.getParentId());
         recObj.put("description",node.getDescription());
         recObj.put("name",node.getName());
         recObj.put("remark",node.getRemark());
         recObj.put("drawingNo",node.getDrawingNo());
         recObj.put("parentIds",node.getParentIds());
         recObj.put("processName",node.getProcessName());
         addSuperProperties(recObj,node);
         jsonArray.add(recObj);
      }
      addInputStreamToZip(zipOut,new ByteArrayInputStream(jsonArray.toJSONString().getBytes(StandardCharsets.UTF_8)),"exp_mdm_nc_node.json");
   }
   void addSuperProperties(JSONObject recObj, BizEntity entity){
@@ -249,7 +240,14 @@
      recObj.put("createDept",entity.getCreateDept());
   }
   public void addInputStreamToZip(ZipOutputStream zipOut, InputStream inputStream, String entryName)
   /**
    * 将 输入流 中的内容写入zip
    * @param zipOut zip输出流
    * @param inputStream 输入流
    * @param entryName 文件名
    * @throws IOException
    */
   void addInputStreamToZip(ZipOutputStream zipOut, InputStream inputStream, String entryName)
      throws IOException {
      // 创建新的 ZIP 条目
      ZipEntry zipEntry = new ZipEntry(entryName);