|
|
|
@ -88,7 +88,7 @@ public class TestXiaoTuPuController extends BaseController { |
|
|
|
|
|
|
|
// 查询某个节点的关系信息
|
|
|
|
@GetMapping("test5/{name}") |
|
|
|
public Map<String, Object> test5(@PathVariable("name")String name) { |
|
|
|
public Map<String, List<Map<String, Object>>> test5(@PathVariable("name")String name) { |
|
|
|
|
|
|
|
List<Map> list = zhyPointMapper.selectPointByName(name); |
|
|
|
if (list.size()==0){ |
|
|
|
@ -112,17 +112,19 @@ public class TestXiaoTuPuController extends BaseController { |
|
|
|
|
|
|
|
List<Map> kk1 = (List<Map>) node1; |
|
|
|
List<Map> rrk1 = (List<Map>) graphNode.get("relationship"); |
|
|
|
System.out.println(kk1); |
|
|
|
System.out.println(rrk1); |
|
|
|
|
|
|
|
Map<String, List<Map<String, Object>>> a = convertToGraphFormat(kk1,rrk1); |
|
|
|
System.out.println(a.get("links")); |
|
|
|
List<Map<String, Object>> relationships = mergeRelationships(a.get("links")); |
|
|
|
a.put("links",relationships); |
|
|
|
|
|
|
|
Map<String, Object> a = convertToGraphFormat(kk1,rrk1); |
|
|
|
System.out.println(a.get("links")); |
|
|
|
return a; |
|
|
|
} |
|
|
|
|
|
|
|
// 查询所有节点的所有关系
|
|
|
|
@GetMapping("test6") |
|
|
|
public Map<String, Object> test6() { |
|
|
|
public Map<String, List<Map<String, Object>>> test6() { |
|
|
|
String cypher = "MATCH (n:TestNode)-[r]-(m:TestNode) RETURN n, m, r"; |
|
|
|
HashMap<String, Object> graphNode = neo4jUtil.getGraphNodeAndShip(cypher); |
|
|
|
Object node1 = graphNode.get("node"); |
|
|
|
@ -132,7 +134,7 @@ public class TestXiaoTuPuController extends BaseController { |
|
|
|
System.out.println(rrk1); |
|
|
|
|
|
|
|
|
|
|
|
Map<String, Object> a = convertToGraphFormat(kk1,rrk1); |
|
|
|
Map<String, List<Map<String, Object>>> a = convertToGraphFormat(kk1,rrk1); |
|
|
|
return a; |
|
|
|
} |
|
|
|
|
|
|
|
@ -164,7 +166,7 @@ public class TestXiaoTuPuController extends BaseController { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Map<String, Object> convertToGraphFormat(List<Map> rawNodes, List<Map> rawRelationships) { |
|
|
|
public Map<String, List<Map<String, Object>>> convertToGraphFormat(List<Map> rawNodes, List<Map> rawRelationships) { |
|
|
|
List<Map<String, Object>> nodes = new ArrayList<>(); |
|
|
|
List<Map<String, Object>> links = new ArrayList<>(); |
|
|
|
|
|
|
|
@ -197,6 +199,7 @@ public class TestXiaoTuPuController extends BaseController { |
|
|
|
String from = String.valueOf(rel.get("sourceid")); |
|
|
|
String to = String.valueOf(rel.get("targetid")); |
|
|
|
String data = String.valueOf(rel.get("data")); |
|
|
|
String uuid = String.valueOf(rel.get("uuid")); |
|
|
|
Map<String, String> datamap = convertStringToMap(data); |
|
|
|
String relType = datamap.get("relType"); // 默认值
|
|
|
|
|
|
|
|
@ -208,13 +211,14 @@ public class TestXiaoTuPuController extends BaseController { |
|
|
|
|
|
|
|
newLink.put("from", from); |
|
|
|
newLink.put("to", to); |
|
|
|
newLink.put("uuid",uuid); |
|
|
|
newLink.put("text", relType.isEmpty() ? "关联" : relType); |
|
|
|
links.add(newLink); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 返回前端格式
|
|
|
|
Map<String, Object> result = new HashMap<>(); |
|
|
|
Map<String, List<Map<String, Object>>> result = new HashMap<>(); |
|
|
|
result.put("nodes", nodes); |
|
|
|
result.put("links", links); |
|
|
|
return result; |
|
|
|
@ -279,6 +283,56 @@ public class TestXiaoTuPuController extends BaseController { |
|
|
|
return map; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static List<Map<String, Object>> mergeRelationships(List<Map<String, Object>> relationships) { |
|
|
|
Map<String, Map<String, Object>> merged = new HashMap<>(); |
|
|
|
|
|
|
|
for (Map rel : relationships) { |
|
|
|
// 提取关键字段
|
|
|
|
String from = String.valueOf(rel.get("from")) ; |
|
|
|
String to =String.valueOf(rel.get("to")); |
|
|
|
String text = String.valueOf(rel.get("text")); // 可作为业务类型,比如 "相关作者"
|
|
|
|
String uuid = String.valueOf(rel.get("uuid")); |
|
|
|
|
|
|
|
// 构建唯一键:from -> to
|
|
|
|
String key = from + "->" + to; |
|
|
|
|
|
|
|
if (!merged.containsKey(key)) { |
|
|
|
// 第一次出现,创建新关系
|
|
|
|
Map<String, Object> newRel = new HashMap<>(); |
|
|
|
newRel.put("from", from); |
|
|
|
newRel.put("to", to); |
|
|
|
newRel.put("text", text); |
|
|
|
|
|
|
|
// 复制其他非核心字段(可选,如 style, color 等)
|
|
|
|
for (Object k : rel.keySet()) { |
|
|
|
String field = (String) k; |
|
|
|
if (!Arrays.asList("from", "to", "text", "uuid").contains(field)) { |
|
|
|
newRel.put(field, rel.get(field)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 初始化合并字段
|
|
|
|
newRel.put("num", 1); |
|
|
|
List<String> uuids = new ArrayList<>(); |
|
|
|
if (uuid != null) uuids.add(uuid); |
|
|
|
newRel.put("uuids", uuids); |
|
|
|
|
|
|
|
merged.put(key, newRel); |
|
|
|
} else { |
|
|
|
// 已存在,进行合并
|
|
|
|
Map<String, Object> existing = merged.get(key); |
|
|
|
existing.put("num", (int) existing.get("num") + 1); |
|
|
|
List<String> uuids = (List<String>) existing.get("uuids"); |
|
|
|
if (uuid != null && !uuids.contains(uuid)) { |
|
|
|
uuids.add(uuid); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return new ArrayList<>(merged.values()); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|