From 9d22be2344ff2aa54692d2f568caf88df6547933 Mon Sep 17 00:00:00 2001 From: liuxiaoxu <1793812695@qq.com> Date: Thu, 17 Oct 2024 13:35:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96FieldCompareUtil=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E5=AF=B9=E6=AF=94=E5=B7=A5=E5=85=B7=E7=B1=BB=EF=BC=9A?= =?UTF-8?q?=20=E6=B3=9B=E5=9E=8B=E5=A4=84=E7=90=86=EF=BC=9A=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20Number=20=E7=B1=BB=E5=9E=8B=E6=9D=A5=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=89=80=E6=9C=89=E7=9A=84=E6=95=B0=E5=80=BC=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=EF=BC=88=E5=A6=82=20Integer,=20Long,=20Double,=20BigD?= =?UTF-8?q?ecimal=EF=BC=89=E3=80=82=20=E6=9E=9A=E4=B8=BE=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=A4=84=E7=90=86=EF=BC=9A=E5=A2=9E=E5=8A=A0=E5=AF=B9=20Enum?= =?UTF-8?q?=20=E7=B1=BB=E5=9E=8B=E7=9A=84=E6=94=AF=E6=8C=81=E3=80=82=20?= =?UTF-8?q?=E6=97=A5=E6=9C=9F=E7=B1=BB=E5=9E=8B=E5=A4=84=E7=90=86=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=B9=20Date=20=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81=EF=BC=8C=E5=B9=B6=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E6=97=A5=E6=9C=9F=E3=80=82=20=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=9A=E9=80=9A=E8=BF=87?= =?UTF-8?q?=20areEqual=20=E5=92=8C=20formatValue=20=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=9D=A5=E5=87=8F=E5=B0=91=E5=86=97=E4=BD=99=E7=9A=84=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=E5=88=A4=E6=96=AD=E3=80=82=20=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86=EF=BC=9A=E5=A2=9E=E5=8A=A0=E4=BA=86=E5=AF=B9?= =?UTF-8?q?=20PropertyDescriptor=20=E5=92=8C=20Method=20=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E8=BF=87=E7=A8=8B=E4=B8=AD=E5=8F=AF=E8=83=BD=E5=87=BA=E7=8E=B0?= =?UTF-8?q?=E7=9A=84=E5=BC=82=E5=B8=B8=E7=9A=84=E5=A4=84=E7=90=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/common/utils/FieldCompareUtil.java | 109 +++++++++++------- 1 file changed, 68 insertions(+), 41 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/FieldCompareUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/FieldCompareUtil.java index 1609af2b..6745dad4 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/FieldCompareUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/FieldCompareUtil.java @@ -9,6 +9,7 @@ import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.math.BigDecimal; +import java.text.SimpleDateFormat; import java.util.*; /** @@ -19,58 +20,84 @@ import java.util.*; * */ public class FieldCompareUtil { + /** + * 比较两个对象的变化并记录。 + * + * @param type 对象的类型 + * @param newObject 新对象 + * @param oldObject 旧对象 + * @return 变更记录列表 + * @throws Exception 如果发生异常 + */ public static List compare(Class type, T newObject, T oldObject) throws Exception { // 变更的记录列表 List changeLogFields = new ArrayList<>(); - Class newObj = newObject.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; - } else { - eq = !oldVal.equals(newVal); - } - String s1 = oldVal == null ? "" : oldVal.toString().trim(); - String s2 = newVal == null ? "" : newVal.toString().trim(); - if (eq) { - Map map = codeToNameMap(newAnnotation); - if (map.size() > 0) { - changeLogFields.add(new SysFieldDifferent(newAnnotation.chineseName(), map.get(s1), map.get(s2))); - } else { - changeLogFields.add(new SysFieldDifferent(newAnnotation.chineseName(), s1, s2)); + Field[] fields = type.getDeclaredFields(); + for (Field field : fields) { + FieldCompare annotation = field.getAnnotation(FieldCompare.class); + if (annotation != null) { + PropertyDescriptor pd = new PropertyDescriptor(field.getName(), newObject.getClass()); + Method getMethod = pd.getReadMethod(); + Object oldVal = getMethod.invoke(oldObject); + Object newVal = getMethod.invoke(newObject); + if (!areEqual(oldVal, newVal)) { + String s1 = formatValue(oldVal); + String s2 = formatValue(newVal); + String chineseName = annotation.chineseName(); + Map codeToNameMap = codeToNameMap(annotation); + if (!codeToNameMap.isEmpty()) { + s1 = codeToNameMap.getOrDefault(s1, s1); + s2 = codeToNameMap.getOrDefault(s2, s2); } + changeLogFields.add(new SysFieldDifferent(chineseName, s1, s2)); } } } return changeLogFields; } + //统一处理条件判断 + private static boolean areEqual(Object oldVal, Object newVal) { + if (oldVal == null && newVal == null) { + return true; + } + if (oldVal == null || newVal == null) { + return false; + } + if (oldVal instanceof String) { + return oldVal.toString().trim().equals(newVal.toString().trim()); + } else if (oldVal instanceof Number) { + return ((Number) oldVal).doubleValue() == ((Number) newVal).doubleValue(); + } else if (oldVal instanceof Enum) { + return oldVal.equals(newVal); + } else if (oldVal instanceof Date) { + return oldVal.equals(newVal); + } else { + return oldVal.equals(newVal); + } + } + + //统一处理格式化值 + private static String formatValue(Object value) { + if (value == null) { + return ""; + } + if (value instanceof String) { + return ((String) value).trim(); + } else if (value instanceof Date) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + return sdf.format(value); + } else if (value instanceof Number) { + return value.toString(); + } else if (value instanceof Enum) { + return value.toString(); + } else { + return value.toString(); + } + } + + /** * 字典映射 使用方式: * @param newAnnotation