add 对接人编辑时取得修改的内容
This commit is contained in:
@@ -1,12 +1,21 @@
|
||||
package com.jero.util;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.jero.common.aspect.annotation.Dict;
|
||||
import com.jero.common.system.api.ISysBaseAPI;
|
||||
import com.jero.common.util.SpringContextUtils;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 比对两个对象,输出变更字段
|
||||
*
|
||||
* @author liJiaRao
|
||||
* @date 2021-09-22 17:05
|
||||
*/
|
||||
@@ -16,42 +25,65 @@ public class ObjRecordUtil {
|
||||
* @param newObj 修改后数据对象
|
||||
* @return
|
||||
*/
|
||||
public static String addRecord(Object oldObj, Object newObj){
|
||||
public static String addRecord(Object oldObj, Object newObj) {
|
||||
try {
|
||||
// 得到类对象
|
||||
Class<?> class1 = oldObj.getClass();
|
||||
Class<?> class2 = newObj.getClass();
|
||||
if(!class1.equals(class2)){
|
||||
if (!class1.equals(class2)) {
|
||||
throw new RuntimeException("请传入两个相同的实体类对象");
|
||||
}
|
||||
// 得到属性集合
|
||||
Field[] fields1 = class1.getDeclaredFields();
|
||||
Field[] fields2 = class2.getDeclaredFields();
|
||||
StringBuilder info = new StringBuilder();
|
||||
Long id = null;
|
||||
String id = null;
|
||||
for (Field field1 : fields1) {
|
||||
field1.setAccessible(true); // 设置属性是可以访问的(私有的也可以)
|
||||
if(id == null && field1.getName().equals("id")){
|
||||
id = (Long)field1.get(oldObj);
|
||||
if (id == null && field1.getName().equals("id")) {
|
||||
id = (String) field1.get(oldObj);
|
||||
}
|
||||
for (Field field2 : fields2) {
|
||||
field2.setAccessible(true); // 设置属性是可以访问的(私有的也可以)
|
||||
if(field1.equals(field2)){ // 比较属性名是否一样
|
||||
if(field2.get(newObj) == null || StringUtils.isEmpty(field2.get(newObj) + "")){
|
||||
if (field1.equals(field2)) { // 比较属性名是否一样
|
||||
if (field2.get(newObj) == null || StringUtils.isEmpty(field2.get(newObj) + "")) {
|
||||
break; // 属性名称一样就退出二级循环
|
||||
}
|
||||
if(!field1.get(oldObj).equals(field2.get(newObj))){ // 比较属性值是否一样
|
||||
if (!Objects.equals(field1.get(oldObj), field2.get(newObj))) { // 比较属性值是否一样
|
||||
// 得到注解
|
||||
ApiModelProperty pn = field1.getAnnotation(ApiModelProperty.class);
|
||||
if(pn != null){
|
||||
info.append(pn.value()).append(":\"").append(field1.get(oldObj)).append("\" 改成 \"").append(field2.get(newObj)).append("\",");
|
||||
Dict dict = field1.getAnnotation(Dict.class);
|
||||
if (pn != null) {
|
||||
Object old = field1.get(oldObj);
|
||||
Object newo = field2.get(newObj);
|
||||
if (pn.value().contains("id")) {
|
||||
break;
|
||||
} else if (pn.value().contains("文件")) {
|
||||
info.append(pn.value()).append("修改了,");
|
||||
|
||||
} else if (pn.value().contains("时间")) {
|
||||
DateTimeFormat dateTimeFormat = field1.getAnnotation(DateTimeFormat.class);
|
||||
String pattern = dateTimeFormat.pattern();
|
||||
old = DateUtil.format((Date) old, pattern);
|
||||
newo = DateUtil.format((Date) newo, pattern);
|
||||
info.append(pn.value()).append(":\"").append(old).append("\" 改成 \"").append(newo).append("\",");
|
||||
|
||||
} else if (dict != null) {
|
||||
String dicCode = dict.dicCode();
|
||||
ISysBaseAPI sysBaseAPI = SpringContextUtils.getBean(ISysBaseAPI.class);
|
||||
old = sysBaseAPI.translateDict(dicCode, String.valueOf(old));
|
||||
newo = sysBaseAPI.translateDict(dicCode, String.valueOf(newo));
|
||||
info.append(pn.value()).append(":\"").append(old).append("\" 改成 \"").append(newo).append("\",");
|
||||
} else {
|
||||
info.append(pn.value()).append(":\"").append(old).append("\" 改成 \"").append(newo).append("\",");
|
||||
}
|
||||
}
|
||||
}
|
||||
break; // 属性名称一样就退出二级循环
|
||||
}
|
||||
}
|
||||
}
|
||||
if(info.length() != 0){
|
||||
if (info.length() != 0) {
|
||||
// 设置日志信息
|
||||
return info.substring(0, info.length() - 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user