From 587b438f50abb725f3dee63eaf6b586c09085f54 Mon Sep 17 00:00:00 2001
From: dengzedong <dengzedong@email>
Date: 星期四, 26 九月 2024 09:59:14 +0800
Subject: [PATCH] bug + 模型方法参数排序

---
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java |  243 ++++++++++++++++++++---------------------------
 1 files changed, 104 insertions(+), 139 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 6ef8959..1bd71b9 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
@@ -4,6 +4,7 @@
 import cn.hutool.core.util.RuntimeUtil;
 import cn.hutool.core.util.ZipUtil;
 import com.alibaba.fastjson.JSON;
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.iailab.framework.common.page.PageData;
@@ -27,15 +28,10 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
 import org.springframework.web.multipart.MultipartFile;
 
-import javax.annotation.PostConstruct;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.IOException;
-import java.io.OutputStream;
+import java.io.*;
 import java.nio.file.Files;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -52,16 +48,13 @@
 
     @Autowired
     private GeneratorCodeHistoryService generatorCodeHistoryService;
-    @Autowired
-    private ProjectModelService projectModelService;
+
     @Autowired
     private ProjectPackageHistoryService projectPackageHistoryService;
+
     @Autowired
     private ModelMethodService modelMethodService;
-    @Autowired
-    private MethodSettingService methodSettingService;
-    @Autowired
-    private SettingSelectService settingSelectService;
+
     @Autowired
     private ProjectPackageHistoryModelService projectPackageHistoryModelService;
 
@@ -93,15 +86,23 @@
         return ConvertUtils.sourceToTarget(entityList, MpkFileDTO.class);
     }
 
-    private QueryWrapper<MpkFileEntity> getWrapper(Map<String, Object> params){
+    private QueryWrapper<MpkFileEntity> getWrapper(Map<String, Object> params) {
         String pyName = (String) params.get("pyName");
         String pyType = (String) params.get("pyType");
         String remark = (String) params.get("remark");
+        String label = (String) params.get("label");
 
         QueryWrapper<MpkFileEntity> wrapper = new QueryWrapper<>();
         wrapper.like(StringUtils.isNotBlank(pyName), "py_name", pyName)
                 .eq(StringUtils.isNotBlank(pyType), "py_type", pyType)
                 .like(StringUtils.isNotBlank(remark), "remark", remark);
+
+        if (StringUtils.isNotBlank(label)) {
+            wrapper.and(w -> {
+                w.eq(StringUtils.isNotBlank(label),"menu_name", label)
+                        .or().eq(StringUtils.isNotBlank(label),"group_name", label);
+            });
+        }
         return wrapper;
     }
 
@@ -113,86 +114,29 @@
     }
 
     @Override
-    @Transactional(rollbackFor = Exception.class)
+    @DSTransactional(rollbackFor = Exception.class)
     public void save(MpkFileDTO dto) {
         MpkFileEntity entity = ConvertUtils.sourceToTarget(dto, MpkFileEntity.class);
-        String mpkId = UUID.randomUUID().toString();
-        entity.setId(mpkId);
+        entity.setId(UUID.randomUUID().toString());
         entity.setCreator(SecurityFrameworkUtils.getLoginUserId());
         entity.setCreateDate(new Date());
         insert(entity);
-
-        // 添加模型方法
-        insertModelMethod(dto.getModelMethods(),mpkId);
+        modelMethodService.insertList(dto.getModelMethods(), entity.getId());
     }
 
     @Override
