From c0ccbb980be6975389f09ad13dcabd7d3156dae3 Mon Sep 17 00:00:00 2001
From: yangys <y_ys79@sina.com>
Date: 星期一, 22 九月 2025 19:03:56 +0800
Subject: [PATCH] 更新提醒-》只提醒编程员;工控网显示临时更改单号

---
 blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java |  229 ++++++++++++++++++++++++++++++++------------------------
 1 files changed, 131 insertions(+), 98 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 4fc6fa8..f5ac1a9 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
@@ -4,6 +4,7 @@
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springblade.core.log.exception.ServiceException;
 import org.springblade.core.mp.base.BizServiceImpl;
@@ -14,6 +15,8 @@
 import org.springblade.core.tool.utils.Func;
 import org.springblade.mdm.basesetting.machine.service.MachineService;
 import org.springblade.mdm.basesetting.machine.entity.Machine;
+import org.springblade.mdm.commons.contants.RegExpConstants;
+import org.springblade.mdm.gkw.programnode.vo.ProgramNameVO;
 import org.springblade.mdm.machinefile.entity.FileSendRecord;
 import org.springblade.mdm.machinefile.service.FileSendRecordService;
 import org.springblade.mdm.program.entity.NcProgramExchange;
@@ -21,6 +24,7 @@
 import org.springblade.mdm.program.service.programannotation.AnnotationProperties;
 import org.springblade.mdm.program.vo.MdmProgramImportVO;
 import org.springblade.mdm.utils.FileContentUtil;
+import org.springblade.mdm.utils.ProgramFileNameParser;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
@@ -32,6 +36,8 @@
 import java.time.Duration;
 import java.util.*;
 import java.nio.file.*;
