package com.gildata.task.platform.common.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gildata.task.platform.common.operationLog.TaskPoolConfigLog;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
public class CompareUtil {
/**
* 比較兩個實體屬性值,返回一個boolean,true則表時兩個物件中的屬性值無差異
*
* @param oldObject 進行屬性比較的物件1
* @param newObject 進行屬性比較的物件2
* @return 屬性差異比較結果boolean
*/
public static boolean isDiff(Object oldObject, Object newObject) {
JSONArray jsonArray = compareFields(oldObject, newObject, null);
if (jsonArray.size() > 0) {
return true;
} else {
return false;
}
}
/**
* 比較兩個實體屬性值,返回一個map以有差異的屬性名為key,value為一個Map分別存oldObject,newObject此屬性名的值
*
* @param obj1 進行屬性比較的物件1
* @param obj2 進行屬性比較的物件2
* @param ignoreArr 忽略比較的欄位
* @return 屬性差異比較結果map
*/
@SuppressWarnings("rawtypes")
public static JSONArray compareFields(Object obj1, Object obj2, String[] ignoreArr) {
JSONArray jsonArray = new JSONArray();
try {
List<String> ignoreList = null;
if (ignoreArr != null && ignoreArr.length > 0) {
// array轉化為list
ignoreList = Arrays.asList(ignoreArr);
}
if (obj1.getClass() == obj2.getClass()) {// 只有兩個物件都是同一型別的才有可比性
Class clazz = obj1.getClass();
// 獲取object的屬性描述
PropertyDescriptor[] pds = Introspector.getBeanInfo(clazz,
Object.class).getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {// 這裡就是所有的屬性了
String name = pd.getName();// 屬性名
if (ignoreList != null && ignoreList.contains(name)) {// 如果當前屬性選擇忽略比較,跳到下一次迴圈
continue;
}
Method readMethod = pd.getReadMethod();// get方法
// 在obj1上呼叫get方法等同於獲得obj1的屬性值
Object o1 = readMethod.invoke(obj1);
// 在obj2上呼叫get方法等同於獲得obj2的屬性值
Object o2 = readMethod.invoke(obj2);
if (o1 instanceof Timestamp) {
o1 = new Date(((Timestamp) o1).getTime());
}
if (o2 instanceof Timestamp) {
o2 = new Date(((Timestamp) o2).getTime());
}
if (o1 == null && o2 == null) {
continue;
} else if ((o1 == null && o2 != null)) {
JSONObject map = new JSONObject();
map.put("fieldName", name);
map.put("oldValue", "");
map.put("newValue", o2);
jsonArray.add(map);
continue;
} else if ((o2 == null && o1 != null)) {
JSONObject map = new JSONObject();
map.put("fieldName", name);
map.put("oldValue", o1);
map.put("newValue", "");
jsonArray.add(map);
continue;
}
if (!o1.equals(o2)) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("fieldName", name);
jsonObject.put("oldValue", o1);
jsonObject.put("newValue", o2);
jsonArray.add(jsonObject);
}
}
}
return jsonArray;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
TaskPoolConfigLog taskPoolConfigLog1 = new TaskPoolConfigLog();
TaskPoolConfigLog taskPoolConfigLog2 = new TaskPoolConfigLog();
taskPoolConfigLog1.setAppCode("1");
taskPoolConfigLog2.setAppCode("2");
taskPoolConfigLog1.setName("張三");
taskPoolConfigLog2.setAppCode("王五");
taskPoolConfigLog2.setUpdateTime(new Date());
String[] ignoreArr = new String[3];
compareFields(taskPoolConfigLog1, taskPoolConfigLog2, ignoreArr);
}
}