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