mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 10:18:42 +08:00 
			
		
		
		
	1. 同步数据表和字段定义
2. 删除数据库表
This commit is contained in:
		| @@ -15,6 +15,7 @@ import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColum | ||||
| import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; | ||||
| import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaTableDO; | ||||
| import cn.iocoder.dashboard.modules.tool.service.codegen.ToolCodegenService; | ||||
| import cn.iocoder.dashboard.util.collection.CollectionUtils; | ||||
| import cn.iocoder.dashboard.util.servlet.ServletUtils; | ||||
| import io.swagger.annotations.Api; | ||||
| import io.swagger.annotations.ApiImplicitParam; | ||||
| @@ -31,6 +32,7 @@ import java.io.ByteArrayOutputStream; | ||||
| import java.io.IOException; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.Set; | ||||
|  | ||||
| import static cn.iocoder.dashboard.common.pojo.CommonResult.success; | ||||
|  | ||||
| @@ -47,7 +49,7 @@ public class ToolCodegenController { | ||||
|     @GetMapping("/db/table/list") | ||||
|     @ApiImplicitParams({ | ||||
|             @ApiImplicitParam(name = "tableName", required = true, example = "yudao", dataTypeClass = String.class), | ||||
|             @ApiImplicitParam(name = "tableComment", required = true, example = "芋道", dataTypeClass = Long.class) | ||||
|             @ApiImplicitParam(name = "tableComment", required = true, example = "芋道", dataTypeClass = String.class) | ||||
|     }) | ||||
| //    @PreAuthorize("@ss.hasPermi('tool:gen:list')") TODO 权限 | ||||
|     public CommonResult<List<ToolSchemaTableRespVO>> getSchemaTableList( | ||||
| @@ -56,33 +58,35 @@ public class ToolCodegenController { | ||||
|         // 获得数据库自带的表定义列表 | ||||
|         List<ToolSchemaTableDO> schemaTables = codegenService.getSchemaTableList(tableName, tableComment); | ||||
|         // 移除在 Codegen 中,已经存在的 | ||||
|  | ||||
|         return null; | ||||
|         Set<String> existsTables = CollectionUtils.convertSet(codegenService.getCodeGenTableList(), ToolCodegenTableDO::getTableName); | ||||
|         schemaTables.removeIf(table -> existsTables.contains(table.getTableName())); | ||||
|         return success(ToolCodegenConvert.INSTANCE.convertList04(schemaTables)); | ||||
|     } | ||||
|  | ||||
|     @ApiOperation("获得表定义分页") | ||||
|     @GetMapping("/table/page") | ||||
|     // TODO 权限 @PreAuthorize("@ss.hasPermi('tool:gen:list')") | ||||
|     public CommonResult<PageResult<ToolCodegenTableRespVO>> getCodeGenTablePage(@Valid ToolCodegenTablePageReqVO pageReqVO) { | ||||
|         PageResult<ToolCodegenTableDO> pageResult = codegenService.getCodeGenTablePage(pageReqVO); | ||||
|         PageResult<ToolCodegenTableDO> pageResult = codegenService.getCodegenTablePage(pageReqVO); | ||||
|         return success(ToolCodegenConvert.INSTANCE.convertPage(pageResult)); | ||||
|     } | ||||
|  | ||||
|     @ApiOperation("获得表和字段的明细") | ||||
|     @GetMapping("/detail") | ||||
|     @ApiImplicitParam(name = "tableId", required = true, example = "表编号", dataTypeClass = Long.class) | ||||
| //   todo @PreAuthorize("@ss.hasPermi('tool:gen:query')") | ||||
|     public CommonResult<ToolCodegenDetailRespVO> getCodeGenDetail(@RequestParam("tableId") Long tableId) { | ||||
|         ToolCodegenTableDO table = codegenService.getCodeGenTablePage(tableId); | ||||
|     public CommonResult<ToolCodegenDetailRespVO> getCodegenDetail(@RequestParam("tableId") Long tableId) { | ||||
|         ToolCodegenTableDO table = codegenService.getCodegenTablePage(tableId); | ||||
|         List<ToolCodegenColumnDO> columns = codegenService.getCodegenColumnListByTableId(tableId); | ||||
|         // 拼装返回 | ||||
|         return success(ToolCodegenConvert.INSTANCE.convert(table, columns)); | ||||
|     } | ||||
|  | ||||
|     @ApiOperation("基于数据库的表结构,创建代码生成器的表定义") | ||||
|     @PostMapping("/create") | ||||
|     @ApiOperation("基于数据库的表结构,创建代码生成器的表和字段定义") | ||||
|     @PostMapping("/create-list") | ||||
|     // TODO 权限 | ||||
|     public CommonResult<Long> createCodeGen(@RequestParam("tableName") String tableName) { | ||||
|         return success(codegenService.createCodegen(tableName)); | ||||
|     public CommonResult<List<Long>> createCodegenList(@RequestParam("tableNames") List<String> tableNames) { | ||||
|         return success(codegenService.createCodeGenList(tableNames)); | ||||
|     } | ||||
|  | ||||
|     @ApiOperation("更新数据库的表和字段定义") | ||||
| @@ -93,6 +97,24 @@ public class ToolCodegenController { | ||||
|         return success(true); | ||||
|     } | ||||
|  | ||||
|     @ApiOperation("基于数据库的表结构,同步数据库的表和字段定义") | ||||
|     @PutMapping("/sync") | ||||
|     @ApiImplicitParam(name = "tableId", required = true, example = "表编号", dataTypeClass = Long.class) | ||||
| //    @PreAuthorize("@ss.hasPermi('tool:gen:edit')") TODO 权限 | ||||
|     public CommonResult<Boolean> syncCodegen(@RequestParam("tableId") Long tableId) { | ||||
|         codegenService.syncCodegen(tableId); | ||||
|         return success(true); | ||||
|     } | ||||
|  | ||||
|     @ApiOperation("删除数据库的表和字段定义") | ||||
|     @DeleteMapping("/delete") | ||||
|     @ApiImplicitParam(name = "tableId", required = true, example = "表编号", dataTypeClass = Long.class) | ||||
| //    @PreAuthorize("@ss.hasPermi('tool:gen:remove')") TODO 权限 | ||||
|     public CommonResult<Boolean> deleteCodegen(@RequestParam("tableId") Long tableId) { | ||||
|         codegenService.deleteCodegen(tableId); | ||||
|         return success(true); | ||||
|     } | ||||
|  | ||||
|     @ApiOperation("预览生成代码") | ||||
|     @GetMapping("/preview") | ||||
|     @ApiImplicitParam(name = "tableId", required = true, example = "表编号", dataTypeClass = Long.class) | ||||
| @@ -119,4 +141,18 @@ public class ToolCodegenController { | ||||
|         ServletUtils.writeAttachment(response, "codegen.zip", outputStream.toByteArray()); | ||||
|     } | ||||
|  | ||||
| //    /** | ||||
| //     * 查询数据表字段列表 | ||||
| //     */ | ||||
| //    @PreAuthorize("@ss.hasPermi('tool:gen:list')") | ||||
| //    @GetMapping(value = "/column/{talbleId}") | ||||
| //    public TableDataInfo columnList(Long tableId) { | ||||
| //        TableDataInfo dataInfo = new TableDataInfo(); | ||||
| //        List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId); | ||||
| //        dataInfo.setRows(list); | ||||
| //        dataInfo.setTotal(list.size()); | ||||
| //        return dataInfo; | ||||
| //    } | ||||
| // | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -6,6 +6,7 @@ import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenPrevie | ||||
| import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.ToolCodegenUpdateReqVO; | ||||
| import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.column.ToolCodegenColumnRespVO; | ||||
| import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.table.ToolCodegenTableRespVO; | ||||
| import cn.iocoder.dashboard.modules.tool.controller.codegen.vo.table.ToolSchemaTableRespVO; | ||||
| import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenColumnDO; | ||||
| import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolCodegenTableDO; | ||||
| import cn.iocoder.dashboard.modules.tool.dal.dataobject.codegen.ToolSchemaColumnDO; | ||||
| @@ -46,6 +47,8 @@ public interface ToolCodegenConvert { | ||||
|  | ||||
|     List<ToolCodegenColumnDO> convertList03(List<ToolCodegenUpdateReqVO.Column> columns); | ||||
|  | ||||
|     List<ToolSchemaTableRespVO> convertList04(List<ToolSchemaTableDO> list); | ||||
|  | ||||
|     // ========== 其它 ========== | ||||
|  | ||||
|     default ToolCodegenDetailRespVO convert(ToolCodegenTableDO table, List<ToolCodegenColumnDO> columns) { | ||||
|   | ||||
| @@ -11,9 +11,12 @@ import java.util.List; | ||||
| public interface ToolCodegenColumnMapper extends BaseMapperX<ToolCodegenColumnDO> { | ||||
|  | ||||
|     default List<ToolCodegenColumnDO> selectListByTableId(Long tableId) { | ||||
|         return selectList(new QueryWrapper<ToolCodegenColumnDO>() | ||||
|                 .eq("table_id", tableId) | ||||
|         return selectList(new QueryWrapper<ToolCodegenColumnDO>().eq("table_id", tableId) | ||||
|                 .orderByAsc("ordinal_position")); | ||||
|     } | ||||
|  | ||||
|     default void deleteListByTableId(Long tableId) { | ||||
|         delete(new QueryWrapper<ToolCodegenColumnDO>().eq("table_id", tableId)); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -14,8 +14,8 @@ public interface ToolSchemaTableMapper extends BaseMapperX<ToolSchemaTableDO> { | ||||
|  | ||||
|     default List<ToolSchemaTableDO> selectList(Collection<String> tableSchemas, String tableName, String tableComment) { | ||||
|         return selectList(new QueryWrapperX<ToolSchemaTableDO>().in("table_schema", tableSchemas) | ||||
|                 .eqIfPresent("table_name", tableName) | ||||
|                 .eqIfPresent("table_comment", tableComment)); | ||||
|                 .likeIfPresent("table_name", tableName) | ||||
|                 .likeIfPresent("table_comment", tableComment)); | ||||
|     } | ||||
|  | ||||
|     default List<ToolSchemaTableDO> selectListByTableSchema(String tableSchema) { | ||||
|   | ||||
| @@ -21,10 +21,18 @@ public interface ToolCodegenService { | ||||
|      * 基于数据库的表结构,创建代码生成器的表定义 | ||||
|      * | ||||
|      * @param tableName 表名称 | ||||
|      * @return 表定义的编号 | ||||
|      * @return 创建的表定义的编号 | ||||
|      */ | ||||
|     Long createCodegen(String tableName); | ||||
|  | ||||
|     /** | ||||
|      * 基于 {@link #createCodegen(String)} 的批量创建 | ||||
|      * | ||||
|      * @param tableNames 表名称数组 | ||||
|      * @return 创建的表定义的编号数组 | ||||
|      */ | ||||
|     List<Long> createCodeGenList(List<String> tableNames); | ||||
|  | ||||
|     /** | ||||
|      * 更新数据库的表和字段定义 | ||||
|      * | ||||
| @@ -32,13 +40,27 @@ public interface ToolCodegenService { | ||||
|      */ | ||||
|     void updateCodegen(ToolCodegenUpdateReqVO updateReqVO); | ||||
|  | ||||
|     /** | ||||
|      * 基于数据库的表结构,同步数据库的表和字段定义 | ||||
|      * | ||||
|      * @param tableId 表编号 | ||||
|      */ | ||||
|     void syncCodegen(Long tableId); | ||||
|  | ||||
|     /** | ||||
|      * 删除数据库的表和字段定义 | ||||
|      * | ||||
|      * @param tableId 数据编号 | ||||
|      */ | ||||
|     void deleteCodegen(Long tableId); | ||||
|  | ||||
|     /** | ||||
|      * 获得表定义分页 | ||||
|      * | ||||
|      * @param pageReqVO 分页条件 | ||||
|      * @return 表定义分页 | ||||
|      */ | ||||
|     PageResult<ToolCodegenTableDO> getCodeGenTablePage(ToolCodegenTablePageReqVO pageReqVO); | ||||
|     PageResult<ToolCodegenTableDO> getCodegenTablePage(ToolCodegenTablePageReqVO pageReqVO); | ||||
|  | ||||
|     /** | ||||
|      * 获得表定义 | ||||
| @@ -46,7 +68,14 @@ public interface ToolCodegenService { | ||||
|      * @param id 表编号 | ||||
|      * @return 表定义 | ||||
|      */ | ||||
|     ToolCodegenTableDO getCodeGenTablePage(Long id); | ||||
|     ToolCodegenTableDO getCodegenTablePage(Long id); | ||||
|  | ||||
|     /** | ||||
|      * 获得全部表定义 | ||||
|      * | ||||
|      * @return 表定义数组 | ||||
|      */ | ||||
|     List<ToolCodegenTableDO> getCodeGenTableList(); | ||||
|  | ||||
|     /** | ||||
|      * 获得指定表的字段定义数组 | ||||
|   | ||||
| @@ -15,12 +15,16 @@ import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolCodegenTableMapper | ||||
| import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolSchemaColumnMapper; | ||||
| import cn.iocoder.dashboard.modules.tool.dal.mysql.coegen.ToolSchemaTableMapper; | ||||
| import cn.iocoder.dashboard.modules.tool.service.codegen.ToolCodegenService; | ||||
| import cn.iocoder.dashboard.util.collection.CollectionUtils; | ||||
| import org.springframework.stereotype.Service; | ||||
| import org.springframework.transaction.annotation.Transactional; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.Set; | ||||
| import java.util.stream.Collectors; | ||||
|  | ||||
| /** | ||||
|  * 代码生成 Service 实现类 | ||||
| @@ -76,6 +80,15 @@ public class ToolCodegenServiceImpl implements ToolCodegenService { | ||||
|         return table.getId(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     @Transactional | ||||
|     public List<Long> createCodeGenList(List<String> tableNames) { | ||||
|         List<Long> ids = new ArrayList<>(tableNames.size()); | ||||
|         // 遍历添加。虽然效率会低一点,但是没必要做成完全批量,因为不会这么大量 | ||||
|         tableNames.forEach(tableName -> ids.add(createCodegen(tableName))); | ||||
|         return ids; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     @Transactional | ||||
|     public void updateCodegen(ToolCodegenUpdateReqVO updateReqVO) { | ||||
| @@ -93,15 +106,70 @@ public class ToolCodegenServiceImpl implements ToolCodegenService { | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public PageResult<ToolCodegenTableDO> getCodeGenTablePage(ToolCodegenTablePageReqVO pageReqVO) { | ||||
|     public void syncCodegen(Long tableId) { | ||||
|         // 校验是否已经存在 | ||||
|         ToolCodegenTableDO table = codegenTableMapper.selectById(tableId); | ||||
|         if (table == null) { | ||||
|             throw new RuntimeException(""); // TODO | ||||
|         } | ||||
|         // 从数据库中,获得数据库表结构 | ||||
|         List<ToolSchemaColumnDO> schemaColumns = schemaColumnMapper.selectListByTableName(table.getTableName()); | ||||
|         if (CollUtil.isEmpty(schemaColumns)) { | ||||
|             throw new RuntimeException(""); // TODO | ||||
|         } | ||||
|         Set<String> schemaColumnNames = CollectionUtils.convertSet(schemaColumns, ToolSchemaColumnDO::getColumnName); | ||||
|  | ||||
|         // 构建 ToolCodegenColumnDO 数组,只同步新增的字段 | ||||
|         List<ToolCodegenColumnDO> codegenColumns = codegenColumnMapper.selectListByTableId(tableId); | ||||
|         Set<String> codegenColumnNames = CollectionUtils.convertSet(codegenColumns, ToolCodegenColumnDO::getColumnName); | ||||
|         // 移除已经存在的字段 | ||||
|         schemaColumns.removeIf(column -> codegenColumnNames.contains(column.getColumnName())); | ||||
|         // 计算需要删除的字段 | ||||
|         Set<Long> deleteColumnIds = codegenColumns.stream().filter(column -> !schemaColumnNames.contains(column.getColumnName())) | ||||
|                 .map(ToolCodegenColumnDO::getId).collect(Collectors.toSet()); | ||||
|         if (CollUtil.isEmpty(schemaColumns) && CollUtil.isEmpty(deleteColumnIds)) { | ||||
|             throw new RuntimeException(""); // TODO | ||||
|         } | ||||
|  | ||||
|         // 插入新增的字段 | ||||
|         List<ToolCodegenColumnDO> columns = codegenBuilder.buildColumns(schemaColumns); | ||||
|         columns.forEach(column -> { | ||||
|             column.setTableId(table.getId()); | ||||
|             codegenColumnMapper.insert(column); // TODO 批量插入 | ||||
|         }); | ||||
|         // 删除不存在的字段 | ||||
|         codegenColumnMapper.deleteBatchIds(deleteColumnIds); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     @Transactional | ||||
|     public void deleteCodegen(Long tableId) { | ||||
|         // 校验是否已经存在 | ||||
|         if (codegenTableMapper.selectById(tableId) == null) { | ||||
|             throw new RuntimeException(""); // TODO | ||||
|         } | ||||
|  | ||||
|         // 删除 table 表定义 | ||||
|         codegenTableMapper.deleteById(tableId); | ||||
|         // 删除 column 字段定义 | ||||
|         codegenColumnMapper.deleteListByTableId(tableId); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public PageResult<ToolCodegenTableDO> getCodegenTablePage(ToolCodegenTablePageReqVO pageReqVO) { | ||||
|         return codegenTableMapper.selectPage(pageReqVO); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public ToolCodegenTableDO getCodeGenTablePage(Long id) { | ||||
|     public ToolCodegenTableDO getCodegenTablePage(Long id) { | ||||
|         return codegenTableMapper.selectById(id); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public List<ToolCodegenTableDO> getCodeGenTableList() { | ||||
|         return codegenTableMapper.selectList(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public List<ToolCodegenColumnDO> getCodegenColumnListByTableId(Long tableId) { | ||||
|         return codegenColumnMapper.selectListByTableId(tableId); | ||||
| @@ -128,4 +196,24 @@ public class ToolCodegenServiceImpl implements ToolCodegenService { | ||||
|         return schemaTableMapper.selectList(codegenProperties.getDbSchemas(), tableName, tableComment); | ||||
|     } | ||||
|  | ||||
| //    /** | ||||
| //     * 修改保存参数校验 | ||||
| //     * | ||||
| //     * @param genTable 业务信息 | ||||
| //     */ | ||||
| //    @Override | ||||
| //    public void validateEdit(GenTable genTable) { | ||||
| //        if (GenConstants.TPL_TREE.equals(genTable.getTplCategory())) { | ||||
| //            String options = JSON.toJSONString(genTable.getParams()); | ||||
| //            JSONObject paramsObj = JSONObject.parseObject(options); | ||||
| //            if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_CODE))) { | ||||
| //                throw new CustomException("树编码字段不能为空"); | ||||
| //            } else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_PARENT_CODE))) { | ||||
| //                throw new CustomException("树父编码字段不能为空"); | ||||
| //            } else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_NAME))) { | ||||
| //                throw new CustomException("树名称字段不能为空"); | ||||
| //            } | ||||
| //        } | ||||
| //    } | ||||
|  | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV