修改代码生成器逻辑.

This commit is contained in:
lijiahang
2023-09-26 15:59:09 +08:00
parent 5e7d5569bf
commit e405855e03
9 changed files with 369 additions and 98 deletions

View File

@@ -15,8 +15,9 @@ import com.orion.lang.utils.ansi.style.color.AnsiForeground;
import com.orion.lang.utils.ext.yml.YmlExt;
import com.orion.ops.framework.mybatis.core.domain.BaseDO;
import com.orion.ops.framework.mybatis.core.mapper.IMapper;
import com.orion.ops.launch.generator.template.Table;
import com.orion.ops.launch.generator.engine.VelocityTemplateEngine;
import com.orion.ops.launch.generator.template.Table;
import com.orion.ops.launch.generator.template.Template;
import org.apache.ibatis.annotations.Mapper;
import java.io.File;
@@ -43,11 +44,17 @@ public class CodeGenerator {
String module = "infra";
// 生成的表
Table[] tables = {
// new GenTable("system_user", "用户", "user")
// .vue("user", "user")
// .enums(UserStatusEnum.class),
new Table("preference", "用户偏好", "preference")
Template.create("preference", "用户偏好", "preference")
.enableProviderApi()
.cache("user:preference:{}:{}", "用户偏好 ${type} ${userId}")
.formatKeys("type", "userId")
.vue("user", "preference")
.enums("type")
.names("APP", "HOST")
.values("label", "应用", "主机")
.values("value", 1)
.color(null, "green")
.build(),
};
// jdbc 配置 - 使用配置文件
File yamlFile = new File("orion-ops-launch/src/main/resources/application-dev.yaml");
@@ -388,6 +395,7 @@ public class CodeGenerator {
.append(AnsiForeground.BRIGHT_BLUE.and(AnsiFont.BOLD), "- 后端代码复制后请先 clean 模块父工程\n")
.append(AnsiForeground.BRIGHT_BLUE.and(AnsiFont.BOLD), "- 后端代码复制后请先执行单元测试检测是否正常\n")
.append(AnsiForeground.BRIGHT_BLUE.and(AnsiFont.BOLD), "- vue 代码需要注意同一模块的 router 需要自行合并\n")
.append(AnsiForeground.BRIGHT_BLUE.and(AnsiFont.BOLD), "- vue 枚举需要自行更改数据类型\n")
.append(AnsiForeground.BRIGHT_BLUE.and(AnsiFont.BOLD), "- 菜单 sql 执行完成后 需要在菜单页面刷新缓存\n")
.toString();
System.out.print(line);

View File

@@ -20,10 +20,10 @@ import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine;
import com.orion.lang.define.collect.MultiLinkedHashMap;
import com.orion.lang.utils.Enums;
import com.orion.lang.utils.Strings;
import com.orion.lang.utils.VariableStyles;
import com.orion.lang.utils.io.Files1;
@@ -31,6 +31,7 @@ import com.orion.lang.utils.reflect.BeanMap;
import com.orion.lang.utils.reflect.Fields;
import com.orion.ops.framework.common.constant.Const;
import com.orion.ops.framework.common.constant.OrionOpsProConst;
import com.orion.ops.launch.generator.template.EnumMeta;
import com.orion.ops.launch.generator.template.Table;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
@@ -137,13 +138,13 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
@NotNull TableInfo tableInfo) {
// 生成文件副本
List<CustomFile> files = originCustomerFile.stream().map(s ->
new CustomFile.Builder()
.enableFileOverride()
.templatePath(s.getTemplatePath())
.filePath(s.getFilePath())
.fileName(s.getFileName())
.packageName(s.getPackageName())
.build())
new CustomFile.Builder()
.enableFileOverride()
.templatePath(s.getTemplatePath())
.filePath(s.getFilePath())
.fileName(s.getFileName())
.packageName(s.getPackageName())
.build())
.collect(Collectors.toList());
// 获取 table
Table table = tables.get(tableInfo.getName());
@@ -324,7 +325,7 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
// 功能名称常量
beanMap.put("featureConst", VariableStyles.SPINE.toSerpentine(table.getFeature()).toUpperCase());
// 枚举
beanMap.put("enums", this.getEnumMap(table));
this.setEnumMap(beanMap, tableInfo, table);
objectMap.put("vue", beanMap);
// 生成文件
@@ -390,19 +391,72 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine {
}
/**
* 获取枚举 map
* 设置枚举
*
* @param table table
* @return enums
* @param beanMap beanMap
* @param tableInfo tableInfo
* @param table table
*/
private Object getEnumMap(Table table) {
List<Class<? extends Enum<?>>> enums = table.getEnums();
private void setEnumMap(BeanMap beanMap, TableInfo tableInfo, Table table) {
// 枚举注解
Map<String, String> enumComment = new HashMap<>();
// 枚举值
Map<String, MultiLinkedHashMap<String, String, Object>> enumMap = new LinkedHashMap<>();
for (Class<? extends Enum<?>> e : enums) {
MultiLinkedHashMap<String, String, Object> fieldValueMap = Enums.getFieldValueMap(e);
enumMap.put(e.getSimpleName(), fieldValueMap);
for (EnumMeta meta : table.getEnums()) {
meta = meta.clone();
// 检查字段是否存在
String originVariable = meta.getVariable();
TableField tableField = tableInfo.getFields()
.stream()
.filter(s -> originVariable.equals(s.getName()) || originVariable.equals(s.getPropertyName()))
.findFirst()
.orElseThrow(() -> new RuntimeException("未查询到枚举映射字段 " + originVariable));
// 转为大驼峰
String enumName = Strings.firstUpper(tableField.getPropertyName()) + "Enum";
// 设置枚举注释
if (meta.getComment() == null) {
enumComment.put(enumName, Strings.def(tableField.getComment(), enumName));
} else {
enumComment.put(enumName, meta.getComment());
}
// 设置枚举
MultiLinkedHashMap<String, String, Object> enumInfo = new MultiLinkedHashMap<>();
for (int i = 0; i < meta.getNames().size(); i++) {
// 枚举名称
String name = meta.getNames().get(i);
// 设置枚举值
for (int j = 0; j < meta.getFields().size(); j++) {
String field = meta.getFields().get(j);
Object value = safeGet(safeGet(meta.getValues(), j), i);
enumInfo.put(name, field, value);
}
}
enumMap.put(enumName, enumInfo);
}
return enumMap;
// 设置到上下文
beanMap.put("enums", enumMap);
beanMap.put("enumComment", enumComment);
}
/**
* 获取值
*
* @param list list
* @param index index
* @param <T> T
* @return value
*/
private <T> T safeGet(List<T> list, int index) {
if (list == null) {
return null;
}
if (list.size() > index) {
return list.get(index);
} else {
return null;
}
}
}

View File

@@ -1,5 +1,7 @@
package com.orion.ops.launch.generator.template;
import com.orion.lang.utils.collect.Lists;
import java.util.concurrent.TimeUnit;
/**
@@ -12,10 +14,20 @@ import java.util.concurrent.TimeUnit;
public class CacheTemplate extends ServerTemplate {
public CacheTemplate(Table table) {
this(table, table.cacheKey, table.cacheDesc);
}
public CacheTemplate(Table table, String key) {
this(table, key, table.cacheDesc);
}
public CacheTemplate(Table table, String key, String desc) {
super(table);
table.enableCache = true;
table.cacheExpireTime = 1;
table.cacheExpireUnit = TimeUnit.HOURS;
table.cacheKey = key;
table.cacheDesc = desc;
}
/**
@@ -29,6 +41,40 @@ public class CacheTemplate extends ServerTemplate {
return this;
}
/**
* 设置缓存 key
*
* @param key key
* @return this
*/
public CacheTemplate key(String key, String desc) {
table.cacheKey = key;
table.cacheDesc = desc;
return this;
}
/**
* 设置缓存描述
*
* @param desc desc
* @return this
*/
public CacheTemplate desc(String desc) {
table.cacheDesc = desc;
return this;
}
/**
* 设置缓存格式化字段
*
* @param keys keys
* @return this
*/
public CacheTemplate formatKeys(String... keys) {
table.cacheFormatKeys.addAll(Lists.of(keys));
return this;
}
/**
* 设置缓存过期时间
*

View File

@@ -0,0 +1,63 @@
package com.orion.ops.launch.generator.template;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
* 枚举元数据
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023/9/26 13:53
*/
@Data
public class EnumMeta {
/**
* 替换的枚举字段 数据库/小驼峰
*/
protected String variable;
/**
* 枚举注释 没有的话使用 variable.comment || variable
*/
protected String comment;
/**
* 枚举名称
*/
protected List<String> names;
/**
* 枚举字段
*/
protected List<String> fields;
/**
* 枚举值
*/
protected List<List<Object>> values;
public EnumMeta(String variable) {
this.variable = variable;
this.names = new ArrayList<>();
this.fields = new ArrayList<>();
this.values = new ArrayList<>();
}
public EnumMeta(String variable, String comment, List<String> names, List<String> fields, List<List<Object>> values) {
this.variable = variable;
this.comment = comment;
this.names = names;
this.fields = fields;
this.values = values;
}
@Override
public EnumMeta clone() {
return new EnumMeta(variable, comment, names, fields, values);
}
}

View File

@@ -1,5 +1,12 @@
package com.orion.ops.launch.generator.template;
import com.orion.lang.utils.Enums;
import com.orion.lang.utils.collect.Lists;
import com.orion.lang.utils.reflect.Fields;
import java.util.List;
import java.util.stream.Collectors;
/**
* 前端代码枚举模板
*
@@ -9,8 +16,118 @@ package com.orion.ops.launch.generator.template;
*/
public class EnumsTemplate extends VueTemplate {
public EnumsTemplate(Table table) {
private final EnumMeta meta;
public EnumsTemplate(Table table, String variable) {
super(table);
this.meta = new EnumMeta(variable);
table.enums.add(meta);
}
public EnumsTemplate(Table table, String variable, Class<? extends Enum<?>> enumClass) {
super(table);
this.meta = new EnumMeta(variable);
table.enums.add(meta);
this.parseEnumMeta(enumClass);
}
/**
* 解析枚举
*
* @param enumClass enumClass
*/
private void parseEnumMeta(Class<? extends Enum<?>> enumClass) {
// 获取枚举
List<? extends Enum<?>> enumList = Lists.of(enumClass.getEnumConstants());
// 枚举名称
List<String> names = enumList.stream()
.map(Enum::name)
.collect(Collectors.toList());
// 枚举字段
List<String> fields = Enums.getFields(enumClass);
// 枚举值
List<List<Object>> values = fields.stream()
.map(field -> enumList.stream()
.map(enumItem -> Fields.getFieldValue(enumItem, field))
.collect(Collectors.toList()))
.collect(Collectors.toList());
meta.names.addAll(names);
meta.fields.addAll(fields);
meta.values.addAll(values);
}
/**
* 设置注释
*
* @param comment comment
* @return this
*/
public EnumsTemplate comment(String comment) {
meta.comment = comment;
return this;
}
/**
* 设置枚举名称
*
* @param names names
* @return this
*/
public EnumsTemplate names(String... names) {
meta.names.addAll(Lists.of(names));
return this;
}
/**
* 设置字段值
*
* @param values values
* @return this
*/
public EnumsTemplate values(String field, Object... values) {
meta.fields.add(field);
meta.values.add(Lists.of(values));
return this;
}
/**
* 添加 label
*
* @param labels labels
* @return this
*/
public EnumsTemplate label(Object... labels) {
return this.values("label", labels);
}
/**
* 添加 value
*
* @param values values
* @return this
*/
public EnumsTemplate value(Object... values) {
return this.values("value", values);
}
/**
* 添加 color
*
* @param colors colors
* @return this
*/
public EnumsTemplate color(Object... colors) {
return this.values("color", colors);
}
/**
* 添加 status
*
* @param status status
* @return this
*/
public EnumsTemplate status(Object... status) {
return this.values("status", status);
}
}

View File

@@ -90,4 +90,25 @@ public class ServerTemplate extends Template {
return new CacheTemplate(table);
}
/**
* 设置 cache
*
* @param key key
* @return cache
*/
public CacheTemplate cache(String key) {
return new CacheTemplate(table, key);
}
/**
* 设置 cache
*
* @param key key
* @param desc desc
* @return cache
*/
public CacheTemplate cache(String key, String desc) {
return new CacheTemplate(table, key, desc);
}
}

View File

@@ -1,8 +1,6 @@
package com.orion.ops.launch.generator.template;
import com.orion.lang.utils.collect.Lists;
import lombok.Data;
import lombok.Getter;
import java.util.ArrayList;
import java.util.List;
@@ -14,7 +12,6 @@ import java.util.concurrent.TimeUnit;
* @since 2023/7/17 10:44
*/
@Data
@Getter
public class Table {
// -------------------- 后端 --------------------
@@ -56,6 +53,16 @@ public class Table {
*/
protected String cacheKey;
/**
* 缓存描述
*/
protected String cacheDesc;
/**
* 缓存格式化字段
*/
protected List<String> cacheFormatKeys;
/**
* 缓存是否会过期
*/
@@ -100,75 +107,12 @@ public class Table {
/**
* 生成的枚举文件
* field name [k,v,k,v,k,v] label value color other
*/
protected List<Class<? extends Enum<?>>> enums;
protected List<EnumMeta> enums;
public Table() {
}
public Table(String tableName, String comment, String bizPackage) {
this.tableName = tableName;
this.comment = comment;
this.bizPackage = bizPackage;
this.enableProviderApi = true;
this.enableUnitTest = true;
protected Table() {
this.cacheFormatKeys = new ArrayList<>();
this.enums = new ArrayList<>();
}
/**
* 忽略生成 api
*
* @return this
*/
public Table ignoreApi() {
this.enableProviderApi = false;
return this;
}
/**
* 忽略生成单元测试
*
* @return this
*/
public Table ignoreTest() {
this.enableUnitTest = false;
return this;
}
/**
* 生成 vue 模板
*
* @param module module
* @param feature feature
* @return this
*/
public Table vue(String module, String feature) {
this.enableVue = true;
this.module = module;
this.feature = feature;
return this;
}
/**
* 使用抽屉表单
*
* @return this
*/
public Table useDrawerForm() {
return this;
}
/**
* 生成 vue 枚举对象
*
* @param enums enums
* @return enums
*/
@SafeVarargs
public final Table enums(Class<? extends Enum<?>>... enums) {
this.enums.addAll(Lists.of(enums));
return this;
}
}

View File

@@ -63,4 +63,25 @@ public class VueTemplate extends Template {
return this;
}
/**
* 设置枚举
*
* @param variable 枚举字段 数据库/小驼峰
* @return enums
*/
public EnumsTemplate enums(String variable) {
return new EnumsTemplate(table, variable);
}
/**
* 设置枚举
*
* @param variable 枚举字段 数据库/小驼峰
* @param enumClass 枚举类
* @return enums
*/
public EnumsTemplate enums(String variable, Class<? extends Enum<?>> enumClass) {
return new EnumsTemplate(table, variable, enumClass);
}
}

View File

@@ -1,15 +1,12 @@
#foreach($enumEntity in ${vue.enums.entrySet()})
/**
*
* $!{vue.enumComment.get($enumEntity.key)}
*/
export const $enumEntity.key = {
#foreach($enumEntityItem in $enumEntity.value.entrySet())
$enumEntityItem.key: {
value: null,
label: '',
color: '',
#foreach($enumEntityItemFields in $enumEntityItem.value.entrySet())
$enumEntityItemFields.key: '$enumEntityItemFields.value',
$enumEntityItemFields.key: '$!enumEntityItemFields.value',
#end
},
#end