From 9810791a41d381a10451f3e9770cfcfedf98e886 Mon Sep 17 00:00:00 2001
From: yangys <y_ys79@sina.com>
Date: 星期五, 22 八月 2025 00:39:34 +0800
Subject: [PATCH] 增加首页搜索接口

---
 blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/MdmProgramImportService.java |  375 +++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 276 insertions(+), 99 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 f180476..9318958 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
@@ -1,33 +1,24 @@
 
 package org.springblade.mdm.program.service;
 
-import com.alibaba.fastjson.JSONArray;
-import com.alibaba.fastjson.JSONObject;
-import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.codec.digest.DigestUtils;
-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.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.StringUtils;
 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.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.basesetting.machine.MachineService;
+import org.springblade.mdm.basesetting.machine.entity.Machine;
 import org.springblade.mdm.program.entity.NcProgramExchange;
 import org.springblade.mdm.program.mapper.NcProgramExchangeMapper;
 import org.springblade.mdm.program.vo.MdmProgramImportVO;
-import org.springblade.mdm.utils.CustomBinaryReader;
+import org.springblade.mdm.utils.FileContentUtil;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.support.SimpleTriggerContext;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -37,7 +28,9 @@
 import java.nio.file.Paths;
 import java.time.Duration;
 import java.util.*;
-
+import java.nio.file.*;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
 /**
  * MDM绋嬪簭瀵煎叆锛堝伐鎺х綉鍔熻兘锛�
  * 鐩墠宸ユ帶缃戝睍鐜板舰寮忔湭瀹氾紝鏆傛椂瀹炵幇鏆傚仠
@@ -45,37 +38,62 @@
  */
 @Slf4j
 @Service
-@AllArgsConstructor
 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;
+
+	@Autowired
+	private BladeRedis bladeRedis;
+
+	@Autowired
+	private ProgramAnnotationService programAnnotationService;
+
+	@Autowired
+	private MachineService machineService;
 
 	private String getFileKey(){
-		return "mdmimpfile-"+ AuthUtil.getUserId();
+		return "mdmgkwimpfile-"+ AuthUtil.getUserId();
 	}
 	/**
-	 * MDM瀵煎叆鏂囦欢涓婁紶
+	 * 宸ユ帶MDM瀵煎叆鏂囦欢涓婁紶
 	 * @param file MDM娑夊瘑缃戝鍑烘枃浠�
 	 * @return
 	 */
 	public List<MdmProgramImportVO> mdmImportUpload(MultipartFile file) {
-		List<MdmProgramImportVO> list;
+		List<MdmProgramImportVO> list = new ArrayList<>();
 		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");
+			//Path extractDir = Files.createTempDirectory(tempPath, "unzip_");
+
+			file.transferTo(tempZipFile);
+
+			// 鍒涘缓瑙e帇鐩爣鐩綍锛堝湪涓存椂鐩綍涓嬪垱寤轰竴涓敮涓�瀛愮洰褰曪級
+			Path extractDir = Files.createTempDirectory(tempPath, "unzip_");
+
+			bladeRedis.setEx(getFileKey(),extractDir.toString(), Duration.ofHours(2));
+
+			extractZipToTempDir(tempZipFile,extractDir);
+
+			//璇诲彇鏂囦欢鐩綍
+			/*
 			BladeFile bfile = ossTemplate.putFile(file);//涓婁紶锛屼緵鍚庣画鍏ュ簱浣跨敤
 
 			//璁剧疆涓�涓紦瀛橈紝2灏忔椂杩囨湡
-			bladeRedis.setEx(getFileKey(),bfile.getName(), Duration.ofHours(2));
-			String pkgFileName = bladeRedis.get(getFileKey());
 			InputStream zipFileInputStream = file.getInputStream();//test
 
 			byte[] bytes = FileUtil.copyToByteArray(zipFileInputStream);
 			list = parseMdmZipFromByteArray(bytes);
-
-
+			*/
+			list = readTempDir(extractDir);
 		} catch (IOException e) {
 			log.error("瀵煎叆娑夊瘑缃戞憜娓℃枃浠跺け璐�",e);
 			throw new ServiceException("瑙f瀽DNC鍥炰紶鏁版嵁澶辫触");
@@ -83,6 +101,65 @@
 		return list;
 	}
 
