From 8a74e9c0f3f9fbf5709201719431d0a2b7cb8794 Mon Sep 17 00:00:00 2001
From: 潘志宝 <979469083@qq.com>
Date: 星期五, 22 十一月 2024 10:06:07 +0800
Subject: [PATCH] 计划数据图表

---
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java |  235 +++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 196 insertions(+), 39 deletions(-)

diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java
index a33aa2e..3db6b2b 100644
--- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java
+++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java
@@ -8,20 +8,23 @@
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.iailab.framework.common.page.PageData;
+import com.iailab.framework.common.pojo.CommonResult;
 import com.iailab.framework.common.service.impl.BaseServiceImpl;
 import com.iailab.framework.common.util.date.DateUtils;
 import com.iailab.framework.common.util.object.ConvertUtils;
 import com.iailab.framework.security.core.util.SecurityFrameworkUtils;
+import com.iailab.framework.tenant.core.context.TenantContextHolder;
 import com.iailab.module.infra.api.config.ConfigApi;
 import com.iailab.module.model.mpk.common.MdkConstant;
+import com.iailab.module.model.mpk.common.utils.DllUtils;
 import com.iailab.module.model.mpk.common.utils.GenUtils;
 import com.iailab.module.model.mpk.dao.MpkFileDao;
 import com.iailab.module.model.mpk.dto.GeneratorCodeHistoryDTO;
 import com.iailab.module.model.mpk.dto.ModelMethodDTO;
 import com.iailab.module.model.mpk.dto.MpkFileDTO;
-import com.iailab.module.model.mpk.dto.ProjectPackageHistoryDTO;
 import com.iailab.module.model.mpk.entity.GeneratorCodeHistoryEntity;
 import com.iailab.module.model.mpk.entity.MpkFileEntity;
+import com.iailab.module.model.mpk.entity.ProjectPackageHistoryEntity;
 import com.iailab.module.model.mpk.entity.ProjectPackageHistoryModelEntity;
 import com.iailab.module.model.mpk.service.*;
 import lombok.extern.slf4j.Slf4j;
@@ -36,6 +39,7 @@
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.*;
+import java.net.URLClassLoader;
 import java.nio.file.Files;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -69,11 +73,6 @@
 
     @Value("${mpk.bak-resources}")
     private String mpkResources;
-
-    /*@PostConstruct
-    public void init() {
-        mpkBakFilePath = configApi.getConfigValueByKey("mpkBakFilePath").getCheckedData();
-    }*/
 
     @Override
     public PageData<MpkFileDTO> page(Map<String, Object> params) {
@@ -130,6 +129,12 @@
         entity.setCreateDate(new Date());
         insert(entity);
         modelMethodService.insertList(dto.getModelMethods(), entity.getId());
+
+        // 替换环境变量MDK_PKGS下的py文件
+//        String mdkPkgs = System.getenv("MDK_PKGS");
+//        String pyFilePath = mdkPkgs + File.separator + entity.getPyModule().replace(".", File.separator) + File.separator + entity.getPyName() + ".pyd";
+//        FileUtil.mkParentDirs(pyFilePath);
+//        FileUtil.copy(entity.getFilePath(), pyFilePath, true);
     }
 
     @Override
@@ -141,6 +146,12 @@
         updateById(entity);
         modelMethodService.deleteModelMethod(entity.getId());
         modelMethodService.insertList(dto.getModelMethods(), entity.getId());
+
+        // 替换环境变量MDK_PKGS下的py文件
+//        String mdkPkgs = System.getenv("MDK_PKGS");
+//        String pyFilePath = mdkPkgs + File.separator + entity.getPyModule().replace(".", File.separator) + File.separator + entity.getPyName() + ".pyd";
+//        FileUtil.mkParentDirs(pyFilePath);
+//        FileUtil.copy(entity.getFilePath(), pyFilePath, true);
     }
 
     @Override