-    @Transactional(rollbackFor = Exception.class)
+    @DSTransactional(rollbackFor = Exception.class)
     public void update(MpkFileDTO dto) {
         MpkFileEntity entity = ConvertUtils.sourceToTarget(dto, MpkFileEntity.class);
         entity.setUpdater(SecurityFrameworkUtils.getLoginUserId());
         entity.setUpdateDate(new Date());
         updateById(entity);
-
-        String mpkId = dto.getId();
-        // 删除模型方法 会级联删除setting和select
-        deleteModelMethod(mpkId);
-
-        // 添加模型方法
-        insertModelMethod(dto.getModelMethods(),mpkId);
-    }
-
-    private void insertModelMethod(List<ModelMethodDTO> modelMethods, String mpkId) {
-        List<MethodSettingDTO> methodSettingList = new ArrayList<>();
-        if (!CollectionUtils.isEmpty(modelMethods)) {
-            modelMethods.forEach(e -> {
-                String methodId = UUID.randomUUID().toString();
-                e.setId(methodId);
-                e.setMpkFileId(mpkId);
-
-                e.getMethodSettings().forEach(s -> {
-                    s.setId(UUID.randomUUID().toString());
-                    s.setMethodId(methodId);
-                    methodSettingList.add(s);
-                });
-
-            });
-            modelMethodService.insertBatch(ConvertUtils.sourceToTarget(modelMethods, ModelMethodEntity.class));
-
-            //添加setting
-            insertMethodSetting(methodSettingList);
-        }
-    }
-
-    private void insertMethodSetting(List<MethodSettingDTO> methodSettings) {
-        List<SettingSelectEntity> settingSelectList = new ArrayList<>();
-        if (!CollectionUtils.isEmpty(methodSettings)) {
-            methodSettings.forEach(e -> {
-                String settingId = UUID.randomUUID().toString();
-                e.setId(settingId);
-
-                e.getSettingSelects().forEach(s -> {
-                    s.setId(UUID.randomUUID().toString());
-                    s.setSettingId(settingId);
-                    settingSelectList.add(ConvertUtils.sourceToTarget(s,SettingSelectEntity.class));
-                });
-
-            });
-            methodSettingService.insertBatch(ConvertUtils.sourceToTarget(methodSettings, MethodSettingEntity.class));
-
-            //添加select
-            settingSelectService.insertBatch(settingSelectList);
-        }
-    }
-
-    private void deleteModelMethod(String mpkId) {
-        Map<String,Object> map = new HashMap<>();
-        map.put("mpkFileId", mpkId);
-        modelMethodService.deleteByMap(map);
+        modelMethodService.deleteModelMethod(entity.getId());
+        modelMethodService.insertList(dto.getModelMethods(), entity.getId());
     }
 
     @Override
-    @Transactional(rollbackFor = Exception.class)
+    @DSTransactional(rollbackFor = Exception.class)
     public void delete(String id) {
 
         //删除源文件
@@ -206,8 +150,8 @@
         }
 
         //删除备份文件
-        Map<String,Object> map1 = new HashMap<>();
-        map1.put("mdkId",id);
+        Map<String, Object> map1 = new HashMap<>();
+        map1.put("mdkId", id);
         List<GeneratorCodeHistoryDTO> list = generatorCodeHistoryService.list(map1);
         list.forEach(e -> {
             File file = new File(e.getFilePath());
@@ -219,18 +163,6 @@
 
         //删除 会级联删除掉关联表
         deleteById(id);
-
-        //删除生成历史
-//        generatorCodeHistoryService.deleteByMap(map1);
-
-        //删除关联项目
-//        Map<String,Object> map = new HashMap<>();
-//        map.put("modelId",id);
-//        projectModelService.deleteByMap(map);
-
-        //删除模型方法
-//        deleteModelMethod(id);
-
     }
 
     @Override
@@ -278,13 +210,13 @@
     }
 
     @Override
