DictionaryAspect.java
3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package cn.com.glsx.entmicro.config;
import cn.com.glsx.entmicro.annotation.Dictionary;
import cn.com.glsx.entmicro.model.response.DictDataResponse;
import cn.com.glsx.entmicro.model.response.UserResponse;
import cn.com.glsx.entmicro.service.MerchantService;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author zhouhaibao
* @version 1.0
* @description: 字典统一处理
* @date 2021/09/07
*/
@Slf4j
@Aspect
@Component
public class DictionaryAspect {
@Autowired
private MerchantService merchantService;
@Pointcut("@annotation(cn.com.glsx.entmicro.annotation.DictionarySupport)")
public void logPointCut() {
}
@Before("logPointCut()")
public void webpre(JoinPoint joinPoint) {
}
/**
* 方法之后调用
*
* @param jp
* @param returnValue 方法返回值
*/
@AfterReturning(pointcut = "logPointCut()", returning = "returnValue")
public void doAfterReturning(JoinPoint jp, Object returnValue) {
Class<?> childClass = returnValue.getClass();
List fieldValue = new ArrayList();
if (returnValue instanceof Object) {
if (returnValue instanceof Page) {
fieldValue = (List) returnValue;
} else {
fieldValue.add(returnValue);
}
} else {
fieldValue = (List) returnValue;
}
// 设置字典值
List<DictDataResponse> allByDictType = merchantService.findAllByDictType("");
fieldValue.stream().forEach(obj -> {
Class clazz = obj.getClass();
Stream<Field> stream = Arrays.stream(getAllFields(clazz));
stream.filter(field1 ->
field1.getAnnotation(Dictionary.class) != null
).forEach(field2 -> {
try {
Dictionary annotation = field2.getAnnotation(Dictionary.class);
field2.setAccessible(true);
Object oldValue = field2.get(obj);
if (annotation != null && oldValue != null) {
String dicttype = annotation.dicttype();
// 设置参数的值
String dictvalue = annotation.dictvalue();
//3.2 获取field的值
String newValue = null;
if (CollectionUtils.isNotEmpty(allByDictType)) {
List<String> collect = allByDictType.stream().filter(index -> index.getDictValue().equals(oldValue) && index.getDictType().equals(dicttype)).map(DictDataResponse::getDictLabel).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(collect)) {
newValue = collect.get(0);
}
}
Field declaredField = clazz.getDeclaredField(dictvalue);
declaredField.setAccessible(true);
declaredField.set(obj, newValue);
}
} catch (Exception e) {
e.printStackTrace();
}
});
});
}
/**
* 获取本类的属性的方法
*
* @param clazz 当前类对象
* @return 字段数组
*/
private static Field[] getAllFields(Class<?> clazz) {
Field[] declaredFields = clazz.getDeclaredFields();
return declaredFields;
}
}