@@ -175,6 +186,10 @@
 
     @Override
     public byte[] generatorCode(String id, String remark, String zipFileName) {
+        Long tenantId = TenantContextHolder.getTenantId();
+        // 备份文件 租户隔离
+        String mpkTenantBakFilePath = mpkBakFilePath + File.separator + tenantId;
+
         MpkFileDTO entity = baseDao.get(id);
         //生成代码
         //设置velocity资源加载器
@@ -231,6 +246,11 @@
         File jniHFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.h");
         GenUtils.drawTemplate("Jni.h.vm", dataContext, jniHFile);
 
+        // 添加python源文件
+        String pyFilePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.ALGS + File.separator + entity.getPyModule().replace(".", File.separator) + File.separator + entity.getPyName() + ".pyd";
+        FileUtil.mkParentDirs(pyFilePath);
+        FileUtil.copy(entity.getFilePath(), pyFilePath, true);
+
         // 资源文件
         FileUtil.copy(mpkResources + File.separator + "libs", dirPath.getAbsolutePath(), true);
 
@@ -238,7 +258,7 @@
         String dllSavePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.LIBS + File.separator + "IAIL.MDK.Mid.Jni.dll";
         createDllFile(dirPath.getAbsolutePath(),cppFilePaths,dllSavePath);
         //备份dll文件,用于后续运行
-        String dllBakPath = mpkBakFilePath + File.separator + MdkConstant.DLL + File.separator + entity.getPyName() + ".dll";
+        String dllBakPath = mpkTenantBakFilePath + File.separator + MdkConstant.DLL + File.separator + entity.getPyName() + ".dll";
         FileUtil.mkParentDirs(dllBakPath);
         FileUtil.copy(dllSavePath, dllBakPath, true);
 
@@ -258,11 +278,11 @@
         // 打jar包
         String jarSavePath = pkgJar(dirPath.getAbsolutePath());
         //备份jar文件,用于后续运行
-        String jarBakPath = mpkBakFilePath + File.separator + MdkConstant.JAR + File.separator + entity.getPyName() + ".jar";
+        String jarBakPath = mpkTenantBakFilePath + File.separator + MdkConstant.JAR + File.separator + entity.getPyName() + ".jar";
         FileUtil.mkParentDirs(dllBakPath);
         FileUtil.copy(jarSavePath, jarBakPath, true);
         // 打zip包
-        String zipPath = mpkBakFilePath + File.separator + zipFileName;
+        String zipPath = mpkTenantBakFilePath + File.separator + zipFileName;
         ZipUtil.zip(dirPath.getAbsolutePath(), zipPath);
         byte[] bytes = FileUtil.readBytes(zipPath);
 
@@ -288,6 +308,10 @@
     @Override
     @DSTransactional(rollbackFor = Exception.class)
     public byte[] packageModel(List<String> ids, String projectId, String projectName, String zipFileName, String logs, String version) throws IOException, InterruptedException {
+        Long tenantId = TenantContextHolder.getTenantId();
+        // 备份文件 租户隔离
+        String mpkTenantBakFilePath = mpkBakFilePath + File.separator + tenantId;
+
         List<MpkFileDTO> entities = baseDao.selectByIds(ids);
 
         //模板数据
@@ -368,6 +392,16 @@
         //生成dll文件
         String dllSavePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.LIBS + File.separator + "IAIL.MDK.Mid.Jni.dll";
         createDllFile(dirPath.getAbsolutePath(),cppFilePaths,dllSavePath);
+        // 打包历史id
+        String historyId = UUID.randomUUID().toString();
+        //备份dll,发布时使用
+        File dllFile = new File(dllSavePath);
+        if (dllFile.exists()) {
+            File dllBakFile = new File(mpkBakFilePath + File.separator + MdkConstant.PROJECT_UNPUBLISH + File.separator + projectId + MdkConstant.SPLIT + historyId + ".dll");
+            FileUtil.copy(dllFile, dllBakFile, true);
+        }else {
+            log.error("dll文件不存在,无法备份。" + dllSavePath);
+        }
 
         //utils + env java文件
         File utilsJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "utils" + File.separator + "AlgsUtils.java");
@@ -383,26 +417,34 @@
         // 删除java源文件
         deleteJavaFile(javaFilePaths);
         // 打jar包
-        pkgJar(dirPath.getAbsolutePath());
+        String jarSavePath = pkgJar(dirPath.getAbsolutePath());
+        //备份jar包,发布时使用
+        File jarFile = new File(jarSavePath);
+        if (jarFile.exists()) {
+            File jarBakFile = new File(mpkBakFilePath + File.separator + MdkConstant.PROJECT_UNPUBLISH + File.separator + projectId + MdkConstant.SPLIT + historyId + ".jar");
+            FileUtil.copy(jarFile, jarBakFile, true);
+        }else {
+            log.error("jar文件不存在,无法备份。" + jarSavePath);
+        }
+
         // 本次更新日志
-        ProjectPackageHistoryDTO dto = new ProjectPackageHistoryDTO();
-        String historyId = UUID.randomUUID().toString();
-        dto.setId(historyId);
-        dto.setProjectId(projectId);
-        dto.setFileName(zipFileName);
-        dto.setLog(logs);
-        dto.setVersion(version);
-        dto.setModelNames(entities.stream().map(MpkFileDTO::getPyName).collect(Collectors.joining(",")));
-        dto.setCreateTime(new Date());
+        ProjectPackageHistoryEntity projectPackageHistoryEntity = new ProjectPackageHistoryEntity();
+        projectPackageHistoryEntity.setId(historyId);
+        projectPackageHistoryEntity.setProjectId(projectId);
+        projectPackageHistoryEntity.setFileName(zipFileName);
+        projectPackageHistoryEntity.setLog(logs);
+        projectPackageHistoryEntity.setVersion(version);
+        projectPackageHistoryEntity.setModelNames(entities.stream().map(MpkFileDTO::getPyName).collect(Collectors.joining(",")));
+        projectPackageHistoryEntity.setCreateTime(new Date());
         // 生成更新日志
-        createLog(projectId, projectName, dirPath.getAbsolutePath(), dto, version);
+        createLog(projectId, projectName, dirPath.getAbsolutePath(), projectPackageHistoryEntity, version);
         // 打zip包
-        String zipPath = mpkBakFilePath + File.separator + zipFileName;
+        String zipPath = mpkTenantBakFilePath + File.separator + zipFileName;
         ZipUtil.zip(dirPath.getAbsolutePath(), zipPath);
         byte[] bytes = FileUtil.readBytes(zipPath);
         // 记录打包日志
-        dto.setFilePath(zipPath);
-        projectPackageHistoryService.save(dto);
+        projectPackageHistoryEntity.setFilePath(zipPath);
+        projectPackageHistoryService.insert(projectPackageHistoryEntity);
         // 插入打包历史-模型关联表
         List<ProjectPackageHistoryModelEntity> historyModelList = new ArrayList<>(entities.size());
         entities.forEach(e -> {
@@ -421,7 +463,7 @@
             }
             historyModelList.add(entity);
         });
