dengzedong
2024-09-26 587b438f50abb725f3dee63eaf6b586c09085f54
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;
@@ -25,16 +26,12 @@
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
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;
@@ -51,23 +48,21 @@
    @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;
    @Autowired
    private ConfigApi configApi;
    private String mpkBakFilePath = "";
    @Value("${mpk.bakFilePath}")
    private String mpkBakFilePath;
    /*@PostConstruct
    public void init() {
@@ -91,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;
    }
@@ -111,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) {
        //删除源文件
@@ -204,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());
@@ -217,18 +163,6 @@
        //删除 会级联删除掉关联表
        deleteById(id);
        //删除生成历史
//        generatorCodeHistoryService.deleteByMap(map1);
        //删除关联项目
//        Map<String,Object> map = new HashMap<>();
//        map.put("modelId",id);
//        projectModelService.deleteByMap(map);
        //删除模型方法
//        deleteModelMethod(id);
    }
    @Override
@@ -276,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);
        //临时文件夹
@@ -296,58 +230,74 @@
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(prop);
        //生成cpp文件
        File cppFile = new File(dirPath.getAbsolutePath() + File.separator + UUID.randomUUID() + ".cpp");
        GenUtils.drawTemplate("pkg.cpp.vm",context,cppFile);
        //生成menu.xml文件
        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);
        File xmlFile = new File(dirPath.getAbsolutePath() + File.separator + "menu.xml");
        GenUtils.drawTemplate("menu.xml.vm", new VelocityContext(map1), xmlFile);
        //生成Jni.cpp文件
        File jniCppFile = new File(dirPath.getAbsolutePath() + File.separator + UUID.randomUUID() + "Jni.cpp");
        GenUtils.drawTemplate("pkg.Jni.cpp.vm",context,jniCppFile);
        // C++源文件
        FileUtil.copy("bak/C++", dirPath.getAbsolutePath(), true);
        //生成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);
@@ -361,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);
@@ -382,6 +332,7 @@
            entity.setProjectId(projectId);
            entity.setPackageHistoryId(historyId);
            entity.setPyName(e.getPyName());
            entity.setPyChineseName(e.getPyChineseName());
            entity.setPkgName(e.getPkgName());
            entity.setPyModule(e.getPyModule());
            entity.setRemark(e.getRemark());
@@ -398,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();
@@ -410,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 {