zhangherong
2025-06-25 23855599412c4d61b38d78f0f3abd3430a48b5b1
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. è§£å¯† (当前已注释)
            // 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());
    }
}