2024-10-14 | dengzedong | |
2024-10-14 | dengzedong | |
2024-10-14 | dengzedong | |
2024-10-14 | Jay | |
2024-10-14 | Jay | |
2024-10-14 | dengzedong |
iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/context/DataContextHolder.java
对比新文件 @@ -0,0 +1,45 @@ package com.iailab.framework.tenant.core.context; import com.alibaba.ttl.TransmittableThreadLocal; import com.iailab.framework.common.enums.DocumentEnum; /** * 数据源上下文 Holder * * @author iailab */ public class DataContextHolder { /** * 数据源id */ private static final ThreadLocal<Long> DATA_SOURCE_ID = new TransmittableThreadLocal<>(); /** * 数据源id * * @return 租户编号 */ public static Long getDataSourceId() { return DATA_SOURCE_ID.get(); } /** * 数据源id。如果不存在,则抛出 NullPointerException 异常 * * @return 租户编号 */ public static Long getRequiredDataSourceId() { Long dataSourceId = getDataSourceId(); if (dataSourceId == null) { throw new NullPointerException("DataContextHolder 不存在数据源id!可参考文档:" + DocumentEnum.TENANT.getUrl()); } return dataSourceId; } public static void setDataSourceId(Long dataSourceId) { DATA_SOURCE_ID.set(dataSourceId); } } iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/db/dynamic/DataDS.java
对比新文件 @@ -0,0 +1,25 @@ package com.iailab.framework.tenant.core.db.dynamic; import com.baomidou.dynamic.datasource.annotation.DS; import java.lang.annotation.*; /** * 使用数据源所在的数据源 * * 使用方式:当我们希望一个表使用租户所在的数据源,可以在该表的 Mapper 上添加该注解 * * @author 芋道源码 */ @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @DS(DataDS.KEY) public @interface DataDS { /** * 数据源的占位符 */ String KEY = "#context.dataSourceId"; } iailab-framework/iailab-common-biz-tenant/src/main/java/com/iailab/framework/tenant/core/db/dynamic/TenantDsProcessor.java
@@ -4,8 +4,11 @@ import com.baomidou.dynamic.datasource.creator.DataSourceProperty; import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator; import com.baomidou.dynamic.datasource.processor.DsProcessor; import com.iailab.framework.tenant.core.context.DataContextHolder; import com.iailab.framework.tenant.core.context.TenantContextHolder; import com.iailab.framework.tenant.core.service.TenantFrameworkService; import com.iailab.module.infra.api.db.DataSourceConfigServiceApi; import com.iailab.module.infra.api.db.dto.DataSourceConfigRespDTO; import lombok.RequiredArgsConstructor; import org.aopalliance.intercept.MethodInvocation; import org.springframework.context.annotation.Lazy; @@ -46,18 +49,36 @@ @Lazy private DefaultDataSourceCreator dataSourceCreator; @Resource @Lazy private DataSourceConfigServiceApi dataSourceConfigServiceApi; @Override public boolean matches(String key) { return Objects.equals(key, TenantDS.KEY); return Objects.equals(key, TenantDS.KEY) || Objects.equals(key, DataDS.KEY); } @Override public String doDetermineDatasource(MethodInvocation invocation, String key) { // 获得数据源配置 Long tenantId = TenantContextHolder.getRequiredTenantId(); DataSourceProperty dataSourceProperty = tenantFrameworkService.getDataSourceProperty(tenantId); // 创建 or 创建数据源,并返回数据源名字 return createDatasourceIfAbsent(dataSourceProperty); if (DataDS.KEY.equals(key)){ // 获得数据源配置 Long dataSourceId = DataContextHolder.getRequiredDataSourceId(); DataSourceConfigRespDTO dataSourceConfigRespDTO = dataSourceConfigServiceApi.getDataSourceConfig(dataSourceId); DataSourceProperty dataSourceProperty = new DataSourceProperty(); dataSourceProperty.setPoolName(dataSourceConfigRespDTO.getName()); dataSourceProperty.setUrl(dataSourceConfigRespDTO.getUrl()); dataSourceProperty.setUsername(dataSourceConfigRespDTO.getUsername()); dataSourceProperty.setPassword(dataSourceConfigRespDTO.getPassword()); // 创建 or 创建数据源,并返回数据源名字 return createDatasourceIfAbsent(dataSourceProperty); }else if(TenantDS.KEY.equals(key)){ // 获得数据源配置 Long tenantId = TenantContextHolder.getRequiredTenantId(); DataSourceProperty dataSourceProperty = tenantFrameworkService.getDataSourceProperty(tenantId); // 创建 or 创建数据源,并返回数据源名字 return createDatasourceIfAbsent(dataSourceProperty); } return key; } private String createDatasourceIfAbsent(DataSourceProperty dataSourceProperty) { iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/enums/IndStatFuncEnum.java
@@ -11,7 +11,8 @@ COUNT("COUNT", "计数"), AVG("AVG", "平均值"), MAX("MAX", "最大值"), MIN("MIN", "最小值"); MIN("MIN", "最小值"), DEFAULT("DEFAULT", "默认值"); private String code; private String desc; iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/collection/utils/IndSqlUtils.java
@@ -4,12 +4,14 @@ import com.iailab.module.data.common.enums.IndStatFuncEnum; import com.iailab.module.data.common.enums.IndTimeLimitEnum; import com.iailab.module.data.ind.data.dto.IndDataSetDTO; import com.iailab.module.data.ind.data.service.IndDataSetFieldService; import com.iailab.module.data.ind.data.service.IndDataSetService; import com.iailab.module.data.ind.item.service.IndItemAtomService; import com.iailab.module.data.ind.item.service.IndItemDerService; import com.iailab.module.data.ind.item.vo.IndItemAtomVO; import com.iailab.module.data.ind.item.vo.IndItemDerVO; import com.iailab.module.data.ind.value.dto.QuerySourceValueDTO; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -29,6 +31,9 @@ @Autowired private IndDataSetService indDataSetService; @Autowired private IndDataSetFieldService indDataSetFieldService; @Autowired private IndItemDerService indItemDerService; @@ -51,7 +56,7 @@ } result.setDataSource(indItem.getDataSource()); StringBuilder selectSql = new StringBuilder(); IndStatFuncEnum statFun = IndStatFuncEnum.getEumByCode(indItem.getStatFunc()); IndStatFuncEnum statFun = IndStatFuncEnum.getEumByCode(StringUtils.isBlank(indItem.getStatFunc())? IndStatFuncEnum.DEFAULT.getCode() : indItem.getStatFunc()); switch (statFun) { case AVG: selectSql.append("AVG("); @@ -68,7 +73,7 @@ case SUM: selectSql.append("SUM("); break; default: case DEFAULT: selectSql.append("("); break; } @@ -89,8 +94,10 @@ // 拼接SELECT StringBuilder selectSql = new StringBuilder(); selectSql.append(indItem.getDimension()); selectSql.append(", "); if (StringUtils.isNotBlank(indItem.getDimension())){ selectSql.append(indItem.getDimension()); selectSql.append(", "); } selectSql.append(result.getSelectSql()); selectSql.append(", "); selectSql.append(indItem.getTimeLabel()); @@ -174,9 +181,12 @@ whereSql.append("'"); break; case CUSTOM: whereSql.append(indItem.getTimeLabel()); whereSql.append(" <= '"); whereSql.append(DateUtils.format(indItem.getTimeEnd(), PATTERN_MON)); whereSql.append("' AND >= '"); whereSql.append("' AND '"); whereSql.append(indItem.getTimeLabel()); whereSql.append("' >= '"); whereSql.append(DateUtils.format(indItem.getTimeStart(), PATTERN_MON)); whereSql.append("'"); break; iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/value/dao/IndItemValueDao.java
@@ -3,6 +3,7 @@ import com.iailab.framework.common.pojo.PageResult; import com.iailab.framework.mybatis.core.mapper.BaseMapperX; import com.iailab.framework.mybatis.core.query.LambdaQueryWrapperX; import com.iailab.framework.tenant.core.db.dynamic.DataDS; import com.iailab.framework.tenant.core.db.dynamic.TenantDS; import com.iailab.module.data.ind.item.vo.IndItemValueVO; import com.iailab.module.data.ind.value.dto.QuerySourceValueDTO; @@ -28,6 +29,6 @@ .leIfPresent(IndItemValueEntity::getDataTime, reqVO.getEndTime()) .orderByDesc(IndItemValueEntity::getDataTime)); } @DataDS List<IndItemValueVO> getSourceValue(QuerySourceValueDTO dto); } iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/ind/value/service/impl/IndItemValueServiceImpl.java
@@ -3,6 +3,7 @@ import com.iailab.framework.common.pojo.PageResult; import com.iailab.framework.common.service.impl.BaseServiceImpl; import com.iailab.framework.common.util.object.BeanUtils; import com.iailab.framework.tenant.core.context.DataContextHolder; import com.iailab.module.data.ind.item.vo.IndItemValueVO; import com.iailab.module.data.ind.value.dao.IndItemValueDao; import com.iailab.module.data.ind.value.dto.QuerySourceValueDTO; @@ -14,7 +15,6 @@ import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; import java.util.UUID; /** @@ -64,8 +64,7 @@ log.warn("数据源不能为空"); return null; } DataContextHolder.setDataSourceId(Long.valueOf(dto.getDataSource())); return baseDao.getSourceValue(dto); } } iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/ModelServiceApplication.java
@@ -46,7 +46,9 @@ //加载项目已发布的dll和jar try { log.info("************开始加载项目已发布的dll和jar**************"); DllUtils.loadProjectPublish(mpkBakFilePath + File.separator + MdkConstant.PROJECT_PUBLISH); log.info("************加载项目已发布的dll和jar结束**************"); } catch (Exception e) { e.printStackTrace(); log.error("加载项目已发布的dll和jar失败"); iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/MdkConstant.java
@@ -39,6 +39,10 @@ */ String SPLIT = "__"; /** * 项目未发布dll和jar目录 */ String PROJECT_UNPUBLISH = "projectUnpublish"; /** * 项目发布dll和jar目录 */ String PROJECT_PUBLISH = "projectPublish"; iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/GeneratorCodeHistoryController.java
@@ -35,34 +35,10 @@ } @GetMapping("{id}") public CommonResult<GeneratorCodeHistoryDTO> get(@PathVariable("id") Long id){ public CommonResult<GeneratorCodeHistoryDTO> get(@PathVariable("id") String id){ GeneratorCodeHistoryDTO data = generatorCodeHistoryService.get(id); return success(data); } @PostMapping public CommonResult save(@RequestBody GeneratorCodeHistoryDTO dto){ generatorCodeHistoryService.save(dto); return CommonResult.success(); } @PutMapping public CommonResult update(@RequestBody GeneratorCodeHistoryDTO dto){ generatorCodeHistoryService.update(dto); return CommonResult.success(); } @DeleteMapping public CommonResult delete(@RequestBody Long[] ids){ generatorCodeHistoryService.delete(ids); return CommonResult.success(); } @GetMapping("download") iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java
@@ -2,12 +2,15 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.iail.utils.RSAUtils; import com.iailab.framework.common.exception.enums.GlobalErrorCodeConstants; import com.iailab.framework.common.pojo.CommonResult; import com.iailab.framework.tenant.core.context.TenantContextHolder; import com.iailab.module.model.mpk.common.MdkConstant; import com.iailab.module.model.mpk.common.utils.DllUtils; import com.iailab.module.model.mpk.common.utils.Readtxt; import com.iailab.module.model.mpk.dto.MdkDTO; import com.iailab.module.model.mpk.dto.MdkRunDTO; import io.swagger.v3.oas.annotations.Operation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -16,6 +19,7 @@ import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.lang.reflect.Method; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.HashMap; @@ -37,16 +41,25 @@ private String mpkBakFilePath; @PostMapping("run") public CommonResult<String> run(@RequestBody MdkDTO dto) { /** * @description: 模型测试运行 * @author: dzd * @date: 2024/10/14 15:26 **/ @PostMapping("test") public CommonResult<String> test(@RequestBody MdkDTO dto) { Long tenantId = TenantContextHolder.getTenantId(); // 备份文件 租户隔离 String mpkTenantBakFilePath = mpkBakFilePath + File.separator + tenantId; Class<?> clazz; URLClassLoader classLoader; try { File jarFile = new File(mpkBakFilePath + File.separator + MdkConstant.JAR + File.separator + dto.getPyName() + ".jar"); File jarFile = new File(mpkTenantBakFilePath + File.separator + MdkConstant.JAR + File.separator + dto.getPyName() + ".jar"); if (!jarFile.exists()) { throw new RuntimeException("jar包不存在,请先生成代码。jarPath:" + jarFile.getAbsolutePath()); } File dllFile = new File(mpkBakFilePath + File.separator + MdkConstant.DLL + File.separator + dto.getPyName() + ".dll"); File dllFile = new File(mpkTenantBakFilePath + File.separator + MdkConstant.DLL + File.separator + dto.getPyName() + ".dll"); if (!dllFile.exists()) { throw new RuntimeException("dll文件不存在,请先生成代码。dllPath:" + dllFile.getAbsolutePath()); } @@ -111,6 +124,37 @@ } } /** * @description: 模型运行 * @author: dzd * @date: 2024/10/14 15:26 **/ @PostMapping("run") public CommonResult<String> run(@RequestBody MdkRunDTO dto) { if (RSAUtils.checkLisenceBean().getCode() != 1) { log.error("Lisence 不可用!"); return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"Lisence 不可用!"); } else { try { URLClassLoader classLoader = DllUtils.getClassLoader(dto.getProjectId()); if (null == classLoader) { return CommonResult.error(GlobalErrorCodeConstants.ERROR_CONFIGURATION.getCode(),"请先发布项目!"); } Class<?> clazz = classLoader.loadClass(dto.getClassName()); Method method = clazz.getMethod(dto.getMethodName(), dto.getParamsClassArray()); HashMap invoke = (HashMap) method.invoke(clazz.newInstance(), dto.getParamsValueArray()); // todo 将结果存入数据库 } catch (Exception e) { log.error("模型运行失败",e); return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"模型运行失败!"); } } return CommonResult.success(); } @PostMapping("/import") @Operation(summary = "导入参数") public CommonResult<List<String>> importExcel(@RequestParam("file") MultipartFile file) throws Exception { iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/dao/ProjectPackageHistoryModelDao.java
@@ -5,6 +5,8 @@ import com.iailab.module.model.mpk.entity.ProjectPackageHistoryModelEntity; import org.apache.ibatis.annotations.Mapper; import java.util.Collection; /** * @description: * @author: dzd @@ -13,5 +15,6 @@ @TenantDS @Mapper public interface ProjectPackageHistoryModelDao extends BaseDao<ProjectPackageHistoryModelEntity> { void insertBatch(Collection<ProjectPackageHistoryModelEntity> entityList); } iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/dto/MdkRunDTO.java
对比新文件 @@ -0,0 +1,27 @@ package com.iailab.module.model.mpk.dto; import lombok.Data; import java.io.Serializable; import java.util.HashMap; import java.util.List; /** * @description: * @author: dzd * @date: 2024/10/14 16:09 **/ @Data public class MdkRunDTO implements Serializable { private static final long serialVersionUID = 1L; private String projectId; private String className; private String methodName; private Class<?>[] paramsClassArray; private Object[] paramsValueArray; } iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/GeneratorCodeHistoryService.java
@@ -1,11 +1,12 @@ package com.iailab.module.model.mpk.service; import com.iailab.framework.common.page.PageData; import com.iailab.framework.common.service.CrudService; import com.iailab.framework.common.service.BaseService; import com.iailab.module.model.mpk.dto.GeneratorCodeHistoryDTO; import com.iailab.module.model.mpk.entity.GeneratorCodeHistoryEntity; import java.io.IOException; import java.util.List; import java.util.Map; /** @@ -13,7 +14,7 @@ * @author: dzd * @date: 2024/8/20 11:49 **/ public interface GeneratorCodeHistoryService extends CrudService<GeneratorCodeHistoryEntity, GeneratorCodeHistoryDTO> { public interface GeneratorCodeHistoryService extends BaseService<GeneratorCodeHistoryEntity> { PageData<GeneratorCodeHistoryDTO> page(Map<String, Object> params); @@ -22,4 +23,8 @@ byte[] download(GeneratorCodeHistoryDTO dto) throws IOException; void deleteByMap(Map<String, Object> map); List<GeneratorCodeHistoryDTO> list(Map<String, Object> map1); void save(GeneratorCodeHistoryDTO dto); } iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/ProjectModelService.java
@@ -1,5 +1,6 @@ package com.iailab.module.model.mpk.service; import com.iailab.framework.common.service.BaseService; import com.iailab.framework.common.service.CrudService; import com.iailab.module.model.mpk.dto.MpkFileDTO; import com.iailab.module.model.mpk.dto.ProjectModelDTO; @@ -13,7 +14,7 @@ * @author: dzd * @date: 2024/8/22 14:41 **/ public interface ProjectModelService extends CrudService<ProjectModelEntity, ProjectModelDTO> { public interface ProjectModelService extends BaseService<ProjectModelEntity> { List<ProjectModelDTO> list(Map<String, Object> params); iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/ProjectPackageHistoryModelService.java
@@ -1,10 +1,12 @@ package com.iailab.module.model.mpk.service; import com.iailab.framework.common.page.PageData; import com.iailab.framework.common.service.BaseService; import com.iailab.framework.common.service.CrudService; import com.iailab.module.model.mpk.dto.ProjectPackageHistoryModelDTO; import com.iailab.module.model.mpk.entity.ProjectPackageHistoryModelEntity; import java.util.Collection; import java.util.List; import java.util.Map; @@ -13,9 +15,11 @@ * @author: dzd * @date: 2024/9/11 11:39 **/ public interface ProjectPackageHistoryModelService extends CrudService<ProjectPackageHistoryModelEntity, ProjectPackageHistoryModelDTO> { public interface ProjectPackageHistoryModelService extends BaseService<ProjectPackageHistoryModelEntity> { PageData<ProjectPackageHistoryModelDTO> page(Map<String, Object> params); void deleteByMap(Map<String,Object> map); void insertList(List<ProjectPackageHistoryModelEntity> list); } iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/ProjectPackageHistoryService.java
@@ -1,5 +1,7 @@ package com.iailab.module.model.mpk.service; import com.iailab.framework.common.page.PageData; import com.iailab.framework.common.service.BaseService; import com.iailab.framework.common.service.CrudService; import com.iailab.module.model.mpk.dto.ProjectPackageHistoryDTO; import com.iailab.module.model.mpk.dto.ProjectPackageHistoryModelDTO; @@ -15,9 +17,13 @@ * @author Dzd * @since 1.0.0 2024-08-22 */ public interface ProjectPackageHistoryService extends CrudService<ProjectPackageHistoryEntity, ProjectPackageHistoryDTO> { public interface ProjectPackageHistoryService extends BaseService<ProjectPackageHistoryEntity> { byte[] download(String filePath) throws IOException; void deleteByMap(Map<String, Object> map); List<ProjectPackageHistoryEntity> list(Map<String, Object> map); PageData<ProjectPackageHistoryDTO> page(Map<String, Object> params); } iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/ProjectService.java
@@ -1,6 +1,7 @@ package com.iailab.module.model.mpk.service; import com.iailab.framework.common.page.PageData; import com.iailab.framework.common.service.BaseService; import com.iailab.framework.common.service.CrudService; import com.iailab.module.model.mpk.dto.MpkFileDTO; import com.iailab.module.model.mpk.dto.ProjectDTO; @@ -14,7 +15,7 @@ * @author: dzd * @date: 2024/8/22 14:41 **/ public interface ProjectService extends CrudService<ProjectEntity, ProjectDTO> { public interface ProjectService extends BaseService<ProjectEntity> { PageData<ProjectDTO> page(Map<String, Object> params); List<ProjectDTO> list(Map<String, Object> params); iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/GeneratorCodeHistoryServiceImpl.java
@@ -3,11 +3,14 @@ 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.service.impl.CrudServiceImpl; import com.iailab.framework.common.util.object.ConvertUtils; import com.iailab.module.model.mpk.dao.GeneratorCodeHistoryDao; import com.iailab.module.model.mpk.dao.ModelMethodDao; import com.iailab.module.model.mpk.dto.GeneratorCodeHistoryDTO; import com.iailab.module.model.mpk.entity.GeneratorCodeHistoryEntity; import com.iailab.module.model.mpk.entity.ModelMethodEntity; import com.iailab.module.model.mpk.service.GeneratorCodeHistoryService; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; @@ -15,6 +18,7 @@ import java.io.File; import java.io.IOException; import java.util.List; import java.util.Map; /** @@ -24,7 +28,7 @@ **/ @Service public class GeneratorCodeHistoryServiceImpl extends CrudServiceImpl<GeneratorCodeHistoryDao, GeneratorCodeHistoryEntity, GeneratorCodeHistoryDTO> implements GeneratorCodeHistoryService { public class GeneratorCodeHistoryServiceImpl extends BaseServiceImpl<GeneratorCodeHistoryDao, GeneratorCodeHistoryEntity> implements GeneratorCodeHistoryService { @Override public PageData<GeneratorCodeHistoryDTO> page(Map<String, Object> params) { @@ -58,6 +62,15 @@ } @Override public List<GeneratorCodeHistoryDTO> list(Map<String, Object> map) { return ConvertUtils.sourceToTarget(baseDao.selectList(getWrapper(map)),GeneratorCodeHistoryDTO.class); } @Override public void save(GeneratorCodeHistoryDTO dto) { } public QueryWrapper<GeneratorCodeHistoryEntity> getWrapper(Map<String, Object> params){ String id = (String)params.get("id"); String mdkId = (String)params.get("mdkId"); iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/MpkFileServiceImpl.java
@@ -13,6 +13,7 @@ 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.mpk.common.MdkConstant; import com.iailab.module.model.mpk.common.utils.DllUtils; @@ -24,6 +25,7 @@ import com.iailab.module.model.mpk.dto.ProjectPackageHistoryDTO; 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; @@ -72,11 +74,6 @@ @Value("${mpk.bak-resources}") private String mpkResources; /*@PostConstruct public void init() { mpkBakFilePath = configApi.getConfigValueByKey("mpkBakFilePath").getCheckedData(); }*/ @Override public PageData<MpkFileDTO> page(Map<String, Object> params) { @@ -133,6 +130,12 @@ entity.setCreateDate(new Date()); insert(entity); modelMethodService.insertList(dto.getModelMethods(), entity.getId()); // 替换环境变量MDK_PKGS下的py文件 String mdkPkgs = System.getenv("MDK_PKGS"); String pyFilePath = mdkPkgs + File.separator + entity.getPyModule().replace(".", File.separator) + File.separator + entity.getPyName() + ".pyd"; FileUtil.mkParentDirs(pyFilePath); FileUtil.copy(entity.getFilePath(), pyFilePath, true); } @Override @@ -144,6 +147,12 @@ updateById(entity); modelMethodService.deleteModelMethod(entity.getId()); modelMethodService.insertList(dto.getModelMethods(), entity.getId()); // 替换环境变量MDK_PKGS下的py文件 String mdkPkgs = System.getenv("MDK_PKGS"); String pyFilePath = mdkPkgs + File.separator + entity.getPyModule().replace(".", File.separator) + File.separator + entity.getPyName() + ".pyd"; FileUtil.mkParentDirs(pyFilePath); FileUtil.copy(entity.getFilePath(), pyFilePath, true); } @Override @@ -178,6 +187,10 @@ @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资源加载器 @@ -241,7 +254,7 @@ String dllSavePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.LIBS + File.separator + "IAIL.MDK.Mid.Jni.dll"; createDllFile(dirPath.getAbsolutePath(),cppFilePaths,dllSavePath); //备份dll文件,用于后续运行 String dllBakPath = mpkBakFilePath + File.separator + MdkConstant.DLL + File.separator + entity.getPyName() + ".dll"; String dllBakPath = mpkTenantBakFilePath + File.separator + MdkConstant.DLL + File.separator + entity.getPyName() + ".dll"; FileUtil.mkParentDirs(dllBakPath); FileUtil.copy(dllSavePath, dllBakPath, true); @@ -261,11 +274,11 @@ // 打jar包 String jarSavePath = pkgJar(dirPath.getAbsolutePath()); //备份jar文件,用于后续运行 String jarBakPath = mpkBakFilePath + File.separator + MdkConstant.JAR + File.separator + entity.getPyName() + ".jar"; String jarBakPath = mpkTenantBakFilePath + File.separator + MdkConstant.JAR + File.separator + entity.getPyName() + ".jar"; FileUtil.mkParentDirs(dllBakPath); FileUtil.copy(jarSavePath, jarBakPath, true); // 打zip包 String zipPath = mpkBakFilePath + File.separator + zipFileName; String zipPath = mpkTenantBakFilePath + File.separator + zipFileName; ZipUtil.zip(dirPath.getAbsolutePath(), zipPath); byte[] bytes = FileUtil.readBytes(zipPath); @@ -291,6 +304,10 @@ @Override @DSTransactional(rollbackFor = Exception.class) public byte[] packageModel(List<String> ids, String projectId, String projectName, String zipFileName, String logs, String version) throws IOException, InterruptedException { Long tenantId = TenantContextHolder.getTenantId(); // 备份文件 租户隔离 String mpkTenantBakFilePath = mpkBakFilePath + File.separator + tenantId; List<MpkFileDTO> entities = baseDao.selectByIds(ids); //模板数据 @@ -376,7 +393,7 @@ //备份dll,发布时使用 File dllFile = new File(dllSavePath); if (dllFile.exists()) { File dllBakFile = new File(mpkBakFilePath + File.separator + projectId + MdkConstant.SPLIT + historyId + ".dll"); 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); @@ -400,30 +417,30 @@ //备份jar包,发布时使用 File jarFile = new File(jarSavePath); if (jarFile.exists()) { File jarBakFile = new File(mpkBakFilePath + File.separator + projectId + MdkConstant.SPLIT + historyId + ".jar"); 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); } // 本次更新日志 ProjectPackageHistoryDTO dto = new ProjectPackageHistoryDTO(); 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()); 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(), dto, version); createLog(projectId, projectName, dirPath.getAbsolutePath(), projectPackageHistoryEntity, version); // 打zip包 String zipPath = mpkBakFilePath + File.separator + zipFileName; String zipPath = mpkTenantBakFilePath + File.separator + zipFileName; ZipUtil.zip(dirPath.getAbsolutePath(), zipPath); byte[] bytes = FileUtil.readBytes(zipPath); // 记录打包日志 dto.setFilePath(zipPath); projectPackageHistoryService.save(dto); projectPackageHistoryEntity.setFilePath(zipPath); projectPackageHistoryService.insert(projectPackageHistoryEntity); // 插入打包历史-模型关联表 List<ProjectPackageHistoryModelEntity> historyModelList = new ArrayList<>(entities.size()); entities.forEach(e -> { @@ -442,7 +459,7 @@ } historyModelList.add(entity); }); projectPackageHistoryModelService.insertBatch(historyModelList); projectPackageHistoryModelService.insertList(historyModelList); // 删除临时文件 FileUtils.deleteDirectory(dirPath); return bytes; @@ -472,7 +489,11 @@ @Override public Map<String, String> savePyFile(MultipartFile file) throws IOException { File dir = new File(mpkBakFilePath); Long tenantId = TenantContextHolder.getTenantId(); // 备份文件 租户隔离 String mpkTenantBakFilePath = mpkBakFilePath + File.separator + tenantId; File dir = new File(mpkTenantBakFilePath); if (!dir.exists()) { dir.mkdirs(); } @@ -497,8 +518,8 @@ String projectId = (String) params.get("projectId"); // 判断新的dll和jar文件是否存在 String jarFileBakPath = mpkBakFilePath + File.separator + projectId + MdkConstant.SPLIT + historyId + ".jar"; String dllFileBakPath = mpkBakFilePath + File.separator + projectId + MdkConstant.SPLIT + historyId + ".dll"; 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); } @@ -542,16 +563,16 @@ return CommonResult.success(); } private void createLog(String projectId, String projectName, String dirPath, ProjectPackageHistoryDTO dto, String version) throws IOException { private void createLog(String projectId, String projectName, String dirPath, ProjectPackageHistoryEntity projectPackageHistoryEntity, String version) { Map<String, Object> map = new HashMap<>(); map.put("projectId", projectId); List<ProjectPackageHistoryDTO> list = projectPackageHistoryService.list(map); list.add(dto); List<ProjectPackageHistoryEntity> list = projectPackageHistoryService.list(map); list.add(projectPackageHistoryEntity); // 按照日期分组再排序 HashMap<String, List<ProjectPackageHistoryDTO>> dataMap = list.stream().collect( 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(ProjectPackageHistoryDTO::getCreateTime)).collect(Collectors.toList())))); 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); iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/ProjectModelServiceImpl.java
@@ -1,6 +1,7 @@ package com.iailab.module.model.mpk.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.iailab.framework.common.service.impl.BaseServiceImpl; import com.iailab.framework.common.service.impl.CrudServiceImpl; import com.iailab.framework.common.util.object.ConvertUtils; import com.iailab.module.model.mpk.dao.ProjectModelDao; @@ -22,7 +23,7 @@ * @date: 2024/8/22 14:41 **/ @Service public class ProjectModelServiceImpl extends CrudServiceImpl<ProjectModelDao, ProjectModelEntity, ProjectModelDTO> implements ProjectModelService { public class ProjectModelServiceImpl extends BaseServiceImpl<ProjectModelDao, ProjectModelEntity> implements ProjectModelService { @Override public List<ProjectModelDTO> list(Map<String, Object> params) { List<ProjectModelEntity> entityList = baseDao.selectList(getWrapper(params)); @@ -48,7 +49,6 @@ baseDao.insert(list); } @Override public QueryWrapper<ProjectModelEntity> getWrapper(Map<String, Object> params){ String id = (String)params.get("id"); String projectId = (String)params.get("projectId"); iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/ProjectPackageHistoryModelServiceImpl.java
@@ -1,8 +1,10 @@ package com.iailab.module.model.mpk.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.enums.SqlMethod; 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.service.impl.CrudServiceImpl; import com.iailab.module.model.mpk.dao.ProjectPackageHistoryModelDao; import com.iailab.module.model.mpk.dto.ProjectPackageHistoryModelDTO; @@ -12,6 +14,8 @@ import org.apache.poi.ss.formula.functions.T; import org.springframework.stereotype.Service; import java.util.Collection; import java.util.List; import java.util.Map; /** @@ -20,7 +24,11 @@ * @date: 2024/8/22 14:41 **/ @Service public class ProjectPackageHistoryModelServiceImpl extends CrudServiceImpl<ProjectPackageHistoryModelDao, ProjectPackageHistoryModelEntity, ProjectPackageHistoryModelDTO> implements ProjectPackageHistoryModelService { public class ProjectPackageHistoryModelServiceImpl extends BaseServiceImpl<ProjectPackageHistoryModelDao, ProjectPackageHistoryModelEntity> implements ProjectPackageHistoryModelService { public void insertList(List<ProjectPackageHistoryModelEntity> entityList) { baseDao.insertBatch(entityList); } @Override public PageData<ProjectPackageHistoryModelDTO> page(Map<String, Object> params) { @@ -37,7 +45,6 @@ baseDao.delete(getWrapper(map)); } @Override public QueryWrapper<ProjectPackageHistoryModelEntity> getWrapper(Map<String, Object> params){ String id = (String)params.get("id"); String packageHistoryId = (String)params.get("packageHistoryId"); @@ -52,6 +59,4 @@ return wrapper; } } iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/ProjectPackageHistoryServiceImpl.java
@@ -1,8 +1,12 @@ package com.iailab.module.model.mpk.service.impl; 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.service.impl.CrudServiceImpl; import com.iailab.module.model.mpk.dao.ProjectPackageHistoryDao; import com.iailab.module.model.mpk.dto.ProjectDTO; import com.iailab.module.model.mpk.dto.ProjectPackageHistoryDTO; import com.iailab.module.model.mpk.entity.ProjectPackageHistoryEntity; import com.iailab.module.model.mpk.service.ProjectPackageHistoryService; @@ -12,6 +16,7 @@ import java.io.File; import java.io.IOException; import java.util.List; import java.util.Map; /** @@ -21,9 +26,8 @@ * @since 1.0.0 2024-08-22 */ @Service public class ProjectPackageHistoryServiceImpl extends CrudServiceImpl<ProjectPackageHistoryDao, ProjectPackageHistoryEntity, ProjectPackageHistoryDTO> implements ProjectPackageHistoryService { public class ProjectPackageHistoryServiceImpl extends BaseServiceImpl<ProjectPackageHistoryDao, ProjectPackageHistoryEntity> implements ProjectPackageHistoryService { @Override public QueryWrapper<ProjectPackageHistoryEntity> getWrapper(Map<String, Object> params){ String id = (String)params.get("id"); String projectId = (String)params.get("projectId"); @@ -54,4 +58,15 @@ public void deleteByMap(Map<String, Object> map) { baseDao.delete(getWrapper(map)); } @Override public List<ProjectPackageHistoryEntity> list(Map<String, Object> map) { return baseDao.selectList(getWrapper(map)); } @Override public PageData<ProjectPackageHistoryDTO> page(Map<String, Object> params) { IPage<ProjectPackageHistoryEntity> page = baseDao.selectPage(getPage(params, "create_time", false), getWrapper(params)); return getPageData(page, ProjectPackageHistoryDTO.class); } } iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/service/impl/ProjectServiceImpl.java
@@ -4,6 +4,7 @@ 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.service.impl.CrudServiceImpl; import com.iailab.framework.common.util.object.ConvertUtils; import com.iailab.module.model.mpk.dao.MpkFileDao; @@ -13,6 +14,7 @@ import com.iailab.module.model.mpk.dto.ProjectModelDTO; import com.iailab.module.model.mpk.dto.ProjectPackageHistoryDTO; import com.iailab.module.model.mpk.entity.ProjectEntity; import com.iailab.module.model.mpk.entity.ProjectPackageHistoryEntity; import com.iailab.module.model.mpk.service.ProjectModelService; import com.iailab.module.model.mpk.service.ProjectPackageHistoryModelService; import com.iailab.module.model.mpk.service.ProjectPackageHistoryService; @@ -34,7 +36,7 @@ **/ @Service @Slf4j public class ProjectServiceImpl extends CrudServiceImpl<ProjectDao, ProjectEntity, ProjectDTO> implements ProjectService { public class ProjectServiceImpl extends BaseServiceImpl<ProjectDao, ProjectEntity> implements ProjectService { @Autowired ProjectModelService projectModelService; @@ -121,7 +123,7 @@ //删除备份文件 Map<String,Object> map = new HashMap<>(); map.put("projectId",id); List<ProjectPackageHistoryDTO> list = projectPackageHistoryService.list(map); List<ProjectPackageHistoryEntity> list = projectPackageHistoryService.list(map); list.forEach(e -> { File file = new File(e.getFilePath()); if (file.exists()) { @@ -161,7 +163,6 @@ return new PageData<>(list,total); } @Override public QueryWrapper<ProjectEntity> getWrapper(Map<String, Object> params){ String id = (String)params.get("id"); String projectName = (String)params.get("projectName"); iailab-module-model/iailab-module-model-biz/src/main/resources/mapper/mpk/ProjectPackageHistoryModelDao.xml
对比新文件 @@ -0,0 +1,13 @@ <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.iailab.module.model.mpk.dao.ProjectPackageHistoryModelDao"> <insert id="insertBatch"> INSERT INTO t_mpk_project_package_history_model VALUES <foreach collection="list" item="item" separator=","> (#{item.id}, #{item.projectId}, #{item.packageHistoryId}, #{item.pyName}, #{item.pyChineseName}, #{item.pkgName}, #{item.pyModule}, #{item.remark}, #{item.methodInfo}) </foreach> </insert> </mapper> iailab-module-model/iailab-module-model-biz/src/main/resources/template/impl.java.vm
@@ -22,12 +22,12 @@ #{end} if (#{foreach} ($column in [1..$entity.dataLength])#{if}($column==1)data${column} == null || data${column}.length == 0 || data${column}[0].length == 0#{else} || data${column} == null || data${column}.length == 0 || data${column}[0].length == 0#{end}#{end}) { result = new HashMap<String, Object>(); result.put("status_code", -4); result.put("status_code", "400"); return result; } result = ${pyName}${entity.methodName}(#foreach ($column in [1..$entity.dataLength])data${column}, #{end}#{if}($entity.model==1)model, #{end}settings); return result; return utils.reverseResult(this.result); } #{end} }