From f4c6e0e1308bccb943ca1cddfdf7f643b6b6a1aa Mon Sep 17 00:00:00 2001
From: yangys <y_ys79@sina.com>
Date: 星期二, 15 七月 2025 23:39:30 +0800
Subject: [PATCH] 测试导出dnc

---
 blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java |  233 +++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 213 insertions(+), 20 deletions(-)

diff --git a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java
index ec40485..f9fdce0 100644
--- a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java
+++ b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java
@@ -9,15 +9,22 @@
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
 import org.apache.commons.compress.archivers.zip.ZipFile;
 import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
+import org.springblade.core.mp.base.BizEntity;
 import org.springblade.core.mp.base.BizServiceImpl;
+import org.springblade.core.oss.OssTemplate;
+import org.springblade.core.oss.model.BladeFile;
+import org.springblade.core.redis.cache.BladeRedis;
+import org.springblade.core.secure.utils.AuthUtil;
 import org.springblade.core.tool.utils.FileUtil;
 import org.springblade.core.tool.utils.Func;
 import org.springblade.core.tool.utils.IoUtil;
+import org.springblade.mdm.flow.entity.ApproveRecord;
+import org.springblade.mdm.flow.service.ApproveRecordService;
 import org.springblade.mdm.flow.service.CureFlowService;
+import org.springblade.mdm.program.entity.NcNode;
 import org.springblade.mdm.program.entity.NcProgram;
 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.program.vo.MdmProgramImportVO;
 import org.springblade.mdm.utils.CustomBinaryReader;
 import org.springframework.stereotype.Service;
@@ -27,7 +34,7 @@
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.time.LocalDateTime;
+import java.time.Duration;
 import java.util.*;
 
 /**
@@ -41,6 +48,14 @@
 public class MdmProgramImportService extends BizServiceImpl<NcProgramExchangeMapper, NcProgramExchange> {
 	private final CureFlowService cureFlowService;
 	private final NcProgramService ncProgramService;
+	private final NcNodeService ncNodeService;
+	private final ApproveRecordService approveRecordService;
+	private final BladeRedis bladeRedis;
+	private final OssTemplate ossTemplate;
+
+	private String getFileKey(){
+		return "mdmimpfile-"+ AuthUtil.getUserId();
+	}
 	/**
 	 * MDM瀵煎叆鏂囦欢涓婁紶
 	 * @param file MDM娑夊瘑缃戝鍑烘枃浠�
@@ -49,12 +64,17 @@
 	public List<MdmProgramImportVO> mdmImportUpload(MultipartFile file) {
 		List<MdmProgramImportVO> list;
 		try {
+			BladeFile bfile = ossTemplate.putFile(file);//涓婁紶锛屼緵鍚庣画鍏ュ簱浣跨敤
+
+			//璁剧疆涓�涓紦瀛橈紝2灏忔椂杩囨湡
+			bladeRedis.setEx(getFileKey(),bfile.getName(), Duration.ofHours(2));
+			String pkgFileName = bladeRedis.get(getFileKey());
 			//String fileName = file.getOriginalFilename();
 			//InputStream zipFileInputStream = FileExchangeUtil.convertFileToZip(file.getInputStream());
 			InputStream zipFileInputStream = file.getInputStream();//test
 
 			byte[] bytes = FileUtil.copyToByteArray(zipFileInputStream);
-			list = parseDncZipFromByteArray(bytes);
+			list = parseMdmZipFromByteArray(bytes);
 
 
 		} catch (IOException e) {
@@ -89,10 +109,8 @@
 		System.out.println("鍒涘缓鐨勪复鏃舵枃浠�: " + tempFile);
 		return tempFile.toFile();
 	}
-	public static List<MdmProgramImportVO> parseDncZipFromByteArray(byte[] zipData) throws IOException {
+	public static List<MdmProgramImportVO> parseMdmZipFromByteArray(byte[] zipData) throws IOException {
 		List<MdmProgramImportVO> list = new ArrayList<>();
-		//List<DncSendBackData> datas  = ZipFileDirectoryScanner.getFilesInDirectoryRecursive(zipData, "");
-
 		Map<String,String> fileMd5Map = new HashMap<>();
 		Map<String,MdmProgramImportVO> fileDataMap = new HashMap<>();
 		try (SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel(zipData);
@@ -101,9 +119,8 @@
 			ZipArchiveEntry entry;
 			Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
 			while (entries.hasMoreElements()) {
-			//while ((entry = zis.getNextZipEntry()) != null) {
 				entry = entries.nextElement();
-				DncSendBackData prog = new DncSendBackData();
+
 				String entryName = entry.getName();
 
 				if (!entry.isDirectory()) {
@@ -120,9 +137,19 @@
 								d.setName(jsonObject.getString("name"));
 								d.setId(jsonObject.getLong("id"));
 								d.setCode(jsonObject.getString("code"));
-								//d.setFileBackTime(LocalDateTime.now());//鍒拌揪鏃堕棿
+								d.setDescription(jsonObject.getString("description"));
+								d.setCategory(jsonObject.getString("category"));
+								d.setBindNcNodeId(jsonObject.getLong("bindNcNodeId"));
+								d.setIsLastEdition(jsonObject.getInteger("isLastEdition"));
+								d.setIsLocked(jsonObject.getInteger("isLocked"));
+								d.setIsTest(jsonObject.getInteger("isTest"));
+								d.setMachineCode(jsonObject.getString("machineCode"));
+								d.setNcNodeId(jsonObject.getLong("ncNodeId"));
+								d.setDrawingNo(jsonObject.getString("drawingNo"));
+								d.setProcessEdition(jsonObject.getString("processEdition"));
 
 								fileDataMap.put(d.getName(),d);
+
 								list.add(d);
 							}
 
@@ -133,11 +160,6 @@
 						}
 
 					}
-					System.out.println("鏂囦欢鍚�: " + entry.getName());
-					System.out.println("澶у皬: " + entry.getSize());
-
-					// 璇诲彇鏂囦欢鍐呭鍒板瓧鑺傛暟缁�
-					ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 
 				}
 
@@ -159,13 +181,16 @@
 	 * @param ids id鍒楄〃閫楀彿鍒嗛殧
 	 * @return
 	 */
