From a049dda2f63d59f5092994b44387fe0aa889d375 Mon Sep 17 00:00:00 2001
From: yangys <y_ys79@sina.com>
Date: 星期四, 14 八月 2025 15:01:04 +0800
Subject: [PATCH] 固化流程

---
 blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/NcProgramExportDNCService.java |  346 +++++++++++++++++++++++++++++++++++----------------------
 1 files changed, 214 insertions(+), 132 deletions(-)

diff --git a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/NcProgramExportDNCService.java b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/NcProgramExportDNCService.java
index 17e2b18..3617173 100644
--- a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/NcProgramExportDNCService.java
+++ b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/NcProgramExportDNCService.java
@@ -1,33 +1,42 @@
 
 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.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 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;
 import org.springblade.core.mp.support.Query;
 import org.springblade.core.oss.OssTemplate;
+import org.springblade.core.tool.api.R;
 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;
 import org.springblade.mdm.program.entity.NcNode;
-import org.springblade.mdm.program.entity.NcProgram;
 import org.springblade.mdm.program.entity.NcProgramApproved;
 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.ArrayList;
+import java.util.Collections;
 import java.util.List;
+import java.util.Optional;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
 
@@ -41,126 +50,249 @@
 @AllArgsConstructor
 public class NcProgramExportDNCService extends BizServiceImpl<NcProgramExchangeMapper, NcProgramExchange> {
 
-	private final NcProgramService progService;
+	//private final NcProgramService progService;
+	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 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";
 
-	/**
-	 * 鏄惁鍘嬬缉鍖呭唴鐨勬暟鎹枃浠�
-	 * @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 鏌ヨ鍙傛暟
 	 * @return
 	 */
 	public IPage<DncSendBackData> dncSendBackPageQuery(Query query) {
-
-		IPage<DncSendBackData> page = this.getBaseMapper().dncSendBackpageQuery(Condition.getPage(query),query);
-
-		return page;
-
+		return this.getBaseMapper().dncSendBackpageQuery(Condition.getPage(query),query);
 	}
-
 
 	/**
 	 * 瀵煎嚭dnc鍘嬬缉鍖�
-	 * @param approvedIdArr 寰呭鍑哄鎵硅〃id鏁扮粍
+	 * @param approvedIdArray 寰呭鍑哄鎵硅〃id鏁扮粍
 	 */
-	public void exportDnc(Long[] approvedIdArr, OutputStream os) throws IOException {
+	public void exportDnc(Long[] approvedIdArray, OutputStream os) throws IOException {
 		//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) {
+			for (Long approvedId : approvedIdArray) {
 				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);
+
+			//鐘舵�佷慨鏀逛负宸插鍑�
+			approvedService.lambdaUpdate().in(NcProgramApproved::getId, approvedIdArray)
+				.set(NcProgramApproved::getStatus,NcProgramApproved.STATUS_EXPORTED).update();
+			//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::getIsLastEdition,1).eq(NcNode::getParentId, approved.getNcNodeId()).list();
+
+		FlowProgramFile programFile;
+		NcNode programPackageNode = this.ncNodeService.getById(approved.getNcNodeId());
+		String status = "SQ";
+		if(programPackageNode.isDeviationProgram()){
+			status = "PL";
+		}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());
+
+			InputStream inputStream = ossTemplate.statFileStream(programFile.getOssName());
+			Machine machine = machineService.getByCode(node.getMachineCode());
+			InputStream addedIns1 = addSendDirAnnotation(inputStream,machine,annotationList);
+			InputStream addedIns2 = addProgramStatusAnnotation(addedIns1,status,machine,annotationList);
+			this.addInputStreamToZip(zipOut,addedIns2,filePathInZip);
+		}
+
+	}
+
+	/**
+	 * 鑾峰彇瀛楀吀涓厤缃殑娉ㄩ噴
+	 * @return 瀛楀吀椤瑰垪琛�
+	 */
+	List<Dict> getAnnotionList(){
+		List<Dict> annotationList;
+		R<List<Dict>> dictsResult = dictClient.getList(ANNOTATION_DICT);
+		if(dictsResult.isSuccess()) {
+			annotationList = dictsResult.getData();
+		}else{
+			annotationList = Collections.emptyList();
+		}
+
+		return annotationList;
+	}
+	/**
+	 * 鍔犲叆鏈哄簥涓嬪彂璺緞鍜岀▼搴忕姸鎬佺殑娉ㄩ噴
+	 * @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);
+	}
+
+	/**
+	 * 娣诲姞绋嬪簭涓撴敼鐨勬敞閲�
+	 * @param inputStream
+	 * @param status 绋嬪簭鐘舵��
+	 * @param machine
+	 * @param annotationList 閰嶇疆鐨勬敞閲婂瓧鍏稿垪琛�
+	 * @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);
+	}
+
+	/**
+	 * 鍒ゆ柇涓�琛屾枃鏈槸鍚︽槸娉ㄩ噴
+	 * @param line
+	 * @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;
+		String begin = "(";
+		String end = ")";
+
+		Optional<Dict> dictOpt = dictsResult.getData().stream().filter(dict -> {return dict.getDictKey().equals(machineGroupCode);}).findFirst();
+		String annotationSetting = "(,)";
+		if(dictOpt.isPresent()) {
+			annotationSetting = dictOpt.get().getDictValue();
+		}
+		String[] arr = annotationSetting.split(",");
+		if(arr.length == 2){
+			begin = arr[0];
+			end = arr[1];
+		}
+
+		annotation = begin + oriTest + end;
+		return annotation;
+
+	}
 	/**
 	 * 瀵煎叆鏁版嵁鏂囦欢
 	 * @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("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 绋嬪簭鍖呭悕鐨刬d鍒楄〃
 	 */
-	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<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();
@@ -171,72 +303,15 @@
 			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.getNcNodeId())){
-				exportNodeIdList.add(program.getNcNodeId());
-			}
-			NcNode ncNode = ncNodeService.getById(program.getNcNodeId());
-
-			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){
 		recObj.put("tenantId",entity.getTenantId());
@@ -248,7 +323,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);

--
Gitblit v1.9.3