-    @Transactional(rollbackFor = Exception.class)
-    public byte[] packageModel(List<String> ids,String projectId,String projectName,String zipFileName,String log,String version) throws IOException, InterruptedException {
+    @DSTransactional(rollbackFor = Exception.class)
+    public byte[] packageModel(List<String> ids, String projectId, String projectName, String zipFileName, String logs, String version) throws IOException, InterruptedException {
         List<MpkFileDTO> entities = baseDao.selectByIds(ids);
 
         //模板数据
         Map<String, Object> map = new HashMap<>();
-        map.put("entities",entities);
+        map.put("entities", entities);
         VelocityContext context = new VelocityContext(map);
 
         //临时文件夹
@@ -299,64 +231,73 @@
         Velocity.init(prop);
 
         //生成menu.xml文件
-        Map<String, Map<String, List<MpkFileDTO>>> collect = entities.stream().collect(Collectors.groupingBy(MpkFileDTO::getMenuName, Collectors.groupingBy(e -> StringUtils.isNotBlank(e.getGroupName()) ? e.getGroupName() : "default_group")));
+        LinkedHashMap<String, LinkedHashMap<String, List<MpkFileDTO>>> collect = entities.stream().collect(Collectors.groupingBy(MpkFileDTO::getMenuName, LinkedHashMap::new, Collectors.groupingBy(e -> StringUtils.isNotBlank(e.getGroupName()) ? e.getGroupName() : "default_group",LinkedHashMap::new,Collectors.toList())));
         Map<String, Object> map1 = new HashMap<>();
-        map1.put("collects",collect);
+        map1.put("collects", collect);
         File xmlFile = new File(dirPath.getAbsolutePath() + File.separator + "menu.xml");
-        GenUtils.drawTemplate("menu.xml.vm",new VelocityContext(map1),xmlFile);
+        GenUtils.drawTemplate("menu.xml.vm", new VelocityContext(map1), xmlFile);
 
-        //生成cpp文件
-        File cppFile = new File(dirPath.getAbsolutePath() + File.separator + UUID.randomUUID() + ".cpp");
-        GenUtils.drawTemplate("pkg.cpp.vm",context,cppFile);
+        // C++源文件
+        FileUtil.copy("bak/C++", dirPath.getAbsolutePath(), true);
 
-        //生成Jni.cpp文件
-        File jniCppFile = new File(dirPath.getAbsolutePath() + File.separator + UUID.randomUUID() + "Jni.cpp");
-        GenUtils.drawTemplate("pkg.Jni.cpp.vm",context,jniCppFile);
-
-        //生成dll文件
-        String dllSavePath = dirPath.getAbsolutePath() + File.separator + "IAIL.MDK.Mid.Windows.dll";
-        String cppFilePath = cppFile.getAbsolutePath();
-        String jniCppFilePath = jniCppFile.getAbsolutePath();
-//        String command = "cmd.exe /c cl -o " + dllSavePath + " /LD " + jniCppFilePath + " " + cppFilePath;
-        String command = "cmd.exe /c cl -o " + dllSavePath + " /LD D:\\work\\mdk\\code\\makeDll\\src\\main\\java\\org\\example\\MakeDll.c D:\\work\\mdk\\code\\makeDll\\src\\main\\java\\org\\example\\MakeDll2.c";
-        Process process = RuntimeUtil.exec(command);
-        // 等待命令执行完成
-        process.waitFor();
         List<String> javaFilePaths = new ArrayList<>();
+        List<String> cppFilePaths = new ArrayList<>();
 
         //生成java文件
         for (MpkFileDTO entity : entities) {
             //封装模板数据
             Map<String, Object> data = new HashMap<>();
-            data.put("pkgName",entity.getPkgName());
-            data.put("modelMethods",entity.getModelMethods());
-            data.put("pyName",entity.getPyName());
-            data.put("pyModule",entity.getPyModule());
+            data.put("pkgName", entity.getPkgName());
+            data.put("modelMethods", entity.getModelMethods());
+            data.put("pyName", entity.getPyName());
+            data.put("pyModule", entity.getPyModule());
             VelocityContext dataContext = new VelocityContext(data);
             //生成java文件
             File javaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + entity.getPyName() + ".java");
-            GenUtils.drawTemplate("abstract.java.vm",dataContext,javaFile);
+            GenUtils.drawTemplate("abstract.java.vm", dataContext, javaFile);
             javaFilePaths.add(javaFile.getAbsolutePath());
 
             //生成Impl.java文件
             File implJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + MdkConstant.IMPL + File.separator + entity.getPyName() + "Impl.java");
-            GenUtils.drawTemplate("impl.java.vm",dataContext,implJavaFile);
+            GenUtils.drawTemplate("impl.java.vm", dataContext, implJavaFile);
             javaFilePaths.add(implJavaFile.getAbsolutePath());
 
+            //生成.cpp文件
+            File cppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".cpp");
+            GenUtils.drawTemplate("cpp.vm", dataContext, cppFile);
+            cppFilePaths.add(cppFile.getAbsolutePath());
+
+            //生成Jni.cpp文件
+            File jniCppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.cpp");
+            GenUtils.drawTemplate("Jni.cpp.vm", dataContext, jniCppFile);
+            cppFilePaths.add(jniCppFile.getAbsolutePath());
+
+            //生成.h文件
+            File hFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".h");
+            GenUtils.drawTemplate("h.vm", dataContext, hFile);
+
+            //生成Jni.h文件
+            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 + "py" + File.separator +  entity.getPyName() + ".pyd";
+            String pyFilePath = dirPath.getAbsolutePath() + File.separator + "py" + File.separator + entity.getPyName() + ".pyd";
             FileUtil.mkParentDirs(pyFilePath);
