|
@@ -31,76 +31,59 @@ public class LogTagUtil {
|
|
StringBuilder description = new StringBuilder();
|
|
StringBuilder description = new StringBuilder();
|
|
// 遍历字段列表
|
|
// 遍历字段列表
|
|
List<Field> fieldList = clazzWrapper.getFieldList();
|
|
List<Field> fieldList = clazzWrapper.getFieldList();
|
|
- String separator = ", ";
|
|
|
|
- for (Field field : fieldList) {
|
|
|
|
- field.setAccessible(true);
|
|
|
|
- FieldWrapper fieldWrapper = new FieldWrapper(field, field.get(oldObject), field.get(newObject));
|
|
|
|
- // 对LogTag标记的属性进行属性对比
|
|
|
|
- if (fieldWrapper.isWithLogTag()) {
|
|
|
|
- LogTag logTag = fieldWrapper.getLogTag();
|
|
|
|
- if (ObjectUtil.equals(logTag.extendedType(), LogTagExtendedType.OBJECT)) {
|
|
|
|
- String fieldDescription = fieldComparison(fieldWrapper.getOldValue(), fieldWrapper.getNewValue());
|
|
|
|
- description.append(fieldWrapper.getAttributeAlias()).append(":").append(fieldDescription).append(separator);
|
|
|
|
- }
|
|
|
|
- if (ObjectUtil.equals(logTag.extendedType(), LogTagExtendedType.COLLECTION)) {
|
|
|
|
- Collection<?> oldValue = fieldWrapper.getOldValue() == null ? Collections.emptyList() : (Collection<?>) fieldWrapper.getOldValue();
|
|
|
|
- Collection<?> newValue = fieldWrapper.getNewValue() == null ? Collections.emptyList() : (Collection<?>) fieldWrapper.getNewValue();
|
|
|
|
-
|
|
|
|
- Map<Long, Object> oldMap = oldValue.stream()
|
|
|
|
- .filter(Objects::nonNull)
|
|
|
|
- .filter(item -> item instanceof BaseIdPo)
|
|
|
|
- .map(item -> (BaseIdPo) item)
|
|
|
|
- .filter(item -> item.getId() != null)
|
|
|
|
- .collect(Collectors.toMap(
|
|
|
|
- BaseIdPo::getId,
|
|
|
|
- Function.identity()
|
|
|
|
- ));
|
|
|
|
-
|
|
|
|
- StringBuilder fieldDescription = new StringBuilder();
|
|
|
|
-
|
|
|
|
- // 1. 遍历新集合:判断是新增还是修改
|
|
|
|
- for (Object newObj : newValue) {
|
|
|
|
-
|
|
|
|
- Object oldObj = null;
|
|
|
|
- Long id = null;
|
|
|
|
- if (newObj instanceof BaseIdPo) {
|
|
|
|
- id = ((BaseIdPo) newObj).getId();
|
|
|
|
- }
|
|
|
|
- if (id != null) {
|
|
|
|
- oldObj = oldMap.get(id);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (oldObj != null) {
|
|
|
|
- String editDesc = fieldComparison(oldObj, newObj);
|
|
|
|
- if (StrUtil.isNotBlank(editDesc)) {
|
|
|
|
- fieldDescription.append("修改数据:").append(editDesc).append("; ");
|
|
|
|
- }
|
|
|
|
- oldMap.remove(id);
|
|
|
|
- } else {
|
|
|
|
- // 不存在:新增
|
|
|
|
- fieldDescription.append("新增数据:").append(getObjectAllLogTag(newObj)).append("; ");
|
|
|
|
- }
|
|
|
|
|
|
+ String separator = ";\n";
|
|
|
|
+ // 筛选出LogTag标记的属性进行属性对比
|
|
|
|
+ List<FieldWrapper> logTagFieldWrappers = fieldList.stream()
|
|
|
|
+ .map(field -> {
|
|
|
|
+ field.setAccessible(true);
|
|
|
|
+ try {
|
|
|
|
+ Object oldValue = field.get(oldObject);
|
|
|
|
+ Object newValue = field.get(newObject);
|
|
|
|
+ return new FieldWrapper(field, oldValue, newValue);
|
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
|
+ throw new RuntimeException("无法访问字段: " + field.getName(), e);
|
|
}
|
|
}
|
|
-
|
|
|
|
- if (ObjectUtil.isNotEmpty(oldMap)) {
|
|
|
|
- String deleteOldDataDescription = oldMap.values().stream().map(LogTagUtil::getObjectAllLogTag).collect(Collectors.joining("; "));
|
|
|
|
- fieldDescription.append("删除数据:").append(deleteOldDataDescription);
|
|
|
|
|
|
+ })
|
|
|
|
+ .filter(FieldWrapper::isWithLogTag)
|
|
|
|
+ .sorted(Comparator.comparing(wrapper -> wrapper.getLogTag().extendedType()))
|
|
|
|
+ .toList();
|
|
|
|
+
|
|
|
|
+ for (FieldWrapper logTagFieldWrapper : logTagFieldWrappers) {
|
|
|
|
+ LogTag logTag = logTagFieldWrapper.getLogTag();
|
|
|
|
+ LogTagExtendedType logTagExtendedType = logTag.extendedType();
|
|
|
|
+
|
|
|
|
+ switch (logTagExtendedType) {
|
|
|
|
+ case OBJECT -> {
|
|
|
|
+ String fieldDescription = fieldComparison(logTagFieldWrapper.getOldValue(), logTagFieldWrapper.getNewValue());
|
|
|
|
+ if (StrUtil.isNotBlank(fieldDescription)) {
|
|
|
|
+ description.append(logTagFieldWrapper.getAttributeAlias())
|
|
|
|
+ .append(":").append(fieldDescription)
|
|
|
|
+ .append(separator);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ case COLLECTION -> {
|
|
|
|
+ String collectionDescription = handleCollectionChange(logTagFieldWrapper);
|
|
|
|
+ if (StrUtil.isNotBlank(collectionDescription)) {
|
|
|
|
+ description.append(logTagFieldWrapper.getAttributeAlias())
|
|
|
|
+ .append(":[").append(collectionDescription).append("]")
|
|
|
|
+ .append(separator);
|
|
}
|
|
}
|
|
-
|
|
|
|
- description.append(fieldWrapper.getAttributeAlias()).append(":").append(fieldDescription).append(separator);
|
|
|
|
}
|
|
}
|
|
- if (!LogTagUtil.nullableEquals(fieldWrapper.getOldValue(), fieldWrapper.getNewValue())) {
|
|
|
|
-
|
|
|
|
- String baseAttributeModel = switch (logTag.extendedType()) {
|
|
|
|
- case NORMAL -> LogTagUtil.handleNormalExtendedTypeItem(fieldWrapper);
|
|
|
|
- case DATE -> LogTagUtil.handleDateExtendedTypeItem(fieldWrapper);
|
|
|
|
- case CONVERT -> LogTagUtil.handleConvertExtendedTypeItem(fieldWrapper);
|
|
|
|
- case DICT -> LogTagUtil.handleDictExtendedTypeItem(fieldWrapper);
|
|
|
|
- case COMPLEX -> LogTagUtil.handleComplexExtendedTypeItem(fieldWrapper);
|
|
|
|
- default -> "";
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- description.append(baseAttributeModel).append(separator);
|
|
|
|
|
|
+ default -> {
|
|
|
|
+ // 非 OBJECT/COLLECTION 类型,且值发生变化时处理
|
|
|
|
+ if (!nullableEquals(logTagFieldWrapper.getOldValue(), logTagFieldWrapper.getNewValue())) {
|
|
|
|
+ String baseModel = switch (logTagExtendedType) {
|
|
|
|
+ case NORMAL -> handleNormalExtendedTypeItem(logTagFieldWrapper);
|
|
|
|
+ case DATE -> handleDateExtendedTypeItem(logTagFieldWrapper);
|
|
|
|
+ case CONVERT -> handleConvertExtendedTypeItem(logTagFieldWrapper);
|
|
|
|
+ case DICT -> handleDictExtendedTypeItem(logTagFieldWrapper);
|
|
|
|
+ case COMPLEX -> handleComplexExtendedTypeItem(logTagFieldWrapper);
|
|
|
|
+ default -> "";
|
|
|
|
+ };
|
|
|
|
+ if (StrUtil.isNotBlank(baseModel)) {
|
|
|
|
+ description.append(baseModel).append(separator);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -165,7 +148,7 @@ public class LogTagUtil {
|
|
* @param newValue
|
|
* @param newValue
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static boolean nullableEquals(Object oldValue, Object newValue) {
|
|
|
|
|
|
+ private static boolean nullableEquals(Object oldValue, Object newValue) {
|
|
if (oldValue == null && newValue == null) {
|
|
if (oldValue == null && newValue == null) {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
@@ -199,7 +182,7 @@ public class LogTagUtil {
|
|
* @param fieldWrapper
|
|
* @param fieldWrapper
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static String handleDateExtendedTypeItem(FieldWrapper fieldWrapper) {
|
|
|
|
|
|
+ private static String handleDateExtendedTypeItem(FieldWrapper fieldWrapper) {
|
|
String oldValueStr = fieldWrapper.getOldValueString();
|
|
String oldValueStr = fieldWrapper.getOldValueString();
|
|
String format = fieldWrapper.getLogTag().format();
|
|
String format = fieldWrapper.getLogTag().format();
|
|
if (StrUtil.isBlank(oldValueStr)) {
|
|
if (StrUtil.isBlank(oldValueStr)) {
|
|
@@ -222,7 +205,7 @@ public class LogTagUtil {
|
|
* @param fieldWrapper
|
|
* @param fieldWrapper
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static String handleConvertExtendedTypeItem(FieldWrapper fieldWrapper) {
|
|
|
|
|
|
+ private static String handleConvertExtendedTypeItem(FieldWrapper fieldWrapper) {
|
|
LogTag logTag = fieldWrapper.getLogTag();
|
|
LogTag logTag = fieldWrapper.getLogTag();
|
|
String oldValueStr = convertByExp(fieldWrapper.getOldValueString(), logTag.readConverterExp(), logTag.separator());
|
|
String oldValueStr = convertByExp(fieldWrapper.getOldValueString(), logTag.readConverterExp(), logTag.separator());
|
|
String newValueStr = convertByExp(fieldWrapper.getNewValueString(), logTag.readConverterExp(), logTag.separator());
|
|
String newValueStr = convertByExp(fieldWrapper.getNewValueString(), logTag.readConverterExp(), logTag.separator());
|
|
@@ -235,7 +218,7 @@ public class LogTagUtil {
|
|
* @param fieldWrapper
|
|
* @param fieldWrapper
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static String handleDictExtendedTypeItem(FieldWrapper fieldWrapper) {
|
|
|
|
|
|
+ private static String handleDictExtendedTypeItem(FieldWrapper fieldWrapper) {
|
|
LogTag logTag = fieldWrapper.getLogTag();
|
|
LogTag logTag = fieldWrapper.getLogTag();
|
|
String oldValueStr = convertByDict(logTag.dictCode(), fieldWrapper.getOldValueString(), logTag.separator());
|
|
String oldValueStr = convertByDict(logTag.dictCode(), fieldWrapper.getOldValueString(), logTag.separator());
|
|
String newValueStr = convertByDict(logTag.dictCode(), fieldWrapper.getNewValueString(), logTag.separator());
|
|
String newValueStr = convertByDict(logTag.dictCode(), fieldWrapper.getNewValueString(), logTag.separator());
|
|
@@ -248,11 +231,64 @@ public class LogTagUtil {
|
|
* @param fieldWrapper
|
|
* @param fieldWrapper
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public static String handleComplexExtendedTypeItem(FieldWrapper fieldWrapper) {
|
|
|
|
|
|
+ private static String handleComplexExtendedTypeItem(FieldWrapper fieldWrapper) {
|
|
return "修改了" + fieldWrapper.getAttributeAlias();
|
|
return "修改了" + fieldWrapper.getAttributeAlias();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * 处理集合对象类型
|
|
|
|
+ * @param fieldWrapper
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private static String handleCollectionChange(FieldWrapper fieldWrapper) {
|
|
|
|
+ Collection<?> oldCollection = fieldWrapper.getOldValue() == null ? Collections.emptyList() : (Collection<?>) fieldWrapper.getOldValue();
|
|
|
|
+ Collection<?> newCollection = fieldWrapper.getNewValue() == null ? Collections.emptyList() : (Collection<?>) fieldWrapper.getNewValue();
|
|
|
|
+
|
|
|
|
+ // 转为 id -> object 的 map
|
|
|
|
+ Map<Long, Object> oldMap = oldCollection.stream()
|
|
|
|
+ .filter(Objects::nonNull)
|
|
|
|
+ .filter(item -> item instanceof BaseIdPo)
|
|
|
|
+ .map(item -> (BaseIdPo) item)
|
|
|
|
+ .filter(item -> item.getId() != null)
|
|
|
|
+ .collect(Collectors.toMap(BaseIdPo::getId, Function.identity()));
|
|
|
|
+
|
|
|
|
+ StringBuilder desc = new StringBuilder();
|
|
|
|
+
|
|
|
|
+ String separator = "; \n";
|
|
|
|
+
|
|
|
|
+ // 遍历新集合:判断新增 or 修改
|
|
|
|
+ for (Object newObj : newCollection) {
|
|
|
|
+ Object oldObj = null;
|
|
|
|
+ Long id = null;
|
|
|
|
+ if (newObj instanceof BaseIdPo) {
|
|
|
|
+ id = ((BaseIdPo) newObj).getId();
|
|
|
|
+ }
|
|
|
|
+ if (id != null && oldMap.containsKey(id)) {
|
|
|
|
+ oldObj = oldMap.remove(id);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (oldObj != null) {
|
|
|
|
+ String editDesc = fieldComparison(oldObj, newObj);
|
|
|
|
+ if (StrUtil.isNotBlank(editDesc)) {
|
|
|
|
+ desc.append("修改数据:{").append(editDesc).append("}").append(separator);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ desc.append("新增数据:{").append(getObjectAllLogTag(newObj)).append("}").append(separator);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 剩余在 oldMap 中的为删除项
|
|
|
|
+ if (!oldMap.isEmpty()) {
|
|
|
|
+ String deleted = oldMap.values().stream()
|
|
|
|
+ .map(LogTagUtil::getObjectAllLogTag)
|
|
|
|
+ .collect(Collectors.joining(separator, "{", "}"));
|
|
|
|
+ desc.append("删除数据:").append(deleted);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return desc.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
* 解析字典值
|
|
* 解析字典值
|
|
*
|
|
*
|
|
* @param dictCode 字典编码
|
|
* @param dictCode 字典编码
|