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 115 116 117 118 119 120 121 122 123 124
|
public class FieldDisplayUtils {
public static List<FieldDisplayResult> compareAndReturnDifferent(Object o, Object n) { Map<Integer, FieldDisplayResult> oMap = FieldDisplayUtils.convertObj2SortMap(o); Map<Integer, FieldDisplayResult> nMap = FieldDisplayUtils.convertObj2SortMap(n); return compareSortMap(oMap, nMap); }
public static Map<Integer, FieldDisplayResult> convertObj2SortMap(Object obj) { Class<?> clazz = obj.getClass(); Field[] fields = clazz.getDeclaredFields(); TreeMap<Integer, FieldDisplayResult> sortMap = new TreeMap<>();
for (Field field : fields) { if ("serialVersionUID".equals(field.getName())) { continue; } field.setAccessible(true);
FieldDisplay annotation = field.getAnnotation(FieldDisplay.class); if (annotation == null) { continue; }
String displayName = annotation.name(); if (StringUtils.isBlank(annotation.name())) { displayName = ""; }
Object value; try { value = new PropertyDescriptor(field.getName(), clazz).getReadMethod().invoke(obj); } catch (Exception e) { e.printStackTrace(); continue; } if (value == null) { value = ""; } else if (value instanceof Date) { String dateFormat = "yyyy-MM-dd HH:mm:ss";
JsonFormat annotationDateJ = field.getAnnotation(JsonFormat.class); if (null != annotationDateJ) { dateFormat = annotationDateJ.pattern(); } Excel annotationDate = field.getAnnotation(Excel.class); if (null != annotationDate) { dateFormat = annotationDate.dateFormat(); } value = DateFormatUtils.format((Date) value, dateFormat); } else if (value instanceof BigDecimal) { value = new BigDecimal(value.toString()).toPlainString(); }
FieldDisplayResult result = new FieldDisplayResult(); result.setName(field.getName()); result.setValue(value.toString()); result.setDisplayName(displayName); sortMap.put(Integer.parseInt(annotation.order()), result); } return sortMap; }
public static List<FieldDisplayResult> compareSortMap(Map<Integer, FieldDisplayResult> beforeMap, Map<Integer, FieldDisplayResult> afterMap) {
List<FieldDisplayResult> resultList = new ArrayList<>(); for (Map.Entry<Integer, FieldDisplayResult> beforeEntry : beforeMap.entrySet()) { FieldDisplayResult r = new FieldDisplayResult(); FieldDisplayResult before = beforeEntry.getValue(); FieldDisplayResult after = afterMap.get(beforeEntry.getKey());
if (StrUtil.equals(before.getValue(), after.getValue())) { r.setCompareFlag("0"); } else { r.setCompareFlag("1"); }
r.setName(before.getName()); r.setDisplayName(before.getDisplayName()); r.setBeforeValue(before.getValue()); r.setAfterValue(after.getValue()); resultList.add(r); } return resultList; } }
|