From 5c724900e300929567d74bd83345bd2427edd38e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= <2270033969@qq.com> Date: Fri, 16 Aug 2024 15:57:55 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90):?= =?UTF-8?q?=20=E9=80=82=E9=85=8Danyline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pigx-visual/pigx-codegen/pom.xml | 2 +- .../controller/GenDsConfController.java | 171 ++++++----- .../controller/GenTableController.java | 225 +++++++-------- .../pigx/codegen/entity/GenCreateTable.java | 116 ++++---- .../codegen/entity/GenTemplateEntity.java | 116 ++++---- .../service/impl/GeneratorServiceImpl.java | 273 ++++++++++++++---- .../pig4cloud/pigx/codegen/util/GenKit.java | 35 +-- 7 files changed, 535 insertions(+), 403 deletions(-) diff --git a/pigx-visual/pigx-codegen/pom.xml b/pigx-visual/pigx-codegen/pom.xml index 03fa294d4..6acaeca91 100755 --- a/pigx-visual/pigx-codegen/pom.xml +++ b/pigx-visual/pigx-codegen/pom.xml @@ -6,7 +6,7 @@ com.pig4cloud pigx-visual - 5.5.0 + 5.6.0-SNAPSHOT pigx-codegen diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenDsConfController.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenDsConfController.java index d71c980c4..4497684f3 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenDsConfController.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenDsConfController.java @@ -49,104 +49,97 @@ import javax.sql.DataSource; @RequestMapping("/dsconf") public class GenDsConfController { - private final GenDatasourceConfService datasourceConfService; + private final GenDatasourceConfService datasourceConfService; - private final Screw screw; + private final Screw screw; - /** - * 分页查询 - * - * @param page 分页对象 - * @param datasourceConf 数据源表 - * @return - */ - @GetMapping("/page") - public R getSysDatasourceConfPage(Page page, GenDatasourceConf datasourceConf) { - return R.ok(datasourceConfService.page(page, - Wrappers.lambdaQuery() - .like(StrUtil.isNotBlank(datasourceConf.getDsName()), GenDatasourceConf::getDsName, - datasourceConf.getDsName()))); - } + /** + * 分页查询 + * @param page 分页对象 + * @param datasourceConf 数据源表 + * @return + */ + @GetMapping("/page") + public R getSysDatasourceConfPage(Page page, GenDatasourceConf datasourceConf) { + return R.ok(datasourceConfService.page(page, + Wrappers.lambdaQuery() + .like(StrUtil.isNotBlank(datasourceConf.getDsName()), GenDatasourceConf::getDsName, + datasourceConf.getDsName()))); + } - /** - * 查询全部数据源 - * - * @return - */ - @GetMapping("/list") - @Inner(value = false) - public R list() { - return R.ok(datasourceConfService.list()); - } + /** + * 查询全部数据源 + * @return + */ + @GetMapping("/list") + @Inner(value = false) + public R list() { + return R.ok(datasourceConfService.list()); + } - /** - * 通过id查询数据源表 - * - * @param id id - * @return R - */ - @GetMapping("/{id}") - public R getById(@PathVariable("id") Long id) { - return R.ok(datasourceConfService.getById(id)); - } + /** + * 通过id查询数据源表 + * @param id id + * @return R + */ + @GetMapping("/{id}") + public R getById(@PathVariable("id") Long id) { + return R.ok(datasourceConfService.getById(id)); + } - /** - * 新增数据源表 - * - * @param datasourceConf 数据源表 - * @return R - */ - @PostMapping - @XssCleanIgnore - public R save(@RequestBody GenDatasourceConf datasourceConf) { - return R.ok(datasourceConfService.saveDsByEnc(datasourceConf)); - } + /** + * 新增数据源表 + * @param datasourceConf 数据源表 + * @return R + */ + @PostMapping + @XssCleanIgnore + public R save(@RequestBody GenDatasourceConf datasourceConf) { + return R.ok(datasourceConfService.saveDsByEnc(datasourceConf)); + } - /** - * 修改数据源表 - * - * @param conf 数据源表 - * @return R - */ - @PutMapping - @XssCleanIgnore - public R updateById(@RequestBody GenDatasourceConf conf) { - return R.ok(datasourceConfService.updateDsByEnc(conf)); - } + /** + * 修改数据源表 + * @param conf 数据源表 + * @return R + */ + @PutMapping + @XssCleanIgnore + public R updateById(@RequestBody GenDatasourceConf conf) { + return R.ok(datasourceConfService.updateDsByEnc(conf)); + } - /** - * 通过id删除数据源表 - * - * @param ids id - * @return R - */ - @DeleteMapping - public R removeById(@RequestBody Long[] ids) { - return R.ok(datasourceConfService.removeByDsId(ids)); - } + /** + * 通过id删除数据源表 + * @param ids id + * @return R + */ + @DeleteMapping + public R removeById(@RequestBody Long[] ids) { + return R.ok(datasourceConfService.removeByDsId(ids)); + } - /** - * 查询数据源对应的文档 - * - * @param dsName 数据源名称 - */ - @SneakyThrows - @GetMapping("/doc") - public void generatorDoc(String dsName, HttpServletResponse response) { - // 设置指定的数据源 - DynamicRoutingDataSource dynamicRoutingDataSource = SpringContextHolder.getBean(DynamicRoutingDataSource.class); - DynamicDataSourceContextHolder.push(dsName); - DataSource dataSource = dynamicRoutingDataSource.determineDataSource(); + /** + * 查询数据源对应的文档 + * @param dsName 数据源名称 + */ + @SneakyThrows + @GetMapping("/doc") + public void generatorDoc(String dsName, HttpServletResponse response) { + // 设置指定的数据源 + DynamicRoutingDataSource dynamicRoutingDataSource = SpringContextHolder.getBean(DynamicRoutingDataSource.class); + DynamicDataSourceContextHolder.push(dsName); + DataSource dataSource = dynamicRoutingDataSource.determineDataSource(); - // 设置指定的目标表 - ScrewProperties screwProperties = SpringContextHolder.getBean(ScrewProperties.class); + // 设置指定的目标表 + ScrewProperties screwProperties = SpringContextHolder.getBean(ScrewProperties.class); - // 生成 - byte[] data = screw.documentGeneration(dsName, dataSource, screwProperties).toByteArray(); - response.reset(); - response.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(data.length)); - response.setContentType("application/octet-stream"); - IoUtil.write(response.getOutputStream(), Boolean.FALSE, data); - } + // 生成 + byte[] data = screw.documentGeneration(dsName, dataSource, screwProperties).toByteArray(); + response.reset(); + response.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(data.length)); + response.setContentType("application/octet-stream"); + IoUtil.write(response.getOutputStream(), Boolean.FALSE, data); + } } diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenTableController.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenTableController.java index ce5ef4687..2d112f644 100644 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenTableController.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenTableController.java @@ -48,134 +48,123 @@ import java.util.List; @SecurityRequirement(name = HttpHeaders.AUTHORIZATION) public class GenTableController { - private final GenTableColumnService tableColumnService; + private final GenTableColumnService tableColumnService; - private final GenTableService tableService; + private final GenTableService tableService; - /** - * 分页查询 - * - * @param page 分页对象 - * @param table 列属性 - * @return - */ - @Operation(summary = "分页查询", description = "分页查询") - @GetMapping("/page") - public R getTablePage(Page page, GenTable table) { - return R.ok(tableService.queryTablePage(page, table)); - } + /** + * 分页查询 + * @param page 分页对象 + * @param table 列属性 + * @return + */ + @Operation(summary = "分页查询", description = "分页查询") + @GetMapping("/page") + public R getTablePage(Page page, GenTable table) { + return R.ok(tableService.queryTablePage(page, table)); + } - /** - * 通过id查询表信息(代码生成设置 + 表 + 字段设置) - * - * @param id id - * @return R - */ - @Operation(summary = "通过id查询", description = "通过id查询") - @GetMapping("/{id}") - public R getTable(@PathVariable("id") Long id) { - return R.ok(tableService.getById(id)); - } + /** + * 通过id查询表信息(代码生成设置 + 表 + 字段设置) + * @param id id + * @return R + */ + @Operation(summary = "通过id查询", description = "通过id查询") + @GetMapping("/{id}") + public R getTable(@PathVariable("id") Long id) { + return R.ok(tableService.getById(id)); + } - /** - * 查询数据源所有表 - * - * @param dsName 数据源 - */ - @GetMapping("/list/{dsName}") - public R listTable(@PathVariable("dsName") String dsName) { - return R.ok(tableService.queryTableList(dsName)); - } + /** + * 查询数据源所有表 + * @param dsName 数据源 + */ + @GetMapping("/list/{dsName}") + public R listTable(@PathVariable("dsName") String dsName) { + return R.ok(tableService.queryTableList(dsName)); + } - /** - * 获取表信息 - * - * @param dsName 数据源 - * @param tableName 表名称 - */ - @GetMapping("/{dsName}/{tableName}") - public R getTable(@PathVariable("dsName") String dsName, @PathVariable String tableName) { - return R.ok(tableService.queryOrBuildTable(dsName, tableName)); - } + /** + * 获取表信息 + * @param dsName 数据源 + * @param tableName 表名称 + */ + @GetMapping("/{dsName}/{tableName}") + public R getTable(@PathVariable("dsName") String dsName, @PathVariable String tableName) { + return R.ok(tableService.queryOrBuildTable(dsName, tableName)); + } - /** - * 查询表DDL语句 - * - * @param dsName 数据源 - * @param tableName 表名称 - */ - @GetMapping("/column/{dsName}/{tableName}") - public R getColumn(@PathVariable("dsName") String dsName, @PathVariable String tableName) throws Exception { - return R.ok(tableService.queryTableColumn(dsName, tableName)); - } + /** + * 查询表DDL语句 + * @param dsName 数据源 + * @param tableName 表名称 + */ + @GetMapping("/column/{dsName}/{tableName}") + public R getColumn(@PathVariable("dsName") String dsName, @PathVariable String tableName) throws Exception { + return R.ok(tableService.queryTableColumn(dsName, tableName)); + } - /** - * 查询表DDL语句 - * - * @param dsName 数据源 - * @param tableName 表名称 - */ - @GetMapping("/ddl/{dsName}/{tableName}") - public R getDdl(@PathVariable("dsName") String dsName, @PathVariable String tableName) throws Exception { - return R.ok(tableService.queryTableDdl(dsName, tableName)); - } + /** + * 查询表DDL语句 + * @param dsName 数据源 + * @param tableName 表名称 + */ + @GetMapping("/ddl/{dsName}/{tableName}") + public R getDdl(@PathVariable("dsName") String dsName, @PathVariable String tableName) throws Exception { + return R.ok(tableService.queryTableDdl(dsName, tableName)); + } + /** + * 同步表信息 + * @param dsName 数据源 + * @param tableName 表名称 + */ + @GetMapping("/sync/{dsName}/{tableName}") + public R syncTable(@PathVariable("dsName") String dsName, @PathVariable String tableName) { + // 表配置删除 + tableService.remove( + Wrappers.lambdaQuery().eq(GenTable::getDsName, dsName).eq(GenTable::getTableName, tableName)); + // 字段配置删除 + tableColumnService.remove(Wrappers.lambdaQuery() + .eq(GenTableColumnEntity::getDsName, dsName) + .eq(GenTableColumnEntity::getTableName, tableName)); + return R.ok(tableService.queryOrBuildTable(dsName, tableName)); + } - /** - * 同步表信息 - * - * @param dsName 数据源 - * @param tableName 表名称 - */ - @GetMapping("/sync/{dsName}/{tableName}") - public R syncTable(@PathVariable("dsName") String dsName, @PathVariable String tableName) { - // 表配置删除 - tableService.remove( - Wrappers.lambdaQuery().eq(GenTable::getDsName, dsName).eq(GenTable::getTableName, tableName)); - // 字段配置删除 - tableColumnService.remove(Wrappers.lambdaQuery() - .eq(GenTableColumnEntity::getDsName, dsName) - .eq(GenTableColumnEntity::getTableName, tableName)); - return R.ok(tableService.queryOrBuildTable(dsName, tableName)); - } + /** + * 修改列属性 + * @param table 列属性 + * @return R + */ + @Operation(summary = "修改列属性", description = "修改列属性") + @SysLog("修改列属性") + @PutMapping + public R updateById(@RequestBody GenTable table) { + return R.ok(tableService.updateById(table)); + } - /** - * 修改列属性 - * @param table 列属性 - * @return R - */ - @Operation(summary = "修改列属性", description = "修改列属性") - @SysLog("修改列属性") - @PutMapping - public R updateById(@RequestBody GenTable table) { - return R.ok(tableService.updateById(table)); - } - - /** - * 修改表字段数据 - * - * @param dsName 数据源 - * @param tableName 表名称 - * @param tableFieldList 字段列表 - */ - @PutMapping("/field/{dsName}/{tableName}") - public R updateTableField(@PathVariable("dsName") String dsName, @PathVariable String tableName, - @RequestBody List tableFieldList) { - tableColumnService.updateTableField(dsName, tableName, tableFieldList); - return R.ok(); - } - - /** - * 导出excel 表格 - * - * @param table 查询条件 - * @return excel 文件流 - */ - @ResponseExcel - @GetMapping("/export") - public List export(GenTable table) { - return tableService.list(Wrappers.query(table)); - } + /** + * 修改表字段数据 + * @param dsName 数据源 + * @param tableName 表名称 + * @param tableFieldList 字段列表 + */ + @PutMapping("/field/{dsName}/{tableName}") + public R updateTableField(@PathVariable("dsName") String dsName, @PathVariable String tableName, + @RequestBody List tableFieldList) { + tableColumnService.updateTableField(dsName, tableName, tableFieldList); + return R.ok(); + } + /** + * 导出excel 表格 + * @param table 查询条件 + * @return excel 文件流 + */ + @ResponseExcel + @GetMapping("/export") + public List export(GenTable table) { + return tableService.list(Wrappers.query(table)); + } } diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenCreateTable.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenCreateTable.java index 21957da29..e9680e669 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenCreateTable.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenCreateTable.java @@ -38,73 +38,73 @@ import java.time.LocalDateTime; @Schema(description = "自动创建表管理") public class GenCreateTable extends Model { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - /** - * 主键ID - */ - @TableId(type = IdType.ASSIGN_ID) - @Schema(description = "主键ID") - private Long id; + /** + * 主键ID + */ + @TableId(type = IdType.ASSIGN_ID) + @Schema(description = "主键ID") + private Long id; - /** - * 数据源名称 - */ - @Schema(description = "数据源名称") - private String dsName; + /** + * 数据源名称 + */ + @Schema(description = "数据源名称") + private String dsName; - /** - * 表名称 - */ - @Schema(description = "表名称") - private String tableName; + /** + * 表名称 + */ + @Schema(description = "表名称") + private String tableName; - /** - * 表注释 - */ - @Schema(description = "表注释") - private String comments; + /** + * 表注释 + */ + @Schema(description = "表注释") + private String comments; - /** - * 字段信息 - */ - @Schema(description = "字段信息") - private String columnInfo; + /** + * 字段信息 + */ + @Schema(description = "字段信息") + private String columnInfo; - /** - * 创建人 - */ - @TableField(fill = FieldFill.INSERT) - @Schema(description = "创建人") - private String createBy; + /** + * 创建人 + */ + @TableField(fill = FieldFill.INSERT) + @Schema(description = "创建人") + private String createBy; - /** - * 修改人 - */ - @TableField(fill = FieldFill.UPDATE) - @Schema(description = "修改人") - private String updateBy; + /** + * 修改人 + */ + @TableField(fill = FieldFill.UPDATE) + @Schema(description = "修改人") + private String updateBy; - /** - * 创建时间 - */ - @Schema(description = "创建时间") - @TableField(fill = FieldFill.INSERT) - private LocalDateTime createTime; + /** + * 创建时间 + */ + @Schema(description = "创建时间") + @TableField(fill = FieldFill.INSERT) + private LocalDateTime createTime; - /** - * 修改时间 - */ - @Schema(description = "修改时间") - @TableField(fill = FieldFill.UPDATE) - private LocalDateTime updateTime; + /** + * 修改时间 + */ + @Schema(description = "修改时间") + @TableField(fill = FieldFill.UPDATE) + private LocalDateTime updateTime; - /** - * 删除标识(0-正常,1-删除) - */ - @TableLogic - @TableField(fill = FieldFill.INSERT) - @Schema(description = "删除标记,1:已删除,0:正常") - private String delFlag; + /** + * 删除标识(0-正常,1-删除) + */ + @TableLogic + @TableField(fill = FieldFill.INSERT) + @Schema(description = "删除标记,1:已删除,0:正常") + private String delFlag; } diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenTemplateEntity.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenTemplateEntity.java index 557f9181b..f979a8b04 100644 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenTemplateEntity.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenTemplateEntity.java @@ -39,73 +39,73 @@ import java.time.LocalDateTime; @Schema(description = "模板") public class GenTemplateEntity extends Model { - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1L; - /** - * 主键 - */ - @TableId(type = IdType.ASSIGN_ID) - @Schema(description = "主键") - private Long id; + /** + * 主键 + */ + @TableId(type = IdType.ASSIGN_ID) + @Schema(description = "主键") + private Long id; - /** - * 模板名称 - */ - @Schema(description = "模板名称") - private String templateName; + /** + * 模板名称 + */ + @Schema(description = "模板名称") + private String templateName; - /** - * 模板路径 - */ - @Schema(description = "模板路径") - private String generatorPath; + /** + * 模板路径 + */ + @Schema(description = "模板路径") + private String generatorPath; - /** - * 模板描述 - */ - @Schema(description = "模板描述") - private String templateDesc; + /** + * 模板描述 + */ + @Schema(description = "模板描述") + private String templateDesc; - /** - * 模板代码 - */ - @Schema(description = "模板代码") - private String templateCode; + /** + * 模板代码 + */ + @Schema(description = "模板代码") + private String templateCode; - /** - * 创建人 - */ - @TableField(fill = FieldFill.INSERT) - @Schema(description = "创建人") - private String createBy; + /** + * 创建人 + */ + @TableField(fill = FieldFill.INSERT) + @Schema(description = "创建人") + private String createBy; - /** - * 修改人 - */ - @TableField(fill = FieldFill.UPDATE) - @Schema(description = "修改人") - private String updateBy; + /** + * 修改人 + */ + @TableField(fill = FieldFill.UPDATE) + @Schema(description = "修改人") + private String updateBy; - /** - * 创建时间 - */ - @Schema(description = "创建时间") - @TableField(fill = FieldFill.INSERT) - private LocalDateTime createTime; + /** + * 创建时间 + */ + @Schema(description = "创建时间") + @TableField(fill = FieldFill.INSERT) + private LocalDateTime createTime; - /** - * 修改时间 - */ - @Schema(description = "修改时间") - @TableField(fill = FieldFill.UPDATE) - private LocalDateTime updateTime; + /** + * 修改时间 + */ + @Schema(description = "修改时间") + @TableField(fill = FieldFill.UPDATE) + private LocalDateTime updateTime; - /** - * 删除标识(0-正常,1-删除) - */ - @TableLogic - @TableField(fill = FieldFill.INSERT) - @Schema(description = "删除标记,1:已删除,0:正常") - private String delFlag; + /** + * 删除标识(0-正常,1-删除) + */ + @TableLogic + @TableField(fill = FieldFill.INSERT) + @Schema(description = "删除标记,1:已删除,0:正常") + private String delFlag; } diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java index a52eb138a..cd58d129c 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java @@ -21,18 +21,31 @@ import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IoUtil; +import cn.hutool.core.map.MapUtil; import cn.hutool.core.text.NamingCase; import cn.hutool.core.util.BooleanUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONObject; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.pig4cloud.pigx.admin.api.entity.SysMenu; +import com.pig4cloud.pigx.admin.api.entity.SysRouteConf; +import com.pig4cloud.pigx.admin.api.feign.RemoteMenuService; +import com.pig4cloud.pigx.admin.api.feign.RemoteRouteConfService; +import com.pig4cloud.pigx.codegen.config.PigxCodeGenDefaultProperties; import com.pig4cloud.pigx.codegen.entity.GenFormConf; import com.pig4cloud.pigx.codegen.entity.GenTable; import com.pig4cloud.pigx.codegen.entity.GenTableColumnEntity; import com.pig4cloud.pigx.codegen.entity.GenTemplateEntity; import com.pig4cloud.pigx.codegen.service.*; +import com.pig4cloud.pigx.codegen.util.DataModelConstants; import com.pig4cloud.pigx.codegen.util.GeneratorStyleEnum; import com.pig4cloud.pigx.codegen.util.VelocityKit; import com.pig4cloud.pigx.codegen.util.vo.GroupVO; +import com.pig4cloud.pigx.common.core.constant.enums.MenuTypeEnum; +import com.pig4cloud.pigx.common.core.constant.enums.YesNoEnum; +import com.pig4cloud.pigx.common.core.exception.CheckedException; +import com.pig4cloud.pigx.common.core.util.R; +import com.pig4cloud.pigx.common.core.util.RetOps; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @@ -55,6 +68,8 @@ import java.util.zip.ZipOutputStream; @RequiredArgsConstructor public class GeneratorServiceImpl implements GeneratorService { + private final PigxCodeGenDefaultProperties defaultProperties; + private final GenTableColumnService columnService; private final GenFormConfService formConfService; @@ -67,6 +82,10 @@ public class GeneratorServiceImpl implements GeneratorService { private final GenTemplateService genTemplateService; + private final RemoteMenuService menuService; + + private final RemoteRouteConfService routeService; + /** * 生成代码zip写出 * @@ -79,22 +98,23 @@ public class GeneratorServiceImpl implements GeneratorService { // 数据模型 Map dataModel = getDataModel(tableId); - Long style = (Long) dataModel.get("style"); + Long style = (Long) dataModel.get(GenTable.Fields.style); GroupVO groupVo = genGroupService.getGroupVoById(style); List templateList = groupVo.getTemplateList(); - Map generatorConfig = tableService.getGeneratorConfig(); - JSONObject project = (JSONObject) generatorConfig.get("project"); - String frontendPath = project.getStr("frontendPath"); - String backendPath = project.getStr("backendPath"); + String frontendPath = defaultProperties.getFrontendPath(); + String backendPath = defaultProperties.getBackendPath(); + + // 同步数据 + this.syncRouteAndMenu(tableId); for (GenTemplateEntity template : templateList) { String templateCode = template.getTemplateCode(); String generatorPath = template.getGeneratorPath(); - dataModel.put("frontendPath", frontendPath); - dataModel.put("backendPath", backendPath); + dataModel.put(GenTable.Fields.frontendPath, frontendPath); + dataModel.put(GenTable.Fields.backendPath, backendPath); String content = VelocityKit.renderStr(templateCode, dataModel); String path = VelocityKit.renderStr(generatorPath, dataModel); @@ -119,34 +139,42 @@ public class GeneratorServiceImpl implements GeneratorService { // 数据模型 Map dataModel = getDataModel(tableId); - Long style = (Long) dataModel.get("style"); + Long style = (Long) dataModel.get(GenTable.Fields.style); // 获取模板列表,Lambda 表达式简化代码 List templateList = genGroupService.getGroupVoById(style).getTemplateList(); - Map generatorConfig = tableService.getGeneratorConfig(); - JSONObject project = (JSONObject) generatorConfig.get("project"); - String frontendPath = project.getStr("frontendPath"); - String backendPath = project.getStr("backendPath"); + String frontendPath = defaultProperties.getFrontendPath(); + String backendPath = defaultProperties.getBackendPath(); + + // 如果是同步菜单的模式则不生成SQL + List> result = new ArrayList<>(); + Long syncMenuId = MapUtil.getLong(dataModel, GenTable.Fields.syncMenuId); + for (GenTemplateEntity template : templateList) { + // 跳过菜单文件生成 + if (Objects.nonNull(syncMenuId) && template.getGeneratorPath().contains("menu.sql")) { + continue; + } - return templateList.stream().map(template -> { String templateCode = template.getTemplateCode(); String generatorPath = template.getGeneratorPath(); // 预览模式下, 使用相对路径展示 - dataModel.put("frontendPath", frontendPath); - dataModel.put("backendPath", backendPath); + dataModel.put(GenTable.Fields.frontendPath, frontendPath); + dataModel.put(GenTable.Fields.backendPath, backendPath); String content = VelocityKit.renderStr(templateCode, dataModel); String path = VelocityKit.renderStr(generatorPath, dataModel); // 使用 map 简化代码 - return new HashMap(4) { + result.add(new HashMap<>(4) { { put("code", content); put("codePath", path); } - }; - }).collect(Collectors.toList()); + }); + } + + return result; } /** @@ -158,18 +186,24 @@ public class GeneratorServiceImpl implements GeneratorService { public void generatorCode(Long tableId) { // 数据模型 Map dataModel = getDataModel(tableId); - Long style = (Long) dataModel.get("style"); + Long style = (Long) dataModel.get(GenTable.Fields.style); // 获取模板列表,Lambda 表达式简化代码 List templateList = genGroupService.getGroupVoById(style).getTemplateList(); + Long syncMenuId = MapUtil.getLong(dataModel, GenTable.Fields.syncMenuId); + this.syncRouteAndMenu(tableId); + for (GenTemplateEntity template : templateList) { + // 跳过菜单文件生成 + if (Objects.nonNull(syncMenuId) && template.getGeneratorPath().contains("menu.sql")) { + continue; + } - templateList.forEach(template -> { String templateCode = template.getTemplateCode(); String generatorPath = template.getGeneratorPath(); String content = VelocityKit.renderStr(templateCode, dataModel); String path = VelocityKit.renderStr(generatorPath, dataModel); FileUtil.writeUtf8String(content, path); - }); + } } /** @@ -189,16 +223,17 @@ public class GeneratorServiceImpl implements GeneratorService { Map dataModel = getDataModel(genTable.getId()); // 获取模板信息,Lambda 表达式简化代码 - GenTemplateEntity genTemplateEntity = Optional - .ofNullable(genTemplateService.getById(GeneratorStyleEnum.VFORM_JSON.getTemplateId())) - .orElseThrow(() -> new Exception("模板不存在")); - + GenTemplateEntity genTemplateEntity = genTemplateService.getOneOpt(Wrappers.lambdaQuery() + .likeRight(GenTemplateEntity::getTemplateName, GeneratorStyleEnum.VFORM_JSON.getDesc()) + .orderByDesc(GenTemplateEntity::getCreateTime), false + ) + .orElseThrow(() -> new CheckedException("模板不存在")); // 渲染模板并返回结果 return VelocityKit.renderStr(genTemplateEntity.getTemplateCode(), dataModel); } /** - * 获取表单设计器需要的 JSON 方法 + * 获取sfc vue * * @param id 表单配置 ID * @return JSON 字符串 @@ -221,15 +256,132 @@ public class GeneratorServiceImpl implements GeneratorService { // 遍历 widgetList dataModel.put("resultMap", resultMap); - // 获取模板信息 - GenTemplateEntity genTemplateEntity = Optional - .ofNullable(genTemplateService.getById(GeneratorStyleEnum.VFORM_FORM.getTemplateId())) - .orElseThrow(() -> new Exception("模板不存在")); + // 获取模板信息 查询模板中最新的 vform.json 文件 + GenTemplateEntity genTemplateEntity = genTemplateService.getOneOpt(Wrappers.lambdaQuery() + .likeRight(GenTemplateEntity::getTemplateName, GeneratorStyleEnum.VFORM_VUE.getDesc()) + .orderByDesc(GenTemplateEntity::getCreateTime), false + ) + .orElseThrow(() -> new CheckedException("模板不存在")); // 渲染模板并返回结果 return VelocityKit.renderStr(genTemplateEntity.getTemplateCode(), dataModel); } + /** + * 同步路由和菜单 + * + * @param tableId 表ID + */ + @Override + public void syncRouteAndMenu(Long tableId) { + GenTable table = tableService.getById(tableId); + syncMenu(table); + syncRoute(table); + } + + /** + * 同步菜单,同步按钮 + * + * @param table 表配置 + */ + private void syncMenu(GenTable table) { + if (Objects.isNull(table.getSyncMenuId())) { + return; + } + + String menuName = String.format("%s管理", table.getTableComment()); + SysMenu query = new SysMenu(); + query.setName(menuName); + query.setMenuType(MenuTypeEnum.LEFT_MENU.getType()); + + List existingMenus = RetOps.of(menuService.getMenuDetails(query)).getData().orElse(Collections.emptyList()); + + if (!CollUtil.isEmpty(existingMenus)) { + return; + } + + SysMenu sysMenu = createMenu(table, menuName); + R sysMenuR = menuService.saveMenu(sysMenu); + + if (sysMenuR.getData() != null) { + createButtons(table, sysMenuR.getData().getMenuId()); + } + } + + /** + * 创建菜单 + * + * @param table 表配置 + * @param menuName 菜单名称 + * @return {@link SysMenu } + */ + private SysMenu createMenu(GenTable table, String menuName) { + SysMenu sysMenu = new SysMenu(); + sysMenu.setParentId(table.getSyncMenuId()); + sysMenu.setName(menuName); + sysMenu.setMenuType(MenuTypeEnum.LEFT_MENU.getType()); + sysMenu.setPath(String.format("/%s/%s/index", table.getModuleName(), table.getFunctionName())); + return sysMenu; + } + + /** + * 创建按钮 + * + * @param table 表配置 + * @param parentId 父 ID + */ + private void createButtons(GenTable table, Long parentId) { + String[] buttonNames = {"查看", "新增", "编辑", "删除", "导入导出"}; + String[] permissions = {"view", "add", "edit", "del", "export"}; + + for (int i = 0; i < buttonNames.length; i++) { + SysMenu button = new SysMenu(); + button.setParentId(parentId); + button.setMenuType(MenuTypeEnum.BUTTON.getType()); + button.setName(buttonNames[i]); + button.setPermission(String.format("%s_%s_%s", table.getModuleName(), table.getFunctionName(), permissions[i])); + menuService.saveMenu(button); + } + } + + /** + * 同步路由 + * + * @param table 表配置 + */ + private void syncRoute(GenTable table) { + if (!YesNoEnum.YES.getCode().equals(table.getSyncRoute())) { + return; + } + + List existingRoutes = RetOps.of(routeService.getRouteDetails()).getData().orElse(Collections.emptyList()); + boolean exist = existingRoutes.stream() + .anyMatch(routeConf -> routeConf.getRouteId().equals(table.getModuleName())); + + if (exist) { + return; + } + + SysRouteConf sysRouteConf = createRoute(table); + routeService.saveSysRouteConf(sysRouteConf); + } + + /** + * 创建路由 + * + * @param table 表配置 + * @return {@link SysRouteConf } + */ + private SysRouteConf createRoute(GenTable table) { + SysRouteConf sysRouteConf = new SysRouteConf(); + sysRouteConf.setRouteId(table.getModuleName()); + sysRouteConf.setRouteName(table.getModuleName()); + sysRouteConf.setPredicates(String.format("[{\"args\": {\"_genkey_0\": \"/%s/**\"}, \"name\": \"Path\"}]", table.getModuleName())); + sysRouteConf.setFilters("[]"); + sysRouteConf.setUri(String.format("lb://%s-biz", table.getModuleName())); + return sysRouteConf; + } + /** * 通过 Lambda 表达式优化的获取数据模型方法 * @@ -252,33 +404,34 @@ public class GeneratorServiceImpl implements GeneratorService { Map dataModel = new HashMap<>(); // 填充数据模型 - dataModel.put("isSpringBoot3", isSpringBoot3()); - dataModel.put("dbType", table.getDbType()); - dataModel.put("package", table.getPackageName()); - dataModel.put("packagePath", table.getPackageName().replace(".", "/")); - dataModel.put("version", table.getVersion()); - dataModel.put("moduleName", table.getModuleName()); - dataModel.put("ModuleName", StrUtil.upperFirst(table.getModuleName())); - dataModel.put("functionName", table.getFunctionName()); - dataModel.put("FunctionName", StrUtil.upperFirst(table.getFunctionName())); - dataModel.put("formLayout", table.getFormLayout()); - dataModel.put("style", table.getStyle()); - dataModel.put("author", table.getAuthor()); - dataModel.put("datetime", DateUtil.now()); - dataModel.put("date", DateUtil.today()); + dataModel.put(DataModelConstants.IS_SPRING_BOOT_3, isSpringBoot3()); + dataModel.put(DataModelConstants.SYNC_MENU_ID, Objects.nonNull(table.getSyncMenuId())); + dataModel.put(DataModelConstants.DB_TYPE, table.getDbType()); + dataModel.put(DataModelConstants.PACKAGE, table.getPackageName()); + dataModel.put(DataModelConstants.PACKAGE_PATH, table.getPackageName().replace(".", "/")); + dataModel.put(DataModelConstants.VERSION, table.getVersion()); + dataModel.put(DataModelConstants.MODULE_NAME, table.getModuleName()); + dataModel.put(DataModelConstants.MODULE_NAME_UPPER_FIRST, StrUtil.upperFirst(table.getModuleName())); + dataModel.put(DataModelConstants.FUNCTION_NAME, table.getFunctionName()); + dataModel.put(DataModelConstants.FUNCTION_NAME_UPPER_FIRST, StrUtil.upperFirst(table.getFunctionName())); + dataModel.put(DataModelConstants.FORM_LAYOUT, table.getFormLayout()); + dataModel.put(DataModelConstants.STYLE, table.getStyle()); + dataModel.put(DataModelConstants.AUTHOR, table.getAuthor()); + dataModel.put(DataModelConstants.DATETIME, DateUtil.now()); + dataModel.put(DataModelConstants.DATE, DateUtil.today()); setFieldTypeList(dataModel, table); // 获取导入的包列表 Set importList = fieldTypeService.getPackageByTableId(table.getDsName(), table.getTableName()); - dataModel.put("importList", importList); - dataModel.put("tableName", table.getTableName()); - dataModel.put("tableComment", table.getTableComment()); - dataModel.put("className", StrUtil.lowerFirst(table.getClassName())); - dataModel.put("ClassName", table.getClassName()); - dataModel.put("fieldList", table.getFieldList()); + dataModel.put(DataModelConstants.IMPORT_LIST, importList); + dataModel.put(DataModelConstants.TABLE_NAME, table.getTableName()); + dataModel.put(DataModelConstants.TABLE_COMMENT, table.getTableComment()); + dataModel.put(DataModelConstants.CLASS_NAME, StrUtil.lowerFirst(table.getClassName())); + dataModel.put(DataModelConstants.CLASS_NAME_UPPER_FIRST, table.getClassName()); + dataModel.put(DataModelConstants.FIELD_LIST, table.getFieldList()); - dataModel.put("backendPath", table.getBackendPath()); - dataModel.put("frontendPath", table.getFrontendPath()); + dataModel.put(DataModelConstants.BACKEND_PATH, table.getBackendPath()); + dataModel.put(DataModelConstants.FRONTEND_PATH, table.getFrontendPath()); // 设置子表 String childTableName = table.getChildTableName(); @@ -287,20 +440,20 @@ public class GeneratorServiceImpl implements GeneratorService { .eq(GenTableColumnEntity::getDsName, table.getDsName()) .eq(GenTableColumnEntity::getTableName, table.getChildTableName()) .list(); - dataModel.put("childFieldList", childFieldList); - dataModel.put("childTableName", childTableName); - dataModel.put("mainField", NamingCase.toCamelCase(table.getMainField())); - dataModel.put("childField", NamingCase.toCamelCase(table.getChildField())); - dataModel.put("ChildClassName", NamingCase.toPascalCase(childTableName)); - dataModel.put("childClassName", StrUtil.lowerFirst(NamingCase.toPascalCase(childTableName))); + dataModel.put(DataModelConstants.CHILD_FIELD_LIST, childFieldList); + dataModel.put(DataModelConstants.CHILD_TABLE_NAME, childTableName); + dataModel.put(DataModelConstants.MAIN_FIELD, NamingCase.toCamelCase(table.getMainField())); + dataModel.put(DataModelConstants.CHILD_FIELD, NamingCase.toCamelCase(table.getChildField())); + dataModel.put(DataModelConstants.CHILD_CLASS_NAME_UPPER_FIRST, NamingCase.toPascalCase(childTableName)); + dataModel.put(DataModelConstants.CHILD_CLASS_NAME, StrUtil.lowerFirst(NamingCase.toPascalCase(childTableName))); // 设置是否是多租户模式 (判断字段列表中是否包含 tenant_id 字段) childFieldList.stream().filter(genTableColumnEntity -> genTableColumnEntity.getFieldName().equals("tenant_id")) - .findFirst().ifPresent(columnEntity -> dataModel.put("isChildTenant", true)); + .findFirst().ifPresent(columnEntity -> dataModel.put(DataModelConstants.IS_CHILD_TENANT, true)); } // 设置是否是多租户模式 (判断字段列表中是否包含 tenant_id 字段) table.getFieldList().stream().filter(genTableColumnEntity -> genTableColumnEntity.getFieldName().equals("tenant_id")) - .findFirst().ifPresent(columnEntity -> dataModel.put("isTenant", true)); + .findFirst().ifPresent(columnEntity -> dataModel.put(DataModelConstants.IS_TENANT, true)); return dataModel; } diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/GenKit.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/GenKit.java index 69d1876bd..f8223d2d8 100644 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/GenKit.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/GenKit.java @@ -12,25 +12,22 @@ import lombok.experimental.UtilityClass; @UtilityClass public class GenKit { - /** - * 获取功能名 sys_a_b sysAb - * - * @param tableName 表名 - * @return 功能名 - */ - public String getFunctionName(String tableName) { - return StrUtil.toCamelCase(tableName); - } - - /** - * 获取模块名称 - * - * @param packageName 包名 - * @return 功能名 - */ - public String getModuleName(String packageName) { - return StrUtil.subAfter(packageName, ".", true); - } + /** + * 获取功能名 sys_a_b sysAb + * @param tableName 表名 + * @return 功能名 + */ + public String getFunctionName(String tableName) { + return StrUtil.toCamelCase(tableName); + } + /** + * 获取模块名称 + * @param packageName 包名 + * @return 功能名 + */ + public String getModuleName(String packageName) { + return StrUtil.subAfter(packageName, ".", true); + } }