-            FileUtil.copy(entity.getFilePath(),pyFilePath,true);
+            FileUtil.copy(entity.getFilePath(), pyFilePath, true);
         }
+
+        //生成dll文件
+        String dllSavePath = dirPath.getAbsolutePath() + File.separator + "IAIL.MDK.Mid.Windows.dll";
+        createDllFile(dirPath.getAbsolutePath(),cppFilePaths,dllSavePath);
 
         //utils + env java文件
         File utilsJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + "utils" + File.separator + "AlgsUtils.java");
         FileUtil.mkParentDirs(utilsJavaFile);
-        FileUtil.copy("bak/AlgsUtils.java",utilsJavaFile.getAbsolutePath(),true);
+        FileUtil.copy("bak/IAILMDK/utils/AlgsUtils.java", utilsJavaFile.getAbsolutePath(), true);
         javaFilePaths.add(utilsJavaFile.getAbsolutePath());
         File envJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + "common" + File.separator + "Environment.java");
         FileUtil.mkParentDirs(envJavaFile);
-        FileUtil.copy("bak/Environment.java",envJavaFile.getAbsolutePath(),true);
+        FileUtil.copy("bak/IAILMDK/common/Environment.java", envJavaFile.getAbsolutePath(), true);
         javaFilePaths.add(envJavaFile.getAbsolutePath());
         // 生成class文件
         createClassFile(javaFilePaths);
@@ -370,15 +311,15 @@
         dto.setId(historyId);
         dto.setProjectId(projectId);
         dto.setFileName(zipFileName);
-        dto.setLog(log);
+        dto.setLog(logs);
         dto.setVersion(version);
         dto.setModelNames(entities.stream().map(MpkFileDTO::getPyName).collect(Collectors.joining(",")));
         dto.setCreateTime(new Date());
         // 生成更新日志
-        createLog(projectId,projectName,dirPath.getAbsolutePath(),dto,version);
+        createLog(projectId, projectName, dirPath.getAbsolutePath(), dto, version);
         // 打zip包
         String zipPath = mpkBakFilePath + File.separator + zipFileName;
-        ZipUtil.zip(dirPath.getAbsolutePath(),zipPath);
+        ZipUtil.zip(dirPath.getAbsolutePath(), zipPath);
         byte[] bytes = FileUtil.readBytes(zipPath);
         // 记录打包日志
         dto.setFilePath(zipPath);
@@ -408,8 +349,32 @@
         return bytes;
     }
 
