--需要比较的用户信息对象 public class PersonalInfo{
private String id;
private User applicant;
private Date applyTime;
private Department applyDept;
private boolean neverNotify;
private DataStatus status;
private DictData expertType;
private Country country;
对应get、set方法略写
}
--实现获取信息变更记录以及变更前后的数据
public class ObjectCompareUtil {
/** * 获取对象比较结果,变更的内容 * @param beforeObject 原对象 * @param afterObject 新对象 * @return 变更的键值对对象字符串 */ public static String getCompareObjChangeInfoJsonStr(Object beforeObject, Object afterObject) { return JSONUtil.toJsonStr(getCompareObjChangeInfoMap(beforeObject, afterObject)); } /** * 获取对象比较结果,变更的内容 * * @param beforeObject 原对象 * @param afterObject 新对象 * @return 变更的键值对对象 */ public static Map<String, Object> getCompareObjChangeInfoMap(Object beforeObject, Object afterObject) { Map<String, Object> changeParam = new HashMap<>(); if (beforeObject.getClass().equals(afterObject.getClass())) { //遍历属性 Class clazz = beforeObject.getClass(); Field[] fieldArr = clazz.getDeclaredFields(); for (Field field : fieldArr) { compareValue(beforeObject,afterObject,field,changeParam); } } else { throw new RuntimeException("请提供相同的对象"); } return changeParam; } private static Map<String, Object> compareValue(Object beforeObject, Object afterObject, Field field, Map<String, Object> changeParam) { if ("java.lang.String".equals(field.getType().getName())) { String beforeValue = getValue(beforeObject,field,String.class); String afterValue = getValue(afterObject,field,String.class); if(!StringUtils.equals(beforeValue,afterValue)){ changeParam.put(field.getName(),beforeValue); } }else if("java.lang.Boolean".equals(field.getType().getName())){ Boolean beforeValue = getValue(beforeObject,field,Boolean.class); Boolean afterValue = getValue(afterObject,field,Boolean.class); if(beforeValue != afterValue){ changeParam.put(field.getName(),beforeValue); } }else if("java.lang.Integer".equals(field.getType().getName())){ Integer beforeValue = getValue(beforeObject,field,Integer.class); Integer afterValue = getValue(afterObject,field,Integer.class); if(beforeValue != afterValue){ changeParam.put(field.getName(),beforeValue); } }else if("java.util.Date".equals(field.getType().getName())){ Method method = ReflectUtil.getMethod(beforeObject.getClass(),true,"get"+field.getName()+"Str"); String v1 = ReflectUtil.invoke(beforeObject,method); String v2 = ReflectUtil.invoke(afterObject,method); if(!StringUtils.equals(v1,v2)){ changeParam.put(field.getName(),v1); } }else if("com.jiuhuasoft.smjFrame.authority.entity.User".equals(field.getType().getName())){ compareObjectValue(field.getName(),getValue(beforeObject,field, User.class),getValue(afterObject,field,User.class),changeParam); }else if("com.jiuhuasoft.smjFrame.authority.entity.Department".equals(field.getType().getName())){ compareObjectValue(field.getName(),getValue(beforeObject,field, Department.class),getValue(afterObject,field,Department.class),changeParam); }else if("com.jiuhuasoft.smjFrame.system.entity.DictData".equals(field.getType().getName())){ compareObjectValue(field.getName(),getValue(beforeObject,field, DictData.class),getValue(afterObject,field,DictData.class),changeParam); }else if("com.jiuhuasoft.smjFrame.system.entity.DataStatus".equals(field.getType().getName())){ compareObjectValue(field.getName(),getValue(beforeObject,field, DataStatus.class),getValue(afterObject,field,DataStatus.class),changeParam); }else if("com.jiuhuasoft.smjFrame.baseData.entity.Country".equals(field.getType().getName())){ compareObjectValue(field.getName(),getValue(beforeObject,field, Country.class),getValue(afterObject,field,Country.class),changeParam,"id","chAbName"); } return changeParam; } /** * 反射获取值 */ private static <T> T getValue(Object o, Field field, Class<T> returnClass) { T value; field.setAccessible(true); try { value = (T)field.get(o); return value; } catch (IllegalAccessException e) { } return null; } /** * 通过反射比较对象属性的值 * @param fix 属性前缀 * @param o1 原对象 * @param o2 现有的对象 * @param changeParam 变更的值 */ private static void compareObjectValue(String fix,Object o1, Object o2,Map<String, Object> changeParam){ compareObjectValue(fix,o1, o2, changeParam,"id","name"); } /** * 通过反射比较对象属性的值 * @param fix 属性前缀 * @param o1 原对象 * @param o2 现有的对象 * @param changeParam 变更的值 * @param compareField 比较的属性 * @param valueFiled 拿到的值 */ private static void compareObjectValue(String fix,Object o1, Object o2,Map<String, Object> changeParam,String compareField,String valueFiled){ if(o1 != null && o2 != null){ Class clazz = o1.getClass(); Field field = ReflectUtil.getField(clazz,compareField); String v1 = getValue(o1,field,String.class); String v2 = getValue(o2,field,String.class); if(!StringUtils.equals(v1,v2)){ Field valueFiledObj = ReflectUtil.getField(clazz,valueFiled); String v1value = getValue(o1,valueFiledObj,String.class); changeParam.put(fix+StringUtils.upperCaseFirstLetter(valueFiled),v1value); } }else if(o1 != null){ Class clazz = o1.getClass(); Field valueFiledObj = ReflectUtil.getField(clazz,valueFiled); String v1value = getValue(o1,valueFiledObj,String.class); changeParam.put(fix+StringUtils.upperCaseFirstLetter(valueFiled),v1value); } }
}