+	public void extractZipToTempDir(Path zipFilePath,Path extractDir) throws IOException {
+		// 鑾峰彇绯荤粺涓存椂鐩綍
+		String tempDir = System.getProperty("java.io.tmpdir");
+		Path tempPath = Paths.get(tempDir);
+
+		// 鍒涘缓瑙e帇鐩爣鐩綍锛堝湪涓存椂鐩綍涓嬪垱寤轰竴涓敮涓�瀛愮洰褰曪級
+		//Path extractDir = Files.createTempDirectory(tempPath, "unzip_");
+		System.out.println("瑙e帇鐩綍: " + extractDir.toString());
+		//Files.newInputStream(Paths.get(zipFilePath));
+		try (InputStream fis = Files.newInputStream(zipFilePath);
+			 ZipInputStream zis = new ZipInputStream(fis)) {
+
+			ZipEntry zipEntry = zis.getNextEntry();
+			while (zipEntry != null) {
+				Path newPath = zipSlipProtect(zipEntry, extractDir);
+
+				if (zipEntry.isDirectory()) {
+					Files.createDirectories(newPath);
+				} else {
+					// 纭繚鐖剁洰褰曞瓨鍦�
+					if (newPath.getParent() != null) {
+						Files.createDirectories(newPath.getParent());
+					}
+
+					// 鍐欏叆鏂囦欢
+					try (OutputStream fos = Files.newOutputStream(newPath)) {
+						byte[] buffer = new byte[1024];
+						int len;
+						while ((len = zis.read(buffer)) > 0) {
+							fos.write(buffer, 0, len);
+						}
+					}
+				}
+				zipEntry = zis.getNextEntry();
+			}
+			zis.closeEntry();
+		}
+	}
+
+	/**
+	 * 闃叉ZIP Slip鏀诲嚮
+	 * @param zipEntry
+	 * @param targetDir
+	 * @return
+	 * @throws IOException
+	 */
+	Path zipSlipProtect(ZipEntry zipEntry, Path targetDir) throws IOException {
+		Path targetDirResolved = targetDir.resolve(zipEntry.getName());
+
+		// 瑙勮寖鍖栬矾寰�
+		Path normalizePath = targetDirResolved.normalize();
+
+		if (!normalizePath.startsWith(targetDir)) {
+			throw new IOException("鎭舵剰ZIP鏉$洰: " + zipEntry.getName());
+		}
+
+		return normalizePath;
+	}
+	/*
 	public static List<MdmProgramImportVO> parseMdmZipFromByteArray(byte[] zipData) throws IOException {
 		List<MdmProgramImportVO> list = new ArrayList<>();
 		Map<String,String> fileMd5Map = new HashMap<>();
@@ -114,7 +191,155 @@
 		});
 		return list;
 	}
+	*/
 