-        projectPackageHistoryModelService.insertBatch(historyModelList);
+        projectPackageHistoryModelService.insertList(historyModelList);
         // 删除临时文件
         FileUtils.deleteDirectory(dirPath);
         return bytes;
@@ -451,35 +493,150 @@
 
     @Override
     public Map<String, String> savePyFile(MultipartFile file) throws IOException {
-        File dir = new File(mpkBakFilePath);
-        if (!dir.exists()) {
-            dir.mkdirs();
+        Long tenantId = TenantContextHolder.getTenantId();
+        // 备份文件 租户隔离
+        String mpkTenantBakFilePath = mpkBakFilePath + File.separator + tenantId;
+        File bakDir = new File(mpkTenantBakFilePath);
+        if (!bakDir.exists()) {
+            bakDir.mkdirs();
+        }
+        //临时文件夹
+        File tempDir = null;
+        try {
+            tempDir = Files.createTempDirectory("pyFileTmp").toFile();
+            log.info("生成临时文件夹," + tempDir.getAbsolutePath());
+        } catch (IOException e) {
+            throw new RuntimeException("创建临时文件夹异常",e);
         }
         String fileName = file.getOriginalFilename();
-        String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
-        File saveFile = new File(dir.getAbsolutePath() + File.separator + UUID.randomUUID() + fileSuffix);
+        File pydBakFile = new File(bakDir.getAbsolutePath() + File.separator + UUID.randomUUID() + ".pyd");
 
-        saveFile.createNewFile();
-        // 保存
-        file.transferTo(saveFile);
+        String pyName = fileName.substring(0, fileName.lastIndexOf("."));
+        try {
+            // py文件存入临时文件夹
+            File saveFile = new File(tempDir.getAbsolutePath() + File.separator + fileName);
+            saveFile.createNewFile();
+            file.transferTo(saveFile);
+
+            // 临时文件夹中生成pyd文件
+            // 生成setup.py文件
+            createSetUpFile(tempDir.getAbsolutePath(),fileName,pyName);
+            // 执行cmd命令编译pyd文件
+            createPydFile(tempDir.getAbsolutePath());
+
+            // 临时文件夹中pyd文件移到dir下,修改为uuid.pyd
+            File pydFile = new File(tempDir.getAbsolutePath() + File.separator + pyName + MdkConstant.PYD_SUFFIX);
+            if (!pydFile.exists()) {
+                throw new RuntimeException("编译pyd文件失败!");
+            }
+            log.info("备份pyd文件," + pydBakFile.getAbsolutePath());
+            FileUtil.copy(pydFile,pydBakFile,true);
+        } catch (Exception e) {
+            throw new RuntimeException("编译pyd文件异常");
+        } finally {
+            if (tempDir.exists()) {
+                tempDir.delete();
+            }
+        }
 
         Map<String, String> result = new HashMap<>(2);
-        result.put("filePath", saveFile.getAbsolutePath());
-        result.put("fileName", fileName);
+        result.put("filePath", pydBakFile.getAbsolutePath());
+        result.put("fileName", pyName);
 
         return result;
     }
 
-    private void createLog(String projectId, String projectName, String dirPath, ProjectPackageHistoryDTO dto, String version) throws IOException {
+    private void createPydFile(String dir) {
+        try {
+            String command = "python setup.py build_ext --inplace";
+            log.info("执行cmd命令编译pyd:" + command);
+            ProcessBuilder builder = new ProcessBuilder("cmd", "/c", command);
+            builder.directory(new File(dir));  // 设置工作目录
+            builder.redirectErrorStream(true);  // 将错误输出和标准输出合并
+            Process process = builder.start();
+            // 获取命令输出结果
+            InputStream inputStream = process.getInputStream();
+            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "GBK")); // 设置编码为GBK
+            String line;
+            while ((line = reader.readLine()) != null) {
+                log.info(line);
+            }
+            // 等待命令执行完成
+            process.waitFor();
+        } catch (Exception e) {
+            throw new RuntimeException("执行cmd命令生成dll异常", e);
+        }
+    }
+
+    private void createSetUpFile(String dir, String pyFileName, String pyName) {
+        // 生成setup.py文件
+        HashMap<String,Object> map = new HashMap<>();
+        map.put("fileName",pyFileName);
+        map.put("pyName",pyName);
+        GenUtils.drawTemplate("setup.py.vm",map,new File(dir + File.separator + MdkConstant.SETUP_PY));
+    }
+
+    @Override
+    public CommonResult<String> publish(Map<String, Object> params) {
+        String historyId = (String) params.get("historyId");
+        String projectId = (String) params.get("projectId");
+
+        // 判断新的dll和jar文件是否存在
+        String jarFileBakPath = mpkBakFilePath + File.separator + MdkConstant.PROJECT_UNPUBLISH + File.separator + projectId + MdkConstant.SPLIT + historyId + ".jar";
+        String dllFileBakPath = mpkBakFilePath + File.separator + MdkConstant.PROJECT_UNPUBLISH + File.separator + projectId + MdkConstant.SPLIT + historyId + ".dll";
+        if (!FileUtil.exist(jarFileBakPath)) {
+            throw new RuntimeException("jar文件不存在," + jarFileBakPath);
+        }
+        if (!FileUtil.exist(dllFileBakPath)) {
+            throw new RuntimeException("dll文件不存在" + dllFileBakPath);
+        }
+
+        try {
+            // 卸载之前的dll和jar
+            DllUtils.removeClassLoaderCache(projectId);
+            // 删除之前的dll和jar备份文件
+            DllUtils.removeOldFile(mpkBakFilePath + File.separator + MdkConstant.PROJECT_PUBLISH,projectId);
+        } catch (Exception e) {
+            throw new RuntimeException("卸载之前的dll和jar异常",e);
+        }
+
+        // 备份新的dll和jar
+        String jarFilePath = mpkBakFilePath + File.separator + MdkConstant.PROJECT_PUBLISH + File.separator + projectId + MdkConstant.SPLIT + historyId + ".jar";
+        String dllFilePath = mpkBakFilePath + File.separator + MdkConstant.PROJECT_PUBLISH + File.separator + projectId + MdkConstant.SPLIT + historyId + ".dll";
+        FileUtil.copy(jarFileBakPath,jarFilePath,true);
+        FileUtil.copy(dllFileBakPath,dllFilePath,true);
+
+        URLClassLoader urlClassLoader = null;
+        try {
+            // 加载新的jar
+            urlClassLoader = DllUtils.loadJar(jarFilePath);
+        } catch (Exception e) {
+            throw new RuntimeException("加载新的jar异常",e);
+        }
+
+        try {
+            // 加载新的dll
+            DllUtils.loadDll(urlClassLoader.loadClass("iail.mdk.model.common.Environment"),dllFilePath);
+        } catch (Exception e) {
+            DllUtils.unloadJar(urlClassLoader);
+            throw new RuntimeException("加载新的dll异常",e);
+        }
+        // 都加载成功后加入缓存
+        DllUtils.addClassLoaderCache(projectId,urlClassLoader);
+
+        return CommonResult.success();
+    }
+
+    private void createLog(String projectId, String projectName, String dirPath, ProjectPackageHistoryEntity projectPackageHistoryEntity, String version) {
         Map<String, Object> map = new HashMap<>();
         map.put("projectId", projectId);
-        List<ProjectPackageHistoryDTO> list = projectPackageHistoryService.list(map);
-        list.add(dto);
+        List<ProjectPackageHistoryEntity> list = projectPackageHistoryService.list(map);
+        list.add(projectPackageHistoryEntity);
         // 按照日期分组再排序
-        HashMap<String, List<ProjectPackageHistoryDTO>> dataMap = list.stream().collect(
+        HashMap<String, List<ProjectPackageHistoryEntity>> dataMap = list.stream().collect(
                 Collectors.groupingBy(e -> DateUtils.format(e.getCreateTime(), DateUtils.DATE_PATTERN_POINT),
                         LinkedHashMap::new,
-                        Collectors.collectingAndThen(Collectors.toList(), e -> e.stream().sorted(Comparator.comparing(ProjectPackageHistoryDTO::getCreateTime)).collect(Collectors.toList()))));
+                        Collectors.collectingAndThen(Collectors.toList(), e -> e.stream().sorted(Comparator.comparing(ProjectPackageHistoryEntity::getCreateTime)).collect(Collectors.toList()))));
 
         Map<String, Object> data = new HashMap<>();
         data.put("dataMap", dataMap);

--
Gitblit v1.9.3