♻️ Refactoring code. 重构代码生成模块适配其他类型数据库

This commit is contained in:
lbw 2023-08-28 22:58:22 +08:00
parent 1c177c4dd2
commit cedb011aba
10 changed files with 105 additions and 69 deletions

View File

@ -623,7 +623,7 @@ CREATE TABLE `sys_user` (
`mini_openid` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '小程序openId', `mini_openid` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '小程序openId',
`qq_openid` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'QQ openId', `qq_openid` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'QQ openId',
`gitee_login` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '码云标识', `gitee_login` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '码云标识',
`osc_id` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '开源中国标识' `osc_id` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '开源中国标识',
PRIMARY KEY (`user_id`) USING BTREE, PRIMARY KEY (`user_id`) USING BTREE,
KEY `user_wx_openid` (`wx_openid`) USING BTREE, KEY `user_wx_openid` (`wx_openid`) USING BTREE,
KEY `user_qq_openid` (`qq_openid`) USING BTREE, KEY `user_qq_openid` (`qq_openid`) USING BTREE,
@ -634,7 +634,7 @@ CREATE TABLE `sys_user` (
-- Records of sys_user -- Records of sys_user
-- ---------------------------- -- ----------------------------
BEGIN; BEGIN;
INSERT INTO `sys_user` VALUES (1, 'admin', '$2a$10$c/Ae0pRjJtMZg3BnvVpO.eIK6WYWVbKTzqgdy3afR7w.vd.xi3Mgy', '', '17034642999', '/admin/sys-file/s3demo/7ff4ca6b7bf446f3a5a13ac016dc21af.png', '管理员', '管理员', 'pig4cloud@qq.com', 4, ' ', 'admin', '2018-04-20 07:15:18', '2023-07-07 14:55:40', '0', '0', NULL, 'oBxPy5E-v82xWGsfzZVzkD3wEX64', NULL, 'log4j', NULL, 1); INSERT INTO `sys_user` VALUES (1, 'admin', '$2a$10$c/Ae0pRjJtMZg3BnvVpO.eIK6WYWVbKTzqgdy3afR7w.vd.xi3Mgy', '', '17034642999', '/admin/sys-file/s3demo/7ff4ca6b7bf446f3a5a13ac016dc21af.png', '管理员', '管理员', 'pig4cloud@qq.com', 4, ' ', 'admin', '2018-04-20 07:15:18', '2023-07-07 14:55:40', '0', '0', NULL, 'oBxPy5E-v82xWGsfzZVzkD3wEX64', NULL, 'log4j', NULL);
COMMIT; COMMIT;
-- ---------------------------- -- ----------------------------

File diff suppressed because one or more lines are too long

View File

@ -108,7 +108,7 @@ public class GenTable extends Model<GenTable> {
* 生成方式 0zip压缩包 1自定义目录 * 生成方式 0zip压缩包 1自定义目录
*/ */
@Schema(description = "生成方式 0zip压缩包 1自定义目录") @Schema(description = "生成方式 0zip压缩包 1自定义目录")
private Integer generatorType; private String generatorType;
/** /**
* 后端生成路径 * 后端生成路径

View File

@ -94,22 +94,22 @@ public class GenTableColumnEntity extends Model<GenDatasourceConf> {
/** /**
* 主键 0 1 * 主键 0 1
*/ */
private boolean primaryPk; private String primaryPk;
/** /**
* 基类字段 0 1 * 基类字段 0 1
*/ */
private boolean baseField; private String baseField;
/** /**
* 表单项 0 1 * 表单项 0 1
*/ */
private boolean formItem; private String formItem;
/** /**
* 表单必填 0 1 * 表单必填 0 1
*/ */
private boolean formRequired; private String formRequired;
/** /**
* 表单类型 * 表单类型
@ -124,17 +124,17 @@ public class GenTableColumnEntity extends Model<GenDatasourceConf> {
/** /**
* 列表项 0 1 * 列表项 0 1
*/ */
private boolean gridItem; private String gridItem;
/** /**
* 列表排序 0 1 * 列表排序 0 1
*/ */
private boolean gridSort; private String gridSort;
/** /**
* 查询项 0 1 * 查询项 0 1
*/ */
private boolean queryItem; private String queryItem;
/** /**
* 查询方式 * 查询方式

View File

@ -37,9 +37,10 @@ import com.pig4cloud.pig.codegen.mapper.GeneratorMapper;
import com.pig4cloud.pig.codegen.service.GenGroupService; import com.pig4cloud.pig.codegen.service.GenGroupService;
import com.pig4cloud.pig.codegen.service.GenTableColumnService; import com.pig4cloud.pig.codegen.service.GenTableColumnService;
import com.pig4cloud.pig.codegen.service.GenTableService; import com.pig4cloud.pig.codegen.service.GenTableService;
import com.pig4cloud.pig.codegen.util.BoolFillEnum;
import com.pig4cloud.pig.codegen.util.CommonColumnFiledEnum; import com.pig4cloud.pig.codegen.util.CommonColumnFiledEnum;
import com.pig4cloud.pig.codegen.util.GenKit; import com.pig4cloud.pig.codegen.util.GenKit;
import com.pig4cloud.pig.codegen.util.GeneratorFileTypeEnum; import com.pig4cloud.pig.codegen.util.GeneratorTypeEnum;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -157,7 +158,7 @@ public class GenTableServiceImpl extends ServiceImpl<GenTableMapper, GenTable> i
table.setTableComment(MapUtil.getStr(queryTable, "tableComment")); table.setTableComment(MapUtil.getStr(queryTable, "tableComment"));
table.setDbType(MapUtil.getStr(queryTable, "dbType")); table.setDbType(MapUtil.getStr(queryTable, "dbType"));
table.setFormLayout(2); table.setFormLayout(2);
table.setGeneratorType(GeneratorFileTypeEnum.ZIP.ordinal()); table.setGeneratorType(GeneratorTypeEnum.ZIP_DOWNLOAD.getValue());
table.setClassName(NamingCase.toPascalCase(tableName)); table.setClassName(NamingCase.toPascalCase(tableName));
table.setModuleName(GenKit.getModuleName(table.getPackageName())); table.setModuleName(GenKit.getModuleName(table.getPackageName()));
table.setFunctionName(GenKit.getFunctionName(tableName)); table.setFunctionName(GenKit.getFunctionName(tableName));
@ -177,10 +178,13 @@ public class GenTableServiceImpl extends ServiceImpl<GenTableMapper, GenTable> i
genTableColumnEntity.setFieldComment(MapUtil.getStr(columnMap, "comments")); genTableColumnEntity.setFieldComment(MapUtil.getStr(columnMap, "comments"));
genTableColumnEntity.setFieldType(MapUtil.getStr(columnMap, "dataType")); genTableColumnEntity.setFieldType(MapUtil.getStr(columnMap, "dataType"));
String columnKey = MapUtil.getStr(columnMap, "columnKey"); String columnKey = MapUtil.getStr(columnMap, "columnKey");
genTableColumnEntity.setPrimaryPk(StringUtils.isNotBlank(columnKey) && "PRI".equalsIgnoreCase(columnKey));
genTableColumnEntity.setAutoFill("DEFAULT"); genTableColumnEntity.setAutoFill("DEFAULT");
genTableColumnEntity.setFormItem(true); genTableColumnEntity.setPrimaryPk((StringUtils.isNotBlank(columnKey) && "PRI".equalsIgnoreCase(columnKey))
genTableColumnEntity.setGridItem(true); ? BoolFillEnum.TRUE.getValue() : BoolFillEnum.FALSE.getValue());
genTableColumnEntity.setAutoFill("DEFAULT");
genTableColumnEntity.setFormItem(BoolFillEnum.TRUE.getValue());
genTableColumnEntity.setGridItem(BoolFillEnum.TRUE.getValue());
// 审计字段处理 // 审计字段处理
if (EnumUtil.contains(CommonColumnFiledEnum.class, columnName)) { if (EnumUtil.contains(CommonColumnFiledEnum.class, columnName)) {

View File

@ -21,6 +21,7 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import com.pig4cloud.pig.codegen.entity.GenTable; import com.pig4cloud.pig.codegen.entity.GenTable;
@ -62,8 +63,6 @@ public class GeneratorServiceImpl implements GeneratorService {
private final GenGroupService genGroupService; private final GenGroupService genGroupService;
private final GenTemplateService genTemplateService;
/** /**
* 生成代码zip写出 * 生成代码zip写出
* @param tableId * @param tableId
@ -225,21 +224,22 @@ public class GeneratorServiceImpl implements GeneratorService {
// 按字段类型分组使用 Map 存储不同类型的字段列表 // 按字段类型分组使用 Map 存储不同类型的字段列表
Map<Boolean, List<GenTableColumnEntity>> typeMap = table.getFieldList() Map<Boolean, List<GenTableColumnEntity>> typeMap = table.getFieldList()
.stream() .stream()
.collect(Collectors.partitioningBy(GenTableColumnEntity::isPrimaryPk)); .collect(Collectors
.partitioningBy(columnEntity -> BooleanUtil.toBoolean(columnEntity.getPrimaryPk())));
// 从分组后的 Map 中获取不同类型的字段列表 // 从分组后的 Map 中获取不同类型的字段列表
List<GenTableColumnEntity> primaryList = typeMap.get(true); List<GenTableColumnEntity> primaryList = typeMap.get(true);
List<GenTableColumnEntity> formList = typeMap.get(false) List<GenTableColumnEntity> formList = typeMap.get(false)
.stream() .stream()
.filter(GenTableColumnEntity::isFormItem) .filter(columnEntity -> BooleanUtil.toBoolean(columnEntity.getFormItem()))
.collect(Collectors.toList()); .collect(Collectors.toList());
List<GenTableColumnEntity> gridList = typeMap.get(false) List<GenTableColumnEntity> gridList = typeMap.get(false)
.stream() .stream()
.filter(GenTableColumnEntity::isGridItem) .filter(columnEntity -> BooleanUtil.toBoolean(columnEntity.getGridItem()))
.collect(Collectors.toList()); .collect(Collectors.toList());
List<GenTableColumnEntity> queryList = typeMap.get(false) List<GenTableColumnEntity> queryList = typeMap.get(false)
.stream() .stream()
.filter(GenTableColumnEntity::isQueryItem) .filter(columnEntity -> BooleanUtil.toBoolean(columnEntity.getQueryItem()))
.collect(Collectors.toList()); .collect(Collectors.toList());
if (CollUtil.isNotEmpty(primaryList)) { if (CollUtil.isNotEmpty(primaryList)) {

View File

@ -0,0 +1,26 @@
package com.pig4cloud.pig.codegen.util;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
* boolean 类型枚举
*
*/
@Getter
@RequiredArgsConstructor
public enum BoolFillEnum {
/**
* true
*/
TRUE("1"),
/**
* false
*/
FALSE("0");
private final String value;
}

View File

@ -16,38 +16,38 @@ public enum CommonColumnFiledEnum {
/** /**
* create_by 字段 * create_by 字段
*/ */
create_by(false, false, "INSERT", 100), create_by("0", "0", "INSERT", 100),
/** /**
* create_time 字段 * create_time 字段
*/ */
create_time(false, false, "INSERT", 101), create_time("0", "0", "INSERT", 101),
/** /**
* update_by 字段 * update_by 字段
*/ */
update_by(false, false, "INSERT_UPDATE", 102), update_by("0", "0", "INSERT_UPDATE", 102),
/** /**
* update_time 字段 * update_time 字段
*/ */
update_time(false, false, "INSERT_UPDATE", 103), update_time("0", "0", "INSERT_UPDATE", 103),
/** /**
* del_flag 字段 * del_flag 字段
*/ */
del_flag(false, false, "DEFAULT", 104), del_flag("0", "0", "DEFAULT", 104),
/** /**
* tenant_id 字段 * tenant_id 字段
*/ */
tenant_id(false, false, "DEFAULT", 105); tenant_id("0", "0", "DEFAULT", 105);
/** /**
* 表单是否默认显示 * 表单是否默认显示
*/ */
private Boolean formItem; private String formItem;
/** /**
* 表格是否默认显示 * 表格是否默认显示
*/ */
private Boolean gridItem; private String gridItem;
/** /**
* 自动填充策略 * 自动填充策略

View File

@ -1,14 +0,0 @@
package com.pig4cloud.pig.codegen.util;
public enum GeneratorFileTypeEnum {
/**
* zip
*/
ZIP,
/**
* 目录直接写入
*/
DIRECTORY;
}

View File

@ -0,0 +1,21 @@
package com.pig4cloud.pig.codegen.util;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@Getter
@RequiredArgsConstructor
public enum GeneratorTypeEnum {
/**
* zip压缩包 0
*/
ZIP_DOWNLOAD("0"),
/**
* 自定义目录 1
*/
CUSTOM_DIRECTORY("1");
private final String value;
}