+	public List<MdmProgramImportVO> readTempDir(Path extractDir) throws IOException {
+		List<MdmProgramImportVO> list = new ArrayList<>();
+
+		List<Machine> machines = machineService.lambdaQuery().eq(Machine::getStatus,Machine.STATUS_ENABLE).list();
+		//璇诲彇鎵�鏈夋枃浠跺す
+		//List<Path> dirs = Files.list(extractDir).filter(Files::isDirectory).toList();
+		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) {
+							if (Files.isRegularFile(subPath)) {
+								System.out.println("鎵惧埌鏂囦欢: " + subPath);
+								//files.add(subPath.getFileName().toString());
+								// 杩欓噷鍙互娣诲姞瀵规枃浠剁殑澶勭悊閫昏緫
+							}
+						}
+					}
+					//vo.setFiles(files);
+
+				} 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());
+						String line1 = FileContentUtil.readLineAt(bas,1);
+						//bas.mark(0);
+						bas.reset();
+						String line2 = FileContentUtil.readLineAt(bas,2);
+						System.out.println("line1="+line1);
+						Machine matchedMachine = null;
+						for (Machine machine : machines) {
+							if(Func.isNotBlank(machine.getProgSendDir()) && line1.contains(machine.getProgSendDir())){
+								matchedMachine = machine;
+								break;
+							}
+
+						}
+						if (matchedMachine != null) {
+							vo.setName(parseProgramName(vo.getFilename()));
+							vo.setMachineCode(matchedMachine.getCode());
+							//String sendPath = programAnnotationService.removeAnnotation(matchedMachine.getMachineGroupCode(),line1,"(",")");
+							vo.setFullPath(path.toString());//鏂囦欢鍦板潃
+							vo.setSendPath(matchedMachine.getProgSendDir());
+							vo.setId(vo.getFullPath());
+							vo.setProgramStatus(programAnnotationService.removeAnnotation(matchedMachine.getMachineGroupCode(),line2,"(",")"));
+
+							list.add(vo);
+						}
+
+					} catch (IOException e) {
+						log.error("璇诲彇鏂囦欢md5澶辫触",e);
+					}
+
+
+					//List<String> files = new ArrayList<>();
+
+
+					// 杩欓噷鍙互娣诲姞瀵规枃浠剁殑澶勭悊閫昏緫
+				}
+			}
+		}
+
+
+		return list;
+	}
+
+
+	/**
+	 * 瑙f瀽鍑洪浂缁勪欢濂�
+	 * @param filename
+	 * @return
+	 */
+	String parseDrawingNo(String filename){
+		String drawingNo = "";
+		int idx = filename.lastIndexOf("-");
+		String temp;
+		if(idx != -1){
+			temp = filename.substring(0,idx);
+
+			idx = temp.lastIndexOf("-");
+			if(idx != -1){
+				temp = temp.substring(0,idx);
+
+				//鍘绘帀宸ュ簭鐗堟
+				idx = temp.lastIndexOf("-");
+				if(idx != -1){
+					temp = temp.substring(0,idx);
+
+					//鍘绘帀宸ュ簭鍙�
+					idx = temp.lastIndexOf("-");
+					if(idx != -1){
+						drawingNo = temp.substring(0,idx);
+					}
+				}
+			}
+			//浠ヤ笂鍘绘帀浜嗘渶鍚�2娈垫鏁板拰娈靛彿
+
+
+		}
+		return drawingNo;
+	}
+
+	String parseProgramName(String filename){
+		String programName = "";
+		int idx = filename.lastIndexOf("-");
+		String temp;
+		if(idx != -1){
+			temp = filename.substring(0,idx);
+
+			idx = temp.lastIndexOf("-");
+			if(idx != -1){
+				temp = temp.substring(0,idx);
+
+				//鍘绘帀宸ュ簭鐗堟
+				idx = temp.lastIndexOf("-");
+				if(idx != -1){
+					programName = temp.substring(0,idx);
+				}
+			}
+			//浠ヤ笂鍘绘帀浜嗘渶鍚�2娈垫鏁板拰娈靛彿
+
+
+		}
+		return programName;
+	}
 
 	/**
 	 * 鍏ュ簱mdm娑夊瘑缃戞枃浠�
@@ -123,77 +348,29 @@
 	 */
 	public void mdmFileAccept(String ids) throws IOException {
 
-		String pkgFileName = bladeRedis.get(getFileKey());
+		List<String> idList = Func.toStrList(ids);
 
-		List<Long> idList = Func.toLongList(ids);
-		//
-		updateProgramFile(pkgFileName,idList);
+		String ditStr = bladeRedis.get(getFileKey());
+		Path extractDir = Paths.get(ditStr);
+
+		List<MdmProgramImportVO> list = readTempDir(extractDir);
+
+		String destFileFull;
+		for(String str : idList){
+			System.out.println(str);
+			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);
+					break;
+				}
+			}
+		}
+
 
 	}
 
-	/**
-	 * 鏇存柊绋嬪簭鏂囦欢
-	 * @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();
-
-			}
-
-		}
-
-
-
-		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