From 23855599412c4d61b38d78f0f3abd3430a48b5b1 Mon Sep 17 00:00:00 2001 From: zhangherong <571457620@qq.com> Date: 星期三, 25 六月 2025 11:51:38 +0800 Subject: [PATCH] Merge branch 'mdc_hyjs_master' --- lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/service/impl/FileFerryService.java | 212 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 212 insertions(+), 0 deletions(-) diff --git a/lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/service/impl/FileFerryService.java b/lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/service/impl/FileFerryService.java new file mode 100644 index 0000000..4661dcd --- /dev/null +++ b/lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/service/impl/FileFerryService.java @@ -0,0 +1,212 @@ +package org.jeecg.modules.dnc.service.impl; + +import org.jeecg.modules.dnc.dto.ComponentHierarchy; +import org.jeecg.modules.dnc.dto.TransferPackage; +import org.jeecg.modules.dnc.entity.ComponentInfo; +import org.jeecg.modules.dnc.entity.DocFile; +import org.jeecg.modules.dnc.service.IDocClassificationService; +import org.jeecg.modules.dnc.service.IDocInfoService; +import org.jeecg.modules.dnc.service.IDocRelativeService; +import org.jeecg.modules.dnc.utils.JsonUtils; +import org.jeecg.modules.mdc.mapper.MdcEquipmentMapper; +import org.jeecg.modules.system.service.IMdcProductionService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.util.ArrayList; +import java.util.List; + +@Service +public class FileFerryService { + + private final DataPackageService dataPackageService; + private final SecurityService securityService; + + private static final Logger logger = LoggerFactory.getLogger(FileFerryService.class); + + @Value("${deploy.secretFolder}") + private String ferryPath; + + @Value("${fileHomePath}") + private String fileHomePath; + + @Autowired + private MdcEquipmentMapper mdcEquipmentMapper; + + @Autowired + private IMdcProductionService mdcProductionService; + + @Autowired + private IDocClassificationService classificationService; + + @Autowired + private IDocRelativeService docRelativeService; + + @Autowired + private IDocInfoService docInfoService; + + @Autowired + public FileFerryService(DataPackageService dataPackageService, SecurityService securityService) { + this.dataPackageService = dataPackageService; + this.securityService = securityService; + } + + public String exportData(TransferPackage.DataType type, String id,String fileName) { + // 1. 鑾峰彇灏佽鏁版嵁 + TransferPackage transferPackage = dataPackageService.packageData(type, id); + + // 2. 鍘嬬缉灞傜骇缁撴瀯 + compressHierarchy(transferPackage); + + // 3. JSON搴忓垪鍖� + String json = JsonUtils.toJson(transferPackage); + +// // 4. 鍘嬬缉鍔犲瘑 +// byte[] compressed = CompressionUtils.gzipCompress(json.getBytes(StandardCharsets.UTF_8)); +// byte[] encrypted = securityService.encrypt(compressed); + //鏆傛椂涓嶅姞瀵� + byte[] compressed = json.getBytes(StandardCharsets.UTF_8); + + // 5. 鐢熸垚鏂囦欢 + Path filePath = Paths.get(ferryPath,fileName); + try { + Files.createDirectories(filePath.getParent()); + Files.write(filePath, compressed); + return filePath.toString(); + } catch (IOException e) { + throw new RuntimeException("鏂囦欢鍐欏叆澶辫触", e); + } + } + + public TransferPackage importData(String filePath) { + try { + // 1. 璇诲彇鏂囦欢 + Path path = Paths.get(filePath); + String fileName = path.getFileName().toString(); + byte[] encrypted = Files.readAllBytes(path); + logger.debug("璇诲彇鏂囦欢瀹屾垚, 澶у皬: {} 瀛楄妭", encrypted.length); + + // 2. 瑙e瘑 (褰撳墠宸叉敞閲�) + // byte[] compressed = securityService.decrypt(encrypted); + + // 3. 瑙e帇缂� +// byte[] jsonBytes = CompressionUtils.gzipDecompress(encrypted); + String json = new String(encrypted, StandardCharsets.UTF_8); + logger.debug("瑙e帇缂╁畬鎴�, JSON闀垮害: {} 瀛楃", json.length()); + + // 璁板綍JSON鍐呭鐢ㄤ簬璋冭瘯 + logger.trace("鍘熷JSON鍐呭:\n{}", json); + + // 4. JSON鍙嶅簭鍒楀寲 + logger.debug("寮�濮嬪弽搴忓垪鍖�..."); + TransferPackage pkg = JsonUtils.fromJson(json, TransferPackage.class); + + // 5. 澶勭悊鏂囦欢鍚� - 绀轰緥: 10A20250614000026_3102038 + String[] split = fileName.split("_"); + if (split.length < 2) { + throw new IllegalArgumentException("鏃犳晥鐨勬枃浠跺悕鏍煎紡: " + fileName); + } + + String id = split[0]; + String equipmentId = split[1].split("\\.")[0]; + + // 鎻愬彇鍓嶇紑鍜屾暟瀛楅儴鍒� + int aIndex = id.indexOf("A"); + if (aIndex == -1 || aIndex == id.length() - 1) { + throw new IllegalArgumentException("鏃犳晥鐨処D鏍煎紡: " + id); + } + + String prefix = id.substring(0, aIndex + 1); + String numericPart = id.substring(aIndex + 1); + + // 璁$畻鍓嶄竴涓枃浠跺悕 + long number = Long.parseLong(numericPart); + number--; // 鑾峰彇鍓嶄竴涓簭鍒楀彿 + + // 淇濇寔鐩稿悓浣嶆暟鏍煎紡 + String newNumeric = String.format("%0" + numericPart.length() + "d", number); + String ncFileName = prefix + newNumeric + "_" + equipmentId+".NC"; + String ncFilePath = path.getParent().resolve(ncFileName).toString(); + + // 6. 鑾峰彇鏂囦欢澶嶅埗鐩爣璺緞 + DocFile docFile = pkg.getTraceChain().getDocFile(); + if (docFile == null) { + throw new IllegalStateException("浼犺緭鍖呬腑缂哄皯鏂囨。鏂囦欢淇℃伅"); + } + + // 鏋勫缓鐩爣璺緞 + String targetDirectory = fileHomePath + docFile.getFilePath(); + String targetPath = Paths.get(targetDirectory, docFile.getFileEncodeName()).toString(); + + // 纭繚鐩爣鐩綍瀛樺湪 + File targetDir = new File(targetDirectory); + if (!targetDir.exists() && !targetDir.mkdirs()) { + throw new IOException("鏃犳硶鍒涘缓鐩爣鐩綍: " + targetDirectory); + } + + // 7. 澶嶅埗鏂囦欢骞堕噸鍛藉悕 + logger.info("澶嶅埗鏂囦欢: {} 鈫� {}", ncFilePath, targetPath); + Path source = Paths.get(ncFilePath); + Files.copy(source, Paths.get(targetPath), StandardCopyOption.REPLACE_EXISTING); + + // 10.鍒犻櫎涓存椂NC鏂囦欢涓巎son鏂囦欢 + logger.info("鍒犻櫎涓存椂鏂囦欢: {}", ncFilePath); + Files.delete(source); + Files.delete(path); + + return JsonUtils.fromJson(json, TransferPackage.class); + + } catch (NumberFormatException e) { + throw new RuntimeException("鏂囦欢鍚嶄腑鐨勬暟瀛楁牸寮忔棤鏁�: " + e.getMessage(), e); + } catch (IOException e) { + throw new RuntimeException("鏂囦欢鎿嶄綔澶辫触: " + e.getMessage(), e); + } catch (Exception e) { + logger.error("鏂囦欢瀵煎叆澶辫触 [璺緞: {}]", filePath, e); + throw new RuntimeException("鏂囦欢瀵煎叆澶辫触: " + e.getMessage(), e); + } + } + + private void compressHierarchy(TransferPackage pkg) { + if (pkg.getTraceChain() == null || + pkg.getTraceChain().getComponentHierarchy() == null || + pkg.getTraceChain().getComponentHierarchy().getComponents().size() < 4) { + return; + } + + ComponentHierarchy hierarchy = pkg.getTraceChain().getComponentHierarchy(); + List<ComponentInfo> compressed = new ArrayList<>(); + + // 淇濈暀鏍归儴浠� + compressed.add(hierarchy.getComponents().get(0)); + + // 淇濈暀鍏抽敭涓棿鑺傜偣 + int step = Math.max(1, hierarchy.getComponents().size() / 3); + for (int i = step; i < hierarchy.getComponents().size() - 1; i += step) { + compressed.add(hierarchy.getComponents().get(i)); + } + + // 淇濈暀鍙跺瓙閮ㄤ欢 + compressed.add(hierarchy.getLeafComponent()); + + // 鏇存柊灞傜骇 + hierarchy.getComponents().clear(); + hierarchy.getComponents().addAll(compressed); + } + + private String generateFilename(TransferPackage.DataType type, String id) { + return String.format("%s_%s_%d.ferry", + type.name().toLowerCase(), + id, + System.currentTimeMillis()); + } +} -- Gitblit v1.9.3