feat:新增值联动和扩展覆盖属性

This commit is contained in:
wangyu 2021-09-27 17:09:36 +08:00
parent 2dddab4f31
commit 10b813142b
5 changed files with 80 additions and 14 deletions

View File

@ -7,7 +7,7 @@ import java.lang.annotation.*;
*
* @author wangyu
*/
@Target(ElementType.FIELD)
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FormItem {

View File

@ -0,0 +1,30 @@
package com.flyfish.framework.annotations;
import org.springframework.core.annotation.AliasFor;
import java.lang.annotation.*;
/**
* 字段映射
*
* @author wangyu
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MappedTo {
/**
* 映射到字段复制值
*
* @return 字段名
*/
String value() default "";
/**
* 表达式支持jsjson支持表达式占位value
*
* @return 表达式
*/
String expression() default "";
}

View File

@ -72,4 +72,10 @@ public @interface Property {
* @return 结果
*/
String group() default "";
/**
* 表单注解
* @return 表单注解仅用于构造器
*/
FormItem[] form() default {};
}

View File

@ -1,5 +1,6 @@
package com.flyfish.framework.domain.base;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.flyfish.framework.annotations.Property;
import com.flyfish.framework.domain.po.User;
import lombok.Getter;
@ -45,6 +46,7 @@ public abstract class Domain implements Po, Serializable {
* 上下文冗余利用内存缓存上下文
*/
@Transient
@JsonIgnore
@Property(readonly = true)
private User currentUser;

View File

@ -1,9 +1,7 @@
package com.flyfish.framework.beans.meta;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.flyfish.framework.annotations.Properties;
import com.flyfish.framework.annotations.*;
import com.flyfish.framework.beans.enums.ValidationCandidate;
@ -48,6 +46,10 @@ public class BeanProperty {
// 属性标题
private String title;
// 旧标题
@JsonIgnore
private transient String oldTitle;
// 描述信息
private String description;
@ -75,6 +77,7 @@ public class BeanProperty {
private List<BeanValidation> validation;
// 排序变量用于排序
@JsonIgnore
private transient int order;
// 所属分组该分组必须在元数据中定义
@ -112,11 +115,17 @@ public class BeanProperty {
// 只有存在馆建注解才会进行初始化
String parentName = Optional.ofNullable(beanClass.getAnnotation(RestBean.class))
.map(RestBean::name).orElse("");
property.setTitle(props.inherited() ? parentName + props.title() : props.title());
property.setDescription(props.description());
property.setReadonly(props.readonly());
property.setInherited(props.inherited());
property.setGroup(props.group());
// 继承模式继承名称
if (property.inherited) {
property.setOldTitle(props.title());
property.setTitle(parentName + property.oldTitle);
} else {
property.setTitle(props.title());
}
// 优雅地设置排序
MergedAnnotation<Order> order = annotations.get(Order.class);
if (order.isPresent()) {
@ -127,15 +136,13 @@ public class BeanProperty {
// 优雅的设置额外的属性
MergedAnnotation<FormItem> item = annotations.get(FormItem.class);
if (item.isPresent()) {
FormItem formItem = item.synthesize();
// 设置额外属性
property.extra.put("component", formItem.component());
property.layout = formItem.layout();
if (ArrayUtils.isNotEmpty(formItem.props())) {
for (FormItem.Prop prop : formItem.props()) {
property.prop(prop.key(), prop.value());
}
}
applyFormItem(property, item.synthesize());
}
// 优雅的设置联动映射
MergedAnnotation<MappedTo> mapping = annotations.get(MappedTo.class);
if (mapping.isPresent()) {
property.extra.put("mapping", mapping.asMap());
MappedTo mappedTo = mapping.synthesize();
}
// 优雅的处理校验
property.setValidation(ValidationCandidate.produce(annotations));
@ -300,7 +307,11 @@ public class BeanProperty {
// 存在校验替换名称文案(仅inherit)
if (property.inherited && CollectionUtils.isNotEmpty(property.validation)) {
property.validation.forEach(validation -> validation.setMessage(validation.getMessage()
.replace(property.title, prop.title())));
.replace(property.oldTitle, prop.title())));
}
// 处理额外的表单属性
if (ArrayUtils.isNotEmpty(prop.form())) {
applyFormItem(property, prop.form()[0]);
}
// 设置标题
property.title = prop.title();
@ -344,6 +355,23 @@ public class BeanProperty {
return Optional.empty();
}
/**
* 将formItem注解赋值到目标属性上
*
* @param property 目标属性
* @param item 注解元
*/
private static void applyFormItem(BeanProperty property, FormItem item) {
// 设置额外属性
property.extra.put("component", item.component());
property.layout = item.layout();
if (ArrayUtils.isNotEmpty(item.props())) {
for (FormItem.Prop prop : item.props()) {
property.prop(prop.key(), prop.value());
}
}
}
/**
* 设置当前对象的键值属性
*