init
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package cn.datax.common.dictionary.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Target({ ElementType.FIELD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface DictAop {
|
||||
|
||||
/** 字典编码 */
|
||||
String code() default "";
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package cn.datax.common.dictionary.config;
|
||||
|
||||
import cn.datax.common.core.JsonPage;
|
||||
import cn.datax.common.core.R;
|
||||
import cn.datax.common.dictionary.annotation.DictAop;
|
||||
import cn.datax.common.dictionary.utils.DictUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
import org.springframework.http.server.ServerHttpResponse;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ControllerAdvice
|
||||
public class DictAnalysis implements ResponseBodyAdvice {
|
||||
|
||||
@Override
|
||||
public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
|
||||
if (o instanceof R) {
|
||||
if (((R) o).getData() instanceof JsonPage) {
|
||||
List list = ((JsonPage) ((R) o).getData()).getData();
|
||||
List<JSONObject> items = new ArrayList<>();
|
||||
for (Object record : list) {
|
||||
JSONObject item = JSONObject.parseObject(JSON.toJSONString(record));
|
||||
for (Field field : record.getClass().getDeclaredFields()) {
|
||||
// 获取自定义注解
|
||||
DictAop dictAop = field.getAnnotation(DictAop.class);
|
||||
if (null != dictAop) {
|
||||
String code = dictAop.code();
|
||||
String text = field.getName();
|
||||
Object object = item.get(field.getName());
|
||||
if (null != object) {
|
||||
// 字典翻译
|
||||
Object dictValue = DictUtil.getInstance().getDictItemValue(code, object.toString());
|
||||
if (null != dictValue) {
|
||||
item.put(field.getName() + "_dictText", dictValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
items.add(item);
|
||||
}
|
||||
((JsonPage) ((R) o).getData()).setData(items);
|
||||
}
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(MethodParameter methodParameter, Class aClass) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package cn.datax.common.dictionary.utils;
|
||||
|
||||
import cn.datax.common.core.RedisConstant;
|
||||
import cn.datax.common.redis.service.RedisService;
|
||||
import cn.datax.common.utils.SpringContextHolder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
public class ConfigUtil {
|
||||
|
||||
private ConfigUtil() {}
|
||||
|
||||
private static volatile ConfigUtil instance;
|
||||
|
||||
public static ConfigUtil getInstance() {
|
||||
if(instance == null) {
|
||||
synchronized (ConfigUtil.class) {
|
||||
if(instance == null) {
|
||||
instance = new ConfigUtil();
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private RedisService redisService = SpringContextHolder.getBean(RedisService.class);
|
||||
|
||||
/**
|
||||
* 获取参数
|
||||
* @param code
|
||||
*/
|
||||
public Object getConfig(String code) {
|
||||
String key = RedisConstant.SYSTEM_CONFIG_KEY;
|
||||
Object o = redisService.hget(key, code);
|
||||
return Optional.ofNullable(o).orElse(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package cn.datax.common.dictionary.utils;
|
||||
|
||||
import cn.datax.common.core.RedisConstant;
|
||||
import cn.datax.common.redis.service.RedisService;
|
||||
import cn.datax.common.utils.SpringContextHolder;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
public class DictUtil {
|
||||
|
||||
private DictUtil() {}
|
||||
|
||||
private static volatile DictUtil instance;
|
||||
|
||||
public static DictUtil getInstance() {
|
||||
if(instance == null) {
|
||||
synchronized (DictUtil.class) {
|
||||
if(instance == null) {
|
||||
instance = new DictUtil();
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private RedisService redisService = SpringContextHolder.getBean(RedisService.class);
|
||||
|
||||
/**
|
||||
* 获取字典项
|
||||
* @param code
|
||||
*/
|
||||
public Object getDictItemList(String code) {
|
||||
String key = RedisConstant.SYSTEM_DICT_KEY;
|
||||
Object object = redisService.get(key);
|
||||
if (null == object) {
|
||||
return null;
|
||||
}
|
||||
JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(object));
|
||||
List<Object> list = jsonArray.stream().filter(obj -> ((JSONObject) obj).get("dictCode").equals(code))
|
||||
.flatMap(obj -> ((JSONObject) obj).getJSONArray("items").stream()).collect(Collectors.toList());
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字典项值
|
||||
* @param code
|
||||
* @param text
|
||||
* @return
|
||||
*/
|
||||
public Object getDictItemValue(String code, String text) {
|
||||
String key = RedisConstant.SYSTEM_DICT_KEY;
|
||||
Object object = redisService.get(key);
|
||||
if (null == object) {
|
||||
return null;
|
||||
}
|
||||
JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(object));
|
||||
Object o = jsonArray.stream().filter(obj -> ((JSONObject) obj).get("dictCode").equals(code))
|
||||
.flatMap(obj -> ((JSONObject) obj).getJSONArray("items").stream())
|
||||
.filter(obj -> ((JSONObject) obj).get("itemText").equals(text))
|
||||
.map(obj -> ((JSONObject) obj).get("itemValue")).findFirst().orElse(null);
|
||||
return o;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
cn.datax.common.dictionary.config.DictAnalysis
|
||||
Reference in New Issue
Block a user