+import java.util.regex.Matcher;
+import java.util.stream.Stream;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 /**
@@ -61,29 +67,34 @@
 	 * @return
 	 */
 	public List<MdmProgramImportVO> mdmImportUpload(MultipartFile file) {
-		List<MdmProgramImportVO> list = new ArrayList<>();
+		List<MdmProgramImportVO> list;
 		try {
 			if(file == null || file.isEmpty()){
 				throw new ServiceException("鏂囦欢涓虹┖");
 			}
-
+			/*
 			if(!StringUtils.endsWith(file.getOriginalFilename(),".zip")){
 				throw new ServiceException("鏂囦欢蹇呴』涓簔ip鍖�");
-			}
+			}*/
 			String tempDir = System.getProperty("java.io.tmpdir");
 			Path tempPath = Paths.get(tempDir);
-
-			// 鍒涘缓瑙e帇鐩爣鐩綍锛堝湪涓存椂鐩綍涓嬪垱寤轰竴涓敮涓�瀛愮洰褰曪級
-			Path tempZipFile = Files.createTempFile("mdmimpfile-"+System.currentTimeMillis(), ".zip");
-
-			file.transferTo(tempZipFile);
-
-			// 鍒涘缓瑙e帇鐩爣鐩綍锛堝湪涓存椂鐩綍涓嬪垱寤轰竴涓敮涓�瀛愮洰褰曪級
-			Path extractDir = Files.createTempDirectory(tempPath, "unzip_");
-
+			Path extractDir = Files.createTempDirectory(tempPath, "unzip_"+System.currentTimeMillis());
 			bladeRedis.setEx(getFileKey(),extractDir.toString(), Duration.ofHours(2));
+			if(StringUtils.endsWithIgnoreCase(file.getOriginalFilename(),".zip")){
+				// 鍒涘缓瑙e帇鐩爣鐩綍锛堝湪涓存椂鐩綍涓嬪垱寤轰竴涓敮涓�瀛愮洰褰曪級
+				Path tempZipFile = Files.createTempFile("mdmimpfile-"+System.currentTimeMillis(), ".zip");
+				file.transferTo(tempZipFile);
+				extractZipToTempDir(tempZipFile,extractDir);
 
-			extractZipToTempDir(tempZipFile,extractDir);
+
+			}else{
+				//鏅�氭枃浠讹紝鐩存帴鏀惧叆extract鏂囦欢澶�
+				String basename = FilenameUtils.getBaseName(file.getOriginalFilename());
+				String ext = FilenameUtils.getExtension(file.getOriginalFilename());
+				//Path filepath = Files.createTempFile(extractDir,basename,StringUtils.isEmpty(ext) ? "": "."+ext);
+				//Path filepath = Files.createFile();
+				file.transferTo(Paths.get(extractDir.toString()+File.separator+file.getOriginalFilename()));
+			}
 
 			//璇诲彇鏂囦欢鐩綍
 			list = readTempDir(extractDir);
@@ -94,10 +105,15 @@
 		return list;
 	}
 
+	/**
+	 * 瑙e帇zip鍖呭埌涓存椂璺緞
+	 * @param zipFilePath zip鍖呮枃浠舵湇鍔″櫒涓婄殑璺緞
+	 * @param extractDir 鐩爣鐩綍
+	 * @throws IOException 鏂囦欢鎿嶄綔寮傚父
+	 */
 	public void extractZipToTempDir(Path zipFilePath,Path extractDir) throws IOException {
 		// 鑾峰彇绯荤粺涓存椂鐩綍
 		String tempDir = System.getProperty("java.io.tmpdir");
-		Path tempPath = Paths.get(tempDir);
 
 		// 鍒涘缓瑙e帇鐩爣鐩綍锛堝湪涓存椂鐩綍涓嬪垱寤轰竴涓敮涓�瀛愮洰褰曪級
 		try (InputStream fis = Files.newInputStream(zipFilePath);
@@ -132,10 +148,10 @@
 
 	/**
 	 * 闃叉ZIP Slip鏀诲嚮
-	 * @param zipEntry
-	 * @param targetDir
-	 * @return
-	 * @throws IOException
+	 * @param zipEntry zip鍐呴儴鏂囦欢璺緞
+	 * @param targetDir 鐩爣鏂囦欢澶�
+	 * @return 鏂囦欢璺緞
+	 * @throws IOException 鎿嶄綔鏂囦欢IO寮傚父
 	 */
 	Path zipSlipProtect(ZipEntry zipEntry, Path targetDir) throws IOException {
 		Path targetDirResolved = targetDir.resolve(zipEntry.getName());
@@ -150,16 +166,28 @@
 		return normalizePath;
 	}
 
-
+	/**
+	 * 璇诲彇瑙e帇鏂囦欢澶逛笅鎵�鏈夋枃浠� 瑙f瀽鎴恦o鍒楄〃
+	 * @param extractDir 瑙e帇鏂囦欢澶�
+	 * @return vo鍒楄〃
+	 * @throws IOException 瑙f瀽鏂囦欢鐨勫紓甯�
+	 */
 	public List<MdmProgramImportVO> readTempDir(Path extractDir) throws IOException {
 		List<MdmProgramImportVO> list = new ArrayList<>();
-
-		//List<Machine> machines = machineService.getEnableMachines();
+		try (Stream<Path> paths = Files.walk(extractDir)) {
+			List<Path> filePathList = paths
+				.filter(Files::isRegularFile).toList();  // 鍙繚鐣欐櫘閫氭枃浠讹紝鎺掗櫎鐩綍
+				//.collect(Collectors.toList());
+			//System.out.println("鎵�鏈夋枃浠�"+filePathList);
+			for(Path path : filePathList){
+				list.add(readFileToVO(path));
+			}
+		}
+		/*
 		//璇诲彇鎵�鏈夋枃浠跺す
 		try (DirectoryStream<Path> stream = Files.newDirectoryStream(extractDir)) {
 			for (Path path : stream) {
 				if (Files.isDirectory(path)) {
-
 					// 濡傛灉鏄瓙鐩綍锛岃鍙栧叾涓殑鏂囦欢
 					try (DirectoryStream<Path> subStream = Files.newDirectoryStream(path)) {
 						for (Path subPath : subStream) {
@@ -171,70 +199,73 @@
 				} else if (Files.isRegularFile(path)) {
 					System.out.println("鎵惧埌鏂囦欢2: " + path);
 					//杩欓噷 鎵惧埌鐨勬枃浠朵笉鏄�
-					MdmProgramImportVO vo = new MdmProgramImportVO();
-					vo.setFilename(path.getFileName().toString());
-					vo.setDrawingNo(parseDrawingNo(vo.getFilename()));
-
-					try (InputStream inputStream = Files.newInputStream(path)) {
-						// 浣跨敤杈撳叆娴佽鍙栨枃浠跺唴瀹�
-						byte[] buffer = new byte[2000];
-						inputStream.read(buffer);
-						vo.setMd5(DigestUtils.md5Hex(buffer));
-					} catch (IOException e) {
-						log.error("璇诲彇鏂囦欢md5澶辫触",e);
-					}
-
-
-					try (InputStream inputStream = Files.newInputStream(path, StandardOpenOption.READ)) {
-						// 浣跨敤杈撳叆娴佽鍙栨枃浠跺唴瀹�
-						ByteArrayInputStream bas = new ByteArrayInputStream(inputStream.readAllBytes());
-
-						AnnotationProperties defAnnoProperties = AnnotationProperties.getDefault();
-						String sendPathLine = FileContentUtil.readLineAt(bas,defAnnoProperties.getSendPathLineIndex());
-						//bas.mark(0);
-						bas.reset();
-						String statusLine = FileContentUtil.readLineAt(bas,defAnnoProperties.getStatusLineIndex());
-						log.info("sendPathLine={}", sendPathLine);
-
-						Machine matchedMachine = machineService.getMachineBySendPathAnnotation(sendPathLine);
-						/*
-						for (Machine machine : machines) {
-							if(Func.isNotBlank(machine.getProgSendDir()) && sendPathLine.contains(machine.getProgSendDir())){
-								matchedMachine = machine;
-								break;
-							}
-
-						}
-
-						 */
-						if (matchedMachine != null) {
-							vo.setName(parseProgramName(vo.getFilename()));
-							vo.setMachineCode(matchedMachine.getCode());
-
-							vo.setFullPath(path.toString());//鏂囦欢鍦板潃
-							vo.setSendPath(matchedMachine.getProgSendDir());
-							vo.setId(vo.getFullPath());
-							vo.setProgramStatus(programAnnotationService.removeAnnotation(matchedMachine.getControlSystem(),statusLine));
-
-							list.add(vo);
-						}
-
-					} catch (IOException e) {
-						log.error("璇诲彇鏂囦欢md5澶辫触",e);
-					}
+					list.add(readFileToVO(path));
 				}
 			}
-		}
+		}*/
 		return list;
 	}
 
