工具类
import com.sun.istack.internal.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;
@Slf4j
public class ChangeRecordUtils {
private static final String DEFAULT_VALUE = "";
private static final String DEFAULT_PATTERN = "yyyy-MM-dd";
private static final String SEPARATOR = ",";
public static <T> String[] getRecordInfo(@NotNull T before, @NotNull T after) {
return getRecordInfoByGroup(before, after, null);
}
public static <T> boolean aequalsB(@NotNull T before, @NotNull T after, @NotNull String group) {
String[] recordInfoByGroup = getRecordInfoByGroup(before, after, group);
if (com.jcxx.saas.common.utils.StringUtils.isBlank(recordInfoByGroup[0]) &&
com.jcxx.saas.common.utils.StringUtils.isBlank(recordInfoByGroup[1])) {
return true;
}
return false;
}
public static <T> String[] getRecordInfoByGroup(@NotNull T before, @NotNull T after, @NotNull String group) {
List<String> oldDataRecord = new LinkedList<>();
List<String> newDataRecord = new LinkedList<>();
String oldData = "";
String newData = "";
String[] result = {oldData, newData};
List<Field> oldObjfields = getObjAndSuperObjFilds(before, group);
if (oldObjfields.size() < 1) {
return result;
}
String fieldAnnotatioValue;
String beforeValue;
String afterValue;
for (Field oldField : oldObjfields) {
String oldFieldName = oldField.getName();
beforeValue = getValueByFieldName(before, oldFieldName);
afterValue = getValueByFieldName(after, oldFieldName);
if (!Objects.equals(afterValue, beforeValue)) {
fieldAnnotatioValue = getFieldAnnotatioValue(oldField);
fieldAnnotatioValue = fieldAnnotatioValue == null ? "" : fieldAnnotatioValue;
fieldAnnotatioValue = fieldAnnotatioValue.replaceAll(":", "");
fieldAnnotatioValue = fieldAnnotatioValue.replaceAll(":", "");
fieldAnnotatioValue = fieldAnnotatioValue + ":";
oldDataRecord.add(fieldAnnotatioValue + beforeValue);
newDataRecord.add(fieldAnnotatioValue + afterValue);
}
}
oldData = Arrays.toString(oldDataRecord.toArray()).replace("]", "").replace("[", "");
newData = Arrays.toString(newDataRecord.toArray()).replace("]", "").replace("[", "");
result[0] = oldData;
result[1] = newData;
return result;
}
private static <T> String getValueByFieldName(T obj, String filedName) {
Class<?> objClass = obj.getClass();
Class<?> objSuperclass = objClass.getSuperclass();
Field field = getField(objClass, filedName);
field = (field == null) ? getField(objSuperclass, filedName) : field;
return (field == null) ? DEFAULT_VALUE : getValueByField(field, obj);
}
private static <T> List<Field> getObjAndSuperObjFilds(T obj, String group) {
List<Field> objfields = new LinkedList<>();
Class<?> objClass = obj.getClass();
List<Field> fields1 = getFildListByClass(objClass, group);
objfields.addAll(fields1);
Class<?> objSuperclass = objClass.getSuperclass();
List<Field> fields2 = getFildListByClass(objSuperclass, group);
objfields.addAll(fields2);
return objfields;
}
private static List<Field> getFildListByClass(Class<?> objClass, String group) {
List<Field> objfields = new LinkedList<>();
Field[] fields = objClass.getDeclaredFields();
if (fields.length > 0) {
for (Field field : fields) {
Field targetField = getField(field);
if (Objects.isNull(targetField)) {
continue;
}
if (targetField.isAnnotationPresent(SysContrast.class)) {
if (StringUtils.isNotBlank(group)) {
SysContrast annotation = targetField.getAnnotation(SysContrast.class);
String annotationGroup = annotation.group();
if (StringUtils.isNotBlank(annotationGroup)) {
String[] split = annotationGroup.split(",");
if (Arrays.asList(split).contains(group)) {
objfields.add(field);
}
}
} else {
objfields.add(field);
}
} else {
objfields.add(field);
}
}
}
return objfields;
}
private static Field getField(Class<?> clazz, String propertyName) {
if (clazz == null) {
return null;
}
try {
return clazz.getDeclaredField(propertyName);
} catch (NoSuchFieldException e) {
return getField(clazz.getSuperclass(), propertyName);
}
}
private static <T> String getValueByField(Field field, T obj) {
field.setAccessible(true);
Object value = null;
try {
value = field.get(obj);
} catch (IllegalAccessException e) {
e.printStackTrace();
log.error("反射获取前信息失败{{}}", e.getMessage());
}
boolean annotationPresent = field.isAnnotationPresent(SysContrast.class);
if (annotationPresent) {
SysContrast annotation = field.getAnnotation(SysContrast.class);
String readConverterExp = annotation.readConverterExp();
if (Date.class.isAssignableFrom(field.getType()) && value != null) {
String pattern = annotation.dateFormat();
if (StringUtils.isBlank(pattern)) {
pattern = DEFAULT_PATTERN;
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
value = sdf.format(value);
} else if (StringUtils.isNotBlank(readConverterExp)) {
value = convertByExp(ConvertUtils.toStr(value), annotation.readConverterExp(), annotation.separator());
}
} else {
if (Date.class.isAssignableFrom(field.getType()) && value != null) {
if (field.isAnnotationPresent(DateTimeFormat.class)) {
DateTimeFormat annotation = field.getAnnotation(DateTimeFormat.class);
String pattern = annotation.pattern();
if (StringUtils.isNotBlank(pattern)) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
value = sdf.format(value);
}
}
}
}
return (value == null) ? DEFAULT_VALUE : ConvertUtils.toStr(value);
}
private static String getFieldAnnotatioValue(Field field) {
SysContrast annotation = field.getAnnotation(SysContrast.class);
return annotation.value();
}
private static Field getField(Field field) {
if (field == null) {
return null;
}
boolean annotationPresent = field.isAnnotationPresent(SysContrast.class);
if (annotationPresent) {
return field;
}
return null;
}
private static String convertByExp(String propertyValue, String converterExp, String separator) {
StringBuilder propertyString = new StringBuilder();
String[] convertSource = converterExp.split(",");
for (String item : convertSource) {
String[] itemArray = item.split("=");
if (StringUtils.containsAny(separator, propertyValue)) {
for (String value : propertyValue.split(separator)) {
if (itemArray[0].equals(value)) {
propertyString.append(itemArray[1] + separator);
break;
}
}
} else {
if (itemArray[0].equals(propertyValue)) {
return itemArray[1];
}
}
}
return StringUtils.stripEnd(propertyString.toString(), separator);
}
}
注解
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
@java.lang.annotation.Target(ElementType.FIELD)
@java.lang.annotation.Retention(RetentionPolicy.RUNTIME)
public @interface SysContrast {
String value() default "";
String dateFormat() default "";
String readConverterExp() default "";
String targetAttr() default "";
String group() default "";
String separator() default ",";
}