¶Ô±ÈÐÂÎļþ |
| | |
| | | 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. è§£å¯ (å½å已注é) |
| | | // byte[] compressed = securityService.decrypt(encrypted); |
| | | |
| | | // 3. è§£å缩 |
| | | // byte[] jsonBytes = CompressionUtils.gzipDecompress(encrypted); |
| | | String json = new String(encrypted, StandardCharsets.UTF_8); |
| | | logger.debug("è§£åç¼©å®æ, 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("æ æçIDæ ¼å¼: " + 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æä»¶ä¸jsonæä»¶ |
| | | 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()); |
| | | } |
| | | } |