Commit f9ee5974 authored by suhuiguang's avatar suhuiguang

fix(大编辑):bug修复

1.json字段比较优化
parent 469b900a
...@@ -72,11 +72,6 @@ ...@@ -72,11 +72,6 @@
<version>0.10.2</version> <version>0.10.2</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.3</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
......
...@@ -23,6 +23,7 @@ import com.yeejoin.amos.boot.module.jg.api.enums.EquipSourceEnum; ...@@ -23,6 +23,7 @@ import com.yeejoin.amos.boot.module.jg.api.enums.EquipSourceEnum;
import com.yeejoin.amos.boot.module.jg.api.mapper.CommonMapper; import com.yeejoin.amos.boot.module.jg.api.mapper.CommonMapper;
import com.yeejoin.amos.boot.module.jg.biz.edit.typeHandler.FormatService; import com.yeejoin.amos.boot.module.jg.biz.edit.typeHandler.FormatService;
import com.yeejoin.amos.boot.module.jg.biz.edit.typeHandler.RegionCodeTypeHandler; import com.yeejoin.amos.boot.module.jg.biz.edit.typeHandler.RegionCodeTypeHandler;
import com.yeejoin.amos.boot.module.jg.biz.edit.utils.JsonDiffUtil;
import com.yeejoin.amos.boot.module.jg.biz.service.*; import com.yeejoin.amos.boot.module.jg.biz.service.*;
import com.yeejoin.amos.boot.module.jg.biz.service.impl.*; import com.yeejoin.amos.boot.module.jg.biz.service.impl.*;
import com.yeejoin.amos.boot.module.ymt.api.entity.*; import com.yeejoin.amos.boot.module.ymt.api.entity.*;
...@@ -692,7 +693,7 @@ public class CommonEquipDataProcessService { ...@@ -692,7 +693,7 @@ public class CommonEquipDataProcessService {
if (displayDefine != null && displayDefine.isExist()) { if (displayDefine != null && displayDefine.isExist()) {
// json 比较逻辑 // json 比较逻辑
if (displayDefine.type().equals(JSON.class)) { if (displayDefine.type().equals(JSON.class)) {
if (!isJsonEqualLoose((String) oldVal, (String) newVal)) { if (!JsonDiffUtil.jsonEqualsIgnoreType((String) oldVal, (String) newVal)) {
String columnName = tableField.value(); String columnName = tableField.value();
wrapper.set(columnName, newVal); wrapper.set(columnName, newVal);
String fieldName = displayDefine.value(); String fieldName = displayDefine.value();
...@@ -704,8 +705,8 @@ public class CommonEquipDataProcessService { ...@@ -704,8 +705,8 @@ public class CommonEquipDataProcessService {
fieldChangeMeta.setIsRepeatColumn(displayDefine.isRepeatColumn()); fieldChangeMeta.setIsRepeatColumn(displayDefine.isRepeatColumn());
// 字段类型前端渲染时使用 // 字段类型前端渲染时使用
fieldChangeMeta.setColumnType(displayDefine.type().getSimpleName()); fieldChangeMeta.setColumnType(displayDefine.type().getSimpleName());
fieldChangeMeta.setColumnOldValue(Objects.toString(oldVal, "")); fieldChangeMeta.setColumnOldValue(Objects.toString(oldVal, null));
fieldChangeMeta.setColumnNewValue(Objects.toString(newVal, "")); fieldChangeMeta.setColumnNewValue(Objects.toString(newVal, null));
fieldChangeMeta.setDisplayOldValue(formatService.format(displayDefine, fieldChangeMeta.getColumnOldValue())); fieldChangeMeta.setDisplayOldValue(formatService.format(displayDefine, fieldChangeMeta.getColumnOldValue()));
fieldChangeMeta.setDisplayNewValue(formatService.format(displayDefine, fieldChangeMeta.getColumnNewValue())); fieldChangeMeta.setDisplayNewValue(formatService.format(displayDefine, fieldChangeMeta.getColumnNewValue()));
changeData.add(fieldChangeMeta); changeData.add(fieldChangeMeta);
...@@ -762,15 +763,6 @@ public class CommonEquipDataProcessService { ...@@ -762,15 +763,6 @@ public class CommonEquipDataProcessService {
return changeData; return changeData;
} }
public static boolean isJsonEqualLoose(String expectedJson, String actualJson) {
try {
JSONAssert.assertEquals(expectedJson, actualJson, JSONCompareMode.LENIENT);
return true;
} catch (Error | JSONException e) {
return false;
}
}
public Map<String, Object> getEquipDetailByRecord(String record) { public Map<String, Object> getEquipDetailByRecord(String record) {
Map<String, Object> re = new HashMap<>(); Map<String, Object> re = new HashMap<>();
......
package com.yeejoin.amos.boot.module.jg.biz.edit.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Iterator;
import java.util.Map;
public class JsonDiffUtil {
public static boolean jsonEqualsIgnoreType(String json1, String json2) {
ObjectMapper mapper = new ObjectMapper();
// 处理输入的 null 和空字符串视为相等
if (isNullOrEmpty(json1) && isNullOrEmpty(json2)) return true;
if (isNullOrEmpty(json1) || isNullOrEmpty(json2)) return false;
JsonNode node1 = null;
try {
node1 = mapper.readTree(json1);
JsonNode node2 = mapper.readTree(json2);
return equalsIgnoreType(node1, node2);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
private static boolean equalsIgnoreType(JsonNode node1, JsonNode node2) {
// 处理输入的 null JsonNode
if (node1 == null && node2 == null) return true;
if (node1 == null || node2 == null) return node1 == node2;
// 处理 JSON null 节点
if (node1.isNull() && node2.isNull()) return true;
if (node1.isNull() || node2.isNull()) return false;
// 节点类型不同时尝试转换为文本比较
if (!node1.getNodeType().equals(node2.getNodeType())) {
return node1.asText().equals(node2.asText());
}
// 根据不同类型处理
switch (node1.getNodeType()) {
case OBJECT:
if (node1.size() != node2.size()) return false;
Iterator<Map.Entry<String, JsonNode>> fields = node1.fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> entry = fields.next();
JsonNode value2 = node2.get(entry.getKey());
if (value2 == null && !node2.has(entry.getKey())) {
return false; // 键不存在
}
if (!equalsIgnoreType(entry.getValue(), value2)) {
return false;
}
}
return true;
case ARRAY:
if (node1.size() != node2.size()) return false;
for (int i = 0; i < node1.size(); i++) {
if (!equalsIgnoreType(node1.get(i), node2.get(i))) {
return false;
}
}
return true;
case STRING:
return node1.asText().equals(node2.asText());
case NUMBER:
return node1.decimalValue().compareTo(node2.decimalValue()) == 0;
case BOOLEAN:
return node1.asBoolean() == node2.asBoolean();
case NULL:
return true; // 前面已经处理过null
default:
return false;
}
}
private static boolean isNullOrEmpty(String str) {
return str == null || str.trim().isEmpty();
}
}
...@@ -91,12 +91,4 @@ public class SingleManageEquipEditHandleImpl { ...@@ -91,12 +91,4 @@ public class SingleManageEquipEditHandleImpl {
commonEquipDataProcessService.dealBizDataForEquip(record, useInfoChangeDataDto); commonEquipDataProcessService.dealBizDataForEquip(record, useInfoChangeDataDto);
return allChangeColumns; return allChangeColumns;
} }
private void buildLogData(List<FieldChangeMeta> allChangeColumns) {
allChangeColumns.forEach(column -> {
column.setBizType("监管端编辑设备信息");
});
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment