From 9100c2214a77cfb02bc8cc4a476491927ad4e135 Mon Sep 17 00:00:00 2001 From: wangyu <727842003@qq.com> Date: Tue, 28 Sep 2021 17:40:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=E6=89=8B=E6=9C=BA?= =?UTF-8?q?=E5=8F=B7=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/annotations/ConditionOn.java | 43 +++++++++++++++++++ .../framework/annotations/Generation.java | 42 ++++++++++++++++++ .../framework/annotations/Property.java | 5 +++ .../flyfish/framework/domain/base/Domain.java | 3 ++ .../framework/beans/meta/BeanProperty.java | 18 +++++++- .../framework/beans/meta/BeanProps.java | 4 ++ 6 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 flyfish-data/src/main/java/com/flyfish/framework/annotations/ConditionOn.java create mode 100644 flyfish-data/src/main/java/com/flyfish/framework/annotations/Generation.java diff --git a/flyfish-data/src/main/java/com/flyfish/framework/annotations/ConditionOn.java b/flyfish-data/src/main/java/com/flyfish/framework/annotations/ConditionOn.java new file mode 100644 index 0000000..68b3202 --- /dev/null +++ b/flyfish-data/src/main/java/com/flyfish/framework/annotations/ConditionOn.java @@ -0,0 +1,43 @@ +package com.flyfish.framework.annotations; + +import com.flyfish.framework.annotations.ConditionOn.List; + +import java.lang.annotation.*; + +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * 条件处理 + * 可以通过条件处理,动态的显隐字段 + * 包括表达式校验,支持repeat + */ +@Target({ANNOTATION_TYPE, FIELD, TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Repeatable(List.class) +public @interface ConditionOn { + + /** + * @return 判定的字段 + */ + String field() default ""; + + /** + * @return 判定的值 + */ + String value() default ""; + + /** + * @return js表达式 + */ + String expression() default ""; + + @Target({FIELD, TYPE, ANNOTATION_TYPE}) + @Retention(RUNTIME) + @Documented + @interface List { + + ConditionOn[] value() default {}; + } +} diff --git a/flyfish-data/src/main/java/com/flyfish/framework/annotations/Generation.java b/flyfish-data/src/main/java/com/flyfish/framework/annotations/Generation.java new file mode 100644 index 0000000..4cfcfa2 --- /dev/null +++ b/flyfish-data/src/main/java/com/flyfish/framework/annotations/Generation.java @@ -0,0 +1,42 @@ +package com.flyfish.framework.annotations; + +import java.lang.annotation.*; + +/** + * 值生成策略 + * 值生成只发生在表单初始化阶段 + * 对于父类使用的场景,如果子类进行了属性覆盖,但未指定生成策略,父类策略会被忽略 + * + * @author wangyu + */ +@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface Generation { + + /** + * 生成策略 + * + * @return 结果 + */ + Strategy value() default Strategy.NONE; + + /** + * 来自某个字段 + * + * @return 结果 + */ + String[] from() default {}; + + /** + * 模板,支持生成模板 + * + * @return 结果 + */ + String template() default ""; + + enum Strategy { + + NONE, NAME, CODE, TIMESTAMP, TRANSACTION + } +} diff --git a/flyfish-data/src/main/java/com/flyfish/framework/annotations/Property.java b/flyfish-data/src/main/java/com/flyfish/framework/annotations/Property.java index bb0a172..c6d8aa4 100644 --- a/flyfish-data/src/main/java/com/flyfish/framework/annotations/Property.java +++ b/flyfish-data/src/main/java/com/flyfish/framework/annotations/Property.java @@ -86,4 +86,9 @@ public @interface Property { * @return 映射的注解,仅用于构造器 */ MappedTo[] mapping() default {}; + + /** + * 自动生成的值 + */ + Generation[] generated() default {}; } diff --git a/flyfish-data/src/main/java/com/flyfish/framework/domain/base/Domain.java b/flyfish-data/src/main/java/com/flyfish/framework/domain/base/Domain.java index 795aca4..d77e6f1 100644 --- a/flyfish-data/src/main/java/com/flyfish/framework/domain/base/Domain.java +++ b/flyfish-data/src/main/java/com/flyfish/framework/domain/base/Domain.java @@ -1,6 +1,7 @@ package com.flyfish.framework.domain.base; import com.fasterxml.jackson.annotation.JsonIgnore; +import com.flyfish.framework.annotations.Generation; import com.flyfish.framework.annotations.Property; import com.flyfish.framework.domain.po.User; import lombok.Getter; @@ -32,6 +33,7 @@ public abstract class Domain implements Po, Serializable { */ @Property(title = "编码", inherited = true) @NotBlank(message = "编码不可为空") + @Generation(Generation.Strategy.CODE) protected String code; /** @@ -40,6 +42,7 @@ public abstract class Domain implements Po, Serializable { @Indexed @Property(title = "名称", inherited = true) @NotBlank(message = "名称不可为空") + @Generation(Generation.Strategy.NAME) protected String name; /** diff --git a/flyfish-web/src/main/java/com/flyfish/framework/beans/meta/BeanProperty.java b/flyfish-web/src/main/java/com/flyfish/framework/beans/meta/BeanProperty.java index 73b0e83..68a053a 100644 --- a/flyfish-web/src/main/java/com/flyfish/framework/beans/meta/BeanProperty.java +++ b/flyfish-web/src/main/java/com/flyfish/framework/beans/meta/BeanProperty.java @@ -134,6 +134,12 @@ public class BeanProperty { } else { property.setOrder(props.order()); } + // 追加生成属性 + MergedAnnotation generation = annotations.get(Generation.class); + if (generation.isPresent()) { + property.extra.put(BeanProps.GENERATED, generation.asMap()); + property.extra.put(BeanProps.COMPONENT, "input-hidden"); + } // 优雅的设置额外的属性 MergedAnnotation item = annotations.get(FormItem.class); if (item.isPresent()) { @@ -185,7 +191,7 @@ public class BeanProperty { case NUMBER: if (null != field) { if (annotations.isPresent(Money.class) && !property.extra.containsKey("component")) { - property.extra.put("component", "money-input"); + property.extra.put(BeanProps.COMPONENT, "money-input"); } } break; @@ -324,6 +330,12 @@ public class BeanProperty { if (ArrayUtils.isNotEmpty(prop.mapping())) { property.extra.put("mapping", MergedAnnotations.from(prop.mapping()).get(MappedTo.class).asMap()); } + // 处理值生成策略,此处可清除原策略 + if (ArrayUtils.isNotEmpty(prop.generated())) { + property.extra.put(BeanProps.GENERATED, MergedAnnotations.from(prop.mapping()).get(MappedTo.class).asMap()); + } else { + property.extra.remove(BeanProps.GENERATED); + } // 设置标题 property.title = prop.title(); } else { @@ -374,7 +386,9 @@ public class BeanProperty { */ private static void applyFormItem(BeanProperty property, FormItem item) { // 设置额外属性 - property.extra.put("component", item.component()); + if (StringUtils.isNotBlank(item.component())) { + property.extra.put(BeanProps.COMPONENT, item.component()); + } property.layout = item.layout(); if (ArrayUtils.isNotEmpty(item.props())) { for (FormItem.Prop prop : item.props()) { diff --git a/flyfish-web/src/main/java/com/flyfish/framework/beans/meta/BeanProps.java b/flyfish-web/src/main/java/com/flyfish/framework/beans/meta/BeanProps.java index 2281eca..1ad7ecf 100644 --- a/flyfish-web/src/main/java/com/flyfish/framework/beans/meta/BeanProps.java +++ b/flyfish-web/src/main/java/com/flyfish/framework/beans/meta/BeanProps.java @@ -17,4 +17,8 @@ public interface BeanProps { String LAYOUT_HALF = "half"; String LAYOUT_TABLE = "table"; + + String COMPONENT = "component"; + + String GENERATED = "generated"; }