+	/**
+	 * 灏嗘枃浠剁粍缁囨垚VO
+	 * @param path 鏂囦欢path
+	 * @return vo
+	 */
+	MdmProgramImportVO readFileToVO(Path path){
+		MdmProgramImportVO vo = new MdmProgramImportVO();
+		vo.setFilename(path.getFileName().toString());
 
+		ProgramNameVO pnmameVO = ProgramFileNameParser.parseProgramName(vo.getFilename());
+		vo.setDrawingNo(pnmameVO.getDrawingNo());
+		//vo.setDrawingNo(parseDrawingNo(vo.getFilename()));
+
+		try (InputStream inputStream = Files.newInputStream(path)) {
+			// 浣跨敤杈撳叆娴佽鍙栨枃浠跺唴瀹�
+			byte[] buffer = new byte[2000];
+			inputStream.read(buffer);
+			vo.setMd5(DigestUtils.md5Hex(buffer));
+		} catch (IOException e) {
+			log.error("璇诲彇鏂囦欢md5澶辫触",e);
+		}
+
+		try (InputStream inputStream = Files.newInputStream(path, StandardOpenOption.READ)) {
+			// 浣跨敤杈撳叆娴佽鍙栨枃浠跺唴瀹�
+			ByteArrayInputStream bas = new ByteArrayInputStream(inputStream.readAllBytes());
+
+			AnnotationProperties defAnnoProperties = AnnotationProperties.getDefault();
+			String sendPathLine = FileContentUtil.readLineAt(bas,defAnnoProperties.getSendPathLineIndex());
+			//bas.mark(0);
+			bas.reset();
+			String statusLine = FileContentUtil.readLineAt(bas,defAnnoProperties.getStatusLineIndex());
+			log.info("sendPathLine={}", sendPathLine);
+
+			Machine matchedMachine = machineService.getMachineBySendPathAnnotation(sendPathLine);
+
+			if (matchedMachine != null) {
+				//vo.setName(parseProgramName(vo.getFilename()));
+				vo.setName(pnmameVO.logicProgramName());
+				vo.setMachineCode(matchedMachine.getCode());
+
+				vo.setFullPath(path.toString());//鏂囦欢鍦板潃
+				vo.setSendPath(matchedMachine.getProgSendDir());
+				vo.setId(vo.getFullPath());
+				vo.setProgramStatus(programAnnotationService.removeAnnotation(matchedMachine.getControlSystem(),statusLine));
+			}
+		} catch (IOException e) {
+			log.error("璇诲彇鏂囦欢澶辫触",e);
+			throw new ServiceException("瀵煎叆绋嬪簭澶辫触"+path.getFileName().toString()+","+e.getMessage());
+		}
+		return vo;
+	}
 	/**
 	 * 瑙f瀽鍑洪浂缁勪欢濂�
 	 * @param filename
 	 * @return
 	 */
