feat:实现相当牛逼的链式反应

This commit is contained in:
wangyu 2021-12-12 11:13:11 +08:00
parent 0355957595
commit 787aa73615
14 changed files with 1197 additions and 160 deletions

View File

@ -5,6 +5,8 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import com.flyfish.framework.annotations.Properties;
import com.flyfish.framework.annotations.*;
import com.flyfish.framework.beans.enums.ValidationCandidate;
import com.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations;
import com.flyfish.framework.beans.meta.parser.SimpleBeanPropertyAnnotations;
import com.flyfish.framework.domain.base.Qo;
import com.flyfish.framework.domain.base.Vo;
import com.flyfish.framework.utils.ReflectionUtils;
@ -110,116 +112,174 @@ public class BeanProperty {
boolean strict = Qo.class.isAssignableFrom(beanClass) || Vo.class.isAssignableFrom(beanClass);
// 尝试获取field
Field field = FieldUtils.getField(beanClass, descriptor.getName(), true);
MergedAnnotations annotations = null == field ? MergedAnnotations.from() : MergedAnnotations.from(field);
// 存在field可以干一些坏事
if (null != field) {
// 开始解析关键注解
if (annotations.isPresent(Property.class)) {
Property props = annotations.get(Property.class).synthesize();
// 只有存在关键注解才会进行初始化
String parentName = Optional.ofNullable(beanClass.getAnnotation(RestBean.class))
.map(RestBean::name).orElse("");
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()) {
property.setOrder(order.synthesize().value());
} else {
property.setOrder(props.order());
}
// 追加生成属性
MergedAnnotation<Generation> generation = annotations.get(Generation.class);
if (generation.isPresent()) {
property.extra.put(BeanProps.GENERATED, generation.asMap());
property.extra.put(BeanProps.COMPONENT, "input-hidden");
}
// 追加属性映射
MergedAnnotation<ComputedProps.List> links = annotations.get(ComputedProps.List.class);
if (links.isPresent()) {
property.extra.put(BeanProps.LINKED, Arrays.stream(
links.getAnnotationArray("value", ComputedProps.class)
).map(MergedAnnotation::asMap).collect(Collectors.toList()));
} else {
MergedAnnotation<ComputedProps> single = annotations.get(ComputedProps.class);
if (single.isPresent()) {
property.extra.put(BeanProps.LINKED, Collections.singletonList(single.asMap()));
}
}
// 优雅的设置额外的属性
MergedAnnotation<FormItem> item = annotations.get(FormItem.class);
if (item.isPresent()) {
applyFormItem(property, item.synthesize());
}
// 优雅的设置联动映射
MergedAnnotation<MappedTo> mapping = annotations.get(MappedTo.class);
if (mapping.isPresent()) {
property.extra.put(BeanProps.MAPPING, mapping.asMap());
}
// 处理联动
MergedAnnotation<ConditionOn.List> condition = annotations.get(ConditionOn.List.class);
if (condition.isPresent()) {
property.extra.put(BeanProps.CONDITION, Arrays.stream(
condition.getAnnotationArray("value", ConditionOn.class)
).map(MergedAnnotation::asMap).collect(Collectors.toList()));
} else {
MergedAnnotation<ConditionOn> single = annotations.get(ConditionOn.class);
if (single.isPresent()) {
property.extra.put(BeanProps.CONDITION, Collections.singletonList(single.asMap()));
}
}
// 优雅的处理校验
property.setValidation(ValidationCandidate.produce(annotations, property.getType()));
} else if (strict) {
property.setReadonly(true);
return property;
}
} else if (strict) {
BeanPropertyAnnotations annotations = new SimpleBeanPropertyAnnotations(field);
// 判断是否需要跳过启用严格模式并未标记的属性将直接跳过
boolean skip = null == field || !annotations.isPresent(Property.class);
if (skip && strict) {
property.setReadonly(true);
return property;
}
// 开始解析关键注解, 存在field可以干一些坏事
Property props = annotations.as(Property.class).synthesize();
if (null != props) {
// 只有存在关键注解才会进行初始化
String parentName = Optional.ofNullable(beanClass.getAnnotation(RestBean.class))
.map(RestBean::name).orElse("");
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());
}
// 开始优雅地操作
annotations
// 优雅地设置排序
.as(Order.class)
.map(item -> item.synthesize().value())
.then(property::setOrder)
.empty(() -> property.setOrder(props.order()))
.and()
// 追加生成属性
.as(Generation.class)
.map(MergedAnnotation::asMap)
.then(map -> {
property.extra.put(BeanProps.GENERATED, map);
property.extra.put(BeanProps.COMPONENT, "input-hidden");
})
.and()
// 追加属性映射
.as(ComputedProps.class, ComputedProps.List.class)
.map(MergedAnnotation::asMap)
.then(list -> property.extra.put(BeanProps.LINKED, list))
.and()
// 优雅地设置额外的属性
.as(FormItem.class)
.map(MergedAnnotation::synthesize)
.then(item -> applyFormItem(property, item))
.and()
// 优雅地设置联动映射
.as(MappedTo.class)
.map(MergedAnnotation::asMap)
.then(map -> property.extra.put(BeanProps.MAPPING, map))
.and()
// 处理联动
.as(ConditionOn.class, ConditionOn.List.class)
.map(MergedAnnotation::asMap)
.then(list -> property.extra.put(BeanProps.CONDITION, list))
.end();
// 优雅地处理校验
property.setValidation(ValidationCandidate.produce(annotations.annotations(), property.getType()));
}
// 优雅地设置排序
// MergedAnnotation<Order> order = annotations.get(Order.class);
// if (order.isPresent()) {
// property.setOrder(order.synthesize().value());
// } else {
// property.setOrder(props.order());
// }
// 追加生成属性
// MergedAnnotation<Generation> generation = annotations.get(Generation.class);
// if (generation.isPresent()) {
//
// }
// 追加属性映射
// MergedAnnotation<ComputedProps.List> links = annotations.get(ComputedProps.List.class);
// if (links.isPresent()) {
// property.extra.put(BeanProps.LINKED, Arrays.stream(
// links.getAnnotationArray("value", ComputedProps.class)
// ).map(MergedAnnotation::asMap).collect(Collectors.toList()));
// } else {
// MergedAnnotation<ComputedProps> single = annotations.get(ComputedProps.class);
// if (single.isPresent()) {
// property.extra.put(BeanProps.LINKED, Collections.singletonList(single.asMap()));
// }
// }
// 优雅的设置额外的属性
// MergedAnnotation<FormItem> item = annotations.get(FormItem.class);
// if (item.isPresent()) {
// applyFormItem(property, item.synthesize());
// }
// 优雅的设置联动映射
// MergedAnnotation<MappedTo> mapping = annotations.get(MappedTo.class);
// if (mapping.isPresent()) {
// property.extra.put(BeanProps.MAPPING, mapping.asMap());
// }
// 处理联动
// MergedAnnotation<ConditionOn.List> condition = annotations.get(ConditionOn.List.class);
// if (condition.isPresent()) {
// property.extra.put(BeanProps.CONDITION, Arrays.stream(
// condition.getAnnotationArray("value", ConditionOn.class)
// ).map(MergedAnnotation::asMap).collect(Collectors.toList()));
// } else {
// MergedAnnotation<ConditionOn> single = annotations.get(ConditionOn.class);
// if (single.isPresent()) {
// property.extra.put(BeanProps.CONDITION, Collections.singletonList(single.asMap()));
// }
// }
Class<?> clazz = descriptor.getPropertyType();
switch (property.getType()) {
case STRING:
// 如果字段使用DictValue注解尝试使用字典值仓库
if (null != field) {
// 添加了字典枚举自动添加code从字典表取得
if (annotations.isPresent(DictValue.class)) {
DictValue dictValue = annotations.get(DictValue.class).synthesize();
property.prop("code", dictValue.value());
} else if (annotations.isPresent(EnumValue.class)) {
// 添加了枚举注解自动注入类型给前端使用
property.setType(BeanPropertyType.ENUM);
EnumValue enumValue = annotations.get(EnumValue.class).synthesize();
String name = StringFormats.camel2Line(ClassUtils.getShortClassName(enumValue.value()));
property.prop("code", name);
} else if (annotations.isPresent(DBRefValue.class)) {
// 添加了数据库引用注解自动注入类型给前端使用
DBRefValue dbRefValue = annotations.get(DBRefValue.class).synthesize();
Optional<String> optional = processDbRef(dbRefValue.value());
if (optional.isPresent()) {
property.setType(BeanPropertyType.DB_REF);
property.prop("uri", optional.get());
} else {
property.setType(BeanPropertyType.STRING);
}
}
}
// 添加了字典枚举自动添加code从字典表取得
annotations.as(DictValue.class)
.map(item -> item.synthesize().value())
.then(value -> property.prop("code", value))
.or()
.as(EnumValue.class)
.map(item -> item.synthesize().value())
.then(value -> {
// 添加了枚举注解自动注入类型给前端使用
property.setType(BeanPropertyType.ENUM);
String name = StringFormats.camel2Line(ClassUtils.getShortClassName(value));
property.prop("code", name);
})
.or()
.as(DBRefValue.class)
.map(item -> item.synthesize().value())
.then(value -> {
Optional<String> optional = processDbRef(value);
if (optional.isPresent()) {
property.setType(BeanPropertyType.DB_REF);
property.prop("uri", optional.get());
} else {
property.setType(BeanPropertyType.STRING);
}
})
.end();
// if (annotations.isPresent(DictValue.class)) {
// DictValue dictValue = annotations.get(DictValue.class).synthesize();
// property.prop("code", dictValue.value());
// } else if (annotations.isPresent(EnumValue.class)) {
// // 添加了枚举注解自动注入类型给前端使用
// property.setType(BeanPropertyType.ENUM);
// EnumValue enumValue = annotations.get(EnumValue.class).synthesize();
// String name = StringFormats.camel2Line(ClassUtils.getShortClassName(enumValue.value()));
// property.prop("code", name);
// } else if (annotations.isPresent(DBRefValue.class)) {
// 添加了数据库引用注解自动注入类型给前端使用
// DBRefValue dbRefValue = annotations.get(DBRefValue.class).synthesize();
// Optional<String> optional = processDbRef(dbRefValue.value());
// if (optional.isPresent()) {
// property.setType(BeanPropertyType.DB_REF);
// property.prop("uri", optional.get());
// } else {
// property.setType(BeanPropertyType.STRING);
// }
// }
break;
case NUMBER:
if (null != field) {
if (annotations.isPresent(Money.class) && !property.extra.containsKey(BeanProps.COMPONENT)) {
property.extra.put(BeanProps.COMPONENT, "money-input");
}
annotations.as(Money.class)
.filter(() -> !property.extra.containsKey(BeanProps.COMPONENT))
.exists(() -> property.extra.put(BeanProps.COMPONENT, "money-input"))
.end();
}
break;
case LIST:
@ -228,33 +288,68 @@ public class BeanProperty {
ReflectionUtils.getGenericType(field)
.filter(property::isAttachment)
.ifPresent(item -> property.prop("attachment", true));
if (annotations.isPresent(SubBean.class)) {
// 尝试获取泛型参数存在时赋值子表单
parseSubClass(field).ifPresent(subClazz -> property.setChildren(from(subClazz)));
// 处理替换
applyReplacement(property, annotations);
} else if (annotations.isPresent(DateRange.class)) {
property.setType(BeanPropertyType.DATE);
property
.prop("type", "range")
.prop("placeholder", Arrays.asList("开始时间", "结束时间"));
} else if (annotations.isPresent(DBRefValue.class)) {
// 添加了数据库引用注解自动注入类型给前端使用
DBRefValue dbRefValue = annotations.get(DBRefValue.class).synthesize();
processDbRef(dbRefValue.value()).ifPresent(ref -> {
property.setType(BeanPropertyType.DB_REF);
property.prop("uri", ref).prop("mode", "multiple");
});
}
annotations
.as(SubBean.class)
.map(MergedAnnotation::synthesize)
.then(item -> {
// 尝试获取泛型参数存在时赋值子表单
parseSubClass(field).ifPresent(subClazz -> property.setChildren(from(subClazz)));
// 处理替换
applyReplacement(property, item);
})
.or()
.as(DateRange.class)
.exists(() -> {
property.setType(BeanPropertyType.DATE);
property
.prop("type", "range")
.prop("placeholder", Arrays.asList("开始时间", "结束时间"));
})
.or()
.as(DBRefValue.class)
.map(item -> item.synthesize().value())
// 添加了数据库引用注解自动注入类型给前端使用
.then(value -> processDbRef(value).ifPresent(ref -> {
property.setType(BeanPropertyType.DB_REF);
property.prop("uri", ref).prop("mode", "multiple");
}))
.end();
// if (annotations.isPresent(SubBean.class)) {
// // 尝试获取泛型参数存在时赋值子表单
// parseSubClass(field).ifPresent(subClazz -> property.setChildren(from(subClazz)));
// // 处理替换
// applyReplacement(property, annotations);
// } else if (annotations.isPresent(DateRange.class)) {
// property.setType(BeanPropertyType.DATE);
// property
// .prop("type", "range")
// .prop("placeholder", Arrays.asList("开始时间", "结束时间"));
// } else if (annotations.isPresent(DBRefValue.class)) {
// // 添加了数据库引用注解自动注入类型给前端使用
// DBRefValue dbRefValue = annotations.get(DBRefValue.class).synthesize();
// processDbRef(dbRefValue.value()).ifPresent(ref -> {
// property.setType(BeanPropertyType.DB_REF);
// property.prop("uri", ref).prop("mode", "multiple");
// });
// }
}
break;
case OBJECT:
// 有子bean注解才处理
if (null != field && annotations.isPresent(SubBean.class)) {
property.setChildren(from(clazz));
// 处理替换
applyReplacement(property, annotations);
}
annotations.as(SubBean.class)
.map(MergedAnnotation::synthesize)
.then(value -> {
property.setChildren(from(clazz));
// 处理替换
applyReplacement(property, value);
})
.end();
// 有子bean注解才处理
// if (null != field && annotations.isPresent(SubBean.class)) {
// property.setChildren(from(clazz));
// // 处理替换
// applyReplacement(property, annotations);
// }
break;
case DB_REF:
// 当存在db-ref时解析为动态数据源
@ -276,28 +371,34 @@ public class BeanProperty {
break;
case DATE:
// 为日期自动放入类型和长度
if (null != field) {
if (annotations.isPresent(JsonFormat.class)) {
String pattern = annotations.get(JsonFormat.class).synthesize().pattern();
// yyyy-MM-dd HH:mm:ss
if (StringUtils.isNotBlank(pattern)) {
int length = pattern.length();
if (length == 7) {
property.prop("type", "month");
} else if (length == 10) {
property.prop("type", "date");
} else if (length == 11) {
property.prop("type", "date")
.prop("format", "YYYY年MM月DD日");
} else if (length == 16) {
property.prop("type", "datetime");
property.prop("format", "YYYY-MM-DD HH:mm");
} else if (length > 16) {
property.prop("format", "YYYY-MM-DD HH:mm:ss");
annotations.as(JsonFormat.class)
.map(item -> item.synthesize().pattern())
.then(pattern -> {
// yyyy-MM-dd HH:mm:ss
if (StringUtils.isNotBlank(pattern)) {
int length = pattern.length();
if (length == 7) {
property.prop("type", "month");
} else if (length == 10) {
property.prop("type", "date");
} else if (length == 11) {
property.prop("type", "date")
.prop("format", "YYYY年MM月DD日");
} else if (length == 16) {
property.prop("type", "datetime");
property.prop("format", "YYYY-MM-DD HH:mm");
} else if (length > 16) {
property.prop("format", "YYYY-MM-DD HH:mm:ss");
}
}
}
}
}
})
.end();
// if (null != field) {
// if (annotations.isPresent(JsonFormat.class)) {
// String pattern = annotations.get(JsonFormat.class).synthesize().pattern();
//
// }
// }
}
// 写入默认值
Object value = ReflectionUtils.getFieldValue(instance, property.name);
@ -472,13 +573,12 @@ public class BeanProperty {
}
/**
* 替换文案的生效
* 替换子表单文案的生效
*
* @param property 属性
* @param annotations 注解
* @param property 属性
* @param subBean 注解
*/
private static void applyReplacement(BeanProperty property, MergedAnnotations annotations) {
SubBean subBean = annotations.get(SubBean.class).synthesize();
private static void applyReplacement(BeanProperty property, SubBean subBean) {
if (CollectionUtils.isNotEmpty(property.getChildren()) && StringUtils.isNotBlank(subBean.search())
&& StringUtils.isNotBlank(subBean.replacement())) {
property.getChildren().forEach(child -> child.setTitle(child.getTitle()

View File

@ -0,0 +1,10 @@
package com.flyfish.framework.beans.meta.parser;
/**
* bean属性注解解析器
* @author wangyu
*/
public class BeanPropertyAnnotationParser {
}

View File

@ -0,0 +1,87 @@
package com.flyfish.framework.beans.meta.parser;
import com.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationBatchChain;
import com.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationChain;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import java.lang.annotation.Annotation;
/**
* 基于spring annotations最高级封装
* 1. 全optional机制
* 2. 完整链式调用
* 3. 支持或与操作
*
* @author wangyu
*/
public interface BeanPropertyAnnotations {
/**
* 判断是否有某个注解
*
* @param annotationType 注解类型
* @param <A> 泛型
* @return 结果
*/
<A extends Annotation> boolean isPresent(Class<A> annotationType);
/**
* 获取纯粹的spring注解属性表
*
* @return 结果
*/
MergedAnnotations annotations();
/**
* 判断某个注解是否存在存在则交给consumer进行处理仅取得找到的第一个注解
*
* @param annotationType 注解类型
* @param <A> 泛型
* @return 结果
*/
<A extends Annotation> BeanPropertyAnnotationChain<A, MergedAnnotation<A>> as(Class<A> annotationType);
/**
* 判断某个注解是否存在存在则交给consumer进行处理取得所有注解并自动判断复数形式
*
* @param annotationType 注解类型
* @param listType 列表类型
* @param <A> 泛型
* @param <L> 列表泛型
* @return 结果
*/
<A extends Annotation, L extends Annotation> BeanPropertyAnnotationBatchChain<A, MergedAnnotation<A>> as(
Class<A> annotationType, Class<L> listType);
/**
* 设置上一步结果
*
* @param success 是否成功
* @return 结果
*/
BeanPropertyAnnotations last(boolean success);
/**
* 返回上一步成功结果
*
* @return 结果
*/
boolean last();
/**
* 返回空的运行链
*
* @param <A> 注解泛型
* @return 结果
*/
<A extends Annotation, E> BeanPropertyAnnotationChain<A, E> empty();
/**
* 返回空的运行链
*
* @param <A> 注解泛型
* @return 结果
*/
<A extends Annotation, E> BeanPropertyAnnotationBatchChain<A, E> batchEmpty();
}

View File

@ -0,0 +1,150 @@
package com.flyfish.framework.beans.meta.parser;
import com.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationBatchChain;
import com.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationChain;
import com.flyfish.framework.beans.meta.parser.chain.impl.EmptyAnnotationBatchChain;
import com.flyfish.framework.beans.meta.parser.chain.impl.EmptyAnnotationChain;
import com.flyfish.framework.beans.meta.parser.chain.impl.SimpleAnnotationBatchChain;
import com.flyfish.framework.beans.meta.parser.chain.impl.SimpleAnnotationChain;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.data.util.CastUtils;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* bean属性注解容器
*
* @author wangyu
*/
public class SimpleBeanPropertyAnnotations implements BeanPropertyAnnotations {
private final Field field;
private final MergedAnnotations annotations;
private final EmptyAnnotationChain<? extends Annotation, ?> emptyChain = new EmptyAnnotationChain<>(this);
private final EmptyAnnotationBatchChain<? extends Annotation, ?> emptyBatchChain = new EmptyAnnotationBatchChain<>(this);
private boolean last;
public SimpleBeanPropertyAnnotations(Field field) {
this.field = field;
this.annotations = null == field ? MergedAnnotations.from() : MergedAnnotations.from(field);
}
/**
* 判断是否有某个注解
*
* @param annotationType 注解类型
* @return 结果
*/
@Override
public <A extends Annotation> boolean isPresent(Class<A> annotationType) {
if (null == field) {
return false;
}
return annotations.isPresent(annotationType);
}
/**
* 获取纯粹的spring注解属性表
*
* @return 结果
*/
@Override
public MergedAnnotations annotations() {
return annotations;
}
/**
* 判断某个注解是否存在存在则交给consumer进行处理仅取得找到的第一个注解
*
* @param annotationType 注解类型
* @return 结果
*/
@Override
public <A extends Annotation> BeanPropertyAnnotationChain<A, MergedAnnotation<A>> as(Class<A> annotationType) {
if (null != field && !this.last) {
MergedAnnotation<A> annotation = this.annotations.get(annotationType);
if (annotation.isPresent()) {
return new SimpleAnnotationChain<>(this.last(true), annotation);
}
}
return empty();
}
/**
* 判断某个注解是否存在存在则交给consumer进行处理取得所有注解并自动判断复数形式
*
* @param annotationType 注解类型
* @param listType 列表类型
* @param <A> 泛型
* @param <L> 列表泛型
* @return 结果
*/
@Override
public <A extends Annotation, L extends Annotation> BeanPropertyAnnotationBatchChain<A, MergedAnnotation<A>> as(
Class<A> annotationType, Class<L> listType) {
if (null != field && !this.last) {
MergedAnnotation<L> annotation = this.annotations.get(listType);
if (annotation.isPresent()) {
return new SimpleAnnotationBatchChain<>(this.last(true),
Arrays.asList(annotation.getAnnotationArray("value", annotationType)));
} else {
List<MergedAnnotation<A>> values = this.annotations.stream(annotationType)
.filter(MergedAnnotation::isPresent)
.collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(values)) {
return new SimpleAnnotationBatchChain<>(this.last(true), values);
}
}
}
return batchEmpty();
}
/**
* 设置上一步结果
*
* @param success 是否成功
* @return 结果
*/
@Override
public SimpleBeanPropertyAnnotations last(boolean success) {
this.last = success;
return this;
}
/**
* 返回上一步成功结果
*
* @return 结果
*/
@Override
public boolean last() {
return this.last;
}
/**
* 返回空的运行链
*
* @return 结果
*/
@Override
public <A extends Annotation, E> BeanPropertyAnnotationChain<A, E> empty() {
return CastUtils.cast(emptyChain);
}
/**
* 返回空的运行链
*
* @return 结果
*/
@Override
public <A extends Annotation, E> BeanPropertyAnnotationBatchChain<A, E> batchEmpty() {
return CastUtils.cast(emptyBatchChain);
}
}

View File

@ -0,0 +1,30 @@
package com.flyfish.framework.beans.meta.parser.chain;
import com.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations;
/**
* 链式支持
*
* @author wangyu
*/
public interface AnnotationChainSupport {
/**
* 下一步要做什么
*
* @return 结果
*/
BeanPropertyAnnotations and();
/**
* 下一步要做什么如果这一步成立下一步不执行反之这一步不成立下一步才执行
*
* @return 结果
*/
BeanPropertyAnnotations or();
/**
* 主动停止操作会清空前面的状态
*/
void end();
}

View File

@ -0,0 +1,75 @@
package com.flyfish.framework.beans.meta.parser.chain;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* 抽象的链式操作作为批量支持
*
* @param <A> 注解泛型
* @param <T> 入参类型
*/
public interface BeanPropertyAnnotationBatchChain<A extends Annotation, T> extends AnnotationChainSupport {
/**
* 映射转换将转换每一条
*
* @param mapper 映射器
* @param <R> 泛型
* @return 结果
*/
<R> BeanPropertyAnnotationBatchChain<A, R> map(Function<T, R> mapper);
/**
* 过滤不满足的将过滤如果为空将empty
*
* @param filter 过滤器
* @return 结果
*/
BeanPropertyAnnotationBatchChain<A, T> filter(Predicate<T> filter);
/**
* 过滤不满足的将过滤如果为空将empty
*
* @param filter 过滤器
* @return 结果
*/
BeanPropertyAnnotationBatchChain<A, T> filter(Supplier<Boolean> filter);
/**
* 如果存在则做
*
* @param consumer 消费者
* @return 结果
*/
BeanPropertyAnnotationBatchChain<A, T> then(Consumer<List<T>> consumer);
/**
* 存在时执行逻辑不需要结果
*
* @param handler 处理器
* @return 结果
*/
BeanPropertyAnnotationBatchChain<A, T> exists(Runnable handler);
/**
* 不存在做的事情
*
* @param fallback 补偿逻辑
* @return 结果
*/
BeanPropertyAnnotationBatchChain<A, T> empty(Runnable fallback);
/**
* 获取所有结果为list
*
* @return 结果
*/
List<A> synthesize();
}

View File

@ -0,0 +1,73 @@
package com.flyfish.framework.beans.meta.parser.chain;
import java.lang.annotation.Annotation;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* 抽象的链式操作
*
* @param <A> 注解泛型
* @param <T> 入参类型
*/
public interface BeanPropertyAnnotationChain<A extends Annotation, T> extends AnnotationChainSupport {
/**
* 映射转换
*
* @param mapper 映射器
* @param <R> 泛型
* @return 结果
*/
<R> BeanPropertyAnnotationChain<A, R> map(Function<T, R> mapper);
/**
* 过滤不满足的将直接empty
*
* @param filter 过滤器
* @return 结果
*/
BeanPropertyAnnotationChain<A, T> filter(Predicate<T> filter);
/**
* 过滤不满足的将直接empty
*
* @param filter 过滤器
* @return 结果
*/
BeanPropertyAnnotationChain<A, T> filter(Supplier<Boolean> filter);
/**
* 如果存在则做
*
* @param consumer 消费者
* @return 结果
*/
BeanPropertyAnnotationChain<A, T> then(Consumer<T> consumer);
/**
* 存在时执行逻辑不需要结果
*
* @param handler 处理器
* @return 结果
*/
BeanPropertyAnnotationChain<A, T> exists(Runnable handler);
/**
* 不存在做的事情
*
* @param fallback 补偿逻辑
* @return 结果
*/
BeanPropertyAnnotationChain<A, T> empty(Runnable fallback);
/**
* 直接获取结果
*
* @return 结果
*/
A synthesize();
}

View File

@ -0,0 +1,45 @@
package com.flyfish.framework.beans.meta.parser.chain.impl;
import com.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations;
import com.flyfish.framework.beans.meta.parser.chain.AnnotationChainSupport;
import lombok.RequiredArgsConstructor;
/**
* 基本的链式支持
*
* @author wangyu
*/
@RequiredArgsConstructor
public class BasicAnnotationChainSupport implements AnnotationChainSupport {
// 父级
protected final BeanPropertyAnnotations parent;
/**
* 下一步要做什么
*
* @return 结果
*/
@Override
public BeanPropertyAnnotations and() {
return parent.last(false);
}
/**
* 下一步要做什么如果这一步不成立才执行
*
* @return 结果
*/
@Override
public BeanPropertyAnnotations or() {
return parent;
}
/**
* 主动停止操作会清空前面的状态
*/
@Override
public void end() {
parent.last(false);
}
}

View File

@ -0,0 +1,106 @@
package com.flyfish.framework.beans.meta.parser.chain.impl;
import com.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations;
import com.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationBatchChain;
import org.springframework.data.util.CastUtils;
import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* 单例的空实现
*
* @param <A> 注解泛型
* @param <T> 实际泛型
*/
public class EmptyAnnotationBatchChain<A extends Annotation, T> extends BasicAnnotationChainSupport implements BeanPropertyAnnotationBatchChain<A, T> {
public EmptyAnnotationBatchChain(BeanPropertyAnnotations parent) {
super(parent);
}
/**
* 映射转换将转换每一条
*
* @param mapper 映射器
* @return 结果
*/
@Override
public <R> BeanPropertyAnnotationBatchChain<A, R> map(Function<T, R> mapper) {
return CastUtils.cast(this);
}
/**
* 过滤不满足的将过滤如果为空将empty
*
* @param filter 过滤器
* @return 结果
*/
@Override
public BeanPropertyAnnotationBatchChain<A, T> filter(Predicate<T> filter) {
return this;
}
/**
* 过滤不满足的将过滤如果为空将empty
*
* @param filter 过滤器
* @return 结果
*/
@Override
public BeanPropertyAnnotationBatchChain<A, T> filter(Supplier<Boolean> filter) {
return this;
}
/**
* 如果存在则做
*
* @param consumer 消费者
* @return 结果
*/
@Override
public BeanPropertyAnnotationBatchChain<A, T> then(Consumer<List<T>> consumer) {
return this;
}
/**
* 存在时执行逻辑不需要结果
*
* @param handler 处理器
* @return 结果
*/
@Override
public BeanPropertyAnnotationBatchChain<A, T> exists(Runnable handler) {
return this;
}
/**
* 不存在做的事情
*
* @param fallback 补偿逻辑
* @return 结果
*/
@Override
public BeanPropertyAnnotationBatchChain<A, T> empty(Runnable fallback) {
if (!parent.last()) {
fallback.run();
}
return this;
}
/**
* 获取所有结果为list
*
* @return 结果
*/
@Override
public List<A> synthesize() {
parent.last(false);
return Collections.emptyList();
}
}

View File

@ -0,0 +1,104 @@
package com.flyfish.framework.beans.meta.parser.chain.impl;
import com.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations;
import com.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationChain;
import org.springframework.data.util.CastUtils;
import java.lang.annotation.Annotation;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* 单例的空实现
*
* @param <A> 注解泛型
* @param <T> 实际泛型
*/
public class EmptyAnnotationChain<A extends Annotation, T> extends BasicAnnotationChainSupport
implements BeanPropertyAnnotationChain<A, T> {
public EmptyAnnotationChain(BeanPropertyAnnotations parent) {
super(parent);
}
/**
* 映射转换
*
* @param mapper 映射器
* @return 结果
*/
@Override
public <R> BeanPropertyAnnotationChain<A, R> map(Function<T, R> mapper) {
return CastUtils.cast(this);
}
/**
* 过滤不满足的将直接empty
*
* @param filter 过滤器
* @return 结果
*/
@Override
public BeanPropertyAnnotationChain<A, T> filter(Predicate<T> filter) {
return this;
}
/**
* 过滤不满足的将直接empty
*
* @param filter 过滤器
* @return 结果
*/
@Override
public BeanPropertyAnnotationChain<A, T> filter(Supplier<Boolean> filter) {
return this;
}
/**
* 如果存在则做
*
* @param consumer 消费者
* @return 结果
*/
@Override
public BeanPropertyAnnotationChain<A, T> then(Consumer<T> consumer) {
return this;
}
/**
* 存在时执行逻辑不需要结果
*
* @param handler 处理器
* @return 结果
*/
@Override
public BeanPropertyAnnotationChain<A, T> exists(Runnable handler) {
return this;
}
/**
* 不存在做的事情
*
* @param fallback 补偿逻辑
* @return 结果
*/
@Override
public BeanPropertyAnnotationChain<A, T> empty(Runnable fallback) {
if (!parent.last()) {
fallback.run();
}
return this;
}
/**
* 直接获取结果
*
* @return 结果
*/
@Override
public A synthesize() {
return null;
}
}

View File

@ -0,0 +1,134 @@
package com.flyfish.framework.beans.meta.parser.chain.impl;
import com.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations;
import com.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationBatchChain;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.data.util.CastUtils;
import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
/**
* 简单的批量注解链式实现
*
* @param <A> 注解类型
* @param <T> 实际类型
*/
public class SimpleAnnotationBatchChain<A extends Annotation, T> extends BasicAnnotationChainSupport
implements BeanPropertyAnnotationBatchChain<A, T> {
// 内置值
private List<T> values;
// 是否map过
private boolean mapped = false;
public SimpleAnnotationBatchChain(BeanPropertyAnnotations parent, List<T> annotations) {
super(parent);
this.values = annotations;
}
/**
* 映射转换将转换每一条
*
* @param mapper 映射器
* @return 结果
*/
@Override
public <R> BeanPropertyAnnotationBatchChain<A, R> map(Function<T, R> mapper) {
List<R> list = values.stream().map(mapper).filter(Objects::nonNull)
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(list)) {
return parent.batchEmpty();
}
this.mapped = true;
this.values = CastUtils.cast(list);
return CastUtils.cast(this);
}
/**
* 过滤不满足的将过滤如果为空将empty
*
* @param filter 过滤器
* @return 结果
*/
@Override
public BeanPropertyAnnotationBatchChain<A, T> filter(Predicate<T> filter) {
this.values = values.stream().filter(filter).collect(Collectors.toList());
if (CollectionUtils.isEmpty(values)) {
return parent.batchEmpty();
}
return this;
}
/**
* 过滤不满足的将过滤如果为空将empty
*
* @param filter 过滤器
* @return 结果
*/
@Override
public BeanPropertyAnnotationBatchChain<A, T> filter(Supplier<Boolean> filter) {
if (!filter.get()) {
return parent.batchEmpty();
}
return this;
}
/**
* 如果存在则做
*
* @param consumer 消费者
* @return 结果
*/
@Override
public BeanPropertyAnnotationBatchChain<A, T> then(Consumer<List<T>> consumer) {
this.parent.last(true);
consumer.accept(this.values);
return this;
}
/**
* 存在时执行逻辑不需要结果
*
* @param handler 处理器
* @return 结果
*/
@Override
public BeanPropertyAnnotationBatchChain<A, T> exists(Runnable handler) {
this.parent.last(true);
handler.run();
return this;
}
/**
* 不存在做的事情
*
* @param fallback 补偿逻辑
* @return 结果
*/
@Override
public BeanPropertyAnnotationBatchChain<A, T> empty(Runnable fallback) {
return this;
}
/**
* 获取所有结果为list
*
* @return 结果
*/
@Override
@SuppressWarnings("unchecked")
public List<A> synthesize() {
return mapped ? Collections.emptyList() : this.values.stream().map(item -> (MergedAnnotation<A>) item)
.map(MergedAnnotation::synthesize).collect(Collectors.toList());
}
}

View File

@ -0,0 +1,124 @@
package com.flyfish.framework.beans.meta.parser.chain.impl;
import com.flyfish.framework.beans.meta.parser.BeanPropertyAnnotations;
import com.flyfish.framework.beans.meta.parser.chain.BeanPropertyAnnotationChain;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.data.util.CastUtils;
import java.lang.annotation.Annotation;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* 简单的注解链
*
* @param <A> 注解类型
* @param <T> 泛型
*/
public class SimpleAnnotationChain<A extends Annotation, T> extends BasicAnnotationChainSupport
implements BeanPropertyAnnotationChain<A, T> {
// 内置值
private T value;
public SimpleAnnotationChain(BeanPropertyAnnotations parent, T value) {
super(parent);
this.value = value;
}
/**
* 映射转换
*
* @param mapper 映射器
* @return 结果
*/
@Override
public <R> BeanPropertyAnnotationChain<A, R> map(Function<T, R> mapper) {
R value = mapper.apply(this.value);
if (null == value) {
return parent.empty();
}
this.value = CastUtils.cast(value);
return CastUtils.cast(this);
}
/**
* 过滤不满足的将直接empty
*
* @param filter 过滤器
* @return 结果
*/
@Override
public BeanPropertyAnnotationChain<A, T> filter(Predicate<T> filter) {
// 测试不通过直接返回空释放内存
if (!filter.test(this.value)) {
return parent.empty();
}
return this;
}
/**
* 过滤不满足的将直接empty
*
* @param filter 过滤器
* @return 结果
*/
@Override
public BeanPropertyAnnotationChain<A, T> filter(Supplier<Boolean> filter) {
if (!filter.get()) {
return parent.empty();
}
return this;
}
/**
* 如果存在则做
*
* @param consumer 消费者
* @return 结果
*/
@Override
public BeanPropertyAnnotationChain<A, T> then(Consumer<T> consumer) {
parent.last(true);
consumer.accept(value);
return this;
}
/**
* 存在时执行逻辑不需要结果
*
* @param handler 处理器
* @return 结果
*/
@Override
public BeanPropertyAnnotationChain<A, T> exists(Runnable handler) {
parent.last(true);
handler.run();
return this;
}
/**
* 不存在做的事情
*
* @param fallback 补偿逻辑
* @return 结果
*/
@Override
public BeanPropertyAnnotationChain<A, T> empty(Runnable fallback) {
return this;
}
/**
* 直接获取结果
*
* @return 结果
*/
@Override
@SuppressWarnings("unchecked")
public A synthesize() {
parent.last(false);
return value instanceof MergedAnnotation ? ((MergedAnnotation<A>) value).synthesize() : null;
}
}

View File

@ -7,7 +7,7 @@ import com.flyfish.framework.configuration.resolver.RequestContextBodyArgumentRe
import com.flyfish.framework.configuration.resolver.UserArgumentResolver;
import com.flyfish.framework.configuration.resolver.ValidRequestBodyMethodArgumentResolver;
import com.flyfish.framework.context.SpringContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jackson.JacksonProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.ReactiveAdapterRegistry;
@ -20,9 +20,9 @@ import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter;
import javax.annotation.Resource;
import java.util.Optional;
import java.util.TimeZone;
/**
@ -35,8 +35,8 @@ import java.util.TimeZone;
@Order(Ordered.HIGHEST_PRECEDENCE)
public class WebfluxConfig implements WebFluxConfigurer {
@Value("${spring.jackson.date-format}")
private String dateFormat = "yyyy-MM-dd HH:mm:ss";
@Resource
private JacksonProperties jacksonProperties;
/**
* Configure resolvers for custom {@code @RequestMapping} method arguments.
@ -64,13 +64,12 @@ public class WebfluxConfig implements WebFluxConfigurer {
TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
CodecConfigurer.DefaultCodecs defaults = configurer.defaultCodecs();
ObjectMapper mapper = Jackson2ObjectMapperBuilder.json()
.simpleDateFormat(dateFormat)
.simpleDateFormat(Optional.ofNullable(jacksonProperties.getDateFormat()).orElse("yyyy-MM-dd HH:mm:ss"))
.timeZone("GMT+8")
.serializationInclusion(JsonInclude.Include.NON_NULL)
.serializationInclusion(Optional.ofNullable(jacksonProperties.getDefaultPropertyInclusion())
.orElse(JsonInclude.Include.NON_NULL))
.build();
defaults.jackson2JsonDecoder(
new Jackson2JsonDecoder(mapper));
defaults.jackson2JsonEncoder(
new Jackson2JsonEncoder(mapper));
defaults.jackson2JsonDecoder(new Jackson2JsonDecoder(mapper));
defaults.jackson2JsonEncoder(new Jackson2JsonEncoder(mapper));
}
}

View File

@ -290,7 +290,7 @@ public class BaseReactiveServiceImpl<T extends Domain> implements BaseReactiveSe
public Mono<Void> deleteBatchByIds(List<String> ids) {
return Flux.fromIterable(ids)
.flatMap(t -> repository.deleteById(t))
.single();
.then(Mono.empty());
}
/**