mirror of
				https://github.com/continew-org/continew-admin.git
				synced 2025-10-31 22:57:17 +08:00 
			
		
		
		
	refactor: 重构查询列映射信息列表接口,支持对已保存的列映射配置同步最新表结构
This commit is contained in:
		| @@ -23,6 +23,7 @@ import javax.validation.constraints.NotBlank; | |||||||
|  |  | ||||||
| import lombok.Data; | import lombok.Data; | ||||||
| import lombok.NoArgsConstructor; | import lombok.NoArgsConstructor; | ||||||
|  | import lombok.NonNull; | ||||||
| import lombok.experimental.Accessors; | import lombok.experimental.Accessors; | ||||||
|  |  | ||||||
| import io.swagger.v3.oas.annotations.media.Schema; | import io.swagger.v3.oas.annotations.media.Schema; | ||||||
| @@ -32,9 +33,11 @@ import com.baomidou.mybatisplus.annotation.TableField; | |||||||
| import com.baomidou.mybatisplus.annotation.TableName; | import com.baomidou.mybatisplus.annotation.TableName; | ||||||
|  |  | ||||||
| import cn.hutool.core.util.StrUtil; | import cn.hutool.core.util.StrUtil; | ||||||
|  | import cn.hutool.db.meta.Column; | ||||||
| import cn.hutool.setting.dialect.Props; | import cn.hutool.setting.dialect.Props; | ||||||
| import cn.hutool.setting.dialect.PropsUtil; | import cn.hutool.setting.dialect.PropsUtil; | ||||||
|  |  | ||||||
|  | import top.charles7c.cnadmin.common.constant.StringConsts; | ||||||
| import top.charles7c.cnadmin.common.enums.QueryTypeEnum; | import top.charles7c.cnadmin.common.enums.QueryTypeEnum; | ||||||
| import top.charles7c.cnadmin.tool.enums.FormTypeEnum; | import top.charles7c.cnadmin.tool.enums.FormTypeEnum; | ||||||
|  |  | ||||||
| @@ -137,8 +140,14 @@ public class ColumnMappingDO implements Serializable { | |||||||
|     @TableField(fill = FieldFill.INSERT) |     @TableField(fill = FieldFill.INSERT) | ||||||
|     private LocalDateTime createTime; |     private LocalDateTime createTime; | ||||||
|  |  | ||||||
|     public ColumnMappingDO(String tableName) { |     public ColumnMappingDO(@NonNull Column column) { | ||||||
|         this.tableName = tableName; |         String columnType = StrUtil.splitToArray(column.getTypeName(), StringConsts.SPACE)[0].toLowerCase(); | ||||||
|  |         boolean isRequired = !column.isPk() && !column.isNullable(); | ||||||
|  |         this.tableName = column.getTableName(); | ||||||
|  |         this.setColumnName(column.getName()).setColumnType(columnType).setComment(column.getComment()) | ||||||
|  |             .setIsRequired(isRequired).setShowInList(true).setShowInForm(isRequired).setShowInQuery(isRequired) | ||||||
|  |             .setFormType(FormTypeEnum.TEXT); | ||||||
|  |         this.setQueryType("String".equals(this.getFieldType()) ? QueryTypeEnum.INNER_LIKE : QueryTypeEnum.EQUAL); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public ColumnMappingDO setColumnName(String columnName) { |     public ColumnMappingDO setColumnName(String columnName) { | ||||||
|   | |||||||
| @@ -64,9 +64,11 @@ public interface GeneratorService { | |||||||
|      * |      * | ||||||
|      * @param tableName |      * @param tableName | ||||||
|      *            表名称 |      *            表名称 | ||||||
|  |      * @param requireSync | ||||||
|  |      *            是否需要同步 | ||||||
|      * @return 列映射信息列表 |      * @return 列映射信息列表 | ||||||
|      */ |      */ | ||||||
|     List<ColumnMappingDO> listColumnMapping(String tableName); |     List<ColumnMappingDO> listColumnMapping(String tableName, Boolean requireSync); | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * 保存代码生成配置信息 |      * 保存代码生成配置信息 | ||||||
|   | |||||||
| @@ -17,9 +17,11 @@ | |||||||
| package top.charles7c.cnadmin.tool.service.impl; | package top.charles7c.cnadmin.tool.service.impl; | ||||||
|  |  | ||||||
| import java.sql.SQLException; | import java.sql.SQLException; | ||||||
| import java.util.ArrayList; |  | ||||||
| import java.util.Collection; | import java.util.Collection; | ||||||
| import java.util.List; | import java.util.List; | ||||||
|  | import java.util.Map; | ||||||
|  | import java.util.function.Function; | ||||||
|  | import java.util.stream.Collectors; | ||||||
|  |  | ||||||
| import javax.sql.DataSource; | import javax.sql.DataSource; | ||||||
|  |  | ||||||
| @@ -38,12 +40,10 @@ import cn.hutool.core.util.StrUtil; | |||||||
| import cn.hutool.db.meta.Column; | import cn.hutool.db.meta.Column; | ||||||
|  |  | ||||||
| import top.charles7c.cnadmin.common.constant.StringConsts; | import top.charles7c.cnadmin.common.constant.StringConsts; | ||||||
| import top.charles7c.cnadmin.common.enums.QueryTypeEnum; |  | ||||||
| import top.charles7c.cnadmin.common.model.query.PageQuery; | import top.charles7c.cnadmin.common.model.query.PageQuery; | ||||||
| import top.charles7c.cnadmin.common.model.vo.PageDataVO; | import top.charles7c.cnadmin.common.model.vo.PageDataVO; | ||||||
| import top.charles7c.cnadmin.common.util.validate.CheckUtils; | import top.charles7c.cnadmin.common.util.validate.CheckUtils; | ||||||
| import top.charles7c.cnadmin.tool.config.properties.GeneratorProperties; | import top.charles7c.cnadmin.tool.config.properties.GeneratorProperties; | ||||||
| import top.charles7c.cnadmin.tool.enums.FormTypeEnum; |  | ||||||
| import top.charles7c.cnadmin.tool.mapper.ColumnMappingMapper; | import top.charles7c.cnadmin.tool.mapper.ColumnMappingMapper; | ||||||
| import top.charles7c.cnadmin.tool.mapper.GenConfigMapper; | import top.charles7c.cnadmin.tool.mapper.GenConfigMapper; | ||||||
| import top.charles7c.cnadmin.tool.model.entity.ColumnMappingDO; | import top.charles7c.cnadmin.tool.model.entity.ColumnMappingDO; | ||||||
| @@ -113,24 +113,36 @@ public class GeneratorServiceImpl implements GeneratorService { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public List<ColumnMappingDO> listColumnMapping(String tableName) { |     public List<ColumnMappingDO> listColumnMapping(String tableName, Boolean requireSync) { | ||||||
|         List<ColumnMappingDO> columnMappingList = columnMappingMapper |         List<ColumnMappingDO> columnMappingList = columnMappingMapper | ||||||
|             .selectList(Wrappers.lambdaQuery(ColumnMappingDO.class).eq(ColumnMappingDO::getTableName, tableName)); |             .selectList(Wrappers.lambdaQuery(ColumnMappingDO.class).eq(ColumnMappingDO::getTableName, tableName)); | ||||||
|         if (CollUtil.isEmpty(columnMappingList)) { |         if (CollUtil.isEmpty(columnMappingList)) { | ||||||
|             Collection<Column> columnList = MetaUtils.getColumns(dataSource, tableName); |             Collection<Column> columnList = MetaUtils.getColumns(dataSource, tableName); | ||||||
|             columnMappingList = new ArrayList<>(columnList.size()); |             return columnList.stream().map(ColumnMappingDO::new).collect(Collectors.toList()); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         // 同步最新数据表列信息 | ||||||
|  |         if (requireSync) { | ||||||
|  |             Collection<Column> columnList = MetaUtils.getColumns(dataSource, tableName); | ||||||
|  |             // 移除已不存在的列映射信息 | ||||||
|  |             List<String> columnNameList = columnList.stream().map(Column::getName).collect(Collectors.toList()); | ||||||
|  |             columnMappingList.removeIf(column -> !columnNameList.contains(column.getColumnName())); | ||||||
|  |             // 新增或更新列映射信息 | ||||||
|  |             Map<String, ColumnMappingDO> columnMappingMap = columnMappingList.stream() | ||||||
|  |                 .collect(Collectors.toMap(ColumnMappingDO::getColumnName, Function.identity(), (key1, key2) -> key2)); | ||||||
|             for (Column column : columnList) { |             for (Column column : columnList) { | ||||||
|                 String columnType = StrUtil.splitToArray(column.getTypeName(), StringConsts.SPACE)[0]; |                 ColumnMappingDO columnMapping = columnMappingMap.get(column.getName()); | ||||||
|                 boolean isRequired = !column.isPk() && !column.isNullable(); |                 if (null != columnMapping) { | ||||||
|                 ColumnMappingDO columnMapping = new ColumnMappingDO(tableName).setColumnName(column.getName()) |                     // 更新已有列映射信息 | ||||||
|                     .setColumnType(columnType.toLowerCase()).setComment(column.getComment()).setIsRequired(isRequired) |                     String columnType = StrUtil.splitToArray(column.getTypeName(), StringConsts.SPACE)[0].toLowerCase(); | ||||||
|                     .setShowInList(true).setShowInForm(isRequired).setShowInQuery(isRequired) |                     columnMapping.setColumnType(columnType).setComment(column.getComment()); | ||||||
|                     .setFormType(FormTypeEnum.TEXT); |                 } else { | ||||||
|                 columnMapping.setQueryType( |                     // 新增列映射信息 | ||||||
|                     "String".equals(columnMapping.getFieldType()) ? QueryTypeEnum.INNER_LIKE : QueryTypeEnum.EQUAL); |                     columnMapping = new ColumnMappingDO(column); | ||||||
|                     columnMappingList.add(columnMapping); |                     columnMappingList.add(columnMapping); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |         } | ||||||
|         return columnMappingList; |         return columnMappingList; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -42,12 +42,11 @@ export interface ColumnMappingRecord { | |||||||
|   showInQuery: boolean; |   showInQuery: boolean; | ||||||
|   formType: string; |   formType: string; | ||||||
|   queryType: string; |   queryType: string; | ||||||
|   createTime: string; |   createTime?: string; | ||||||
|   updateTime: string; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| export function listColumnMapping(tableName: string) { | export function listColumnMapping(tableName: string, requireSync: boolean) { | ||||||
|   return axios.get<ColumnMappingRecord[]>(`${BASE_URL}/column/${tableName}`); |   return axios.get<ColumnMappingRecord[]>(`${BASE_URL}/column/${tableName}?requireSync=${requireSync}`); | ||||||
| } | } | ||||||
|  |  | ||||||
| export interface GenConfigRecord { | export interface GenConfigRecord { | ||||||
| @@ -59,8 +58,8 @@ export interface GenConfigRecord { | |||||||
|   author: string; |   author: string; | ||||||
|   tablePrefix: string; |   tablePrefix: string; | ||||||
|   isOverride: boolean; |   isOverride: boolean; | ||||||
|   createTime: string; |   createTime?: string; | ||||||
|   updateTime: string; |   updateTime?: string; | ||||||
| } | } | ||||||
|  |  | ||||||
| export function getGenConfig(tableName: string) { | export function getGenConfig(tableName: string) { | ||||||
|   | |||||||
| @@ -126,17 +126,26 @@ | |||||||
|       > |       > | ||||||
|         <a-card title="字段配置" class="field-config"> |         <a-card title="字段配置" class="field-config"> | ||||||
|           <template #extra> |           <template #extra> | ||||||
|             <a-space> |             <a-popconfirm | ||||||
|  |               content="确定要同步最新数据表结构吗?同步后只要不点击确定保存,则不影响原有配置数据。" | ||||||
|  |               type="warning" | ||||||
|  |               @ok="handleRefresh(form.tableName)" | ||||||
|  |             > | ||||||
|  |               <a-tooltip content="同步最新数据表结构"> | ||||||
|                 <a-button |                 <a-button | ||||||
|                   type="primary" |                   type="primary" | ||||||
|                   status="success" |                   status="success" | ||||||
|                   size="small" |                   size="small" | ||||||
|                   title="同步" |                   title="同步" | ||||||
|                 disabled |                   :disabled=" | ||||||
|  |                     columnMappingList.length !== 0 && | ||||||
|  |                     columnMappingList[0].createTime === null | ||||||
|  |                   " | ||||||
|                 > |                 > | ||||||
|                   <template #icon><icon-sync /></template>同步 |                   <template #icon><icon-sync /></template>同步 | ||||||
|                 </a-button> |                 </a-button> | ||||||
|             </a-space> |               </a-tooltip> | ||||||
|  |             </a-popconfirm> | ||||||
|           </template> |           </template> | ||||||
|           <a-table |           <a-table | ||||||
|             ref="columnMappingRef" |             ref="columnMappingRef" | ||||||
| @@ -376,20 +385,40 @@ | |||||||
|     tableComment = tableComment ? `(${tableComment})` : ' '; |     tableComment = tableComment ? `(${tableComment})` : ' '; | ||||||
|     title.value = `${tableName}${tableComment}配置`; |     title.value = `${tableName}${tableComment}配置`; | ||||||
|     visible.value = true; |     visible.value = true; | ||||||
|  |     // 查询列映射信息 | ||||||
|  |     getColumnMappingList(tableName, false); | ||||||
|  |     // 查询生成配置 | ||||||
|  |     getGenConfig(tableName).then((res) => { | ||||||
|  |       form.value = res.data; | ||||||
|  |       form.value.isOverride = false; | ||||||
|  |     }); | ||||||
|  |   }; | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 同步 | ||||||
|  |    * | ||||||
|  |    * @param tableName 表名称 | ||||||
|  |    */ | ||||||
|  |   const handleRefresh = (tableName: string) => { | ||||||
|  |     getColumnMappingList(tableName, true); | ||||||
|  |   }; | ||||||
|  |  | ||||||
|  |   /** | ||||||
|  |    * 查询列映射信息 | ||||||
|  |    * | ||||||
|  |    * @param tableName 表名称 | ||||||
|  |    * @param requireSync 是否需要同步 | ||||||
|  |    */ | ||||||
|  |   const getColumnMappingList = (tableName: string, requireSync: boolean) => { | ||||||
|     // 查询列映射信息 |     // 查询列映射信息 | ||||||
|     columnMappingLoading.value = true; |     columnMappingLoading.value = true; | ||||||
|     listColumnMapping(tableName) |     listColumnMapping(tableName, requireSync) | ||||||
|       .then((res) => { |       .then((res) => { | ||||||
|         columnMappingList.value = res.data; |         columnMappingList.value = res.data; | ||||||
|       }) |       }) | ||||||
|       .finally(() => { |       .finally(() => { | ||||||
|         columnMappingLoading.value = false; |         columnMappingLoading.value = false; | ||||||
|       }); |       }); | ||||||
|     // 查询生成配置 |  | ||||||
|     getGenConfig(tableName).then((res) => { |  | ||||||
|       form.value = res.data; |  | ||||||
|       form.value.isOverride = false; |  | ||||||
|     }); |  | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   /** |   /** | ||||||
|   | |||||||
| @@ -66,8 +66,9 @@ public class GeneratorController { | |||||||
|  |  | ||||||
|     @Operation(summary = "查询列映射信息列表", description = "查询列映射信息列表") |     @Operation(summary = "查询列映射信息列表", description = "查询列映射信息列表") | ||||||
|     @GetMapping("/column/{tableName}") |     @GetMapping("/column/{tableName}") | ||||||
|     public R<List<ColumnMappingDO>> listColumnMapping(@PathVariable String tableName) { |     public R<List<ColumnMappingDO>> listColumnMapping(@PathVariable String tableName, | ||||||
|         return R.ok(generatorService.listColumnMapping(tableName)); |         @RequestParam(required = false, defaultValue = "false") Boolean requireSync) { | ||||||
|  |         return R.ok(generatorService.listColumnMapping(tableName, requireSync)); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Operation(summary = "保存配置信息", description = "保存配置信息") |     @Operation(summary = "保存配置信息", description = "保存配置信息") | ||||||
|   | |||||||
| @@ -2,9 +2,9 @@ | |||||||
|  |  | ||||||
| -- changeset Charles7c:1 | -- changeset Charles7c:1 | ||||||
| CREATE TABLE IF NOT EXISTS `gen_config` ( | CREATE TABLE IF NOT EXISTS `gen_config` ( | ||||||
|     `table_name` varchar(100) COMMENT '表名称', |     `table_name` varchar(64) COMMENT '表名称', | ||||||
|     `module_name` varchar(50) NOT NULL COMMENT '模块名称', |     `module_name` varchar(60) NOT NULL COMMENT '模块名称', | ||||||
|     `package_name` varchar(50) NOT NULL COMMENT '包名称', |     `package_name` varchar(60) NOT NULL COMMENT '包名称', | ||||||
|     `frontend_path` varchar(255) DEFAULT NULL COMMENT '前端路径', |     `frontend_path` varchar(255) DEFAULT NULL COMMENT '前端路径', | ||||||
|     `business_name` varchar(50) NOT NULL COMMENT '业务名称', |     `business_name` varchar(50) NOT NULL COMMENT '业务名称', | ||||||
|     `author` varchar(100) NOT NULL COMMENT '作者', |     `author` varchar(100) NOT NULL COMMENT '作者', | ||||||
| @@ -16,10 +16,10 @@ CREATE TABLE IF NOT EXISTS `gen_config` ( | |||||||
| ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='生成配置表'; | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='生成配置表'; | ||||||
|  |  | ||||||
| CREATE TABLE IF NOT EXISTS `gen_column_mapping` ( | CREATE TABLE IF NOT EXISTS `gen_column_mapping` ( | ||||||
|     `table_name` varchar(100) NOT NULL COMMENT '表名称', |     `table_name` varchar(64) NOT NULL COMMENT '表名称', | ||||||
|     `column_name` varchar(50) NOT NULL COMMENT '列名称', |     `column_name` varchar(64) NOT NULL COMMENT '列名称', | ||||||
|     `column_type` varchar(25) NOT NULL COMMENT '列类型', |     `column_type` varchar(25) NOT NULL COMMENT '列类型', | ||||||
|     `field_name` varchar(50) NOT NULL COMMENT '字段名称', |     `field_name` varchar(64) NOT NULL COMMENT '字段名称', | ||||||
|     `field_type` varchar(25) NOT NULL COMMENT '字段类型', |     `field_type` varchar(25) NOT NULL COMMENT '字段类型', | ||||||
|     `comment` varchar(512) DEFAULT NULL COMMENT '注释', |     `comment` varchar(512) DEFAULT NULL COMMENT '注释', | ||||||
|     `is_required` bit(1) DEFAULT b'1' COMMENT '是否必填', |     `is_required` bit(1) DEFAULT b'1' COMMENT '是否必填', | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user