|
@@ -1,5 +1,6 @@
|
|
|
package com.qizhen.healsphere.repository.neo4j;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
import com.qizhen.healsphere.config.Neo4jUtil;
|
|
|
import org.neo4j.driver.v1.*;
|
|
|
import com.qizhen.healsphere.repository.neo4j.entity.BaseEntity;
|
|
@@ -8,6 +9,7 @@ import org.neo4j.driver.v1.Session;
|
|
|
import org.neo4j.driver.v1.StatementResult;
|
|
|
import org.neo4j.driver.v1.Values;
|
|
|
import org.neo4j.driver.v1.types.Node;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Repository;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
@@ -19,6 +21,9 @@ import java.util.stream.Collectors;
|
|
|
@Repository
|
|
|
public class BaseNodeRepository {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private Neo4jUtil neo4jUtil;
|
|
|
+
|
|
|
/**
|
|
|
* 根据名字和标签查询实体属性
|
|
|
* @param label
|
|
@@ -26,8 +31,8 @@ public class BaseNodeRepository {
|
|
|
* @return 实体map
|
|
|
*/
|
|
|
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);
|
|
|
- try(Session session = Neo4jUtil.getSession()) {
|
|
|
+ String query = String.format("MATCH (n:`%s`) WHERE n.name = $name and (n.is_deleted = 'N' or NOT exists(n.is_deleted)) RETURN n", label);
|
|
|
+ try(Session session = neo4jUtil.getSession()) {
|
|
|
StatementResult result = session.run(query,Values.parameters("name", name));
|
|
|
Map<String,Object> map = new HashMap<>();
|
|
|
if(result.hasNext()) {
|
|
@@ -43,8 +48,8 @@ public class BaseNodeRepository {
|
|
|
}
|
|
|
|
|
|
public List<String> findNodeByNameLike(String label,String name) {
|
|
|
- String query = String.format("MATCH (n:`%s`) WHERE n.name contains $name and n.is_deleted = 'N' return distinct n.name limit 20", label);
|
|
|
- try(Session session = Neo4jUtil.getSession()) {
|
|
|
+ String query = String.format("MATCH (n:`%s`) WHERE n.name contains $name and (n.is_deleted = 'N' or NOT exists(n.is_deleted)) return distinct n.name limit 20", label);
|
|
|
+ try(Session session = neo4jUtil.getSession()) {
|
|
|
StatementResult result = session.run(query,Values.parameters("name", name));
|
|
|
List<String> names = new ArrayList<>();
|
|
|
if(result.hasNext()) {
|
|
@@ -64,8 +69,8 @@ public class BaseNodeRepository {
|
|
|
* @return 实体map
|
|
|
*/
|
|
|
public Map<String,Object> findNodeById(Long id) {
|
|
|
- String query = "MATCH (n) WHERE id(n) = $nodeId and n.is_deleted = 'N' RETURN n";
|
|
|
- try(Session session = Neo4jUtil.getSession()) {
|
|
|
+ String query = "MATCH (n) WHERE id(n) = $nodeId and (n.is_deleted = 'N' or NOT exists(n.is_deleted)) RETURN n";
|
|
|
+ try(Session session = neo4jUtil.getSession()) {
|
|
|
StatementResult result = session.run(query,Values.parameters("nodeId", id));
|
|
|
Map<String,Object> map = new HashMap<>();
|
|
|
if(result.hasNext()) {
|
|
@@ -87,9 +92,9 @@ public class BaseNodeRepository {
|
|
|
* @return 实体map列表
|
|
|
*/
|
|
|
public List<Map<String,Object>> findNodeListByName(String label, String name) {
|
|
|
- String query = String.format("MATCH (n:`%s`) WHERE n.name contains $name and n.is_deleted = 'N' RETURN n", label);
|
|
|
+ String query = String.format("MATCH (n:`%s`) WHERE n.name contains $name and (n.is_deleted = 'N' or NOT exists(n.is_deleted)) RETURN n", label);
|
|
|
|
|
|
- try(Session session = Neo4jUtil.getSession()) {
|
|
|
+ try(Session session = neo4jUtil.getSession()) {
|
|
|
StatementResult result = session.run(query,Values.parameters("name", name));
|
|
|
List<Map<String,Object>> listMap = new ArrayList<>();
|
|
|
if(result.hasNext()) {
|
|
@@ -116,6 +121,9 @@ public class BaseNodeRepository {
|
|
|
*/
|
|
|
public Map<String, Object> createNode(String label, Map<String, Object> properties) {
|
|
|
StringBuilder createClause = new StringBuilder();
|
|
|
+ if (CollUtil.isEmpty(properties)) {
|
|
|
+ properties = new HashMap<>();
|
|
|
+ }
|
|
|
properties.put("is_deleted","N");
|
|
|
properties.forEach((key, value) -> createClause.append(key).append(": '").append(value).append("'").append(", "));
|
|
|
|
|
@@ -126,7 +134,7 @@ public class BaseNodeRepository {
|
|
|
|
|
|
String query = String.format("MERGE (n:`%s` { %s }) RETURN n", label, createClause);
|
|
|
|
|
|
- try(Session session = Neo4jUtil.getSession()) {
|
|
|
+ try(Session session = neo4jUtil.getSession()) {
|
|
|
StatementResult result = session.run(query);
|
|
|
Map<String,Object> map = new HashMap<>();
|
|
|
if(result.hasNext()) {
|
|
@@ -147,7 +155,7 @@ public class BaseNodeRepository {
|
|
|
*/
|
|
|
public Map<String, Object> updateNode(Long nodeId, String name) {
|
|
|
String query = "MATCH (n) WHERE id(n) = $nodeId SET n.name=$name RETURN n";
|
|
|
- try(Session session = Neo4jUtil.getSession()) {
|
|
|
+ try(Session session = neo4jUtil.getSession()) {
|
|
|
StatementResult result = session.run(query,Values.parameters("nodeId", nodeId,"name", name));
|
|
|
Map<String,Object> map = new HashMap<>();
|
|
|
if(result.hasNext()) {
|
|
@@ -168,11 +176,45 @@ public class BaseNodeRepository {
|
|
|
String query = "MATCH (n) WHERE id(n) = $nodeId SET n.is_deleted = 'Y' " +
|
|
|
"WITH n MATCH (n)-[r]-() " +
|
|
|
"DELETE r";
|
|
|
- try(Session session = Neo4jUtil.getSession()) {
|
|
|
+ try(Session session = neo4jUtil.getSession()) {
|
|
|
session.run(query,Values.parameters("nodeId", nodeId));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 实体合并
|
|
|
+ * @param label
|
|
|
+ * @param properties
|
|
|
+ * @return 实体map
|
|
|
+ */
|
|
|
+ public Map<String, Object> mergeNode(String label, Map<String, Object> properties) {
|
|
|
+ StringBuilder createClause = new StringBuilder();
|
|
|
+ if (CollUtil.isEmpty(properties)) {
|
|
|
+ properties = new HashMap<>();
|
|
|
+ }
|
|
|
+ properties.put("is_deleted","N");
|
|
|
+ properties.forEach((key, value) -> createClause.append(key).append(": '").append(value).append("'").append(", "));
|
|
|
+
|
|
|
+ // 移除最后的逗号
|
|
|
+ if (createClause.length() > 0) {
|
|
|
+ createClause.setLength(createClause.length() - 2);
|
|
|
+ }
|
|
|
+
|
|
|
+ String query = String.format("MERGE (n:`%s` { %s }) RETURN n", label, createClause);
|
|
|
+
|
|
|
+ try(Session session = neo4jUtil.getSession()) {
|
|
|
+ StatementResult result = session.run(query);
|
|
|
+ Map<String,Object> map = new HashMap<>();
|
|
|
+ if(result.hasNext()) {
|
|
|
+ Node node = result.single().get("n").asNode();
|
|
|
+ map.put("id", node.id());
|
|
|
+ map.put("labels", node.labels());
|
|
|
+ map.put("properties", node.asMap());
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 新增实体
|
|
|
* @param label
|
|
@@ -191,7 +233,7 @@ public class BaseNodeRepository {
|
|
|
|
|
|
String query = String.format("MERGE (n:`%s` { %s }) RETURN n", label, createClause);
|
|
|
|
|
|
- try(Session session = Neo4jUtil.getSession()) {
|
|
|
+ try(Session session = neo4jUtil.getSession()) {
|
|
|
StatementResult result = session.run(query);
|
|
|
|
|
|
if(result.hasNext()) {
|
|
@@ -208,8 +250,8 @@ public class BaseNodeRepository {
|
|
|
* @return 实体map
|
|
|
*/
|
|
|
public BaseEntity byName(String label,String name) {
|
|
|
- String query = String.format("MATCH (n:`%s`) WHERE n.name = $name and n.is_deleted = 'N' RETURN n", label);
|
|
|
- try(Session session = Neo4jUtil.getSession()) {
|
|
|
+ String query = String.format("MATCH (n:`%s`) WHERE n.name = $name and (n.is_deleted = 'N' or NOT exists(n.is_deleted)) RETURN n", label);
|
|
|
+ try(Session session = neo4jUtil.getSession()) {
|
|
|
StatementResult result = session.run(query,Values.parameters("name", name));
|
|
|
if(result.hasNext()) {
|
|
|
return buildBaseEntity(result);
|