浏览代码

demo提交3

yuchengwei 5 月之前
父节点
当前提交
68c7aa65c0

+ 69 - 90
src/main/java/com/qizhen/healsphere/repository/neo4j/BaseNodeRepository.java

@@ -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);
 }

+ 92 - 0
src/main/java/com/qizhen/healsphere/repository/neo4j/BasePropertyRepository.java

@@ -0,0 +1,92 @@
+package com.qizhen.healsphere.repository.neo4j;
+
+import org.neo4j.driver.types.Node;
+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;
+
+@Repository
+public class BasePropertyRepository {
+
+    @Autowired
+    private Neo4jClient neo4jClient;
+
+    public BasePropertyRepository(Neo4jClient neo4jClient) {
+        this.neo4jClient = neo4jClient;
+    }
+
+    public Map<String,Object> createProperty(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(", "));
+
+        // 移除最后的逗号
+        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);
+
+        Optional<Map> optionalMap = neo4jClient.query(query)
+                .bindAll(properties)
+                .bind(nodeId).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 Map<String,Object> deleteProperty(String label, Long nodeId,String propertyName) {
+        String query = String.format("MATCH (n:`%s`) WHERE id(n) = $nodeId REMOVE n.%s RETURN n", label,propertyName);
+
+        Optional<Map> optionalMap = neo4jClient.query(query)
+                .bind(nodeId).to("nodeId")
+//                .bind(propertyName).to("propertyName")
+                .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 Map<String,Object> updateProperty(String label,Long nodeId,String oldPropertyName,String newPropertyName,String newPropertyValue) {
+        String query = String.format("MATCH (n:`%s`) WHERE id(n) = $nodeId REMOVE n.%s " +
+                "SET n.%s = $newPropertyValue RETURN n", label,oldPropertyName,newPropertyName);
+        Optional<Map> optionalMap = neo4jClient.query(query)
+                .bind(nodeId).to("nodeId")
+//                .bind(oldPropertyName).to("oldPropertyName")
+//                .bind(newPropertyName).to("newPropertyName")
+                .bind(newPropertyValue).to("newPropertyValue")
+                .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);
+
+    }
+}

+ 82 - 0
src/main/java/com/qizhen/healsphere/repository/neo4j/BaseRelationshipRepository.java

@@ -0,0 +1,82 @@
+package com.qizhen.healsphere.repository.neo4j;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.neo4j.core.Neo4jClient;
+import org.springframework.stereotype.Repository;
+
+import java.util.*;
+
+@Repository
+public class BaseRelationshipRepository {
+
+    @Autowired
+    private Neo4jClient neo4jClient;
+
+    public BaseRelationshipRepository(Neo4jClient neo4jClient) {
+        this.neo4jClient = neo4jClient;
+    }
+
+    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);
+        }
+
+        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
+        );
+
+        neo4jClient.query(query)
+                .bind(startId).to("startId")
+                .bind(endId).to("endId")
+                .bindAll(properties)
+                .run();
+    }
+
+    public void updateRelationship(String oldRelationshipType, String newRelationshipType) {
+
+        String query = String.format(
+                "MATCH (a)-[r:`%s`]->(b) " +
+                        "CREATE (a)-[new_r:`%s`]->(b) " +
+                        "SET new_r += r " +
+                        "DELETE r",
+                oldRelationshipType, newRelationshipType
+        );
+
+        neo4jClient.query(query)
+                .run();
+    }
+
+    public List<String> findRelationshipNames(String name) {
+
+        String query = "match ()-[r]-() where type(r) contains $name return distinct type(r)";
+
+        Collection<String> collection = neo4jClient.query(query)
+                .bind(name).to("name")
+                .fetchAs(String.class)
+                .all();
+        if (collection.isEmpty()) {
+            return Collections.emptyList();
+        }
+        return new ArrayList<>(collection);
+    }
+
+
+
+    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
+        );
+
+        neo4jClient.query(query)
+                .bind(startId).to("startId")
+                .bind(endId).to("endId")
+                .run();
+    }
+}

+ 1 - 1
src/main/java/com/qizhen/healsphere/service/impl/BaseNodeServiceImpl.java

@@ -15,6 +15,6 @@ public class BaseNodeServiceImpl implements BaseNodeService {
 
     @Override
     public Map<String, Object> findNode(String label, String name) {
-        return nodeRepository.findNode(label, name);
+        return nodeRepository.findNodeByName(label, name);
     }
 }

+ 7 - 7
src/main/java/com/qizhen/healsphere/web/KgController.java

