-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path数据更新前后对比工具类
91 lines (83 loc) · 3.48 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
import org.apache.commons.lang3.StringUtils;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SerializableFieldCompare {
public static <T> List<String> compare (Class<T> type, T newObject, T oldObject ) throws Exception {
List<String> logList = new ArrayList<>();
Class<?> newObj = newObject.getClass();
Class<?> oldObj = oldObject.getClass();
Field[] newFields = type.getDeclaredFields();
for (int i = 0; i < newFields.length; i++) {
FieldCompare newAnnotation = newFields[i].getAnnotation(FieldCompare.class);
if(null != newAnnotation){
PropertyDescriptor newPd = new PropertyDescriptor(newFields[i].getName(), newObj);
Method getMethodNew = newPd.getReadMethod();
Object oldVal = getMethodNew.invoke(oldObject);
Object newVal = getMethodNew.invoke(newObject);
boolean eq = false;
if (oldVal instanceof String){
String s1 = String.valueOf(oldVal).trim();
String s2 = String.valueOf(newVal).trim();
eq = !s1.equals(s2);
}else if(oldVal instanceof Integer){
int i1 = (Integer) oldVal;
int i2 = (Integer) newVal;
eq = i1 != i2;
}else if(oldVal instanceof Double){
double d1 = (Double) oldVal;
double d2 = (Double) newVal;
eq = d1 != d2;
}else if(oldVal instanceof BigDecimal){
BigDecimal b1 = (BigDecimal) oldVal;
BigDecimal b2 = (BigDecimal) newVal;
eq = b1.compareTo(b2) != 0;
}else if(oldVal instanceof Long){
long l1 = (Long) oldVal;
long l2 = (Long) newVal;
eq = l1 != l2;
}
String s1 = oldVal == null ? "" : oldVal.toString().trim();
String s2 = newVal == null ? "" : newVal.toString().trim();
if (eq) {
Map<String, String> map = codeToNameMap(newAnnotation);
if (map.size() > 0) {
logList.add(newAnnotation.chineseName() + "由:[" + map.get(s1) + "]更改为了:[" + map.get(s2) + "]");
}else {
logList.add(newAnnotation.chineseName() + "由:[" + s1 + "]更改为了:[" + s2 + "]");
}
}
}
}
return logList;
}
private static Map<String,String> codeToNameMap(FieldCompare newAnnotation){
Map<String,String> map = new HashMap<>();
String properties = newAnnotation.properties();
if(StringUtils.isNotBlank(properties)){
String[] propertiesArr = properties.split(",");
for (String propertie : propertiesArr) {
String[] split = propertie.split(":");
map.put(split[0],split[1]);
}
}
return map;
}
}
#自定义注解
import java.lang.annotation.*;
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface FieldCompare {
//字段名称
String chineseName();
//类型映射
String properties() default "";
}