|
@@ -1,9 +1,10 @@
|
|
|
package thyyxxk.webserver.utils;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import thyyxxk.webserver.config.ColumnName;
|
|
|
+import thyyxxk.webserver.config.exception.BizException;
|
|
|
+import thyyxxk.webserver.config.exception.ExceptionEnum;
|
|
|
import thyyxxk.webserver.entity.login.UserInfo;
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
@@ -11,25 +12,44 @@ import java.util.*;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
+/**
|
|
|
+ * @author xc
|
|
|
+ */
|
|
|
@Slf4j
|
|
|
public class CampareObject {
|
|
|
|
|
|
- public static List<String> getChangedFieldsOfObject(Object oldObj, Object newObj) {
|
|
|
- JSONObject oldJson = JSONObject.parseObject(JSON.toJSONString(oldObj));
|
|
|
- JSONObject newJson = JSONObject.parseObject(JSON.toJSONString(newObj));
|
|
|
- List<String> resultArray = new ArrayList<>();
|
|
|
- for (String key : oldJson.keySet()) {
|
|
|
- if (null != newJson.get(key)) {
|
|
|
- if (!newJson.get(key).equals(oldJson.get(key))) {
|
|
|
- resultArray.add(key);
|
|
|
+ public static JSONObject getChangedFieldsOfObject(Object oldObj, Object newObj) {
|
|
|
+ JSONObject resultObject = new JSONObject();
|
|
|
+ if (oldObj != null && newObj != null) {
|
|
|
+ Field[] fields = oldObj.getClass().getDeclaredFields();
|
|
|
+ for (Field f : fields) {
|
|
|
+ // 获取字段名 在实体类中是什么样子这里就是什么样子
|
|
|
+ String key = f.getName();
|
|
|
+ try {
|
|
|
+ boolean annotationPresent2 = f.isAnnotationPresent(ColumnName.class);
|
|
|
+ if (annotationPresent2) {
|
|
|
+ Object oldValue = EntityStringTrim.getFieldValue(oldObj, key);
|
|
|
+ ColumnName name = f.getAnnotation(ColumnName.class);
|
|
|
+ // 获取注解值
|
|
|
+ String nameStr = name.name();
|
|
|
+ if (StringUtil.isBlank(nameStr)) {
|
|
|
+ nameStr = humpToUnderline(key);
|
|
|
+ }
|
|
|
+ Object newValue = EntityStringTrim.getFieldValue(newObj, key);
|
|
|
+ if (!Objects.equals(newValue, oldValue)) {
|
|
|
+ resultObject.put(nameStr, newValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- return resultArray;
|
|
|
+ return resultObject;
|
|
|
}
|
|
|
|
|
|
- public static JSONObject getChangedFieldsOfObjectTest(Object oldObj, Object newObj) {
|
|
|
- JSONObject resultObject = new JSONObject();
|
|
|
+ public static String getChangedFieldsOfSql(Object oldObj, Object newObj) {
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
if (oldObj != null && newObj != null) {
|
|
|
Field[] fields = oldObj.getClass().getDeclaredFields();
|
|
|
for (Field f : fields) {
|
|
@@ -38,18 +58,22 @@ public class CampareObject {
|
|
|
try {
|
|
|
boolean annotationPresent2 = f.isAnnotationPresent(ColumnName.class);
|
|
|
if (annotationPresent2) {
|
|
|
- Object value = EntityStringTrim.getFieldValue(oldObj, key);
|
|
|
+ Object oldValue = EntityStringTrim.getFieldValue(oldObj, key);
|
|
|
ColumnName name = f.getAnnotation(ColumnName.class);
|
|
|
// 获取注解值
|
|
|
String nameStr = name.name();
|
|
|
if (StringUtil.isBlank(nameStr)) {
|
|
|
nameStr = humpToUnderline(key);
|
|
|
}
|
|
|
- Object newData = EntityStringTrim.getFieldValue(newObj, key);
|
|
|
- if (null == newData) {
|
|
|
- resultObject.put(nameStr, null);
|
|
|
- } else if (!Objects.equals(newData, value)) {
|
|
|
- resultObject.put(nameStr, newData);
|
|
|
+ Object newValue = EntityStringTrim.getFieldValue(newObj, key);
|
|
|
+
|
|
|
+ if (!Objects.equals(newValue, oldValue)) {
|
|
|
+ if (null == newValue) {
|
|
|
+ nameStr = nameStr + "=" + null;
|
|
|
+ stringBuilder.append(nameStr).append(",");
|
|
|
+ } else {
|
|
|
+ stringBuilder.append(nameStr).append("='").append(newValue).append("',");
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
@@ -57,7 +81,11 @@ public class CampareObject {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- return resultObject;
|
|
|
+ String sql = stringBuilder.toString();
|
|
|
+ if (StringUtil.isBlank(sql)) {
|
|
|
+ throw new BizException(ExceptionEnum.LOGICAL_ERROR, "没有需要更新的字段");
|
|
|
+ }
|
|
|
+ return sql.substring(0, stringBuilder.length() - 1);
|
|
|
}
|
|
|
|
|
|
public static String humpToUnderline(String str) {
|
|
@@ -75,12 +103,10 @@ public class CampareObject {
|
|
|
UserInfo aa = new UserInfo();
|
|
|
UserInfo bb = new UserInfo();
|
|
|
aa.setDeptCode("123123").setName("aaa").setSid("123123").setToken("123123");
|
|
|
- bb.setDeptCode("adfd").setName("aaa").setSid("123123").setToken("123123");
|
|
|
+ bb.setDeptCode("adfd").setName("aaa1").setSid("123123").setToken("123123");
|
|
|
|
|
|
- String a = null;
|
|
|
- if (!Objects.equals(a, "12")) {
|
|
|
- System.out.println("123");
|
|
|
- }
|
|
|
- System.out.println(getChangedFieldsOfObjectTest(aa, bb));
|
|
|
+
|
|
|
+ System.out.println(getChangedFieldsOfObject(aa, bb));
|
|
|
+ System.out.println(getChangedFieldsOfSql(aa, bb));
|
|
|
}
|
|
|
}
|