From b4d10ff7535002dddb63a0b28ddb37fee7ed1e9d Mon Sep 17 00:00:00 2001
From: yangys <y_ys79@sina.com>
Date: 星期三, 03 十二月 2025 16:30:53 +0800
Subject: [PATCH] 增加pdf,xlsx,docx文件预览功能

---
 blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/DNCSendBackService.java |  134 +++++++++++++++++++++++++-------------------
 1 files changed, 77 insertions(+), 57 deletions(-)

diff --git a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/DNCSendBackService.java b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/DNCSendBackService.java
index 5708e6f..33d7a5b 100644
--- a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/DNCSendBackService.java
+++ b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/program/service/DNCSendBackService.java
@@ -1,7 +1,6 @@
 
 package org.springblade.mdm.program.service;
 
-import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.FilenameUtils;
@@ -18,10 +17,7 @@
 import org.springblade.core.tool.utils.Func;
 import org.springblade.mdm.basesetting.machine.entity.Machine;
 import org.springblade.mdm.basesetting.machine.service.MachineService;
-import org.springblade.mdm.basesetting.producedivision.entity.QinzheFgb;
-import org.springblade.mdm.basesetting.producedivision.service.QinzheFgbService;
-import org.springblade.mdm.commons.contants.RegExpConstants;
-import org.springblade.mdm.commons.service.ParamService;
+import org.springblade.mdm.commons.contants.ZipConstants;
 import org.springblade.mdm.flow.entity.FlowProgramFile;
 import org.springblade.mdm.flow.service.CureFlowService;
 import org.springblade.mdm.flow.service.FlowCommonService;
@@ -32,23 +28,20 @@
 import org.springblade.mdm.program.service.programannotation.*;
 import org.springblade.mdm.program.vo.DncSendBackData;
 import org.springblade.mdm.program.vo.DncSendBackFile;
-import org.springblade.mdm.program.vo.ProgramAnnotation;
 import org.springblade.mdm.utils.FileContentUtil;
-import org.springblade.mdm.utils.ProgramFileNameParser;
 import org.springblade.mdm.utils.ZipTextFileContentUtil;
-import org.springblade.system.pojo.entity.DictBiz;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.*;
+import java.nio.charset.Charset;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.StandardOpenOption;
 import java.time.Duration;
 import java.util.*;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 
@@ -59,23 +52,26 @@
  */
 @Slf4j
 @Service
