mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-10 08:57:14 +08:00
feat: 新增保存代码生成配置信息接口
This commit is contained in:
@@ -16,10 +16,6 @@
|
||||
|
||||
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;
|
||||
|
||||
@@ -29,14 +25,4 @@ import top.charles7c.cnadmin.tool.model.entity.GenConfigDO;
|
||||
* @author Charles7c
|
||||
* @since 2023/4/12 23:56
|
||||
*/
|
||||
public interface GenConfigMapper extends BaseMapper<GenConfigDO> {
|
||||
|
||||
/**
|
||||
* 查询推荐作者名
|
||||
*
|
||||
* @param lessThanDate
|
||||
* 截止时间
|
||||
* @return 推荐作者名
|
||||
*/
|
||||
String selectRecommendAuthor(@Param("lessThanDate") Date lessThanDate);
|
||||
}
|
||||
public interface GenConfigMapper extends BaseMapper<GenConfigDO> {}
|
||||
|
@@ -19,14 +19,16 @@ package top.charles7c.cnadmin.tool.model.entity;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
@@ -44,47 +46,46 @@ import top.charles7c.cnadmin.tool.enums.FormTypeEnum;
|
||||
*/
|
||||
@Data
|
||||
@TableName("gen_column_mapping")
|
||||
@NoArgsConstructor
|
||||
@Accessors(chain = true)
|
||||
@Schema(description = "列映射信息")
|
||||
public class ColumnMappingDO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* ID
|
||||
*/
|
||||
@TableId
|
||||
@Schema(description = "ID")
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 表名称
|
||||
*/
|
||||
@Schema(description = "表名称")
|
||||
@NotBlank(message = "表名称不能为空")
|
||||
private String tableName;
|
||||
|
||||
/**
|
||||
* 列名称
|
||||
*/
|
||||
@Schema(description = "列名称")
|
||||
@NotBlank(message = "列名称不能为空")
|
||||
private String columnName;
|
||||
|
||||
/**
|
||||
* 列类型
|
||||
*/
|
||||
@Schema(description = "列类型")
|
||||
@NotBlank(message = "列类型不能为空")
|
||||
private String columnType;
|
||||
|
||||
/**
|
||||
* 字段名称
|
||||
*/
|
||||
@Schema(description = "字段名称")
|
||||
@NotBlank(message = "字段名称不能为空")
|
||||
private String fieldName;
|
||||
|
||||
/**
|
||||
* 字段类型
|
||||
*/
|
||||
@Schema(description = "字段类型")
|
||||
@NotBlank(message = "字段类型不能为空")
|
||||
private String fieldType;
|
||||
|
||||
/**
|
||||
@@ -93,12 +94,6 @@ public class ColumnMappingDO implements Serializable {
|
||||
@Schema(description = "注释")
|
||||
private String comment;
|
||||
|
||||
/**
|
||||
* 排序
|
||||
*/
|
||||
@Schema(description = "排序")
|
||||
private Integer sort;
|
||||
|
||||
/**
|
||||
* 是否必填
|
||||
*/
|
||||
@@ -142,12 +137,9 @@ public class ColumnMappingDO implements Serializable {
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private LocalDateTime createTime;
|
||||
|
||||
/**
|
||||
* 修改时间
|
||||
*/
|
||||
@Schema(description = "修改时间")
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
private LocalDateTime updateTime;
|
||||
public ColumnMappingDO(String tableName) {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
public ColumnMappingDO setColumnName(String columnName) {
|
||||
this.columnName = columnName;
|
||||
|
@@ -19,15 +19,16 @@ package top.charles7c.cnadmin.tool.model.entity;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
|
||||
/**
|
||||
* 生成配置实体
|
||||
@@ -37,35 +38,33 @@ import com.baomidou.mybatisplus.annotation.TableName;
|
||||
*/
|
||||
@Data
|
||||
@TableName("gen_config")
|
||||
@NoArgsConstructor
|
||||
@Accessors(chain = true)
|
||||
@Schema(description = "生成配置信息")
|
||||
public class GenConfigDO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* ID
|
||||
*/
|
||||
@TableId
|
||||
@Schema(description = "ID")
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 表名称
|
||||
*/
|
||||
@TableId(type = IdType.INPUT)
|
||||
@Schema(description = "表名称")
|
||||
@NotBlank(message = "表名称不能为空")
|
||||
private String tableName;
|
||||
|
||||
/**
|
||||
* 模块名称
|
||||
*/
|
||||
@Schema(description = "模块名称")
|
||||
@NotBlank(message = "模块名称不能为空")
|
||||
private String moduleName;
|
||||
|
||||
/**
|
||||
* 包名称
|
||||
*/
|
||||
@Schema(description = "包名称")
|
||||
@NotBlank(message = "包名称不能为空")
|
||||
private String packageName;
|
||||
|
||||
/**
|
||||
@@ -78,12 +77,14 @@ public class GenConfigDO implements Serializable {
|
||||
* 业务名称
|
||||
*/
|
||||
@Schema(description = "业务名称")
|
||||
@NotBlank(message = "业务名称不能为空")
|
||||
private String businessName;
|
||||
|
||||
/**
|
||||
* 作者
|
||||
*/
|
||||
@Schema(description = "作者")
|
||||
@NotBlank(message = "作者名称不能为空")
|
||||
private String author;
|
||||
|
||||
/**
|
||||
@@ -96,6 +97,7 @@ public class GenConfigDO implements Serializable {
|
||||
* 是否覆盖
|
||||
*/
|
||||
@Schema(description = "是否覆盖")
|
||||
@NotNull(message = "是否覆盖不能为空")
|
||||
private Boolean isOverride;
|
||||
|
||||
/**
|
||||
@@ -111,4 +113,8 @@ public class GenConfigDO implements Serializable {
|
||||
@Schema(description = "修改时间")
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
public GenConfigDO(String tableName) {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.charles7c.cnadmin.tool.model.request;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import top.charles7c.cnadmin.tool.model.entity.ColumnMappingDO;
|
||||
import top.charles7c.cnadmin.tool.model.entity.GenConfigDO;
|
||||
|
||||
/**
|
||||
* 代码生成配置信息
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/8/8 20:40
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "代码生成配置信息")
|
||||
public class GenConfigRequest implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 列映射信息列表
|
||||
*/
|
||||
@Schema(description = "列映射信息列表")
|
||||
private List<ColumnMappingDO> columnMappings = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 生成配置信息
|
||||
*/
|
||||
@Schema(description = "生成配置信息")
|
||||
private GenConfigDO genConfig;
|
||||
}
|
@@ -24,6 +24,7 @@ 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.request.GenConfigRequest;
|
||||
import top.charles7c.cnadmin.tool.model.vo.TableVO;
|
||||
|
||||
/**
|
||||
@@ -66,4 +67,14 @@ public interface GeneratorService {
|
||||
* @return 列映射信息列表
|
||||
*/
|
||||
List<ColumnMappingDO> listColumnMapping(String tableName);
|
||||
|
||||
/**
|
||||
* 保存代码生成配置信息
|
||||
*
|
||||
* @param request
|
||||
* 代码生成配置信息
|
||||
* @param tableName
|
||||
* 表名称
|
||||
*/
|
||||
void saveConfig(GenConfigRequest request, String tableName);
|
||||
}
|
||||
|
@@ -27,12 +27,12 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
|
||||
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;
|
||||
@@ -41,6 +41,7 @@ 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;
|
||||
@@ -48,6 +49,7 @@ 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.request.GenConfigRequest;
|
||||
import top.charles7c.cnadmin.tool.model.vo.TableVO;
|
||||
import top.charles7c.cnadmin.tool.service.GeneratorService;
|
||||
import top.charles7c.cnadmin.tool.util.MetaUtils;
|
||||
@@ -83,21 +85,25 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
|
||||
@Override
|
||||
public GenConfigDO getGenConfig(String tableName) throws SQLException {
|
||||
GenConfigDO genConfig =
|
||||
genConfigMapper.selectOne(Wrappers.lambdaQuery(GenConfigDO.class).eq(GenConfigDO::getTableName, tableName));
|
||||
GenConfigDO genConfig = genConfigMapper.selectById(tableName);
|
||||
if (null == genConfig) {
|
||||
genConfig = new GenConfigDO().setTableName(tableName);
|
||||
genConfig = new GenConfigDO(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);
|
||||
// 默认作者名称(上次保存使用的作者名称)
|
||||
GenConfigDO lastGenConfig = genConfigMapper.selectOne(
|
||||
Wrappers.lambdaQuery(GenConfigDO.class).orderByDesc(GenConfigDO::getCreateTime).last("LIMIT 1"));
|
||||
if (null != lastGenConfig) {
|
||||
genConfig.setAuthor(lastGenConfig.getAuthor());
|
||||
}
|
||||
// 默认表前缀(sys_user -> sys_)
|
||||
int underLineIndex = StrUtil.indexOf(tableName, StringConsts.C_UNDERLINE);
|
||||
if (-1 != underLineIndex) {
|
||||
genConfig.setTablePrefix(StrUtil.subPre(tableName, underLineIndex + 1));
|
||||
@@ -116,10 +122,10 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
for (Column column : columnList) {
|
||||
String columnType = StrUtil.splitToArray(column.getTypeName(), StringConsts.SPACE)[0];
|
||||
boolean isRequired = !column.isPk() && !column.isNullable();
|
||||
ColumnMappingDO columnMapping = new ColumnMappingDO().setTableName(tableName)
|
||||
.setColumnName(column.getName()).setColumnType(columnType.toLowerCase())
|
||||
.setComment(column.getComment()).setIsRequired(isRequired).setShowInList(true)
|
||||
.setShowInForm(isRequired).setShowInQuery(isRequired).setFormType(FormTypeEnum.TEXT);
|
||||
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);
|
||||
@@ -127,4 +133,44 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
}
|
||||
return columnMappingList;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void saveConfig(GenConfigRequest request, String tableName) {
|
||||
// 保存列映射信息
|
||||
columnMappingMapper
|
||||
.delete(Wrappers.lambdaQuery(ColumnMappingDO.class).eq(ColumnMappingDO::getTableName, tableName));
|
||||
List<ColumnMappingDO> columnMappingList = request.getColumnMappings();
|
||||
for (ColumnMappingDO columnMapping : columnMappingList) {
|
||||
if (columnMapping.getShowInForm()) {
|
||||
CheckUtils.throwIfNull(columnMapping.getFormType(), "字段 [{}] 的表单类型不能为空", columnMapping.getFieldName());
|
||||
} else {
|
||||
// 在表单中不显示,不需要设置必填
|
||||
columnMapping.setIsRequired(false);
|
||||
}
|
||||
if (columnMapping.getShowInQuery()) {
|
||||
CheckUtils.throwIfNull(columnMapping.getFormType(), "字段 [{}] 的表单类型不能为空", columnMapping.getFieldName());
|
||||
CheckUtils.throwIfNull(columnMapping.getQueryType(), "字段 [{}] 的查询方式不能为空", columnMapping.getFieldName());
|
||||
} else {
|
||||
// 在查询中不显示,不需要设置查询方式
|
||||
columnMapping.setQueryType(null);
|
||||
}
|
||||
// 既不在表单也不在查询中显示,不需要设置表单类型
|
||||
if (!columnMapping.getShowInForm() && !columnMapping.getShowInQuery()) {
|
||||
columnMapping.setFormType(null);
|
||||
}
|
||||
columnMapping.setTableName(tableName);
|
||||
}
|
||||
columnMappingMapper.insertBatch(columnMappingList);
|
||||
|
||||
// 保存或更新生成配置信息
|
||||
GenConfigDO newGenConfig = request.getGenConfig();
|
||||
GenConfigDO oldGenConfig = genConfigMapper.selectById(tableName);
|
||||
if (null != oldGenConfig) {
|
||||
BeanUtil.copyProperties(newGenConfig, oldGenConfig);
|
||||
genConfigMapper.updateById(oldGenConfig);
|
||||
} else {
|
||||
genConfigMapper.insert(newGenConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,12 +0,0 @@
|
||||
<?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>
|
Reference in New Issue
Block a user