fix: 新增字典处理

This commit is contained in:
wangyu 2021-03-21 23:50:12 +08:00
parent 4b0a346088
commit 2a39d64ff3
14 changed files with 379 additions and 0 deletions

View File

@ -0,0 +1,14 @@
package com.flyfish.framework.enums;
/**
* 空白的枚举
* @author wangyu
*/
public enum BlankEnum implements NamedEnum {
NONE;
@Override
public String getName() {
return "";
}
}

View File

@ -0,0 +1,26 @@
package com.flyfish.framework.annotations;
import com.flyfish.framework.enums.BlankEnum;
import com.flyfish.framework.enums.NamedEnum;
import java.lang.annotation.*;
/**
* 字典值
*
* @author wangyu
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
@Documented
public @interface DictValue {
// 字典值所属code
String value() default "";
// 使用枚举类标记
Class<? extends NamedEnum> enumType() default BlankEnum.class;
// 中文名称
String name() default "";
}

28
flyfish-dict/pom.xml Normal file
View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>flyfish-framework</artifactId>
<groupId>com.flyfish.framework</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>flyfish-dict</artifactId>
<description>字典框架,集成字典解析,注解,服务,可自动注册和利用枚举反向注册</description>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.flyfish.framework</groupId>
<artifactId>flyfish-web</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,25 @@
package com.flyfish.framework.dict.annotations;
import com.flyfish.framework.dict.config.DictionaryConfig;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* 开启字典处理逻辑
*
* @author wangyu
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DictionaryConfig.class)
public @interface EnableDictProcess {
/**
* 扫描基本路径
*
* @return 结果
*/
String[] basePackages() default "com.flyfish.framework";
}

View File

@ -0,0 +1,23 @@
package com.flyfish.framework.dict.config;
import com.flyfish.framework.dict.service.DictionaryService;
import org.springframework.context.annotation.Bean;
/**
* 字典配置
*
* @author wangyu
*/
public class DictionaryConfig {
/**
* 注册字典处理器开启类扫描注册
*
* @param dictionaryService 字典服务
* @return 结果
*/
@Bean
public DictionaryProcessor dictionaryProcessor(DictionaryService dictionaryService) {
return new DictionaryProcessor(dictionaryService);
}
}

View File

@ -0,0 +1,102 @@
package com.flyfish.framework.dict.config;
import com.flyfish.framework.annotations.DictValue;
import com.flyfish.framework.dict.annotations.EnableDictProcess;
import com.flyfish.framework.dict.domain.Dictionary;
import com.flyfish.framework.dict.domain.DictionaryValue;
import com.flyfish.framework.dict.service.DictionaryService;
import com.flyfish.framework.enums.BlankEnum;
import com.flyfish.framework.enums.NamedEnum;
import com.flyfish.framework.utils.Assert;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.reflections.Reflections;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* 字典处理器
*
* @author wangyu
* 用于反向注册默认值
*/
@RequiredArgsConstructor
public class DictionaryProcessor implements InitializingBean, ImportBeanDefinitionRegistrar {
private final DictionaryService dictionaryService;
private String[] basePackages;
/**
* spring加载完成后尝试注入值
*/
@Override
public void afterPropertiesSet() {
Assert.notNull(basePackages, "未指定明确的字典扫描路径!");
Reflections reflections = new Reflections(Stream.of(basePackages).collect(Collectors.toSet()));
Set<Field> fields = reflections.getFieldsAnnotatedWith(DictValue.class);
if (CollectionUtils.isNotEmpty(fields)) {
// 查找是否存在不存在插入存在无视
List<Dictionary> dictionaries = fields.stream().map(field -> field.getAnnotation(DictValue.class))
.filter(annotation -> null != annotation && StringUtils.isNotBlank(annotation.value()) &&
BlankEnum.class != annotation.enumType())
.map(annotation -> dictionaryService
.getByCode(annotation.value())
.map(dictionary -> {
if (CollectionUtils.isEmpty(dictionary.getValues())) {
dictionary.setValues(mapValues(annotation.enumType()));
}
return dictionary;
})
.orElseGet(() -> {
Dictionary dictionary = new Dictionary();
dictionary.setCode(annotation.value());
dictionary.setValues(mapValues(annotation.enumType()));
dictionary.setName(StringUtils.isNotBlank(annotation.name()) ? annotation.name() : annotation.value());
return dictionary;
}))
.distinct()
.collect(Collectors.toList());
dictionaryService.updateBatch(dictionaries);
}
}
/**
* 映射值转换为字典表的值
*
* @return 结果
*/
private List<DictionaryValue> mapValues(Class<? extends NamedEnum> enumClass) {
return Arrays.stream(enumClass.getEnumConstants()).map(item -> {
DictionaryValue value = new DictionaryValue();
value.setEnable(true);
value.setText(item.getName());
value.setValue(((Enum<?>) item).name());
return value;
}).collect(Collectors.toList());
}
/**
* 注册bean定义可以从这里取得元数据自动加载bean
*
* @param metadata 元数据
* @param registry 注册器
*/
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
Map<String, Object> attrs = metadata.getAnnotationAttributes(EnableDictProcess.class.getCanonicalName(), true);
this.basePackages = (String[]) MapUtils.getObject(attrs, "basePackages");
}
}

