比较两个实体属性值,返回一个map以有差异的属性名为key,value为一个Map分别存oldObject,newObject此属性名的值

 1     /**
 2      * 比较两个实体属性值,返回一个map以有差异的属性名为key,value为一个Map分别存oldObject,newObject此属性名的值
 3      *
 4      * @param oldObject 进行属性比较的对象1
 5      * @param newObject 进行属性比较的对象2
 6      * @return 属性差异比较结果map
 7      */
 8     @SuppressWarnings("rawtypes")
 9     public static Map<String, Map<String, BigDecimal>> compareFields(Object oldObject, Object newObject) {
10         Map<String, Map<String, BigDecimal>> map = null;
11         try {
12             /**
13              * 只有两个对象都是同一类型的才有可比性
14              */
15             if (oldObject.getClass() == newObject.getClass()) {
16                 map = new HashMap<String, Map<String, BigDecimal>>();
17                 Class clazz = oldObject.getClass();
18                 //获取object的所有属性
19                 PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz, Object.class).getPropertyDescriptors();
20                 for (PropertyDescriptor pd : pds) {
21                     //遍历获取属性名
22                     String name = pd.getName();
23                     //获取属性的get方法
24                     Method readMethod = pd.getReadMethod();
25                     // 在oldObject上调用get方法等同于获得oldObject的属性值
26                     Object oldValue = readMethod.invoke(oldObject);
27                     // 在newObject上调用get方法等同于获得newObject的属性值
28                     Object newValue = readMethod.invoke(newObject);
29                     if (oldValue instanceof List) {
30                         continue;
31                     }
32                     if (newValue instanceof List) {
33                         continue;
34                     }
35                     if (oldValue instanceof Timestamp) {
36                         oldValue = new Date(((Timestamp) oldValue).getTime());
37                     }
38                     if (newValue instanceof Timestamp) {
39                         newValue = new Date(((Timestamp) newValue).getTime());
40                     }
41                     if (newValue == null) {
42                         continue;
43                     }
44                     if (oldValue == null && newValue == null) {
45                         continue;
46                     } else if (oldValue == null && newValue != null) {
47                         Map<String, BigDecimal> valueMap = new HashMap<>();
48                         if (oldValue == null) {
49                             valueMap.put(StockPredictConstants.PROFIT_OLD_VALUE, null);
50                         } else {
51                             valueMap.put(StockPredictConstants.PROFIT_OLD_VALUE, new BigDecimal(oldValue.toString()));
52                         }
53                         if (newValue == null) {
54                             valueMap.put(StockPredictConstants.PROFIT_NEW_VALUE, null);
55                         } else {
56                             valueMap.put(StockPredictConstants.PROFIT_NEW_VALUE, new BigDecimal(newValue.toString()));
57                         }
58                         map.put(name, valueMap);
59                         continue;
60                     }
61                     BigDecimal oldBigDecimal = null;
62                     BigDecimal newBigDecimal = null;
63                     if (oldValue == null) {
64                         oldBigDecimal = null;
65                     } else {
66                         oldBigDecimal = new BigDecimal(oldValue.toString());
67                     }
68                     if (newValue == null) {
69                         newBigDecimal = null;
70                     } else {
71                         newBigDecimal = new BigDecimal(newalue.toString());
72                     }
73                     // 比较这两个值是否相等,不等就可以放入map了
74                     if (oldBigDecimal.compareTo(newBigDecimal) != 0) {
75                         Map<String, BigDecimal> valueMap = new HashMap<>();
76                         valueMap.put(StockPredictConstants.PROFIT_OLD_VALUE, oldBigDecimal);
77                         valueMap.put(StockPredictConstants.PROFIT_NEW_VALUE, newBigDecimal);
78                         map.put(name, valueMap);
79                     }
80                 }
81             }
82         } catch (Exception e) {
83             LOGGER.error("比较两个实体属性值比较两个实体属性值出错", e);
84         }
85         return map;
86     }

 

posted @ 2022-11-15 10:58  酷盖的小机灵  阅读(22)  评论(0编辑  收藏  举报