From c647864b9bc3a1f5c80ae26b8dccdf8b2bb65814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= <2270033969@qq.com> Date: Mon, 15 Jul 2024 15:16:05 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E7=9A=84=E8=B7=AF=E5=BE=84=E9=BB=98=E8=AE=A4=E4=B8=BA=20admin?= =?UTF-8?q?=20,=20up=20screw=200.0.6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 582bbcabbea1e84ef4b33c78c0e8f54897106299) --- pigx-visual/pigx-codegen/pom.xml | 2 +- .../service/impl/GenTableServiceImpl.java | 363 +++++++++--------- .../service/impl/GenTemplateServiceImpl.java | 5 +- .../src/main/resources/template/config.json | 4 +- 4 files changed, 184 insertions(+), 190 deletions(-) diff --git a/pigx-visual/pigx-codegen/pom.xml b/pigx-visual/pigx-codegen/pom.xml index 034cb2a41..121addc9d 100755 --- a/pigx-visual/pigx-codegen/pom.xml +++ b/pigx-visual/pigx-codegen/pom.xml @@ -16,7 +16,7 @@ 2.0.2 - 0.0.5 + 0.0.6 11.5.0.0 8.7.1-jdk17-20232101 1.10 diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenTableServiceImpl.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenTableServiceImpl.java index 675b1ec3e..fc493e120 100644 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenTableServiceImpl.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenTableServiceImpl.java @@ -60,211 +60,206 @@ import java.util.*; @RequiredArgsConstructor public class GenTableServiceImpl extends ServiceImpl implements GenTableService { - /** - * 默认配置信息 - */ - private static final String CONFIG_PATH = "template/config.json"; + /** + * 默认配置信息 + */ + private static final String CONFIG_PATH = "template/config.json"; - private final GenTableColumnService columnService; + private final GenTableColumnService columnService; - private final GenGroupService genGroupService; + private final GenGroupService genGroupService; - /** - * 获取配置信息 - * - * @return map - */ - @Override - public Map getGeneratorConfig() { - ClassPathResource classPathResource = new ClassPathResource(CONFIG_PATH); - JSONObject jsonObject = JSONUtil.parseObj(IoUtil.readUtf8(classPathResource.getStream())); - return jsonObject.getRaw(); - } + /** + * 获取配置信息 + * @return map + */ + @Override + public Map getGeneratorConfig() { + ClassPathResource classPathResource = new ClassPathResource(CONFIG_PATH); + JSONObject jsonObject = JSONUtil.parseObj(IoUtil.readUtf8(classPathResource.getStream())); + return jsonObject.getRaw(); + } - /** - * 查询表ddl 语句 - * - * @param dsName 数据源名称 - * @param tableName 表名称 - * @return ddl 语句 - * @throws Exception - */ - @Override - public String queryTableDdl(String dsName, String tableName) throws Exception { - // 手动切换数据源 - DynamicDataSourceContextHolder.push(dsName); - Table table = ServiceProxy.metadata().table(tableName); // 获取表结构 - table.execute(false);// 不执行SQL - ServiceProxy.ddl().create(table); - return table.getDdl();// 返回创建表的DDL - } + /** + * 查询表ddl 语句 + * @param dsName 数据源名称 + * @param tableName 表名称 + * @return ddl 语句 + * @throws Exception + */ + @Override + public String queryTableDdl(String dsName, String tableName) throws Exception { + // 手动切换数据源 + DynamicDataSourceContextHolder.push(dsName); + Table table = ServiceProxy.metadata().table(tableName); // 获取表结构 + table.execute(false);// 不执行SQL + ServiceProxy.ddl().create(table); + return table.getDdl();// 返回创建表的DDL + } - /** - * 查询表的全部字段 - * - * @param dsName 数据源 - * @param tableName 表名称 - * @return column - */ - @Override - public List queryTableColumn(String dsName, String tableName) { - // 手动切换数据源 - DynamicDataSourceContextHolder.push(dsName); - return ServiceProxy.metadata().columns(tableName).values().stream().map(Column::getName).toList(); - } + /** + * 查询表的全部字段 + * @param dsName 数据源 + * @param tableName 表名称 + * @return column + */ + @Override + public List queryTableColumn(String dsName, String tableName) { + // 手动切换数据源 + DynamicDataSourceContextHolder.push(dsName); + return ServiceProxy.metadata().columns(tableName).values().stream().map(Column::getName).toList(); + } - /** - * 查询对应数据源的表 - * - * @param page 分页信息 - * @param table 查询条件 - * @return 表 - */ - @Override - public IPage queryTablePage(Page page, GenTable table) { - // 手动切换数据源 - DynamicDataSourceContextHolder.push(table.getDsName()); - List
tableList = ServiceProxy.metadata().tables().values().stream() - .filter(t -> { - if (StrUtil.isBlank(table.getTableName())) { - return true; - } - return StrUtil.containsIgnoreCase(t.getName(false), table.getTableName()); - }).toList(); + /** + * 查询对应数据源的表 + * @param page 分页信息 + * @param table 查询条件 + * @return 表 + */ + @Override + public IPage queryTablePage(Page
page, GenTable table) { + // 手动切换数据源 + DynamicDataSourceContextHolder.push(table.getDsName()); + List
tableList = ServiceProxy.metadata().tables().values().stream().filter(t -> { + if (StrUtil.isBlank(table.getTableName())) { + return true; + } + return StrUtil.containsIgnoreCase(t.getName(false), table.getTableName()); + }).toList(); - // 根据 page 进行分页 - List
records = CollUtil.page((int) page.getCurrent() - 1, (int) page.getSize(), tableList); - page.setTotal(tableList.size()); - page.setRecords(records); - return page; - } + // 根据 page 进行分页 + List
records = CollUtil.page((int) page.getCurrent() - 1, (int) page.getSize(), tableList); + page.setTotal(tableList.size()); + page.setRecords(records); + return page; + } - /** - * 查询数据源里面的全部表 - * - * @param dsName 数据源名称 - * @return table - */ - @Override - public List queryTableList(String dsName) { - // 手动切换数据源 - DynamicDataSourceContextHolder.push(dsName); - return ServiceProxy.metadata().tables().values().stream().map(Table::getName).toList(); - } + /** + * 查询数据源里面的全部表 + * @param dsName 数据源名称 + * @return table + */ + @Override + public List queryTableList(String dsName) { + // 手动切换数据源 + DynamicDataSourceContextHolder.push(dsName); + return ServiceProxy.metadata().tables().values().stream().map(Table::getName).toList(); + } - /** - * 查询表信息(列),然后插入到中间表中 - * - * @param dsName 数据源 - * @param tableName 表名 - * @return GenTable - */ - @Override - public GenTable queryOrBuildTable(String dsName, String tableName) { - GenTable genTable = baseMapper.selectOne( - Wrappers.lambdaQuery().eq(GenTable::getTableName, tableName).eq(GenTable::getDsName, dsName)); - // 如果 genTable 为空, 执行导入 - if (Objects.isNull(genTable)) { - genTable = this.tableImport(dsName, tableName); - } + /** + * 查询表信息(列),然后插入到中间表中 + * @param dsName 数据源 + * @param tableName 表名 + * @return GenTable + */ + @Override + public GenTable queryOrBuildTable(String dsName, String tableName) { + GenTable genTable = baseMapper.selectOne( + Wrappers.lambdaQuery().eq(GenTable::getTableName, tableName).eq(GenTable::getDsName, dsName)); + // 如果 genTable 为空, 执行导入 + if (Objects.isNull(genTable)) { + genTable = this.tableImport(dsName, tableName); + } - List fieldList = columnService.list(Wrappers.lambdaQuery() - .eq(GenTableColumnEntity::getDsName, dsName) - .eq(GenTableColumnEntity::getTableName, tableName) - .orderByAsc(GenTableColumnEntity::getSort)); - genTable.setFieldList(fieldList); + List fieldList = columnService.list(Wrappers.lambdaQuery() + .eq(GenTableColumnEntity::getDsName, dsName) + .eq(GenTableColumnEntity::getTableName, tableName) + .orderByAsc(GenTableColumnEntity::getSort)); + genTable.setFieldList(fieldList); - // 查询模板分组信息 - List groupEntities = genGroupService.list(); - genTable.setGroupList(groupEntities); - return genTable; - } + // 查询模板分组信息 + List groupEntities = genGroupService.list(); + genTable.setGroupList(groupEntities); + return genTable; + } - @Transactional(rollbackFor = Exception.class) - protected GenTable tableImport(String dsName, String tableName) { - // 手动切换数据源 - DynamicDataSourceContextHolder.push(dsName); + @Transactional(rollbackFor = Exception.class) + protected GenTable tableImport(String dsName, String tableName) { + // 手动切换数据源 + DynamicDataSourceContextHolder.push(dsName); - // 查询表是否存在 - GenTable table = new GenTable(); - // 从数据库获取表信息 - AnylineService service = ServiceProxy.service(); - Table tableMetadata = service.metadata().table(tableName); - Database database = service.metadata().database(); - // 获取默认表配置信息 () - Map generatorConfig = getGeneratorConfig(); + // 查询表是否存在 + GenTable table = new GenTable(); + // 从数据库获取表信息 + AnylineService service = ServiceProxy.service(); + Table tableMetadata = service.metadata().table(tableName); + Database database = service.metadata().database(); + // 获取默认表配置信息 () + Map generatorConfig = getGeneratorConfig(); - JSONObject project = (JSONObject) generatorConfig.get("project"); - JSONObject developer = (JSONObject) generatorConfig.get("developer"); + JSONObject project = (JSONObject) generatorConfig.get("project"); + JSONObject developer = (JSONObject) generatorConfig.get("developer"); - table.setPackageName(project.getStr("packageName")); - table.setVersion(project.getStr("version")); - table.setBackendPath(project.getStr("backendPath")); - table.setFrontendPath(project.getStr("frontendPath")); - table.setAuthor(developer.getStr("author")); - table.setEmail(developer.getStr("email")); - table.setTableName(tableName); - table.setDsName(dsName); - table.setTableComment(tableMetadata.getComment()); + table.setPackageName(project.getStr("packageName")); + table.setVersion(project.getStr("version")); + table.setBackendPath(project.getStr("backendPath")); + table.setFrontendPath(project.getStr("frontendPath")); + table.setAuthor(developer.getStr("author")); + table.setEmail(developer.getStr("email")); + table.setTableName(tableName); + table.setDsName(dsName); + table.setTableComment(tableMetadata.getComment()); - table.setDbType(database.getDatabase().title()); - table.setFormLayout(FormLayoutEnum.TWO.getValue()); - table.setGeneratorType(GeneratorTypeEnum.ZIP_DOWNLOAD.getValue()); - table.setClassName(NamingCase.toPascalCase(tableName)); - table.setModuleName(GenKit.getModuleName(table.getPackageName())); - table.setFunctionName(GenKit.getFunctionName(tableName)); - table.setCreateTime(LocalDateTime.now()); + table.setDbType(database.getDatabase().title()); + table.setFormLayout(FormLayoutEnum.TWO.getValue()); + table.setGeneratorType(GeneratorTypeEnum.ZIP_DOWNLOAD.getValue()); + table.setClassName(NamingCase.toPascalCase(tableName)); + // 模块名称默认为 admin + table.setModuleName("admin"); + table.setFunctionName(GenKit.getFunctionName(tableName)); + table.setCreateTime(LocalDateTime.now()); - // 使用默认数据源 - DynamicDataSourceContextHolder.clear(); - this.save(table); + // 使用默认数据源 + DynamicDataSourceContextHolder.clear(); + this.save(table); - // 获取原生字段数据 - List tableFieldList = getGenTableColumnEntities(dsName, tableName, tableMetadata); + // 获取原生字段数据 + List tableFieldList = getGenTableColumnEntities(dsName, tableName, tableMetadata); - // 初始化字段数据 - columnService.initFieldList(tableFieldList); - // 保存列数据 - columnService.saveOrUpdateBatch(tableFieldList); + // 初始化字段数据 + columnService.initFieldList(tableFieldList); + // 保存列数据 + columnService.saveOrUpdateBatch(tableFieldList); - table.setFieldList(tableFieldList); - return table; - } + table.setFieldList(tableFieldList); + return table; + } - /** - * 获取表字段信息 - * - * @param dsName 数据源信息 - * @param tableName 表名称 - * @param tableMetadata 表的元数据 - * @return list - */ - private static @NotNull List getGenTableColumnEntities(String dsName, String tableName, Table tableMetadata) { - List tableFieldList = new ArrayList<>(); - LinkedHashMap columns = tableMetadata.getColumns(); - columns.forEach((columnName, column) -> { - GenTableColumnEntity genTableColumnEntity = new GenTableColumnEntity(); - genTableColumnEntity.setTableName(tableName); - genTableColumnEntity.setDsName(dsName); - genTableColumnEntity.setFieldName(column.getName()); - genTableColumnEntity.setFieldComment(column.getComment()); - genTableColumnEntity.setFieldType(column.getTypeName()); - genTableColumnEntity.setPrimaryPk(column.isPrimaryKey() == 1 ? BoolFillEnum.TRUE.getValue() : BoolFillEnum.FALSE.getValue()); - genTableColumnEntity.setAutoFill(AutoFillEnum.DEFAULT.name()); - genTableColumnEntity.setFormItem(BoolFillEnum.TRUE.getValue()); - genTableColumnEntity.setGridItem(BoolFillEnum.TRUE.getValue()); + /** + * 获取表字段信息 + * @param dsName 数据源信息 + * @param tableName 表名称 + * @param tableMetadata 表的元数据 + * @return list + */ + private static @NotNull List getGenTableColumnEntities(String dsName, String tableName, + Table tableMetadata) { + List tableFieldList = new ArrayList<>(); + LinkedHashMap columns = tableMetadata.getColumns(); + columns.forEach((columnName, column) -> { + GenTableColumnEntity genTableColumnEntity = new GenTableColumnEntity(); + genTableColumnEntity.setTableName(tableName); + genTableColumnEntity.setDsName(dsName); + genTableColumnEntity.setFieldName(column.getName()); + genTableColumnEntity.setFieldComment(column.getComment()); + genTableColumnEntity.setFieldType(column.getTypeName()); + genTableColumnEntity.setPrimaryPk( + column.isPrimaryKey() == 1 ? BoolFillEnum.TRUE.getValue() : BoolFillEnum.FALSE.getValue()); + genTableColumnEntity.setAutoFill(AutoFillEnum.DEFAULT.name()); + genTableColumnEntity.setFormItem(BoolFillEnum.TRUE.getValue()); + genTableColumnEntity.setGridItem(BoolFillEnum.TRUE.getValue()); - // 审计字段处理 - if (EnumUtil.contains(CommonColumnFiledEnum.class, column.getName())) { - CommonColumnFiledEnum commonColumnFiledEnum = CommonColumnFiledEnum.valueOf(column.getName()); - genTableColumnEntity.setFormItem(commonColumnFiledEnum.getFormItem()); - genTableColumnEntity.setGridItem(commonColumnFiledEnum.getGridItem()); - genTableColumnEntity.setAutoFill(commonColumnFiledEnum.getAutoFill()); - genTableColumnEntity.setSort(commonColumnFiledEnum.getSort()); - } - tableFieldList.add(genTableColumnEntity); - }); - return tableFieldList; - } + // 审计字段处理 + if (EnumUtil.contains(CommonColumnFiledEnum.class, column.getName())) { + CommonColumnFiledEnum commonColumnFiledEnum = CommonColumnFiledEnum.valueOf(column.getName()); + genTableColumnEntity.setFormItem(commonColumnFiledEnum.getFormItem()); + genTableColumnEntity.setGridItem(commonColumnFiledEnum.getGridItem()); + genTableColumnEntity.setAutoFill(commonColumnFiledEnum.getAutoFill()); + genTableColumnEntity.setSort(commonColumnFiledEnum.getSort()); + } + tableFieldList.add(genTableColumnEntity); + }); + return tableFieldList; + } } diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenTemplateServiceImpl.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenTemplateServiceImpl.java index f0e95ac95..9ec494f0b 100644 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenTemplateServiceImpl.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenTemplateServiceImpl.java @@ -22,6 +22,7 @@ import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpStatus; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; +import cn.smallbun.screw.core.constant.DefaultConstants; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.pig4cloud.pigx.codegen.entity.GenGroupEntity; @@ -54,8 +55,6 @@ import java.util.Set; public class GenTemplateServiceImpl extends ServiceImpl implements GenTemplateService { - private static final String URL = "https://git.pig4cloud.com/pig/CGTM/raw/master"; - private final GenTemplateGroupMapper genTemplateGroupMapper; private final GenGroupMapper genGroupMapper; @@ -138,7 +137,7 @@ public class GenTemplateServiceImpl extends ServiceImpl