View File

@ -0,0 +1,17 @@
package com.flyfish.framework.dict.controller;
import com.flyfish.framework.beans.annotations.RestMapping;
import com.flyfish.framework.controller.BaseController;
import com.flyfish.framework.dict.domain.Dictionary;
import com.flyfish.framework.dict.domain.DictionaryQo;
/**
* 字典表controller
*
* @author wangyu
*/
@RestMapping("dictionaries")
public class DictionaryController extends BaseController<Dictionary, DictionaryQo> {
}

View File

@ -0,0 +1,47 @@
package com.flyfish.framework.dict.domain;
import com.flyfish.framework.domain.base.AuditDomain;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.List;
import java.util.Objects;
/**
* 字典表包含字典值
*
* @author wangyu
*/
@Getter
@Setter
@Document(collection = "dictionaries")
public class Dictionary extends AuditDomain {
// 字典表的值
private List<DictionaryValue> values;
// 字典描述
private String description;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Dictionary)) return false;
if (StringUtils.isBlank(code)) {
return super.equals(o);
}
Dictionary that = (Dictionary) o;
return StringUtils.equals(this.code, that.code);
}
@Override
public int hashCode() {
if (StringUtils.isBlank(code)) {
return super.hashCode();
}
return Objects.hash(getCode());
}
}

View File

@ -0,0 +1,15 @@
package com.flyfish.framework.dict.domain;
import com.flyfish.framework.domain.base.NameLikeQo;
import lombok.Getter;
import lombok.Setter;
/**
* 字典查询实体
*
* @author wangyu
*/
@Getter
@Setter
public class DictionaryQo extends NameLikeQo<Dictionary> {
}

View File

@ -0,0 +1,24 @@
package com.flyfish.framework.dict.domain;
import lombok.Data;
/**
* 字典表的值
*
* @author wangyu
*/
@Data
public class DictionaryValue {
// 显示文字
private String text;
//
private String value;
// 描述
private String description;
// 是否启用
private boolean enable;
}

View File

@ -0,0 +1,22 @@
package com.flyfish.framework.dict.repository;
import com.flyfish.framework.dict.domain.Dictionary;
import com.flyfish.framework.repository.DefaultRepository;
import java.util.Optional;
/**
* 字典的仓库
*
* @author wangyu
*/
public interface DictionaryRepository extends DefaultRepository<Dictionary> {
/**
* 通过code查找
*
* @param code 编码
* @return 结果
*/
Optional<Dictionary> findByCode(String code);
}

View File

@ -0,0 +1,28 @@
package com.flyfish.framework.dict.service;
import com.flyfish.framework.dict.domain.Dictionary;
import com.flyfish.framework.dict.repository.DictionaryRepository;
import com.flyfish.framework.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import java.util.Optional;
/**
* 字典表服务
*
* @author wangyu
*/
@Service
public class DictionaryService extends BaseServiceImpl<Dictionary> {
/**
* 通过code查询
*
* @return 结果
*/
public Optional<Dictionary> getByCode(String code) {
DictionaryRepository repository = this.getRepository();
return repository.findByCode(code);
}
}

View File

@ -1,5 +1,6 @@
package com.flyfish.framework.beans.meta;
import com.flyfish.framework.annotations.DictValue;
import com.flyfish.framework.annotations.Property;
import com.flyfish.framework.utils.StringFormats;
import lombok.Data;
@ -73,6 +74,12 @@ public class BeanProperty {
}
Class<?> clazz = descriptor.getPropertyType();
switch (property.getType()) {
case STRING:
// 如果字段使用DictValue注解尝试使用字典值仓库
if (null != field && field.isAnnotationPresent(DictValue.class)) {
DictValue dictValue = field.getAnnotation(DictValue.class);
property.prop("code", dictValue.value());
}
case LIST:
// 尝试获取泛型参数存在时赋值子表单
if (null != field && field.isAnnotationPresent(SubBean.class)) {

View File

@ -43,6 +43,7 @@
<module>flyfish-user</module>
<module>flyfish-file</module>
<module>flyfish-logging</module>
<module>flyfish-dict</module>
</modules>
<repositories>