+    private void createDllFile(String dirPath, List<String> cppFilePaths, String dllSavePath) throws InterruptedException, IOException {
+        cppFilePaths.add(dirPath + File.separator + "C++" + File.separator + "pch.cpp");
+        cppFilePaths.add(dirPath + File.separator + "C++" + File.separator + "dllmain.cpp");
+        cppFilePaths.add(dirPath + File.separator + "C++" + File.separator + "Environment.cpp");
+        cppFilePaths.add(dirPath + File.separator + "C++" + File.separator + "pyutils.cpp");
+        cppFilePaths.add(dirPath + File.separator + "C++" + File.separator + "convertutils.cpp");
+        String paths = cppFilePaths.stream().collect(Collectors.joining(" "));
+        String command = "cl /LD " + paths + " /EHsc /o " + dllSavePath;
+//        String command = "cmd.exe /c cl -o " + dllSavePath + " /LD D:\\work\\mdk\\code\\makeDll\\src\\main\\java\\org\\example\\MakeDll.c D:\\work\\mdk\\code\\makeDll\\src\\main\\java\\org\\example\\MakeDll2.c";
+        log.info("执行cmd命令生成dll:" + command);
+        ProcessBuilder builder = new ProcessBuilder("cmd", "/c", command);
+        builder.directory(new File(dirPath + File.separator + "C++"));
+        Process process = builder.start();
+        // 获取命令输出结果
+        InputStream inputStream = process.getInputStream();
+        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "GBK")); // 设置编码为GBK
+        String line;
+        while ((line = reader.readLine()) != null) {
+            System.out.println(line);
+        }
+        // 等待命令执行完成
+        process.waitFor();
+    }
+
     @Override
-    public Map<String,String> savePyFile(MultipartFile file) throws IOException {
+    public Map<String, String> savePyFile(MultipartFile file) throws IOException {
         File dir = new File(mpkBakFilePath);
         if (!dir.exists()) {
             dir.mkdirs();
@@ -420,32 +385,32 @@
         // 保存
         file.transferTo(saveFile);
 
-        Map<String,String> result = new HashMap<>(2);
-        result.put("filePath",saveFile.getAbsolutePath());
+        Map<String, String> result = new HashMap<>(2);
+        result.put("filePath", saveFile.getAbsolutePath());
         result.put("fileName", fileName);
 
         return result;
     }
 
-    private void createLog(String projectId,String projectName,String dirPath,ProjectPackageHistoryDTO dto,String version) throws IOException {
-        Map<String,Object> map = new HashMap<>();
-        map.put("projectId",projectId);
+    private void createLog(String projectId, String projectName, String dirPath, ProjectPackageHistoryDTO dto, String version) throws IOException {
+        Map<String, Object> map = new HashMap<>();
+        map.put("projectId", projectId);
         List<ProjectPackageHistoryDTO> list = projectPackageHistoryService.list(map);
         list.add(dto);
         // 按照日期分组再排序
         HashMap<String, List<ProjectPackageHistoryDTO>> 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()))));
+                        LinkedHashMap::new,
+                        Collectors.collectingAndThen(Collectors.toList(), e -> e.stream().sorted(Comparator.comparing(ProjectPackageHistoryDTO::getCreateTime)).collect(Collectors.toList()))));
 
         Map<String, Object> data = new HashMap<>();
-        data.put("dataMap",dataMap);
-        data.put("projectName",projectName);
-        data.put("version",version);
-        data.put("now",DateUtils.format(new Date(),DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
+        data.put("dataMap", dataMap);
+        data.put("projectName", projectName);
+        data.put("version", version);
+        data.put("now", DateUtils.format(new Date(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
 
         File logFile = new File(dirPath + File.separator + "更新日志.txt");
-        GenUtils.drawTemplate("log.txt.vm",data,logFile);
+        GenUtils.drawTemplate("log.txt.vm", data, logFile);
     }
 
     private void pkgJar(String dirPath) throws InterruptedException {

--
Gitblit v1.9.3