From e405855e0369dd78b78dd88b0623b349ae8dd17c Mon Sep 17 00:00:00 2001 From: lijiahang Date: Tue, 26 Sep 2023 15:59:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=99=A8=E9=80=BB=E8=BE=91.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ops/launch/generator/CodeGenerator.java | 18 ++- .../engine/VelocityTemplateEngine.java | 90 ++++++++++--- .../generator/template/CacheTemplate.java | 46 +++++++ .../launch/generator/template/EnumMeta.java | 63 ++++++++++ .../generator/template/EnumsTemplate.java | 119 +++++++++++++++++- .../generator/template/ServerTemplate.java | 21 ++++ .../ops/launch/generator/template/Table.java | 82 ++---------- .../generator/template/VueTemplate.java | 21 ++++ .../orion-vue-views-types-enum.types.ts.vm | 7 +- 9 files changed, 369 insertions(+), 98 deletions(-) create mode 100644 orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/EnumMeta.java diff --git a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/CodeGenerator.java b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/CodeGenerator.java index 1b35237d..96cd0483 100644 --- a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/CodeGenerator.java +++ b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/CodeGenerator.java @@ -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); diff --git a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/engine/VelocityTemplateEngine.java b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/engine/VelocityTemplateEngine.java index 7b519a94..3b5c55d6 100644 --- a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/engine/VelocityTemplateEngine.java +++ b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/engine/VelocityTemplateEngine.java @@ -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 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>> enums = table.getEnums(); + private void setEnumMap(BeanMap beanMap, TableInfo tableInfo, Table table) { + // 枚举注解 + Map enumComment = new HashMap<>(); + // 枚举值 Map> enumMap = new LinkedHashMap<>(); - for (Class> e : enums) { - MultiLinkedHashMap 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 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 + * @return value + */ + private T safeGet(List list, int index) { + if (list == null) { + return null; + } + if (list.size() > index) { + return list.get(index); + } else { + return null; + } + } } diff --git a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/CacheTemplate.java b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/CacheTemplate.java index 629b268e..b14a23ab 100644 --- a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/CacheTemplate.java +++ b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/CacheTemplate.java @@ -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; + } + /** * 设置缓存过期时间 * diff --git a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/EnumMeta.java b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/EnumMeta.java new file mode 100644 index 00000000..8f1ea122 --- /dev/null +++ b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/EnumMeta.java @@ -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 names; + + /** + * 枚举字段 + */ + protected List fields; + + /** + * 枚举值 + */ + protected List> 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 names, List fields, List> 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); + } + +} diff --git a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/EnumsTemplate.java b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/EnumsTemplate.java index 298070b8..ec46b874 100644 --- a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/EnumsTemplate.java +++ b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/EnumsTemplate.java @@ -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> enumClass) { + super(table); + this.meta = new EnumMeta(variable); + table.enums.add(meta); + this.parseEnumMeta(enumClass); + } + + /** + * 解析枚举 + * + * @param enumClass enumClass + */ + private void parseEnumMeta(Class> enumClass) { + // 获取枚举 + List> enumList = Lists.of(enumClass.getEnumConstants()); + // 枚举名称 + List names = enumList.stream() + .map(Enum::name) + .collect(Collectors.toList()); + // 枚举字段 + List fields = Enums.getFields(enumClass); + // 枚举值 + List> 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); } } diff --git a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/ServerTemplate.java b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/ServerTemplate.java index ec1a4214..2d0ed54d 100644 --- a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/ServerTemplate.java +++ b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/ServerTemplate.java @@ -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); + } + } diff --git a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/Table.java b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/Table.java index 1a62b9b8..44931fe4 100644 --- a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/Table.java +++ b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/Table.java @@ -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 cacheFormatKeys; + /** * 缓存是否会过期 */ @@ -100,75 +107,12 @@ public class Table { /** * 生成的枚举文件 - * field name [k,v,k,v,k,v] label value color other */ - protected List>> enums; + protected List 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>... enums) { - this.enums.addAll(Lists.of(enums)); - return this; - } - } diff --git a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/VueTemplate.java b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/VueTemplate.java index 51a08c64..77ff3511 100644 --- a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/VueTemplate.java +++ b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/VueTemplate.java @@ -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> enumClass) { + return new EnumsTemplate(table, variable, enumClass); + } + } diff --git a/orion-ops-launch/src/main/resources/templates/orion-vue-views-types-enum.types.ts.vm b/orion-ops-launch/src/main/resources/templates/orion-vue-views-types-enum.types.ts.vm index ec68b4b1..f8f4437e 100644 --- a/orion-ops-launch/src/main/resources/templates/orion-vue-views-types-enum.types.ts.vm +++ b/orion-ops-launch/src/main/resources/templates/orion-vue-views-types-enum.types.ts.vm @@ -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