提交 | 用户 | 时间
|
1ac2bc
|
1 |
/** |
懒 |
2 |
* Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng) |
|
3 |
* <p> |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* <p> |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* <p> |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
package cn.stylefeng.guns.sys.core.util; |
|
17 |
|
|
18 |
import cn.hutool.core.date.DateUtil; |
|
19 |
import cn.hutool.core.util.StrUtil; |
|
20 |
import cn.stylefeng.guns.base.dict.AbstractDictMap; |
|
21 |
import cn.stylefeng.guns.sys.core.constant.dictmap.factory.DictFieldWarpperFactory; |
|
22 |
|
|
23 |
import java.beans.PropertyDescriptor; |
|
24 |
import java.lang.reflect.Field; |
|
25 |
import java.lang.reflect.Method; |
|
26 |
import java.util.Date; |
|
27 |
import java.util.Map; |
|
28 |
|
|
29 |
/** |
|
30 |
* 对比两个对象的变化的工具类 |
|
31 |
* |
|
32 |
* @author fengshuonan |
|
33 |
* @Date 2017/3/31 10:36 |
|
34 |
*/ |
|
35 |
public class Contrast { |
|
36 |
|
|
37 |
//记录每个修改字段的分隔符 |
|
38 |
public static final String separator = ";;;"; |
|
39 |
|
|
40 |
/** |
|
41 |
* 比较两个对象,并返回不一致的信息 |
|
42 |
* |
|
43 |
* @author stylefeng |
|
44 |
* @Date 2017/5/9 19:34 |
|
45 |
*/ |
|
46 |
public static String contrastObj(Object pojo1, Object pojo2) { |
|
47 |
String str = ""; |
|
48 |
try { |
|
49 |
Class clazz = pojo1.getClass(); |
|
50 |
Field[] fields = pojo1.getClass().getDeclaredFields(); |
|
51 |
int i = 1; |
|
52 |
for (Field field : fields) { |
|
53 |
if ("serialVersionUID".equals(field.getName())) { |
|
54 |
continue; |
|
55 |
} |
|
56 |
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz); |
|
57 |
Method getMethod = pd.getReadMethod(); |
|
58 |
Object o1 = getMethod.invoke(pojo1); |
|
59 |
Object o2 = getMethod.invoke(pojo2); |
|
60 |
if (o1 == null || o2 == null) { |
|
61 |
continue; |
|
62 |
} |
|
63 |
if (o1 instanceof Date) { |
|
64 |
o1 = DateUtil.formatDate((Date) o1); |
|
65 |
} |
|
66 |
if (!o1.toString().equals(o2.toString())) { |
|
67 |
if (i != 1) { |
|
68 |
str += separator; |
|
69 |
} |
|
70 |
str += "字段名称" + field.getName() + ",旧值:" + o1 + ",新值:" + o2; |
|
71 |
i++; |
|
72 |
} |
|
73 |
} |
|
74 |
} catch (Exception e) { |
|
75 |
e.printStackTrace(); |
|
76 |
} |
|
77 |
return str; |
|
78 |
} |
|
79 |
|
|
80 |
/** |
|
81 |
* 比较两个对象pojo1和pojo2,并输出不一致信息 |
|
82 |
* |
|
83 |
* @author stylefeng |
|
84 |
* @Date 2017/5/9 19:34 |
|
85 |
*/ |
|
86 |
public static String contrastObj(Class dictClass, String key, Object pojo1, Map<String, String> pojo2) throws IllegalAccessException, InstantiationException { |
|
87 |
AbstractDictMap dictMap = (AbstractDictMap) dictClass.newInstance(); |
|
88 |
String str = parseMutiKey(dictMap, key, pojo2) + separator; |
|
89 |
try { |
|
90 |
Class clazz = pojo1.getClass(); |
|
91 |
Field[] fields = pojo1.getClass().getDeclaredFields(); |
|
92 |
int i = 1; |
|
93 |
for (Field field : fields) { |
|
94 |
if ("serialVersionUID".equals(field.getName())) { |
|
95 |
continue; |
|
96 |
} |
|
97 |
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz); |
|
98 |
Method getMethod = pd.getReadMethod(); |
|
99 |
Object o1 = getMethod.invoke(pojo1); |
|
100 |
Object o2 = pojo2.get(StrUtil.lowerFirst(getMethod.getName().substring(3))); |
|
101 |
if (o1 == null || o2 == null) { |
|
102 |
continue; |
|
103 |
} |
|
104 |
if (o1 instanceof Date) { |
|
105 |
o1 = DateUtil.formatDate((Date) o1); |
|
106 |
} else if (o1 instanceof Integer) { |
|
107 |
o2 = Integer.parseInt(o2.toString()); |
|
108 |
} |
|
109 |
if (!o1.toString().equals(o2.toString())) { |
|
110 |
if (i != 1) { |
|
111 |
str += separator; |
|
112 |
} |
|
113 |
String fieldName = dictMap.get(field.getName()); |
|
114 |
String fieldWarpperMethodName = dictMap.getFieldWarpperMethodName(field.getName()); |
|
115 |
if (fieldWarpperMethodName != null) { |
|
116 |
Object o1Warpper = DictFieldWarpperFactory.createFieldWarpper(o1, fieldWarpperMethodName); |
|
117 |
Object o2Warpper = DictFieldWarpperFactory.createFieldWarpper(o2, fieldWarpperMethodName); |
|
118 |
str += "字段名称:" + fieldName + ",旧值:" + o1Warpper + ",新值:" + o2Warpper; |
|
119 |
} else { |
|
120 |
str += "字段名称:" + fieldName + ",旧值:" + o1 + ",新值:" + o2; |
|
121 |
} |
|
122 |
i++; |
|
123 |
} |
|
124 |
} |
|
125 |
} catch (Exception e) { |
|
126 |
e.printStackTrace(); |
|
127 |
} |
|
128 |
return str; |
|
129 |
} |
|
130 |
|
|
131 |
/** |
|
132 |
* 比较两个对象pojo1和pojo2,并输出不一致信息 |
|
133 |
* |
|
134 |
* @author stylefeng |
|
135 |
* @Date 2017/5/9 19:34 |
|
136 |
*/ |
|
137 |
public static String contrastObjByName(Class dictClass, String key, Object pojo1, Map<String, String> pojo2) throws IllegalAccessException, InstantiationException { |
|
138 |
AbstractDictMap dictMap = (AbstractDictMap) dictClass.newInstance(); |
|
139 |
String str = parseMutiKey(dictMap, key, pojo2) + separator; |
|
140 |
try { |
|
141 |
Class clazz = pojo1.getClass(); |
|
142 |
Field[] fields = pojo1.getClass().getDeclaredFields(); |
|
143 |
int i = 1; |
|
144 |
for (Field field : fields) { |
|
145 |
if ("serialVersionUID".equals(field.getName())) { |
|
146 |
continue; |
|
147 |
} |
|
148 |
String prefix = "get"; |
|
149 |
int prefixLength = 3; |
|
150 |
if (field.getType().getName().equals("java.lang.Boolean")) { |
|
151 |
prefix = "is"; |
|
152 |
prefixLength = 2; |
|
153 |
} |
|
154 |
Method getMethod = null; |
|
155 |
try { |
|
156 |
getMethod = clazz.getDeclaredMethod(prefix + StrUtil.upperFirst(field.getName())); |
|
157 |
} catch (NoSuchMethodException e) { |
|
158 |
System.err.println("this className:" + clazz.getName() + " is not methodName: " + e.getMessage()); |
|
159 |
continue; |
|
160 |
} |
|
161 |
Object o1 = getMethod.invoke(pojo1); |
|
162 |
Object o2 = pojo2.get(StrUtil.lowerFirst(getMethod.getName().substring(prefixLength))); |
|
163 |
if (o1 == null || o2 == null) { |
|
164 |
continue; |
|
165 |
} |
|
166 |
if (o1 instanceof Date) { |
|
167 |
o1 = DateUtil.formatDate((Date) o1); |
|
168 |
} else if (o1 instanceof Integer) { |
|
169 |
o2 = Integer.parseInt(o2.toString()); |
|
170 |
} |
|
171 |
if (!o1.toString().equals(o2.toString())) { |
|
172 |
if (i != 1) { |
|
173 |
str += separator; |
|
174 |
} |
|
175 |
String fieldName = dictMap.get(field.getName()); |
|
176 |
String fieldWarpperMethodName = dictMap.getFieldWarpperMethodName(field.getName()); |
|
177 |
if (fieldWarpperMethodName != null) { |
|
178 |
Object o1Warpper = DictFieldWarpperFactory.createFieldWarpper(o1, fieldWarpperMethodName); |
|
179 |
Object o2Warpper = DictFieldWarpperFactory.createFieldWarpper(o2, fieldWarpperMethodName); |
|
180 |
str += "字段名称:" + fieldName + ",旧值:" + o1Warpper + ",新值:" + o2Warpper; |
|
181 |
} else { |
|
182 |
str += "字段名称:" + fieldName + ",旧值:" + o1 + ",新值:" + o2; |
|
183 |
} |
|
184 |
i++; |
|
185 |
} |
|
186 |
} |
|
187 |
} catch (Exception e) { |
|
188 |
e.printStackTrace(); |
|
189 |
} |
|
190 |
return str; |
|
191 |
} |
|
192 |
|
|
193 |
/** |
|
194 |
* 解析多个key(逗号隔开的) |
|
195 |
* |
|
196 |
* @author stylefeng |
|
197 |
* @Date 2017/5/16 22:19 |
|
198 |
*/ |
|
199 |
public static String parseMutiKey(AbstractDictMap dictMap, String key, Map<String, String> requests) { |
|
200 |
StringBuilder sb = new StringBuilder(); |
|
201 |
if (key.contains(",")) { |
|
202 |
String[] keys = key.split(","); |
|
203 |
for (String item : keys) { |
|
204 |
String fieldWarpperMethodName = dictMap.getFieldWarpperMethodName(item); |
|
205 |
String value = requests.get(item); |
|
206 |
if (fieldWarpperMethodName != null) { |
|
207 |
Object valueWarpper = DictFieldWarpperFactory.createFieldWarpper(value, fieldWarpperMethodName); |
|
208 |
sb.append(dictMap.get(item)).append("=").append(valueWarpper).append(","); |
|
209 |
} else { |
|
210 |
sb.append(dictMap.get(item)).append("=").append(value).append(","); |
|
211 |
} |
|
212 |
} |
|
213 |
return StrUtil.removeSuffix(sb.toString(), ","); |
|
214 |
} else { |
|
215 |
String fieldWarpperMethodName = dictMap.getFieldWarpperMethodName(key); |
|
216 |
String value = requests.get(key); |
|
217 |
if (fieldWarpperMethodName != null) { |
|
218 |
Object valueWarpper = DictFieldWarpperFactory.createFieldWarpper(value, fieldWarpperMethodName); |
|
219 |
sb.append(dictMap.get(key)).append("=").append(valueWarpper); |
|
220 |
} else { |
|
221 |
sb.append(dictMap.get(key)).append("=").append(value); |
|
222 |
} |
|
223 |
return sb.toString(); |
|
224 |
} |
|
225 |
} |
|
226 |
|
|
227 |
} |