package com.iailab.module.model.mpk.service.impl; import cn.hutool.core.io.FileUtil; 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.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.mcs.pre.service.MmModelArithSettingsService; import com.iailab.module.model.mcs.sche.service.StScheduleModelSettingService; import com.iailab.module.model.mpk.common.MdkConstant; import com.iailab.module.model.mpk.common.utils.CmdUtils; 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.MethodSettingDTO; import com.iailab.module.model.mpk.dto.ModelMethodDTO; import com.iailab.module.model.mpk.dto.MpkFileDTO; 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; import org.apache.commons.io.FileUtils; 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.File; import java.io.IOException; import java.net.URLClassLoader; import java.nio.file.Files; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; /** * @author PanZhibao * @Description * @createTime 2024å¹´08月14æ—¥ */ @Slf4j @Service public class MpkFileServiceImpl extends BaseServiceImpl<MpkFileDao, MpkFileEntity> implements MpkFileService { @Autowired private GeneratorCodeHistoryService generatorCodeHistoryService; @Autowired private ProjectPackageHistoryService projectPackageHistoryService; @Autowired private ModelMethodService modelMethodService; @Autowired private ProjectPackageHistoryModelService projectPackageHistoryModelService; @Autowired private MmModelArithSettingsService mmModelArithSettingsService; @Autowired private StScheduleModelSettingService stScheduleModelSettingService; @Autowired private ConfigApi configApi; @Value("${mpk.bak-file-path}") private String mpkBakFilePath; @Value("${mpk.bak-resources}") private String mpkResources; @Override public PageData<MpkFileDTO> page(Map<String, Object> params) { IPage<MpkFileEntity> page = baseDao.selectPage( getPage(params, "create_date", false), getWrapper(params) ); return getPageData(page, MpkFileDTO.class); } @Override public List<MpkFileDTO> list(Map<String, Object> params) { // List<MpkFileEntity> entityList = baseDao.selectList(getWrapper(params).orderByDesc("create_date")); List<MpkFileDTO> list = baseDao.list(params); return list; } private QueryWrapper<MpkFileEntity> getWrapper(Map<String, Object> params) { String pyChineseName = (String) params.get("pyChineseName"); 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(pyChineseName), "py_chinese_name", pyChineseName) .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); // 将备份的pyd文件,转移到MDK_PKGS环境å˜é‡ä¸‹ï¼Œå¹¶æ·»åŠ 方法的默认å‚数(pyFile,模型路径) String mdkPkgs = System.getenv("MDK_PKGS"); String fileName = entity.getFilePath().substring(entity.getFilePath().lastIndexOf("\\") + 1,entity.getFilePath().lastIndexOf(".pyd")); String pyFilePath = mdkPkgs + File.separator + entity.getPyModule().replace(".", File.separator) + File.separator + fileName + ".pyd"; FileUtil.mkParentDirs(pyFilePath); FileUtil.copy(entity.getFilePath(), pyFilePath, true); // æ·»åŠ å‚æ•° for (ModelMethodDTO method : dto.getModelMethods()) { List<MethodSettingDTO> methodSettings = method.getMethodSettings(); if (methodSettings.stream().anyMatch(e -> e.getSettingKey().equals(MdkConstant.PY_FILE_KEY))) { methodSettings.forEach(e -> { if (e.getSettingKey().equals(MdkConstant.PY_FILE_KEY)) { e.setValue(entity.getPyModule() + "." + fileName); } }); }else { MethodSettingDTO setting = new MethodSettingDTO(); setting.setId(UUID.randomUUID().toString()); setting.setMethodId(method.getId()); setting.setSettingKey(MdkConstant.PY_FILE_KEY); setting.setValue(entity.getPyModule() + "." + fileName); setting.setName("模型路径"); setting.setType("input"); setting.setValueType("string"); methodSettings.add(setting); } } modelMethodService.insertList(dto.getModelMethods(), entity.getId()); } @Override @DSTransactional(rollbackFor = Exception.class) public void update(MpkFileDTO dto) { // æ·»åŠ æ–¹æ³•çš„é»˜è®¤å‚数(pyFile,模型路径) String mdkPkgs = System.getenv("MDK_PKGS"); String fileName = dto.getFilePath().substring(dto.getFilePath().lastIndexOf("\\") + 1,dto.getFilePath().lastIndexOf(".pyd")); String pyFilePath = mdkPkgs + File.separator + dto.getPyModule().replace(".", File.separator) + File.separator + fileName + ".pyd"; // 判æ–文件是å¦å˜åœ¨,ä¸å˜åœ¨çš„è¯å°†å¤‡ä»½çš„pyd文件,转移到MDK_PKGS环境å˜é‡ä¸‹ File pyFile = new File(pyFilePath); if (!pyFile.exists()) { FileUtil.mkParentDirs(pyFilePath); FileUtil.copy(dto.getFilePath(), pyFilePath, true); } // æ·»åŠ /修改å‚æ•° for (ModelMethodDTO method : dto.getModelMethods()) { List<MethodSettingDTO> methodSettings = method.getMethodSettings(); if (methodSettings.stream().anyMatch(e -> e.getSettingKey().equals(MdkConstant.PY_FILE_KEY))) { methodSettings.forEach(e -> { if (e.getSettingKey().equals(MdkConstant.PY_FILE_KEY)) { e.setValue(dto.getPyModule() + "." + fileName); } }); }else { MethodSettingDTO setting = new MethodSettingDTO(); setting.setId(UUID.randomUUID().toString()); setting.setMethodId(method.getId()); setting.setSettingKey(MdkConstant.PY_FILE_KEY); setting.setValue(dto.getPyModule() + "." + fileName); setting.setName("模型路径"); setting.setType("input"); setting.setValueType("string"); methodSettings.add(setting); } } // 修改预测项pyFileå‚æ•° mmModelArithSettingsService.updatePyFile(dto.getPyModule(),fileName); // 修改调度项pyFileå‚æ•° stScheduleModelSettingService.updatePyFile(dto.getPyModule(),fileName); 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<String, Object> map1 = new HashMap<>(); map1.put("mdkId", id); List<GeneratorCodeHistoryDTO> 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) { Long tenantId = TenantContextHolder.getTenantId(); // 备份文件 租户隔离 String mpkTenantBakFilePath = mpkBakFilePath + File.separator + tenantId; MpkFileDTO entity = baseDao.get(id); //生æˆä»£ç //设置velocity资æºåŠ 载器 Properties prop = new Properties(); prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); Velocity.init(prop); //å°è£…模æ¿æ•°æ® Map<String, Object> map = new HashMap<>(); map.put("pkgName",entity.getPkgName()); map.put("modelMethods",entity.getModelMethods()); map.put("pyName",entity.getPyName()); map.put("pyModule",entity.getPyModule()); VelocityContext dataContext = new VelocityContext(map); //临时文件夹 File dirPath = null; try { dirPath = Files.createTempDirectory("generatorCodeTmp").toFile(); log.info("生æˆä¸´æ—¶æ–‡ä»¶å¤¹ï¼Œ" + dirPath.getAbsolutePath()); } catch (IOException e) { throw new RuntimeException("创建临时文件夹异常",e); } List<String> javaFilePaths = new ArrayList<>(); //生æˆjava文件 File javaFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.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 + MdkConstant.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); //生æˆ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); //生æˆ.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 + MdkConstant.ALGS + File.separator + entity.getPyModule().replace(".", File.separator) + File.separator + entity.getFilePath().substring(entity.getFilePath().lastIndexOf("\\")); FileUtil.mkParentDirs(pyFilePath); FileUtil.copy(entity.getFilePath(), pyFilePath, true); // 资æºæ–‡ä»¶ FileUtil.copy(mpkResources + File.separator + "libs", dirPath.getAbsolutePath(), true); //生æˆdll文件 String dllSavePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.LIBS + File.separator + "IAIL.MDK.Mid.Jni.dll"; createDllFile(dirPath.getAbsolutePath(),dllSavePath); //备份dll文件,用于åŽç»è¿è¡Œ String dllBakPath = mpkTenantBakFilePath + File.separator + MdkConstant.DLL + File.separator + entity.getPyName() + ".dll"; FileUtil.mkParentDirs(dllBakPath); FileUtil.copy(dllSavePath, dllBakPath, true); //utils + env java文件 File utilsJavaFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.IAILMDK + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "utils" + File.separator + "AlgsUtils.java"); FileUtil.mkParentDirs(utilsJavaFile); FileUtil.copy(mpkResources + File.separator +"IAILMDK/utils/AlgsUtils.java", utilsJavaFile.getAbsolutePath(), true); javaFilePaths.add(utilsJavaFile.getAbsolutePath()); File envJavaFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.IAILMDK + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "common" + File.separator + "Environment.java"); FileUtil.mkParentDirs(envJavaFile); FileUtil.copy(mpkResources + File.separator +"IAILMDK/common/Environment.java", envJavaFile.getAbsolutePath(), true); javaFilePaths.add(envJavaFile.getAbsolutePath()); // 生æˆclass文件 createClassFile(dirPath.getAbsolutePath(),javaFilePaths); // åˆ é™¤javaæºæ–‡ä»¶ deleteJavaFile(javaFilePaths); // 打jar包 String jarSavePath = pkgJar(dirPath.getAbsolutePath()); //备份jar文件,用于åŽç»è¿è¡Œ String jarBakPath = mpkTenantBakFilePath + File.separator + MdkConstant.JAR + File.separator + entity.getPyName() + ".jar"; FileUtil.mkParentDirs(jarBakPath); FileUtil.copy(jarSavePath, jarBakPath, true); // 打zip包 String zipPath = mpkTenantBakFilePath + File.separator + zipFileName; ZipUtil.zip(dirPath.getAbsolutePath(), zipPath); byte[] bytes = FileUtil.readBytes(zipPath); //代ç 生æˆåŽ†å²è®°å½• GeneratorCodeHistoryEntity historyEntity = new GeneratorCodeHistoryEntity(); historyEntity.setId(UUID.randomUUID().toString()); historyEntity.setMdkId(id); historyEntity.setFileName(zipFileName); historyEntity.setFilePath(zipPath); historyEntity.setRemark(remark); historyEntity.setCreateTime(new Date()); generatorCodeHistoryService.insert(historyEntity); // åˆ é™¤ä¸´æ—¶æ–‡ä»¶ try { FileUtils.deleteDirectory(dirPath); } catch (IOException e) { throw new RuntimeException("åˆ é™¤ä¸´æ—¶æ–‡ä»¶å¼‚å¸¸",e); } return bytes; } @Override @DSTransactional(rollbackFor = Exception.class) public byte[] packageModel(String projectId, String projectName, String zipFileName, String logs, String version) throws IOException { Long tenantId = TenantContextHolder.getTenantId(); // 备份文件 租户隔离 String mpkTenantBakFilePath = mpkBakFilePath + File.separator + tenantId; List<MpkFileDTO> entities = baseDao.selectByProjectId(projectId); //模æ¿æ•°æ® // Map<String, Object> map = new HashMap<>(); // map.put("entities", entities); // VelocityContext context = new VelocityContext(map); //临时文件夹 File dirPath = null; try { dirPath = Files.createTempDirectory("packageModelTmp").toFile(); log.info("生æˆä¸´æ—¶æ–‡ä»¶å¤¹ï¼Œ" + dirPath.getAbsolutePath()); } catch (IOException e) { throw new RuntimeException("创建临时文件夹异常",e); } //设置velocity资æºåŠ 载器 Properties prop = new Properties(); prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); Velocity.init(prop); //生æˆ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 + MdkConstant.LIBS + File.separator + "menu.xml"); GenUtils.drawTemplate("menu.xml.vm", new VelocityContext(map1), xmlFile); List<String> javaFilePaths = 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()); VelocityContext dataContext = new VelocityContext(data); //生æˆjava文件 File javaFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.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 + MdkConstant.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); //生æˆ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); //生æˆ.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 + MdkConstant.ALGS + File.separator + entity.getPyModule().replace(".", File.separator) + File.separator + entity.getFilePath().substring(entity.getFilePath().lastIndexOf("\\")); FileUtil.mkParentDirs(pyFilePath); FileUtil.copy(entity.getFilePath(), pyFilePath, true); } // 资æºæ–‡ä»¶ FileUtil.copy(mpkResources + File.separator + "libs", dirPath.getAbsolutePath(), true); //生æˆdll文件 String dllSavePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.LIBS + File.separator + "IAIL.MDK.Mid.Jni.dll"; createDllFile(dirPath.getAbsolutePath(),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 + MdkConstant.IAILMDK + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "utils" + File.separator + "AlgsUtils.java"); FileUtil.mkParentDirs(utilsJavaFile); FileUtil.copy(mpkResources + File.separator +"IAILMDK/utils/AlgsUtils.java", utilsJavaFile.getAbsolutePath(), true); javaFilePaths.add(utilsJavaFile.getAbsolutePath()); File envJavaFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.IAILMDK + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "common" + File.separator + "Environment.java"); FileUtil.mkParentDirs(envJavaFile); FileUtil.copy(mpkResources + File.separator +"IAILMDK/common/Environment.java", envJavaFile.getAbsolutePath(), true); javaFilePaths.add(envJavaFile.getAbsolutePath()); // 生æˆclass文件 createClassFile(dirPath.getAbsolutePath(),javaFilePaths); // åˆ é™¤javaæºæ–‡ä»¶ deleteJavaFile(javaFilePaths); // 打jar包 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); } // 本次更新日志 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(), projectPackageHistoryEntity, version); // 打zip包 String zipPath = mpkTenantBakFilePath + File.separator + zipFileName; ZipUtil.zip(dirPath.getAbsolutePath(), zipPath); byte[] bytes = FileUtil.readBytes(zipPath); // 记录打包日志 projectPackageHistoryEntity.setFilePath(zipPath); projectPackageHistoryService.insert(projectPackageHistoryEntity); // æ’入打包历å²-模型关è”表 List<ProjectPackageHistoryModelEntity> 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<ModelMethodDTO> methods = e.getModelMethods(); if (!CollectionUtils.isEmpty(methods)) { entity.setMethodInfo(JSON.toJSONString(methods)); } historyModelList.add(entity); }); projectPackageHistoryModelService.insertList(historyModelList); // åˆ é™¤ä¸´æ—¶æ–‡ä»¶ FileUtils.deleteDirectory(dirPath); return bytes; } private void createDllFile(String dirPath, String dllSavePath) { try { String command = "cl /LD *.cpp /EHsc /o " + dllSavePath + " /link " + "IAIL.MDK.Mid.Windows.lib"; CmdUtils.exec(command,dirPath + File.separator + MdkConstant.C); } catch (Exception e) { throw new RuntimeException("执行cmd命令生æˆdll异常",e); } } @Override public Map<String, String> savePyFile(MultipartFile file) throws IOException { 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 pyName = fileName.substring(0, fileName.lastIndexOf(".")); String pyName_uuTime = pyName + "_" + new Date().getTime(); File pydBakFile = new File(bakDir.getAbsolutePath() + File.separator + pyName_uuTime + ".pyd"); try { // py文件å˜å…¥ä¸´æ—¶æ–‡ä»¶å¤¹ File saveFile = new File(tempDir.getAbsolutePath() + File.separator + fileName); saveFile.createNewFile(); file.transferTo(saveFile); // 临时文件夹ä¸ç”Ÿæˆpyd文件 // 生æˆsetup.py文件 createSetUpFile(tempDir.getAbsolutePath(),fileName,pyName_uuTime); // 执行cmd命令编译pyd文件 createPydFile(tempDir.getAbsolutePath()); // 临时文件夹ä¸pyd文件移到dir下,修改为uuid.pyd File pydFile = new File(tempDir.getAbsolutePath() + File.separator + pyName_uuTime + 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", pydBakFile.getAbsolutePath()); result.put("fileName", pyName); return result; } private void createPydFile(String dir) { try { String command = "python setup.py build_ext --inplace"; log.info("执行cmd命令编译pyd:" + command); CmdUtils.exec(command,dir); } 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<ProjectPackageHistoryEntity> list = projectPackageHistoryService.list(map); list.add(projectPackageHistoryEntity); // 按照日期分组å†æŽ’åº 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(ProjectPackageHistoryEntity::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)); File logFile = new File(dirPath + File.separator + "更新日志.txt"); GenUtils.drawTemplate("log.txt.vm", data, logFile); } private String pkgJar(String dirPath) { try { String jarSavePath = dirPath + File.separator + MdkConstant.LIBS + File.separator + "IAILMDK.jar"; StringBuilder sb = new StringBuilder(); sb.append("jar -cvf"); sb.append(" ").append(jarSavePath); sb.append(" -C ").append(dirPath).append(File.separator).append(MdkConstant.IAILMDK).append(File.separator).append(" ."); CmdUtils.exec(sb.toString()); return jarSavePath; } catch (Exception e) { throw new RuntimeException("执行cmd命令打jar包异常",e); } } private void deleteJavaFile(List<String> javaFilePaths) { for (String javaFilePath : javaFilePaths) { FileUtil.del(javaFilePath); } } private void createClassFile(String dirPath,List<String> javaFilePaths){ try { Set<String> pathSet = javaFilePaths.stream().map(path -> { // 替æ¢ä¸ºç›¸å¯¹è·¯å¾„ path = path.replace(dirPath + File.separator + MdkConstant.IAILMDK, "."); // åˆ é™¤æ–‡ä»¶å,替æ¢ä¸ºé€šé…符* path = path.substring(0, path.lastIndexOf("\\") + 1) + "*.java"; return path; }).collect(Collectors.toSet()); StringBuilder sb = new StringBuilder(); sb.append("javac -encoding utf-8"); for (String path : pathSet) { sb.append(" ").append(path); } CmdUtils.exec(sb.toString(),dirPath + File.separator + MdkConstant.IAILMDK); } catch (Exception e) { throw new RuntimeException("执行cmd命令生æˆclass异常",e); } } }