Skip to content

Commit

Permalink
feat: 新增查询生成配置信息接口
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Aug 7, 2023
1 parent f4c6d83 commit abae964
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package top.charles7c.cnadmin.tool.mapper;

import java.util.Date;

import org.apache.ibatis.annotations.Param;

import top.charles7c.cnadmin.common.base.BaseMapper;
import top.charles7c.cnadmin.tool.model.entity.GenConfigDO;

Expand All @@ -25,4 +29,14 @@
* @author Charles7c
* @since 2023/4/12 23:56
*/
public interface GenConfigMapper extends BaseMapper<GenConfigDO> {}
public interface GenConfigMapper extends BaseMapper<GenConfigDO> {

/**
* 查询推荐作者名
*
* @param lessThanDate
* 截止时间
* @return 推荐作者名
*/
String selectRecommendAuthor(@Param("lessThanDate") Date lessThanDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,10 @@ public class ColumnMappingDO implements Serializable {
private Boolean showInList;

/**
* 是否在新增中显示
* 是否在表单中显示
*/
@Schema(description = "是否在新增中显示")
private Boolean showInAdd;

/**
* 是否在修改中显示
*/
@Schema(description = "是否在修改中显示")
private Boolean showInUpdate;
@Schema(description = "是否在表单中显示")
private Boolean showInForm;

/**
* 是否在查询中显示
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import java.time.LocalDateTime;

import lombok.Data;
import lombok.experimental.Accessors;

import io.swagger.v3.oas.annotations.media.Schema;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
Expand All @@ -34,6 +37,8 @@
*/
@Data
@TableName("gen_config")
@Accessors(chain = true)
@Schema(description = "生成配置信息")
public class GenConfigDO implements Serializable {

private static final long serialVersionUID = 1L;
Expand All @@ -42,57 +47,68 @@ public class GenConfigDO implements Serializable {
* ID
*/
@TableId
@Schema(description = "ID")
private Long id;

/**
* 表名称
*/
@Schema(description = "表名称")
private String tableName;

/**
* 模块名称
*/
@Schema(description = "模块名称")
private String moduleName;

/**
* 包名称
*/
@Schema(description = "包名称")
private String packageName;

/**
* 前端路径
*/
@Schema(description = "前端路径")
private String frontendPath;

/**
* 业务名称
*/
@Schema(description = "业务名称")
private String businessName;

/**
* 作者
*/
@Schema(description = "作者")
private String author;

/**
* 前端路径
*/
private String frontendPath;

/**
* 表前缀
*/
@Schema(description = "表前缀")
private String tablePrefix;

/**
* 是否覆盖
*/
@Schema(description = "是否覆盖")
private Boolean isOverride;

/**
* 创建时间
*/
@Schema(description = "创建时间")
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;

/**
* 修改时间
*/
@Schema(description = "修改时间")
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import top.charles7c.cnadmin.common.model.query.PageQuery;
import top.charles7c.cnadmin.common.model.vo.PageDataVO;
import top.charles7c.cnadmin.tool.model.entity.ColumnMappingDO;
import top.charles7c.cnadmin.tool.model.entity.GenConfigDO;
import top.charles7c.cnadmin.tool.model.query.TableQuery;
import top.charles7c.cnadmin.tool.model.vo.TableVO;

Expand All @@ -41,9 +42,22 @@ public interface GeneratorService {
* @param pageQuery
* 分页查询条件
* @return 表信息分页列表
* @throws SQLException
* /
*/
PageDataVO<TableVO> pageTable(TableQuery query, PageQuery pageQuery) throws SQLException;

/**
* 查询生成配置信息
*
* @param tableName
* 表名称
* @return 生成配置信息
* @throws SQLException
* /
*/
GenConfigDO getGenConfig(String tableName) throws SQLException;

/**
* 查询列映射信息列表
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.meta.Column;

Expand All @@ -42,7 +44,9 @@
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;
import top.charles7c.cnadmin.tool.model.entity.GenConfigDO;
import top.charles7c.cnadmin.tool.model.query.TableQuery;
import top.charles7c.cnadmin.tool.model.vo.TableVO;
import top.charles7c.cnadmin.tool.service.GeneratorService;
Expand All @@ -63,6 +67,7 @@ public class GeneratorServiceImpl implements GeneratorService {
private final DataSource dataSource;
private final GeneratorProperties generatorProperties;
private final ColumnMappingMapper columnMappingMapper;
private final GenConfigMapper genConfigMapper;

@Override
public PageDataVO<TableVO> pageTable(TableQuery query, PageQuery pageQuery) throws SQLException {
Expand All @@ -76,6 +81,31 @@ public PageDataVO<TableVO> pageTable(TableQuery query, PageQuery pageQuery) thro
return PageDataVO.build(pageQuery.getPage(), pageQuery.getSize(), tableVOList);
}

@Override
public GenConfigDO getGenConfig(String tableName) throws SQLException {
GenConfigDO genConfig =
genConfigMapper.selectOne(Wrappers.lambdaQuery(GenConfigDO.class).eq(GenConfigDO::getTableName, tableName));
if (null == genConfig) {
genConfig = new GenConfigDO().setTableName(tableName);
String packageName = ClassUtil.getPackage(GeneratorService.class);
genConfig.setPackageName(StrUtil.subBefore(packageName, StringConsts.DOT, true));
List<Table> tableList = MetaUtils.getTables(dataSource, tableName);
if (CollUtil.isNotEmpty(tableList)) {
Table table = tableList.get(0);
genConfig.setBusinessName(StrUtil.replace(table.getComment(), "表", StringConsts.EMPTY));
}
String recommendAuthor = genConfigMapper.selectRecommendAuthor(DateUtil.lastWeek().toJdkDate());
if (StrUtil.isNotBlank(recommendAuthor)) {
genConfig.setAuthor(recommendAuthor);
}
int underLineIndex = StrUtil.indexOf(tableName, StringConsts.C_UNDERLINE);
if (-1 != underLineIndex) {
genConfig.setTablePrefix(StrUtil.subPre(tableName, underLineIndex + 1));
}
}
return genConfig;
}

@Override
public List<ColumnMappingDO> listColumnMapping(String tableName) {
List<ColumnMappingDO> columnMappingList = columnMappingMapper
Expand All @@ -89,8 +119,9 @@ public List<ColumnMappingDO> listColumnMapping(String tableName) {
ColumnMappingDO columnMapping = new ColumnMappingDO().setTableName(tableName)
.setColumnName(column.getName()).setColumnType(columnType.toLowerCase())
.setComment(column.getComment()).setIsRequired(isRequired).setShowInList(true)
.setShowInAdd(isRequired).setShowInUpdate(isRequired).setShowInQuery(isRequired)
.setFormType(FormTypeEnum.TEXT).setQueryType(QueryTypeEnum.EQUAL);
.setShowInForm(isRequired).setShowInQuery(isRequired).setFormType(FormTypeEnum.TEXT);
columnMapping.setQueryType(
"String".equals(columnMapping.getFieldType()) ? QueryTypeEnum.INNER_LIKE : QueryTypeEnum.EQUAL);
columnMappingList.add(columnMapping);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import lombok.NoArgsConstructor;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.meta.Column;
Expand All @@ -49,7 +50,27 @@ public class MetaUtils {
* @return 表信息列表
*/
public static List<Table> getTables(DataSource dataSource) throws SQLException {
List<Entity> tableEntityList = Db.use(dataSource).query("SHOW TABLE STATUS");
return getTables(dataSource, null);
}

/**
* 获取所有表信息
*
* @param dataSource
* 数据源
* @param tableName
* 表名称
* @return 表信息列表
*/
public static List<Table> getTables(DataSource dataSource, String tableName) throws SQLException {
String querySql = "SHOW TABLE STATUS";
List<Entity> tableEntityList;
Db db = Db.use(dataSource);
if (StrUtil.isNotBlank(tableName)) {
tableEntityList = db.query(String.format("%s WHERE NAME = ?", querySql), tableName);
} else {
tableEntityList = db.query(querySql);
}
List<Table> tableList = new ArrayList<>(tableEntityList.size());
for (Entity tableEntity : tableEntityList) {
Table table = new Table(tableEntity.getStr("NAME"));
Expand Down
12 changes: 12 additions & 0 deletions continew-admin-tool/src/main/resources/mapper/GenConfigMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="top.charles7c.cnadmin.tool.mapper.GenConfigMapper">
<select id="selectRecommendAuthor" resultType="java.lang.String">
SELECT `author`
FROM `gen_config`
WHERE #{lessThanDate} > `create_time`
GROUP BY `author`
ORDER BY COUNT(*) DESC
LIMIT 1
</select>
</mapper>
13 changes: 8 additions & 5 deletions continew-admin-ui/src/api/tool/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,18 @@ export interface ColumnMappingRecord {
sort: number;
isRequired: boolean;
showInList: boolean;
showInAdd: boolean;
showInUpdate: boolean;
showInForm: boolean;
showInQuery: boolean;
formType: string;
queryType: string;
createTime: string;
updateTime: string;
}

export function listColumnMapping(tableName: string) {
return axios.get<ColumnMappingRecord[]>(`${BASE_URL}/column/${tableName}`);
}

export interface GenConfigRecord {
id: string;
tableName: string;
Expand All @@ -63,6 +66,6 @@ export interface GenConfigRecord {
updateTime: string;
}

export function listColumnMapping(tableName: string) {
return axios.get<ColumnMappingRecord[]>(`${BASE_URL}/column/${tableName}`);
}
export function getGenConfig(tableName: string) {
return axios.get<GenConfigRecord>(`${BASE_URL}/table/${tableName}`);
}
Loading

0 comments on commit abae964

Please sign in to comment.