+	/*
 	String parseDrawingNo(String filename){
+
+		/*
 		String drawingNo = "";
 		int idx = filename.lastIndexOf("-");
 		String temp;
@@ -262,6 +293,8 @@
 
 		}
 		return drawingNo;
+
+
 	}
 
 	String parseProgramName(String filename){
@@ -282,12 +315,10 @@
 				}
 			}
 			//浠ヤ笂鍘绘帀浜嗘渶鍚�2娈垫鏁板拰娈靛彿
-
-
 		}
 		return programName;
 	}
-
+*/
 	/**
 	 * 鍏ュ簱mdm娑夊瘑缃戞枃浠�
 	 * @param ids id鍒楄〃閫楀彿鍒嗛殧
@@ -298,35 +329,37 @@
 
 		String dictStr = bladeRedis.get(getFileKey());
 		if(dictStr == null){
-			throw new ServiceException("");
+			throw new ServiceException("鏂囦欢缂撳瓨宸茶繃鏈燂紝璇烽噸鏂颁笂浼犳枃浠躲��");
 		}
 		Path extractDir = Paths.get(dictStr);
-
 		List<MdmProgramImportVO> list = readTempDir(extractDir);
 
 		String destFileFull;
 		for(String str : idList){
-			for(MdmProgramImportVO vo : list){
-				if(StringUtils.equals(vo.getFullPath(),str)){
-					destFileFull = vo.getSendPath()+File.separator+vo.getFilename();
-					File destFile = new File(destFileFull);
-					FileUtils.forceMkdirParent(destFile);
-					FileUtils.copyFile(new File(str),destFile);
+			Optional<MdmProgramImportVO> optVO = list.stream().filter(vo -> vo.getId().equals(str)).findFirst();
 
-					FileSendRecord record = new FileSendRecord();
-					record.setName(destFile.getName());
-					Path destPath = Paths.get(destFileFull);
-					record.setMachineCode(vo.getMachineCode());
-					record.setFileSize(Files.size(destPath));
-
-					try(InputStream inputStream = new FileInputStream(destFile)){
-						BladeFile bFile = ossTemplate.putFile(record.getName(), inputStream);
-						record.setOssName(bFile.getName());
-					}
-					fileSendRecordService.save(record);
-					break;
-				}
+			if(optVO.isEmpty()){
+				continue;
 			}
+
+			MdmProgramImportVO vo = optVO.get();
+
+			destFileFull = vo.getSendPath()+File.separator+vo.getFilename();
+			File destFile = new File(destFileFull);
+			FileUtils.forceMkdirParent(destFile);
+			FileUtils.copyFile(new File(str),destFile);
+
+			FileSendRecord record = new FileSendRecord();
+			record.setName(destFile.getName());
+			Path destPath = Paths.get(destFileFull);
+			record.setMachineCode(vo.getMachineCode());
+			record.setFileSize(Files.size(destPath));
+
+			try(InputStream inputStream = new FileInputStream(destFile)){
+				BladeFile bFile = ossTemplate.putFile(record.getName(), inputStream);
+				record.setOssName(bFile.getName());
+			}
+			fileSendRecordService.save(record);
 		}
 
 	}

--
Gitblit v1.9.3