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.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.GeneratorCodeHistoryDTO; 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.ModelMethodEntity; import com.iailab.module.model.mpk.entity.MpkFileEntity; import com.iailab.module.model.mpk.entity.ProjectPackageHistoryModelEntity; 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.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import org.springframework.web.multipart.MultipartFile; import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.OutputStream; 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 MdkFileServiceImpl extends BaseServiceImpl implements MdkFileService { @Autowired private GeneratorCodeHistoryService generatorCodeHistoryService; @Autowired private ProjectModelService projectModelService; @Autowired private ProjectPackageHistoryService projectPackageHistoryService; @Autowired private ModelMethodService modelMethodService; @Autowired private ProjectPackageHistoryModelService projectPackageHistoryModelService; @Autowired private ConfigApi configApi; 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"); 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); return wrapper; } @Override public MpkFileDTO get(String id) { MpkFileDTO entity = baseDao.get(id); return entity; } @Override @Transactional(rollbackFor = Exception.class) public void save(MpkFileDTO dto) { MpkFileEntity entity = ConvertUtils.sourceToTarget(dto, MpkFileEntity.class); String mpkId = UUID.randomUUID().toString(); entity.setId(mpkId); entity.setCreator(SecurityFrameworkUtils.getLoginUserId()); entity.setCreateDate(new Date()); insert(entity); // 添加模型方法 insertModelMethod(dto.getModelMethods(),mpkId); } @Override @Transactional(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(); // 删除模型方法 deleteModelMethod(mpkId); // 添加模型方法 insertModelMethod(dto.getModelMethods(),mpkId); } private void insertModelMethod(List modelMethods,String mpkId) { if (!CollectionUtils.isEmpty(modelMethods)) { modelMethods.forEach(e -> { e.setId(UUID.randomUUID().toString()); e.setMpkFileId(mpkId); }); modelMethodService.insertBatch(modelMethods); } } private void deleteModelMethod(String mpkId) { Map map = new HashMap<>(); map.put("mpkFileId", mpkId); modelMethodService.deleteByMap(map); } @Override @Transactional(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()); } } //删除 deleteById(id); //删除备份文件 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()); } }); //删除生成历史 generatorCodeHistoryService.deleteByMap(map1); //删除关联项目 Map map = new HashMap<>(); map.put("modelId",id); projectModelService.deleteByMap(map); //删除模型方法 deleteModelMethod(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 @Transactional(rollbackFor = Exception.class) public byte[] packageModel(List ids,String projectId,String projectName,String zipFileName,String log,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); //生成cpp文件 File cppFile = new File(dirPath.getAbsolutePath() + File.separator + UUID.randomUUID() + ".cpp"); GenUtils.drawTemplate("pkg.cpp.vm",context,cppFile); //生成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 javaFilePaths = 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()); // 添加python源文件 String pyFilePath = dirPath.getAbsolutePath() + File.separator + "py" + File.separator + entity.getPyName() + ".pyd"; FileUtil.mkParentDirs(pyFilePath); FileUtil.copy(entity.getFilePath(),pyFilePath,true); } //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); 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); 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(log); 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.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; } @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(); } }