-	public void mdmFileAccept(String ids) {
-		/*
+	public void mdmFileAccept(String ids) throws IOException {
+
+		String pkgFileName = bladeRedis.get(getFileKey());
+
 		List<Long> idList = Func.toLongList(ids);
+		//
+		updateProgramFile(pkgFileName,idList);
+
 		List<NcProgram> progList = ncProgramService.listByIds(idList);
 		NcProgramExchange exchange;
-		//NcProgram program;
-		//NcNode programNode;
 
 		for(NcProgram prog:progList){
 			exchange = new NcProgramExchange();
@@ -173,11 +198,179 @@
 			exchange.setExchangeType(2);//鍥炰紶
 			exchange.setNcProgramId(prog.getId());
 
+
 			this.save(exchange);
 
 		}
-	//鐩存帴鍏ュ簱锛屾棤娴佺▼
-		*/
 
+
+	}
+
+	/**
+	 * 鏇存柊绋嬪簭鏂囦欢
+	 * @param pkgFileName mdm绋嬪簭鍖�
+	 * @param idList 閫夊畾鍏ュ簱鐨�
+	 */
+	private void updateProgramFile(String pkgFileName,List<Long> idList) throws IOException {
+		InputStream inputStream = this.ossTemplate.statFileStream(pkgFileName);
+		byte[] bytes = FileUtil.copyToByteArray(inputStream);
+
+		List<NcNode> nodeList = new ArrayList<>();
+		List<NcProgram> progList = new ArrayList<>();
+		List<ApproveRecord> recordList = new ArrayList<>();
+
+		try (SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel(bytes);
+			 ZipFile zipFile = new ZipFile(channel)) {
+
+			ZipArchiveEntry entry;
+			Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
+			while (entries.hasMoreElements()) {
+				entry = entries.nextElement();
+
+				String entryName = entry.getName();
+
+				if (!entry.isDirectory()) {
+					if(entryName.equals(NcProgramExportDNCService.PROGRAM_JSON_FILE)){
+
+						try (InputStream insJson = zipFile.getInputStream(entry)) {
+							String jsonStr = IoUtil.readToString(insJson);
+
+							JSONArray jsonArray = JSONArray.parseArray(jsonStr);
+							for(int i=0;i<jsonArray.size();i++){
+								JSONObject jsonObject = jsonArray.getJSONObject(i);
+								NcProgram prog = new NcProgram();
+								prog.setId(jsonObject.getLong("id"));
+								prog.setName(jsonObject.getString("name"));
+
+								prog.setCode(jsonObject.getString("code"));
+								prog.setDescription(jsonObject.getString("description"));
+								prog.setCategory(jsonObject.getString("category"));
+								prog.setBindNcNodeId(jsonObject.getLong("bindNcNodeId"));
+								prog.setIsLastEdition(jsonObject.getInteger("isLastEdition"));
+								prog.setIsLocked(jsonObject.getInteger("isLocked"));
+								prog.setIsTest(jsonObject.getInteger("isTest"));
+								prog.setMachineCode(jsonObject.getString("machineCode"));
+								prog.setNcNodeId(jsonObject.getLong("ncNodeId"));
+								prog.setDrawingNo(jsonObject.getString("drawingNo"));
+								prog.setProcessEdition(jsonObject.getString("processEdition"));
+
+								setBaseProperties(prog,jsonObject);
+
+								progList.add(prog);
+							}
+
+						}
+					}else if(entryName.equals(NcProgramExportDNCService.NODE_JSON_FILE)){
+						try (InputStream insJson = zipFile.getInputStream(entry)) {
+							String jsonStr = IoUtil.readToString(insJson);
+							JSONArray jsonArray = JSONArray.parseArray(jsonStr);
+							for (int i = 0; i < jsonArray.size(); i++) {
+								JSONObject jsonObject = jsonArray.getJSONObject(i);
+								NcNode node = new NcNode();
+								node.setId(jsonObject.getLong("id"));
+								node.setName(jsonObject.getString("name"));
+								node.setDescription(jsonObject.getString("description"));
+								node.setNodeType(jsonObject.getString("nodeType"));
+								node.setProcessName(jsonObject.getString("processName"));
+								node.setDrawingNo(jsonObject.getString("drawingNo"));
+								node.setMachineCode(jsonObject.getString("machineCode"));
+								node.setParentId(jsonObject.getLong("parentId"));
+								node.setParentIds(jsonObject.getString("parentIds"));
+								node.setIsCured(jsonObject.getInteger("isCured"));
+
+								setBaseProperties(node,jsonObject);
+
+								nodeList.add(node);
+							}
+						}
+					}else if(entryName.equals(NcProgramExportDNCService.APPROVE_RECORD_JSON_FILE)){
+						try (InputStream insJson = zipFile.getInputStream(entry)) {
+							String jsonStr = IoUtil.readToString(insJson);
+							JSONArray jsonArray = JSONArray.parseArray(jsonStr);
+							for (int i = 0; i < jsonArray.size(); i++) {
+								JSONObject jsonObject = jsonArray.getJSONObject(i);
+								ApproveRecord record = new ApproveRecord();
+								record.setId(jsonObject.getLong("id"));
+								record.setNcProgramId(jsonObject.getLong("ncProgramId"));
+								record.setUserId(jsonObject.getLong("userId"));
+								record.setUserNickname(jsonObject.getString("userNickname"));
+								record.setOperateResult(jsonObject.getString("operateResult"));
+								record.setOperateTime(jsonObject.getDate("operateTime"));
+								record.setComment(jsonObject.getString("comment"));
+								setBaseProperties(record,jsonObject);
+
+								recordList.add(record);
+							}
+						}
+					}
+
+				}
+			}
+
+		}
+
+
+		try (SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel(bytes);
+			 ZipFile zipFile = new ZipFile(channel)) {
+
+			ZipArchiveEntry entry;
+			Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
+			while (entries.hasMoreElements()) {
+				entry = entries.nextElement();
+
+				String entryName = entry.getName();
+
+				if (!entry.isDirectory() && !entryName.equals(NcProgramExportDNCService.PROGRAM_JSON_FILE)) {
+
+					for(NcProgram prog:progList){
+						if(prog.getName().equals(entryName)){
+							try (InputStream ncFileStream = zipFile.getInputStream(entry)) {
+								BladeFile bfile = this.ossTemplate.putFile(prog.getName(),ncFileStream);
+								prog.setOssName(bfile.getName());
+								prog.setUrl(bfile.getLink());
+							}
+							break;
+						}
+					}
+
+				}
+			}
+		}
+
+		for(NcNode node:nodeList){
+			NcNode nodeTemp = this.ncNodeService.getById(node.getId());
+			if(nodeTemp == null){
+				ncNodeService.save(node);
+			}else{
+				ncNodeService.updateById(node);
+			}
+		}
+
+		for(NcProgram prog:progList){
+			NcProgram ncTemp = ncProgramService.getById(prog.getId());
+			if(ncTemp == null){
+				ncProgramService.save(prog);
+			}else{
+				ncProgramService.updateById(prog);
+			}
+		}
+
+		for(ApproveRecord record:recordList) {
+			ApproveRecord recordTemp = this.approveRecordService.getById(record.getId());
+			if (recordTemp == null) {
+				approveRecordService.save(record);
+			} else {
+				approveRecordService.updateById(record);
+			}
+		}
+
+	}
+
+	void setBaseProperties(BizEntity entity,JSONObject jsonObject){
+		entity.setCreateTime(jsonObject.getDate("createTime"));
+		entity.setUpdateTime(jsonObject.getDate("updateTime"));
+		entity.setStatus(jsonObject.getInteger("status"));
+		entity.setCreateUser(jsonObject.getLong("createUser"));
+		entity.setUpdateUser(jsonObject.getLong("updateUser"));
 	}
 }

--
Gitblit v1.9.3