package com.casic.missiles.utils;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Slf4j
public class CasicBeanUtil {
/**
* 对象属性比较
*
* @param beforeObj
* @param afterObj
* @return 全部相同返回true,有不相同的返回false
*/
public static boolean fieldCompare(Object beforeObj, Object afterObj, List<String> fields) {
List<Field> beforeFieldList = Arrays.asList(beforeObj.getClass().getDeclaredFields());
List<Field> superBeforeFileList = Arrays.asList(beforeObj.getClass().getSuperclass().getDeclaredFields());
beforeFieldList.addAll(new ArrayList<>(superBeforeFileList));
Field[] beforeFields = beforeFieldList.toArray(new Field[beforeFieldList.size()]);
List<Field> afterFieldList = new ArrayList<>(Arrays.asList(afterObj.getClass().getDeclaredFields()));
List<Field> superAfterFieldList = new ArrayList<>(Arrays.asList(afterObj.getClass().getSuperclass().getDeclaredFields()));
afterFieldList.addAll(superAfterFieldList);
Field[] afterFields = afterFieldList.toArray(new Field[beforeFieldList.size()]);
Field.setAccessible(beforeFields, true); //设置私有属性可以访问到
Field.setAccessible(afterFields, true);
//循环遍历比较属性
if (beforeFields.length > 0) {
for (int i = 0; i < beforeFields.length; i++) {
if (ObjectUtil.isNotEmpty(fields) && fields.contains(beforeFields[i].getName())) {
try {
Object beforeValue = beforeFields[i].get(beforeObj); //取出对应的属性值
Object afterValue = null;
for (int j = 0; j < afterFields.length; j++) {
if(afterFields[j].getName().equals(beforeFields[i].getName())){
afterValue = afterFields[j].get(afterObj); //取出对应的属性值
break;
}
}
if ((beforeValue != null && !"".equals(beforeValue) && !beforeValue.equals(afterValue)) || ((beforeValue == null || "".equals(beforeValue)) && afterValue != null && !"".equals(afterValue))) {
return false;
}
} catch (IllegalAccessException e) {
log.error(e.getMessage());
}
}
}
}
return true;
}
public static JSONArray fieldCompareDetail(Object beforeObj, Object afterObj, List<String> fields) {
List<Field> beforeFieldList = Arrays.asList(beforeObj.getClass().getDeclaredFields());
List<Field> superBeforeFileList = Arrays.asList(beforeObj.getClass().getSuperclass().getDeclaredFields());
beforeFieldList.addAll(new ArrayList<>(superBeforeFileList));
Field[] beforeFields = beforeFieldList.toArray(new Field[beforeFieldList.size()]);
List<Field> afterFieldList = new ArrayList<>(Arrays.asList(afterObj.getClass().getDeclaredFields()));
List<Field> superAfterFieldList = new ArrayList<>(Arrays.asList(afterObj.getClass().getSuperclass().getDeclaredFields()));
afterFieldList.addAll(superAfterFieldList);
Field[] afterFields = afterFieldList.toArray(new Field[beforeFieldList.size()]);
Field.setAccessible(beforeFields, true); //设置私有属性可以访问到
Field.setAccessible(afterFields, true);
JSONArray res = new JSONArray();
//循环遍历比较属性
if (beforeFields.length > 0) {
for (int i = 0; i < beforeFields.length; i++) {
if (ObjectUtil.isEmpty(fields) || fields.contains(beforeFields[i].getName())) {
try {
Object beforeValue = beforeFields[i].get(beforeObj); //取出对应的属性值
Object afterValue = null;
for (int j = 0; j < afterFields.length; j++) {
if(afterFields[j].getName().equals(beforeFields[i].getName())){
afterValue = afterFields[j].get(afterObj); //取出对应的属性值
break;
}
}
if ((beforeValue != null && !"".equals(beforeValue) && !beforeValue.equals(afterValue)) || ((beforeValue == null || "".equals(beforeValue)) && afterValue != null && !"".equals(afterValue))) {
JSONObject item = new JSONObject();
item.put("field",beforeFields[i].getName());
item.put("oldVal",beforeValue);
item.put("newVal",afterValue);
res.add(item);
}
} catch (IllegalAccessException e) {
log.error(e.getMessage());
}
}
}
}
return res;
}
public static void copySpecifyProperties(Object source, Object target, String... copyProperties) throws BeansException {
Assert.notNull(source, "Source must not be null");
Assert.notNull(target, "Target must not be null");
Class<?> actualEditable = target.getClass();
PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(actualEditable);
List<String> ignoreList = (copyProperties != null ? Arrays.asList(copyProperties) : null);
for (PropertyDescriptor targetPd : targetPds) {
Method writeMethod = targetPd.getWriteMethod();
if (writeMethod != null && ignoreList != null && ignoreList.contains(targetPd.getName())) {
PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), targetPd.getName());
if (sourcePd != null) {
Method readMethod = sourcePd.getReadMethod();
if (readMethod != null &&
ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
try {
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(source);
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
writeMethod.setAccessible(true);
}
writeMethod.invoke(target, value);
} catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
}
}
}
}
}
}