-@AllArgsConstructor
 public class DNCSendBackService extends BizServiceImpl<NcProgramExchangeMapper, NcProgramExchange> {
-	private final CureFlowService cureFlowService;
-	private final QinzheFgbService qinzheFbgService;
-	private final NcNodeService ncNodeService;
-	private final OssTemplate ossTemplate;
-	private final BladeRedis bladeRedis;
-	private final FlowCommonService flowCommonService;
-	private final DncBackFileService dncBackFileService;
-	private final MachineService machineService;
-	private final ParamService paramService;
+	@Autowired
+	private CureFlowService cureFlowService;
+	@Autowired
+	private NcNodeService ncNodeService;
+	@Autowired
+	private OssTemplate ossTemplate;
+	@Autowired
+	private BladeRedis bladeRedis;
+	@Autowired
+	private FlowCommonService flowCommonService;
+	@Autowired
+	private DncBackFileService dncBackFileService;
+	@Autowired
+	private MachineService machineService;
 
 	/**
 	 * 鍋忕鍗曟枃浠舵湯灏剧殑妯″紡锛歅+鏁板瓧
 	 */
 	private static final String P_NUMBER_PATTERN = "(?i)P\\d+";
-
 	private String getFileKey(){
 		return "dncimpfile-"+ AuthUtil.getUserId();
 	}
@@ -112,19 +108,50 @@
 	/**
 	 * 浠庡帇缂╁寘 瑙f瀽鍥炰紶绋嬪簭鍒楄〃锛岃繖閲岃В鏋愮洰褰曞嵆鍙紝鐩綍灏辨槸绋嬪簭鍖呭悕
 	 * @param inputStream 鍘嬬缉鍖呰緭鍏ユ祦
+	 * @return 鏂囦欢鎿嶄綔寮傚父
+	 */
+	List<DncSendBackData> parseProgramListFromZip(InputStream inputStream) throws IOException {
+		List<DncSendBackData> result = null;
+
+		ByteArrayInputStream byteInsStream = new ByteArrayInputStream(FileUtil.copyToByteArray(inputStream));
+		for (String encoding : ZipConstants.TRY_ENCODINGS) {
+			try {
+				result = parseProgramListByCharset(byteInsStream,Charset.forName(encoding));
+				log.error("浣跨敤缂栫爜 {} 瑙f瀽鎴愬姛 ",encoding);
+				break;
+			}catch (ServiceException se) {
+				log.error("鏁版嵁涓嶆纭紓甯�:",se);
+				break;
+			} catch (Exception e) {
+				byteInsStream.reset();
+				log.error("浣跨敤缂栫爜 {} 瑙f瀽澶辫触:",encoding,e);
+			}
+		}
+		if(result != null) {
+			return result;
+		}else{
+			return Collections.emptyList();
+		}
+	}
+	/**
+	 * 浠庡帇缂╁寘 瑙f瀽鍥炰紶绋嬪簭鍒楄〃锛岃繖閲岃В鏋愮洰褰曞嵆鍙紝鐩綍灏辨槸绋嬪簭鍖呭悕
+	 * @param inputStream 鍘嬬缉鍖呰緭鍏ユ祦
 	 * @return 鍥炰紶绋嬪簭鍒楄〃
 	 * @throws IOException 鏂囦欢鎿嶄綔寮傚父
 	 */
-	List<DncSendBackData> parseProgramListFromZip(InputStream inputStream) throws IOException {
+	List<DncSendBackData> parseProgramListByCharset(InputStream inputStream,Charset charset) throws IOException {
 		List<DncSendBackData> list = new ArrayList<>();
+
 		Path tempZipFile = createTempFile(inputStream);
 
 		List<String> fileEntryNameList = new ArrayList<>();
 		List<String> dirEntryNameList = new ArrayList<>();//绋嬪簭鍖呭悕+宸ュ簭鐗堟 浣滀负鏂囦欢澶瑰悕
-		try (ZipFile zipFile = new ZipFile(tempZipFile.toFile())) {
 
+
+		ZipEntry entry;
+		try (ZipFile zipFile = new ZipFile(tempZipFile.toFile(),charset)) {
 			Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
-			ZipEntry entry;
+
 			//鑾峰彇鎵�鏈夌殑entry鍚嶇О
 			while (zipEntries.hasMoreElements()) {
 				entry = zipEntries.nextElement();
@@ -208,36 +235,7 @@
 		return list;
 	}
 
-	/**
-	 * 淇绋嬪簭鍖呭悕,fanuc涓嶈瘑鍒笅鍒掔嚎锛屼笅鍙戞椂杞崲涓轰簡-锛岃繖閲岄渶瑕佺‘璁ゃ�傚簲璇ユ槸浠庡鍑鸿褰曚腑鏌ユ壘淇敼鍚庣殑鍖呭悕锛屼絾鏄幇鍦虹紪鍒剁殑娌℃湁鍜嬪姙锛�
-	 * @param packageNameInZip
-	 * @return
-	 */
-	/*
-	private String fixProgramPackageName(String packageNameInZip) {
-		Matcher matcher = RegExpConstants.PROGRAM_PACKAGE_PATTERN.matcher(packageNameInZip);
 
-		String drawingNo = null;
-		if(matcher.find()) {
-			drawingNo = matcher.group(1);
-		}
-		String processNo = null;
-		if(matcher.find()) {
-			processNo = matcher.group(2);
-		}
-
-		String processEdition = null;
-		if(matcher.find()) {
-			processEdition = matcher.group(3);
-		}
-		if(drawingNo != null && processNo != null && processEdition != null) {
-			if(drawingNo.contains("_")) {
-
-			}
-		}
-		return packageNameInZip;
-	}
-	*/
 
 	/**
 	 * 鍏ュ簱鍥炰紶鏂囦欢,骞跺惎鍔ㄥ浐鍖栨祦绋�
@@ -246,8 +244,7 @@
 	@Transactional
 	public void dncFileAccept(String ids) throws IOException {
 		List<Long> acceptIdList = Func.toLongList(ids);
-		//
-		NcProgramExchange exchange;
+
 		String filekey = getFileKey();
 		String zipFileName = bladeRedis.get(filekey);
 		log.info("filekey={},鏂囦欢鍚�={}",filekey,zipFileName);
@@ -257,7 +254,6 @@
 		cureFlowService.startCureNew(pkgIdFileMap);
 
 	}
-
 	/**
 	 * 澶勭悊鍥炰紶鏂囦欢
 	 * @param ossFileName
@@ -268,12 +264,36 @@
 	private Map<Long, List<FlowProgramFile>> dealWithBackFile(String ossFileName, List<Long> acceptIdList) throws IOException{
 		Map<Long, List<FlowProgramFile>> pkgIdFileMap = new HashMap<>();
 
+		//ByteArrayInputStream byteInsStream = new ByteArrayInputStream(FileUtil.copyToByteArray(inputStream));
+		for (String encoding : ZipConstants.TRY_ENCODINGS) {
+			try {
+				pkgIdFileMap = dealWithBackFileWithCharset(ossFileName,acceptIdList,Charset.forName(encoding));
+				log.error("浣跨敤缂栫爜 {} 瑙f瀽鎴愬姛 ",encoding);
+				break;
+			} catch (Exception e) {
+				//byteInsStream.reset();
+				log.error("浣跨敤缂栫爜 {} 瑙f瀽澶辫触: ",encoding,e);
+			}
+		}
+
+		return pkgIdFileMap;
+	}
+
+	/**
+	 * 澶勭悊鍥炰紶鏂囦欢
+	 * @param ossFileName
+	 * @param acceptIdList
+	 * @return
+	 * @throws IOException
+	 */
+	private Map<Long, List<FlowProgramFile>> dealWithBackFileWithCharset(String ossFileName, List<Long> acceptIdList,Charset charset) throws IOException{
+		Map<Long, List<FlowProgramFile>> pkgIdFileMap = new HashMap<>();
 		InputStream inputStream = this.ossTemplate.statFileStream(ossFileName);
 		Path tempZipFile = createTempFile(inputStream);
 		List<String> entryNameList = new ArrayList<>();
 
 		ZipEntry entry;
-		try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(tempZipFile.toFile())) {
+		try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(tempZipFile.toFile(),charset)) {
 			Enumeration<? extends ZipEntry> entries = zipFile.entries();
 			while(entries.hasMoreElements()) {
 				entry = entries.nextElement();

--
Gitblit v1.9.3