田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
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
package com.vci.server.base.utility;
 
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
 
import com.vci.common.annotaion.CustomAnnotaion;
 
public class LogHelper {
    
    public LogHelper() {
    }
    
    public static String toNewLogString(Object obj) {
        if (obj == null)
            return "";
        
        Class<?> objClass = obj.getClass();
        
        Field[] fields = objClass.getDeclaredFields();
        
        StringBuffer sbLog = new StringBuffer();
        for(Field f : fields){
            if(!f.isAnnotationPresent(CustomAnnotaion.class)){
                continue;
            }
            
            CustomAnnotaion caField = f.getAnnotation(CustomAnnotaion.class);
            if(caField.InToString()){
                String fieldDes = caField.CustomDescription();
                Object value = getTargetObjectValue(f, obj);
 
                if (sbLog.length() > 0)
                    sbLog.append(", ");
                
                sbLog.append(String.format("%s=%s", fieldDes, value));
            }
        }
 
        return sbLog.toString();
    }
    
    public static String toUpdataLogString(Object oldObj, Object newObj) {
        if (oldObj == null || newObj == null) {
            //System.out.println("================================\n oldObj == null || newObj == null");
            return "";
        }
        
        Class<?> objClass = newObj.getClass();
        
        Field[] fields = objClass.getDeclaredFields();
        
        StringBuffer sbBefor = new StringBuffer();
        StringBuffer sbAfter = new StringBuffer();
        for(Field f : fields){
            if(!f.isAnnotationPresent(CustomAnnotaion.class)){
                continue;
            }
            
            CustomAnnotaion caField = f.getAnnotation(CustomAnnotaion.class);
            if(caField.InToString()){
                String fieldDes = caField.CustomDescription();
                String oldValue = getTargetObjectValue(f, oldObj);
                String newValue = getTargetObjectValue(f, newObj);
                //System.out.println(String.format("============== %s =======================\noldValue = %s;   newValue = %s", fieldDes, oldValue, newValue));
 
                if (oldValue.equals(newValue))
                    continue;
 
                if (sbBefor.length() > 0)
                    sbBefor.append(", ");
                sbBefor.append(String.format("%s=%s", fieldDes, oldValue));
                
                if (sbAfter.length() > 0)
                    sbAfter.append(", ");
                sbAfter.append(String.format("%s=%s", fieldDes, newValue));
            }
        }
        
        if (sbAfter.length() == 0 || sbBefor.length() == 0) {
            //System.out.println(String.format("=====================================\noldObj = %s;   newObj = %s", sbBefor, sbAfter));
            return "";
        }
        
        return String.format("更改前[%s];更改后[%s]", sbBefor.toString(), sbAfter.toString());
    }
    
    
 
    /**
     * 通过内省机制获取字段值
     * @param declaredField 字段
     * @param t 对象
     * @return 值
     */
    private static String getTargetObjectValue(Field field, Object t) {
        Object value;
        try {
            PropertyDescriptor propDesc = new PropertyDescriptor(field.getName(), t.getClass());
            value = propDesc.getReadMethod().invoke(t);
            
            if (value == null)
                value = "";
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取对应数据失败");
        }
        return value.toString();
    }
 
}