|
@@ -5,9 +5,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.neo4j.core.Neo4jClient;
|
|
|
import org.springframework.stereotype.Repository;
|
|
|
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Optional;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Repository
|
|
|
public class BaseNodeRepository {
|
|
@@ -19,8 +18,8 @@ public class BaseNodeRepository {
|
|
|
this.neo4jClient = neo4jClient;
|
|
|
}
|
|
|
|
|
|
- public Map<String,Object> findNode(String label,String name) {
|
|
|
- String query = String.format("MATCH (n:`%s`) WHERE n.name = $name RETURN n", label);
|
|
|
+ public Map<String,Object> findNodeByName(String label,String name) {
|
|
|
+ String query = String.format("MATCH (n:`%s`) WHERE n.name = $name and n.is_deleted = 'N' RETURN n", label);
|
|
|
Optional<Map> optionalMap = neo4jClient.query(query)
|
|
|
.bind(name).to("name")
|
|
|
.fetchAs(Map.class)
|
|
@@ -38,8 +37,50 @@ public class BaseNodeRepository {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ public Map<String,Object> findNodeById(Long id) {
|
|
|
+ String query = "MATCH (n) WHERE id(n) = $nodeId and n.is_deleted = 'N' RETURN n";
|
|
|
+ Optional<Map> optionalMap = neo4jClient.query(query)
|
|
|
+ .bind(id).to("nodeId")
|
|
|
+ .fetchAs(Map.class)
|
|
|
+ .mappedBy((typeSystem, record) -> {
|
|
|
+ Node node = record.get("n").asNode();
|
|
|
+
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ result.put("id", node.id());
|
|
|
+ result.put("labels", node.labels()); // 获取节点的标签
|
|
|
+ result.put("properties", node.asMap()); // 获取节点的属性
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }).one();
|
|
|
+ return (Map<String, Object>) optionalMap.orElse(null);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Map<String,Object>> findNodeListByName(String label, String name) {
|
|
|
+ String query = String.format("MATCH (n:`%s`) WHERE n.name =~ %s$name%s and n.is_deleted = 'N' RETURN n", label,".*",".*");
|
|
|
+ Collection<Map> mapCollection = neo4jClient.query(query)
|
|
|
+ .bind(name).to("name")
|
|
|
+ .fetchAs(Map.class)
|
|
|
+ .mappedBy((typeSystem, record) -> {
|
|
|
+ Node node = record.get("n").asNode();
|
|
|
+
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ result.put("id", node.id());
|
|
|
+ result.put("labels", node.labels()); // 获取节点的标签
|
|
|
+ result.put("properties", node.asMap()); // 获取节点的属性
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }).all();
|
|
|
+ if (mapCollection.isEmpty()) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ return mapCollection.stream().map(e -> (Map<String, Object>) e).collect(Collectors.toList());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
public Map<String, Object> createNode(String label, Map<String, Object> properties) {
|
|
|
StringBuilder createClause = new StringBuilder();
|
|
|
+ properties.put("is_deleted","N");
|
|
|
properties.forEach((key, value) -> createClause.append(key).append(": $").append(key).append(", "));
|
|
|
|
|
|
// 移除最后的逗号
|
|
@@ -49,106 +90,44 @@ public class BaseNodeRepository {
|
|
|
|
|
|
String query = String.format("CREATE (n:`%s` { %s }) RETURN n", label, createClause);
|
|
|
|
|
|
- return (Map<String, Object>)neo4jClient.query(query)
|
|
|
+ return (Map<String, Object>) neo4jClient.query(query)
|
|
|
.bindAll(properties)
|
|
|
.fetchAs(Map.class).mappedBy((typeSystem, record) -> {
|
|
|
- Node node = record.get("n").asNode();
|
|
|
+ Node node = record.get("n").asNode();
|
|
|
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("id", node.id());
|
|
|
- result.put("labels", node.labels()); // 获取节点的标签
|
|
|
- result.put("properties", node.asMap()); // 获取节点的属性
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ result.put("id", node.id());
|
|
|
+ result.put("labels", node.labels()); // 获取节点的标签
|
|
|
+ result.put("properties", node.asMap()); // 获取节点的属性
|
|
|
|
|
|
- return result;
|
|
|
- }).one().orElse(null);
|
|
|
+ return result;
|
|
|
+ }).one().orElse(null);
|
|
|
}
|
|
|
|
|
|
- public void updateNode(String label, Long nodeId, Map<String, Object> properties) {
|
|
|
- StringBuilder setClause = new StringBuilder();
|
|
|
- properties.forEach((key, value) -> setClause.append("n.").append(key).append(" = $").append(key).append(", "));
|
|
|
+ public Map<String, Object> updateNode(Long nodeId, String name) {
|
|
|
|
|
|
- // 移除最后的逗号
|
|
|
- if (setClause.length() > 0) {
|
|
|
- setClause.setLength(setClause.length() - 2);
|
|
|
- }
|
|
|
-
|
|
|
- String query = String.format("MATCH (n:`%s`) WHERE id(n) = $nodeId SET %s RETURN n", label, setClause);
|
|
|
+ String query = "MATCH (n) WHERE id(n) = $nodeId SET n.name=$name RETURN n";
|
|
|
|
|
|
- neo4jClient.query(query)
|
|
|
- .bindAll(properties)
|
|
|
+ return neo4jClient.query(query)
|
|
|
.bind(nodeId).to("nodeId")
|
|
|
- .run();
|
|
|
- }
|
|
|
-
|
|
|
- public void deleteNode(String label, Long nodeId) {
|
|
|
- String query = String.format("MATCH (n:`%s`) WHERE id(n) = $nodeId DELETE n", label);
|
|
|
-
|
|
|
- neo4jClient.query(query)
|
|
|
- .bind(nodeId).to("nodeId")
|
|
|
- .run();
|
|
|
- }
|
|
|
-
|
|
|
- public void createRelationship(String startLabel, Long startId, String endLabel, Long endId, String relationshipType, Map<String, Object> properties) {
|
|
|
- StringBuilder setClause = new StringBuilder();
|
|
|
- properties.forEach((key, value) -> setClause.append("r.").append(key).append(" = $").append(key).append(", "));
|
|
|
-
|
|
|
- if (setClause.length() > 0) {
|
|
|
- setClause.setLength(setClause.length() - 2);
|
|
|
- }
|
|
|
+ .bind(name).to("name")
|
|
|
+ .fetchAs(Map.class).mappedBy((typeSystem, record) -> {
|
|
|
+ Node node = record.get("n").asNode();
|
|
|
|
|
|
- String query = String.format(
|
|
|
- "MATCH (a:`%s`), (b:`%s`) WHERE id(a) = $startId AND id(b) = $endId " +
|
|
|
- "CREATE (a)-[r:`%s`]->(b) " +
|
|
|
- "SET %s RETURN r",
|
|
|
- startLabel, endLabel, relationshipType, setClause
|
|
|
- );
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ result.put("id", node.id());
|
|
|
+ result.put("labels", node.labels()); // 获取节点的标签
|
|
|
+ result.put("properties", node.asMap()); // 获取节点的属性
|
|
|
|
|
|
- neo4jClient.query(query)
|
|
|
- .bind(startId).to("startId")
|
|
|
- .bind(endId).to("endId")
|
|
|
- .bindAll(properties)
|
|
|
- .run();
|
|
|
+ return result;
|
|
|
+ }).one().orElse(null);
|
|
|
}
|
|
|
|
|
|
- public void deleteRelationship(String startLabel, Long startId, String endLabel, Long endId, String relationshipType) {
|
|
|
- String query = String.format(
|
|
|
- "MATCH (a:`%s`)-[r:`%s`]->(b:`%s`) WHERE id(a) = $startId AND id(b) = $endId DELETE r",
|
|
|
- startLabel, relationshipType, endLabel
|
|
|
- );
|
|
|
+ public void deleteNode(Long nodeId) {
|
|
|
+ String query = "MATCH (n) WHERE id(n) = $nodeId SET n.is_deleted = 'Y'";
|
|
|
|
|
|
neo4jClient.query(query)
|
|
|
- .bind(startId).to("startId")
|
|
|
- .bind(endId).to("endId")
|
|
|
+ .bind(nodeId).to("nodeId")
|
|
|
.run();
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-// public List<Map<String, Object>> findByDynamicLabel(String label) {
|
|
|
-// // 动态拼接 Cypher 查询字符串
|
|
|
-// String query = "MATCH (n:`" + label + "`) RETURN n";
|
|
|
-//
|
|
|
-// // 执行查询,返回结果
|
|
|
-// return session.query(query, Map.of()).queryResults();
|
|
|
-// }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-// @Query("MATCH (n:`{0}`) RETURN n")
|
|
|
-// List<BaseNode> findByDynamicLabel(@Param("0") String label);
|
|
|
-//
|
|
|
-// @Transactional
|
|
|
-// @Query("CREATE (n:`{0}` {props}) RETURN n")
|
|
|
-// BaseNode createNodeWithDynamicLabel(@Param("0") String label, @Param("props") Map<String, Object> properties);
|
|
|
-//
|
|
|
-// @Transactional
|
|
|
-// @Query("MATCH (n:`{0}`) WHERE id(n) = {1} SET n += {props} RETURN n")
|
|
|
-// BaseNode updateNodeWithDynamicLabel(@Param("0") String label, @Param("1") Long nodeId, @Param("props") Map<String, Object> properties);
|
|
|
-//
|
|
|
-// @Transactional
|
|
|
-// @Query("MATCH (n:`{0}`) WHERE id(n) = {1} DELETE n")
|
|
|
-// void deleteNodeWithDynamicLabel(@Param("0") String label, @Param("1") Long nodeId);
|
|
|
}
|