From b508ec38ddf9ed93d4435e8f1e3c4effef798aaa Mon Sep 17 00:00:00 2001 From: lyh <925863403@qq.com> Date: 星期一, 15 九月 2025 17:18:04 +0800 Subject: [PATCH] 起落架DNC项目更改sql 更改yml配置 集成3DE接口 --- src/main/java/com/lxzn/webservice/controller/DncWebController.java | 294 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 224 insertions(+), 70 deletions(-) diff --git a/src/main/java/com/lxzn/webservice/controller/DncWebController.java b/src/main/java/com/lxzn/webservice/controller/DncWebController.java index bf17007..04a9b22 100644 --- a/src/main/java/com/lxzn/webservice/controller/DncWebController.java +++ b/src/main/java/com/lxzn/webservice/controller/DncWebController.java @@ -1,11 +1,14 @@ package com.lxzn.webservice.controller; +import com.lxzn.framework.domain.webservice.request.ThirdDeProgramSource; import com.lxzn.framework.domain.webservice.request.PlmProgramSource; import com.lxzn.webservice.DncWebService; import com.lxzn.webservice.ext.MesResultModel; import lombok.extern.slf4j.Slf4j; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipFile; +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.transaction.annotation.Transactional; @@ -17,7 +20,6 @@ import java.nio.charset.StandardCharsets; import java.nio.file.*; import java.util.*; -import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -29,16 +31,22 @@ @Autowired private DncWebService dncWebService; - @Value("${ncPdm.file_path}") + @Value("${fileHomePath}") private String filePath; // 瀹氫箟鍏抽敭鏂囦欢绫诲瀷 - private static final String INNER_ZIP_PREFIX = "銆愬叕寮�銆慛C绋嬪簭鏂囦欢 "; private static final String JSON_EXT = ".json"; + private static final String TXT_EXT = ".txt"; + /** + * 闆嗘垚plm鏁版嵁锛屽帇缂╁寘锛堥噷闈㈡槸NC鍘嬬缉鍖呬笌JSON鏂囦欢锛� + * @param file + * @return + */ @PostMapping(value = "/syncPlmNcLogProgram", consumes = "multipart/form-data") @Transactional public String processNcPackage(MultipartFile file) { + Path workDir = null; try { if (file == null || file.isEmpty()) { log.error("鎺ユ敹鍒扮殑鍘嬬缉鏂囦欢涓虹┖"); @@ -47,7 +55,7 @@ // 鍒涘缓涓存椂鐩綍 String timestamp = String.valueOf(System.currentTimeMillis()); - Path workDir = Paths.get(filePath, timestamp); + workDir = Paths.get(filePath, "plm_" + timestamp); Files.createDirectories(workDir); // 淇濆瓨鍘熷鏂囦欢 @@ -92,7 +100,6 @@ for (Map.Entry<String, byte[]> entry : ncFiles.entrySet()) { String fileName = entry.getKey(); if (allNcFiles.containsKey(fileName)) { - // 淇敼鐐�5锛氳褰曢噸澶嶈鍛婏紙瀹為檯涓氬姟涓彲鑳介渶瑕佹洿涓ユ牸澶勭悊锛� log.warn("妫�娴嬪埌閲嶅NC鏂囦欢鍚�: {}", fileName); } allNcFiles.put(fileName, entry.getValue()); @@ -108,19 +115,183 @@ // 涓氬姟澶勭悊 return JSONObject.toJSONString( - dncWebService.setTree(plmPrograms) + dncWebService.setPlmTree(plmPrograms) ); } catch (Exception e) { log.error("澶勭悊NC鏂囦欢鍖呭け璐�", e); return MesResultModel.error("鏂囦欢澶勭悊澶辫触: " + e.getMessage()); + } finally { + // 娓呯悊涓存椂鏂囦欢 + if (workDir != null) { + deleteDirectory(workDir.toFile()); + } } + } + + /** + * 闆嗘垚3DE鏁版嵁,鍘嬬缉鍖咃紙鍗曚釜鍘嬬缉鍖呭寘鍚涓狽C鏂囦欢銆佷竴涓伐鑹烘暟鎹�.TXT锛� + * @param file 涓婁紶鐨勫帇缂╁寘鏂囦欢 + * @return 澶勭悊缁撴灉 + */ + @PostMapping(value = "/integration3DEData", consumes = "multipart/form-data") + @Transactional + public String integration3DEData(MultipartFile file) { + Path workDir = null; + try { + if (file == null || file.isEmpty()) { + log.error("鎺ユ敹鍒扮殑3DE鍘嬬缉鏂囦欢涓虹┖"); + return MesResultModel.error("鎺ユ敹鍒扮殑3DE鍘嬬缉鏂囦欢涓虹┖"); + } + // 鍒涘缓涓存椂宸ヤ綔鐩綍 + String timestamp = String.valueOf(System.currentTimeMillis()); + workDir = Paths.get(filePath, "3de_" + timestamp); + Files.createDirectories(workDir); + // 淇濆瓨鍘熷鏂囦欢 + Path originalPath = workDir.resolve( + Objects.requireNonNull(file.getOriginalFilename()) + ); + Files.copy(file.getInputStream(), originalPath, StandardCopyOption.REPLACE_EXISTING); + log.info("3DE鍘熷鏂囦欢宸蹭繚瀛�: {} (澶у皬: {} 瀛楄妭)", + originalPath, Files.size(originalPath)); + // 瑙e帇ZIP鍖呰幏鍙栨墍鏈夋枃浠� + Map<String, byte[]> packageContents = extractOuterZipContents(originalPath); + log.info("3DE鍘嬬缉鍖呰В鏋愭垚鍔燂紝鍖呭惈 {} 涓枃浠�", packageContents.size()); + + // 鏌ユ壘宸ヨ壓鏁版嵁TXT鏂囦欢 + byte[] processData = findFileByExtension(packageContents, TXT_EXT); + if (processData == null) { + return MesResultModel.error("鏈壘鍒板伐鑹烘暟鎹甌XT鏂囦欢"); + } + + // 瑙f瀽宸ヨ壓鏁版嵁TXT鏂囦欢 + ThirdDeProgramSource thirdDeProgramSource = parseProcessTxtFile(processData); + if (thirdDeProgramSource == null) { + return MesResultModel.error("瑙f瀽宸ヨ壓鏁版嵁TXT鏂囦欢澶辫触"); + } + + // 鎻愬彇鎵�鏈塏C鏂囦欢 + Map<String, byte[]> ncFiles = extractNcFiles(packageContents); + if (ncFiles.isEmpty()) { + return MesResultModel.error("鏈壘鍒癗C绋嬪簭鏂囦欢"); + } + log.info("鎵惧埌 {} 涓狽C绋嬪簭鏂囦欢", ncFiles.size()); + + // 璁剧疆NC鏂囦欢闆嗗悎鍜屾枃浠惰矾寰勪俊鎭� + thirdDeProgramSource.setNcFiles(ncFiles); + thirdDeProgramSource.setFilePath(workDir.toString()); + thirdDeProgramSource.setFileName(originalPath.getFileName().toString()); + + // 璋冪敤鏈嶅姟澶勭悊3DE鏁版嵁 + return JSONObject.toJSONString( + dncWebService.processThirdDEProgram(thirdDeProgramSource) + ); + } catch (Exception e) { + log.error("澶勭悊3DE鏁版嵁鍖呭け璐�", e); + return MesResultModel.error("3DE鏂囦欢澶勭悊澶辫触: " + e.getMessage()); + } finally { + // 娓呯悊涓存椂鏂囦欢 + if (workDir != null) { + deleteDirectory(workDir.toFile()); + } + } + } + + /** + * 瑙f瀽宸ヨ壓鏁版嵁TXT鏂囦欢 + * @param txtData TXT鏂囦欢鍐呭 + * @return ThirdDeProgramSource瀵硅薄 + */ + private ThirdDeProgramSource parseProcessTxtFile(byte[] txtData) { + try { + String content = new String(txtData, StandardCharsets.UTF_8); + log.info("宸ヨ壓鏁版嵁TXT鍐呭:\n{}", content); + + // 浣跨敤FastJSON瑙f瀽JSON鏍煎紡鐨凾XT + JSONObject json = JSONObject.parseObject(content); + if (json == null) { + log.error("瑙f瀽宸ヨ壓鏁版嵁澶辫触: 涓嶆槸鏈夋晥鐨凧SON鏍煎紡"); + return null; + } + + // 鍒涘缓瀵硅薄骞惰缃睘鎬� + ThirdDeProgramSource programSource = new ThirdDeProgramSource(); + programSource.setPartName(json.getString("part_name")); + programSource.setPartNo(json.getString("part_no")); + programSource.setRevisionNo(json.getString("revision_no")); + programSource.setSkgxId(json.getString("skgx_id")); + programSource.setSkgxName(json.getString("skgx_name")); + programSource.setNcJcid(json.getString("nc_jcid")); + programSource.setNcOs(json.getString("nc_os")); + programSource.setEquipmentId(json.getString("equipment_id")); + programSource.setNcFileName(json.getString("nc_file_name")); + programSource.setNcPlantNo(json.getString("nc_plant_no")); + programSource.setNcPlantName(json.getString("nc_plant_name")); + log.debug("瑙f瀽鐨勫伐鑹烘暟鎹�: partName={}, partNo={}, revisionNo={}, skgxId={}, skgxName={}, " + + "ncJcid={}, ncOs={}, equipmentId={}, ncFileName={}, ncPlantNo={}, ncPlantName={}", + programSource.getPartName(), programSource.getPartNo(), + programSource.getRevisionNo(), programSource.getSkgxId(), + programSource.getSkgxName(), programSource.getNcJcid(), + programSource.getNcOs(), programSource.getEquipmentId(), + programSource.getNcFileName(), programSource.getNcPlantNo(), + programSource.getNcPlantName()); + + return programSource; + } catch (Exception e) { + log.error("瑙f瀽宸ヨ壓鏁版嵁TXT鏂囦欢澶辫触", e); + return null; + } + } + + /** + * 瑙f瀽閿�煎鏍煎紡鐨凾XT鏂囦欢 + * @param content TXT鏂囦欢鍐呭 + * @return 閿�煎鏄犲皠 + */ + private Map<String, String> parseKeyValueTxt(String content) { + Map<String, String> result = new HashMap<>(); + String[] lines = content.split("\\r?\\n"); + + for (String line : lines) { + line = line.trim(); + if (line.isEmpty() || line.startsWith("#") || line.startsWith("//")) { + continue; // 璺宠繃绌鸿鍜屾敞閲� + } + + int separatorIndex = line.indexOf('='); + if (separatorIndex > 0) { + String key = line.substring(0, separatorIndex).trim(); + String value = line.substring(separatorIndex + 1).trim(); + result.put(key, value); + } + } + + return result; + } + + /** + * 浠庢枃浠跺唴瀹逛腑鎻愬彇NC鏂囦欢 + * @param packageContents 鍖呭唴瀹规槧灏� + * @return NC鏂囦欢鏄犲皠 + */ + private Map<String, byte[]> extractNcFiles(Map<String, byte[]> packageContents) { + Map<String, byte[]> ncFiles = new HashMap<>(); + + for (Map.Entry<String, byte[]> entry : packageContents.entrySet()) { + String fileName = entry.getKey(); + String fileExt = FilenameUtils.getExtension(fileName).toLowerCase(); + if (fileExt.equals("nc")) { + ncFiles.put(fileName, entry.getValue()); + log.debug("鎵惧埌NC鏂囦欢: {}", fileName); + } + } + + return ncFiles; } // 浼樺寲鍚庣殑瑙e帇鏂规硶锛堜娇鐢╟ommons-compress锛� private Map<String, byte[]> extractOuterZipContents(Path filePath) throws IOException { Map<String, byte[]> contents = new HashMap<>(); log.info("寮�濮嬭В鏋怹IP鏂囦欢: {}", filePath); - try { try (ZipFile zipFile = new ZipFile(filePath.toFile())) { log.info("ZIP鏂囦欢鏍煎紡妫�娴�: {}", zipFile.getEncoding()); @@ -131,17 +302,9 @@ if (entry.isDirectory()) continue; try (InputStream is = zipFile.getInputStream(entry)) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - byte[] buffer = new byte[8192]; - int len; - while ((len = is.read(buffer)) > 0) { - baos.write(buffer, 0, len); - } - - // 淇濈暀鍘熷鏂囦欢鍚� - String name = entry.getName(); - contents.put(name, baos.toByteArray()); - log.debug("鎻愬彇鏂囦欢: {} ({} 瀛楄妭)", name, baos.size()); + byte[] data = IOUtils.toByteArray(is); + contents.put(entry.getName(), data); + log.debug("鎻愬彇鏂囦欢: {} ({} 瀛楄妭)", entry.getName(), data.length); } } } @@ -175,14 +338,15 @@ private List<byte[]> findAllFilesByExtension(Map<String, byte[]> contents, String extension) { String extLower = extension.toLowerCase(); - return contents.entrySet().stream() - .filter(e -> { - String fileName = e.getKey(); - // 浣跨敤灏忓啓姣旇緝閬垮厤澶у皬鍐欐晱鎰熼棶棰� - return fileName.toLowerCase().endsWith(extLower); - }) - .map(Map.Entry::getValue) - .collect(Collectors.toList()); + List<byte[]> result = new ArrayList<>(); + + for (Map.Entry<String, byte[]> entry : contents.entrySet()) { + String fileName = entry.getKey().toLowerCase(); + if (fileName.endsWith(extLower)) { + result.add(entry.getValue()); + } + } + return result; } // 鍐呴儴ZIP瑙f瀽鏂规硶 @@ -213,57 +377,25 @@ * 鏍规嵁鎵╁睍鍚嶆煡鎵炬枃浠� */ private byte[] findFileByExtension(Map<String, byte[]> contents, String extension) { - return contents.entrySet().stream() - .filter(e -> e.getKey().toLowerCase().endsWith(extension)) - .map(Map.Entry::getValue) - .findFirst() - .orElse(null); + String extLower = extension.toLowerCase(); + for (Map.Entry<String, byte[]> entry : contents.entrySet()) { + if (entry.getKey().toLowerCase().endsWith(extLower)) { + return entry.getValue(); + } + } + return null; } /** * 鑾峰彇鏂囦欢鍚嶏紙鏍规嵁鎵╁睍鍚嶏級 */ private String getFileNameByExtension(Map<String, byte[]> contents) { - return contents.keySet().stream() - .filter(k -> k.toLowerCase().endsWith(DncWebController.JSON_EXT)) - .findFirst() - .orElse("Unknown"); - } - - /** - * 鏌ユ壘涓庢寚瀹氬墠缂�鍖归厤鐨勬枃浠跺唴瀹� - * @param contents 鏂囦欢鍐呭鏄犲皠锛堟枃浠跺悕 -> 鏂囦欢鍐呭锛� - * @param prefix 瑕佸尮閰嶇殑鏂囦欢鍚嶅墠缂� - * @return 鍖归厤鐨勭涓�涓枃浠跺唴瀹癸紝鏈壘鍒拌繑鍥� null - */ - private byte[] findFileByPrefix(Map<String, byte[]> contents, String prefix) { - if (prefix == null || prefix.isEmpty()) { - log.warn("鏂囦欢鍓嶇紑涓嶈兘涓虹┖"); - return null; + for (String key : contents.keySet()) { + if (key.toLowerCase().endsWith(JSON_EXT)) { + return key; + } } - - return contents.entrySet().stream() - .filter(entry -> { - String fileName = entry.getKey(); - - // 璺緞瑙勮寖鍖栧鐞� - String normalizedKey = fileName.replace('\\', '/'); - - // 鑾峰彇绾枃浠跺悕 - int lastSlash = normalizedKey.lastIndexOf('/'); - String pureFileName = (lastSlash >= 0) ? - normalizedKey.substring(lastSlash + 1) : - normalizedKey; - - // 璋冭瘯鏃ュ織 - log.debug("妫�鏌ユ枃浠�: [鍘熷: {}], [绾枃浠跺悕: {}], 鍓嶇紑: {}", - fileName, pureFileName, prefix); - - return pureFileName.contains("NC"); - }) - .map(Map.Entry::getValue) - .findFirst() - .orElse(null); + return "Unknown"; } /** @@ -274,4 +406,26 @@ return JSONObject.parseObject(json, PlmProgramSource.class); } + /** + * 閫掑綊鍒犻櫎鐩綍 + */ + private void deleteDirectory(File directory) { + if (!directory.exists()) return; + + File[] files = directory.listFiles(); + if (files != null) { + for (File file : files) { + if (file.isDirectory()) { + deleteDirectory(file); + } else { + if (!file.delete()) { + log.warn("鏃犳硶鍒犻櫎鏂囦欢: {}", file.getAbsolutePath()); + } + } + } + } + if (!directory.delete()) { + log.warn("鏃犳硶鍒犻櫎鐩綍: {}", directory.getAbsolutePath()); + } + } } -- Gitblit v1.9.3