@@ -11,10 +11,8 @@ import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cache.annotation.Cacheable;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.*;
 
 import javax.validation.Valid;
 
@@ -23,7 +21,7 @@ import javax.validation.Valid;
  * @author: gaodm
  * @time: 2018/8/30 10:12
  */
-@RestController
+@Controller
 @RequestMapping("/kg")
 @Api(value = "朗通知识图谱API", tags = { "朗通知识图谱API" })
 @SuppressWarnings("unchecked")
@@ -35,7 +33,8 @@ public class KgController {
     private KgFacade kgFacade;
 
     @ApiOperation(value = "获取图谱", notes = "获取图谱")
-    @PostMapping("/getGraph")
+    @RequestMapping(value = "/getGraph", method = RequestMethod.POST)
+    @ResponseBody
     public RespDTO<GraphLabelDTO> getGraph(@RequestBody @Valid KgQuery kgQuery) {
         return RespDTO.onSuc(kgFacade.getGraphFac(kgQuery));
     }
@@ -48,7 +47,8 @@ public class KgController {
                     "(手术和操作:t:4,st:0)<br>" +
                     "(实验室检查:t:5,st:0)<br>" +
                     "(辅助检查:t:6,st:0)<br>")
-    @PostMapping("/getTree")
+    @RequestMapping(value = "/getTree", method = RequestMethod.POST)
+    @ResponseBody
     @Cacheable(value = KGTREECACHE, key = "'kgtree:t_' + #kgTree.type + '_st_' + #kgTree.subType")
     public RespDTO<TreeDTO> getTree(@RequestBody @Valid KgTree kgTree) {
         log.info("获取树形分类缓存成功");

+ 36 - 1
src/test/java/com/qizhen/healsphere/DiseaseTests.java

@@ -2,6 +2,8 @@ package com.qizhen.healsphere;
 
 import cn.hutool.json.JSONUtil;
 import com.qizhen.healsphere.repository.neo4j.BaseNodeRepository;
+import com.qizhen.healsphere.repository.neo4j.BasePropertyRepository;
+import com.qizhen.healsphere.repository.neo4j.BaseRelationshipRepository;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -10,6 +12,7 @@ import org.springframework.context.annotation.ComponentScan;
 import org.springframework.test.context.junit4.SpringRunner;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 @RunWith(SpringRunner.class)
@@ -19,6 +22,12 @@ public class DiseaseTests {
     @Autowired
     BaseNodeRepository baseNodeRepository;
 
+    @Autowired
+    BaseRelationshipRepository baseRelationshipRepository;
+
+    @Autowired
+    BasePropertyRepository basePropertyRepository;
+
 
     @Test
     public void contextLoads() {}
@@ -76,7 +85,33 @@ public class DiseaseTests {
 //        baseNodeRepository.createRelationship(startLabel,startId,endLabel,endId,relationShipType,map);
 
 
-        baseNodeRepository.deleteRelationship(startLabel,startId,endLabel,endId,relationShipType);
+//        baseRelationshipRepository.deleteRelationship(startLabel,startId,endLabel,endId,relationShipType);
+//        List<String> relationshipNames = baseRelationshipRepository.findRelationshipNames("111");
+//        System.out.println(JSONUtil.toJsonStr(relationshipNames));
+
+//        baseRelationshipRepository.updateRelationship("疾病症状之间的关系111","疾病症状之间的关系222333");
+        baseRelationshipRepository.deleteRelationship(startLabel,startId,endLabel,endId,"疾病症状之间的关系222333");
+
+
+    }
+
+    @Test
+    public void testCrudPropertyDemo() {
+
+        String label = "疾病名称";
+        Long nodeId = 1L;
+//        Map<String,Object> map = new HashMap<>();
+//        map.put("prop1","测试属性1");
+//        map.put("prop2","测试属性2");
+
+//        Map<String, Object> objectMap = basePropertyRepository.createProperty(label, nodeId, map);
+//        System.out.println(JSONUtil.toJsonStr(objectMap));
+
+//        Map<String, Object> stringObjectMap = basePropertyRepository.updateProperty(label, nodeId, "prop1", "prop3", "最新的属性");
+//        System.out.println(JSONUtil.toJsonStr(stringObjectMap));
+
+        Map<String, Object> stringObjectMap1 = basePropertyRepository.deleteProperty(label, nodeId, "prop2");
+        System.out.println(JSONUtil.toJsonStr(stringObjectMap1));
 
 
     }