package com.iailab.module.model.mpk.service.impl; import cn.hutool.core.io.FileUtil; 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; 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.module.infra.api.config.ConfigApi; import com.iailab.module.model.mpk.common.MdkConstant; import com.iailab.module.model.mpk.common.utils.GenUtils; import com.iailab.module.model.mpk.dao.MpkFileDao; import com.iailab.module.model.mpk.dto.*; import com.iailab.module.model.mpk.entity.*; import com.iailab.module.model.mpk.service.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; 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.util.CollectionUtils; import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.nio.file.Files; import java.util.*; import java.util.stream.Collectors; import java.util.zip.ZipOutputStream; /** * @author PanZhibao * @Description * @createTime 2024年08月14日 */ @Slf4j @Service public class MpkFileServiceImpl extends BaseServiceImpl implements MpkFileService { @Autowired private GeneratorCodeHistoryService generatorCodeHistoryService; @Autowired private ProjectPackageHistoryService projectPackageHistoryService; @Autowired private ModelMethodService modelMethodService; @Autowired private ProjectPackageHistoryModelService projectPackageHistoryModelService; @Autowired private ConfigApi configApi; @Value("${mpk.bakFilePath}") private String mpkBakFilePath; /*@PostConstruct public void init() { mpkBakFilePath = configApi.getConfigValueByKey("mpkBakFilePath").getCheckedData(); }*/ @Override public PageData page(Map params) { IPage page = baseDao.selectPage( getPage(params, "create_date", false), getWrapper(params) ); return getPageData(page, MpkFileDTO.class); } @Override public List list(Map params) { List entityList = baseDao.selectList(getWrapper(params).orderByDesc("create_date")); return ConvertUtils.sourceToTarget(entityList, MpkFileDTO.class); } private QueryWrapper getWrapper(Map 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 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; } @Override public MpkFileDTO get(String id) { MpkFileDTO entity = baseDao.get(id); return entity; } @Override @DSTransactional(rollbackFor = Exception.class) public void save(MpkFileDTO dto) { MpkFileEntity entity = ConvertUtils.sourceToTarget(dto, MpkFileEntity.class); entity.setId(UUID.randomUUID().toString()); entity.setPkgName(dto.getPkgName().trim()); entity.setFilePath(dto.getFilePath().trim()); entity.setCreator(SecurityFrameworkUtils.getLoginUserId()); entity.setCreateDate(new Date()); insert(entity); modelMethodService.insertList(dto.getModelMethods(), entity.getId()); } @Override @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); modelMethodService.deleteModelMethod(entity.getId()); modelMethodService.insertList(dto.getModelMethods(), entity.getId()); } @Override @DSTransactional(rollbackFor = Exception.class) public void delete(String id) { //删除源文件 MpkFileEntity mpkFileEntity = selectById(id); if (StringUtils.isNoneBlank(mpkFileEntity.getFilePath())) { File mpkFile = new File(mpkFileEntity.getFilePath()); if (mpkFile.exists()) { mpkFile.delete(); log.info("删除源文件备份文件:" + mpkFileEntity.getFilePath()); } } //删除备份文件 Map map1 = new HashMap<>(); map1.put("mdkId", id); List list = generatorCodeHistoryService.list(map1); list.forEach(e -> { File file = new File(e.getFilePath()); if (file.exists()) { file.delete(); log.info("删除生成代码备份文件:" + e.getFilePath()); } }); //删除 会级联删除掉关联表 deleteById(id); } @Override public byte[] generatorCode(String id, String remark, String zipFileName) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(outputStream); MpkFileDTO entity = baseDao.get(id); //生成代码 GenUtils.generatorCode(entity, zip); IOUtils.closeQuietly(zip); File file = null; OutputStream fileOutputStream = null; //文件备份 try { String dirPath = mpkBakFilePath; File dir = new File(dirPath); if (!dir.exists()) { dir.mkdirs(); } String filePath = dirPath + File.separator + zipFileName; file = new File(filePath); file.createNewFile(); fileOutputStream = Files.newOutputStream(file.toPath()); IOUtils.write(outputStream.toByteArray(), fileOutputStream); } catch (IOException e) { log.error("生成代码文件备份失败," + entity.getPyName(), e); throw new RuntimeException(e); } finally { IOUtils.closeQuietly(fileOutputStream); } //代码生成历史记录 GeneratorCodeHistoryEntity historyEntity = new GeneratorCodeHistoryEntity(); historyEntity.setId(UUID.randomUUID().toString()); historyEntity.setMdkId(id); historyEntity.setFileName(file.getName()); historyEntity.setFilePath(file.getAbsolutePath()); historyEntity.setRemark(remark); historyEntity.setCreateTime(new Date()); generatorCodeHistoryService.insert(historyEntity); return outputStream.toByteArray(); } @Override @DSTransactional(rollbackFor = Exception.class) public byte[] packageModel(List ids, String projectId, String projectName, String zipFileName, String logs, String version) throws IOException, InterruptedException { List entities = baseDao.selectByIds(ids); //模板数据 Map map = new HashMap<>(); map.put("entities", entities); VelocityContext context = new VelocityContext(map); //临时文件夹 File dirPath = new File("C:/DLUT/tmp/"); if (!dirPath.exists()) { dirPath.mkdirs(); } //设置velocity资源加载器 Properties prop = new Properties(); prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); Velocity.init(prop); //生成menu.xml文件 LinkedHashMap>> 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 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); // C++源文件 FileUtil.copy("bak/C++", dirPath.getAbsolutePath(), true); List javaFilePaths = new ArrayList<>(); List cppFilePaths = new ArrayList<>(); //生成java文件 for (MpkFileDTO entity : entities) { //封装模板数据 Map data = new HashMap<>(); 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); 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); 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"; FileUtil.mkParentDirs(pyFilePath); 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/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/IAILMDK/common/Environment.java", envJavaFile.getAbsolutePath(), true); javaFilePaths.add(envJavaFile.getAbsolutePath()); // 生成class文件 createClassFile(javaFilePaths); // 删除java源文件 deleteJavaFile(javaFilePaths); // 打jar包 pkgJar(dirPath.getAbsolutePath()); // 本次更新日志 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()); // 生成更新日志 createLog(projectId, projectName, dirPath.getAbsolutePath(), dto, version); // 打zip包 String zipPath = mpkBakFilePath + File.separator + zipFileName; ZipUtil.zip(dirPath.getAbsolutePath(), zipPath); byte[] bytes = FileUtil.readBytes(zipPath); // 记录打包日志 dto.setFilePath(zipPath); projectPackageHistoryService.save(dto); // 插入打包历史-模型关联表 List historyModelList = new ArrayList<>(entities.size()); entities.forEach(e -> { ProjectPackageHistoryModelEntity entity = new ProjectPackageHistoryModelEntity(); entity.setId(UUID.randomUUID().toString()); 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()); List methods = e.getModelMethods(); if (!CollectionUtils.isEmpty(methods)) { entity.setMethodInfo(JSON.toJSONString(methods)); } historyModelList.add(entity); }); projectPackageHistoryModelService.insertBatch(historyModelList); // 删除临时文件 FileUtils.deleteDirectory(dirPath); // FileUtils.delete(new File(zipPath)); return bytes; } private void createDllFile(String dirPath, List 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 savePyFile(MultipartFile file) throws IOException { File dir = new File(mpkBakFilePath); if (!dir.exists()) { dir.mkdirs(); } String fileName = file.getOriginalFilename(); File saveFile = new File(dir.getAbsolutePath() + File.separator + fileName); saveFile.createNewFile(); // 保存 file.transferTo(saveFile); Map 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 map = new HashMap<>(); map.put("projectId", projectId); List list = projectPackageHistoryService.list(map); list.add(dto); // 按照日期分组再排序 HashMap> 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())))); Map 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)); File logFile = new File(dirPath + File.separator + "更新日志.txt"); GenUtils.drawTemplate("log.txt.vm", data, logFile); } private void pkgJar(String dirPath) throws InterruptedException { StringBuilder sb = new StringBuilder(); sb.append("jar -cvf"); sb.append(" ").append(dirPath).append(File.separator).append("IAILMDK.jar"); sb.append(" -C ").append(dirPath).append(File.separator).append("IAILMDK").append(File.separator).append(" ."); log.info("执行cmd命令打jar包:" + sb); Process process = RuntimeUtil.exec(sb.toString()); process.waitFor(); } private void deleteJavaFile(List javaFilePaths) { for (String javaFilePath : javaFilePaths) { FileUtil.del(javaFilePath); } } private void createClassFile(List javaFilePaths) throws InterruptedException { StringBuilder sb = new StringBuilder(); sb.append("javac -encoding utf-8"); for (String path : javaFilePaths) { sb.append(" ").append(path); } log.info("执行cmd命令生成class:" + sb); Process process = RuntimeUtil.exec(sb.toString()); process.waitFor(); } }