瀏覽代碼

实体、关系API

wanghn 5 月之前
父節點
當前提交
96a734b477

+ 35 - 2
src/main/java/com/qizhen/healsphere/repository/neo4j/BaseNodeRepository.java

@@ -13,6 +13,12 @@ import java.util.*;
 @Repository
 public class BaseNodeRepository {
 
+    /**
+     * 根据名字和标签查询实体属性
+     * @param label
+     * @param name
+     * @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()) {
@@ -30,6 +36,11 @@ public class BaseNodeRepository {
 
     }
 
+    /**
+     * 根据id查询实体属性
+     * @param id
+     * @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()) {
@@ -47,8 +58,14 @@ public class BaseNodeRepository {
 
     }
 
+    /**
+     * 根据名字模糊查询实体
+     * @param label
+     * @param name
+     * @return 实体map列表
+     */
     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,".*",".*");
+        String query = String.format("MATCH (n:`%s`) WHERE n.name contains $name and n.is_deleted = 'N' RETURN n", label);
 
         try(Session session = Neo4jUtil.getSession()) {
             StatementResult result = session.run(query,Values.parameters("name", name));
@@ -70,6 +87,12 @@ public class BaseNodeRepository {
 
     }
 
+    /**
+     * 新增实体
+     * @param label
+     * @param properties
+     * @return 实体map
+     */
     public Map<String, Object> createNode(String label, Map<String, Object> properties) {
         StringBuilder createClause = new StringBuilder();
         properties.put("is_deleted","N");
@@ -96,10 +119,16 @@ public class BaseNodeRepository {
         }
     }
 
+    /**
+     * 更新实体名称
+     * @param nodeId
+     * @param name
+     * @return 实体map
+     */
     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()) {
-            StatementResult result = session.run(query,Values.parameters("name", name));
+            StatementResult result = session.run(query,Values.parameters("nodeId", nodeId,"name", name));
             Map<String,Object> map = new HashMap<>();
             if(result.hasNext()) {
                 Node node = result.single().get("n").asNode();
@@ -111,6 +140,10 @@ public class BaseNodeRepository {
         }
     }
 
+    /**
+     * 删除实体
+     * @param nodeId
+     */
     public void deleteNode(Long nodeId) {
         String query = "MATCH (n) WHERE id(n) = $nodeId SET n.is_deleted = 'Y' " +
                 "WITH n MATCH (n)-[r]-() " +

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

@@ -14,6 +14,13 @@ import java.util.Map;
 public class BasePropertyRepository {
 
 
+    /**
+     * 新增实体属性
+     * @param label
+     * @param nodeId
+     * @param properties
+     * @return 实体map
+     */
     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(value).append("'").append(", "));
@@ -39,6 +46,13 @@ public class BasePropertyRepository {
         }
     }
 
+    /**
+     * 删除实体属性
+     * @param label
+     * @param nodeId
+     * @param propertyName
+     * @return 实体map
+     */
     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);
 
@@ -55,6 +69,15 @@ public class BasePropertyRepository {
         }
     }
 
+    /**
+     * 更新实体属性
+     * @param label
+     * @param nodeId
+     * @param oldPropertyName
+     * @param newPropertyName
+     * @param newPropertyValue
+     * @return 实体map
+     */
     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);

+ 28 - 3
src/main/java/com/qizhen/healsphere/repository/neo4j/BaseRelationshipRepository.java

@@ -2,7 +2,6 @@ package com.qizhen.healsphere.repository.neo4j;
 
 import com.qizhen.healsphere.config.Neo4jUtil;
 import org.neo4j.driver.v1.*;
-import org.neo4j.driver.v1.types.Node;
 import org.springframework.stereotype.Repository;
 
 import java.util.*;
@@ -11,6 +10,15 @@ import java.util.stream.Collectors;
 @Repository
 public class BaseRelationshipRepository {
 
+    /**
+     * 新增关系
+     * @param startLabel
+     * @param startId
+     * @param endLabel
+     * @param endId
+     * @param relationshipType
+     * @param properties
+     */
     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(value).append("'").append(", "));
@@ -31,6 +39,11 @@ public class BaseRelationshipRepository {
         }
     }
 
+    /**
+     * 更新关系
+     * @param oldRelationshipType
+     * @param newRelationshipType
+     */
     public void updateRelationship(String oldRelationshipType, String newRelationshipType) {
 
         String query = String.format(
@@ -46,6 +59,11 @@ public class BaseRelationshipRepository {
         }
     }
 
+    /**
+     * 根据关系名字模糊查询关系类型
+     * @param name
+     * @return
+     */
     public List<String> findRelationshipNames(String name) {
 
         String query = "match ()-[r]-() where type(r) contains $name return distinct type(r)";
@@ -63,8 +81,15 @@ public class BaseRelationshipRepository {
         }
     }
 
-
-
+    /**
+     * 删除关系
+     *
+     * @param startLabel
+     * @param startId
+     * @param endLabel
+     * @param endId
+     * @param relationshipType
+     */
     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",

+ 22 - 0
src/main/java/com/qizhen/healsphere/service/EntityService.java

@@ -0,0 +1,22 @@
+package com.qizhen.healsphere.service;
+
+import com.qizhen.healsphere.web.vo.CreateEntityVO;
+import com.qizhen.healsphere.web.vo.EntityVO;
+import com.qizhen.healsphere.web.vo.UpdateEntityVO;
+import org.springframework.web.bind.annotation.RequestBody;
+
+import java.util.List;
+import java.util.Map;
+
+public interface EntityService {
+
+    List<Map<String, Object>> createEntity(List<CreateEntityVO> createEntityList);
+
+    Boolean deleteEntity(List<Long> ids);
+
+    List<Map<String, Object>> updateEntityName(List<UpdateEntityVO> updateEntityList);
+
+    List<Map<String, Object>> findEntityListByName(String label, String name);
+
+    Map<String, Object> findEntityById(Long id);
+}

+ 20 - 0
src/main/java/com/qizhen/healsphere/service/RelationshipService.java

@@ -0,0 +1,20 @@
+package com.qizhen.healsphere.service;
+
+import com.qizhen.healsphere.web.vo.EntityVO;
+import com.qizhen.healsphere.web.vo.RelationshipVO;
+import com.qizhen.healsphere.web.vo.UpdateRelationTypeVO;
+
+import java.util.List;
+import java.util.Map;
+
+public interface RelationshipService {
+
+
+    Boolean createRelationship(List<RelationshipVO> relationshipList);
+
+    Boolean deleteRelationship(List<RelationshipVO> relationshipList);
+
+    List<String> findRelationshipType(String relationshipType);
+
+    Boolean updateRelationshipType(List<UpdateRelationTypeVO> updateRelationshipTypeList);
+}

+ 95 - 0
src/main/java/com/qizhen/healsphere/service/impl/EntityServiceImpl.java

@@ -0,0 +1,95 @@
+package com.qizhen.healsphere.service.impl;
+
+import com.qizhen.healsphere.repository.neo4j.BaseNodeRepository;
+import com.qizhen.healsphere.service.EntityService;
+import com.qizhen.healsphere.web.vo.CreateEntityVO;
+import com.qizhen.healsphere.web.vo.EntityVO;
+import com.qizhen.healsphere.web.vo.UpdateEntityVO;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+@Service
+public class EntityServiceImpl implements EntityService {
+
+    @Autowired
+    BaseNodeRepository nodeRepository;
+
+    @Override
+    public List<Map<String, Object>> createEntity(List<CreateEntityVO> createEntityList) {
+
+        //封装entityVO
+        List<EntityVO> entityList = new ArrayList<>();
+        for (CreateEntityVO entityVO : createEntityList) {
+            EntityVO entity = new EntityVO();
+            HashMap<String, Object> property = new HashMap<>();
+            entity.setLabel(entityVO.getLabel());
+            property.put("name", entityVO.getName());
+            entity.setProperty(property);
+            entityList.add(entity);
+        }
+
+        List<Map<String, Object>> createdEntityList = entityList.stream()
+                .map(entityVO -> {
+                    // 新增单个实体,只添加名字属性和标签
+                    return nodeRepository.createNode(entityVO.getLabel(), entityVO.getProperty());
+                })
+                .collect(Collectors.toList());
+
+        return createdEntityList;
+    }
+
+    @Override
+    public Boolean deleteEntity(List<Long> ids) {
+
+        //查询是否存在删除实体
+        boolean allExist = ids.stream()
+                .allMatch(id -> nodeRepository.findNodeById(id) != null);
+        if (!allExist) {
+            return false;
+        }
+
+        // 删除单个实体
+        ids.forEach(id -> nodeRepository.deleteNode(id));
+
+        return true;
+    }
+
+    @Override
+    public List<Map<String, Object>> updateEntityName(List<UpdateEntityVO> updateEntityList) {
+
+        //封装entityVO
+        List<EntityVO> entityList = new ArrayList<>();
+        for (UpdateEntityVO entityVO : updateEntityList) {
+            EntityVO entity = new EntityVO();
+            HashMap<String, Object> property = new HashMap<>();
+            entity.setNodeId(entityVO.getNoteId());
+            property.put("name", entityVO.getName());
+            entity.setProperty(property);
+            entityList.add(entity);
+        }
+
+        List<Map<String, Object>> updatedEntityList = entityList.stream()
+                //更新单个实体
+                .map(entityVO -> nodeRepository.updateNode(entityVO.getNodeId(), entityVO.getProperty().get("name").toString()))
+                .collect(Collectors.toList());
+
+        return updatedEntityList;
+    }
+
+    @Override
+    public List<Map<String, Object>> findEntityListByName(String label, String name) {
+
+        List<Map<String, Object>> entityListByName = nodeRepository.findNodeListByName(label, name);
+        return entityListByName;
+    }
+
+    @Override
+    public Map<String, Object>findEntityById(Long id) {
+
+        Map<String, Object> entityById = nodeRepository.findNodeById(id);
+        return entityById;
+    }
+}

+ 87 - 0
src/main/java/com/qizhen/healsphere/service/impl/RelationshipServiceImpl.java

@@ -0,0 +1,87 @@
+package com.qizhen.healsphere.service.impl;
+
+import com.qizhen.healsphere.repository.neo4j.BaseNodeRepository;
+import com.qizhen.healsphere.repository.neo4j.BaseRelationshipRepository;
+import com.qizhen.healsphere.service.EntityService;
+import com.qizhen.healsphere.service.RelationshipService;
+import com.qizhen.healsphere.web.vo.EntityVO;
+import com.qizhen.healsphere.web.vo.RelationshipVO;
+import com.qizhen.healsphere.web.vo.UpdateRelationTypeVO;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+@Service
+public class RelationshipServiceImpl implements RelationshipService {
+
+    @Autowired
+    BaseRelationshipRepository relationshipRepository;
+
+
+    @Override
+    public Boolean createRelationship(List<RelationshipVO> relationshipList) {
+        try {
+            relationshipList.stream().forEach(relationship ->
+                    relationshipRepository.createRelationship(
+                            relationship.getStartLabel(),
+                            relationship.getStartId(),
+                            relationship.getEndLabel(),
+                            relationship.getEndId(),
+                            relationship.getRelationshipType(),
+                            relationship.getProperty()
+                    )
+            );
+            // 如果所有关系都成功创建,则返回 true
+            return true;
+        } catch (Exception e) {
+            return false;
+        }
+    }
+
+    @Override
+    public Boolean deleteRelationship(List<RelationshipVO> relationshipList) {
+        try {
+            relationshipList.stream().forEach(relationship ->
+                    relationshipRepository.deleteRelationship(
+                            relationship.getStartLabel(),
+                            relationship.getStartId(),
+                            relationship.getEndLabel(),
+                            relationship.getEndId(),
+                            relationship.getRelationshipType()
+                    )
+            );
+            // 如果所有关系都成功删除,则返回 true
+            return true;
+        } catch (Exception e) {
+            return false;
+        }
+    }
+
+    @Override
+    public List<String> findRelationshipType(String relationshipType) {
+
+        return relationshipRepository.findRelationshipNames(relationshipType);
+    }
+
+    @Override
+    public Boolean updateRelationshipType(List<UpdateRelationTypeVO> updateRelationshipTypeList) {
+        try {
+            updateRelationshipTypeList.stream().forEach(relationship ->
+                    relationshipRepository.updateRelationship(
+                            relationship.getOldRelationshipType(),
+                            relationship.getNewRelationshipType()
+                    )
+            );
+            // 如果名称都成功更新,则返回 true
+            return true;
+        } catch (Exception e) {
+            return false;
+        }
+    }
+
+}

+ 79 - 0
src/main/java/com/qizhen/healsphere/web/EntityController.java

@@ -0,0 +1,79 @@
+package com.qizhen.healsphere.web;
+
+import com.qizhen.healsphere.service.EntityService;
+import com.qizhen.healsphere.web.dto.RespDTO;
+import com.qizhen.healsphere.web.vo.CreateEntityVO;
+import com.qizhen.healsphere.web.vo.UpdateEntityVO;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@Controller
+@RequestMapping("/entity")
+@Api(value = "实体API", tags = { "实体API" })
+public class EntityController {
+
+    @Autowired
+    EntityService entityService;
+
+    @ApiOperation(value = "批量新增实体",
+            notes = "")
+    @RequestMapping(value = "/createEntity",method = RequestMethod.POST)
+    @ResponseBody
+    public RespDTO<List<Map<String, Object>>> createEntity(@RequestBody List<CreateEntityVO> createEntityList) {
+
+        List<Map<String, Object>> list = entityService.createEntity(createEntityList);
+        return RespDTO.onSuc(list);
+    }
+
+
+    @ApiOperation(value = "批量删除实体",
+            notes = "")
+    @RequestMapping(value = "/deleteEntity",method = RequestMethod.DELETE)
+    @ResponseBody
+    public RespDTO<Boolean> deleteEntity(@RequestBody List<Long> ids) {
+
+        Boolean flag = entityService.deleteEntity(ids);
+        return RespDTO.onSuc(flag);
+    }
+
+
+    @ApiOperation(value = "批量修改实体名称",
+            notes = "")
+    @RequestMapping(value = "/updateEntityName",method = RequestMethod.PUT)
+    @ResponseBody
+    public RespDTO<List<Map<String, Object>>> updateEntityName(@RequestBody List<UpdateEntityVO> updateEntityList) {
+
+        List<Map<String, Object>> list = entityService.updateEntityName(updateEntityList);
+        return RespDTO.onSuc(list);
+    }
+
+
+    @ApiOperation(value = "实体列表查询(根据标签和名字模糊查询实体)",
+            notes = "")
+    @RequestMapping(value = "/findEntityListByName",method = RequestMethod.GET)
+    @ResponseBody
+    public RespDTO<List<Map<String, Object>>> findEntityListByName(String label, String name) {
+
+        List<Map<String, Object>> list = entityService.findEntityListByName(label, name);
+        return RespDTO.onSuc(list);
+    }
+
+
+    @ApiOperation(value = "实体详情查询(根据id查询实体)",
+            notes = "")
+    @RequestMapping(value = "/findEntity",method = RequestMethod.GET)
+    @ResponseBody
+    public RespDTO<Map<String, Object>> findEntity(Long id) {
+
+        Map<String, Object> entity = entityService.findEntityById(id);
+        return RespDTO.onSuc(entity);
+    }
+}

+ 66 - 0
src/main/java/com/qizhen/healsphere/web/RelationshipController.java

@@ -0,0 +1,66 @@
+package com.qizhen.healsphere.web;
+
+import com.qizhen.healsphere.service.RelationshipService;
+import com.qizhen.healsphere.web.dto.RespDTO;
+import com.qizhen.healsphere.web.vo.RelationshipVO;
+import com.qizhen.healsphere.web.vo.UpdateRelationTypeVO;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+import java.util.Map;
+
+@Controller
+@RequestMapping("/relationship")
+@Api(value = "关系API", tags = { "关系API" })
+public class RelationshipController {
+
+    @Autowired
+    RelationshipService relationshipService;
+
+
+    @ApiOperation(value = "批量新增关系",
+            notes = "")
+    @RequestMapping(value = "/createRelationship",method = RequestMethod.POST)
+    @ResponseBody
+    public RespDTO<Boolean> createRelationship(@RequestBody List<RelationshipVO> relationshipList) {
+
+        Boolean flag = relationshipService.createRelationship(relationshipList);
+        return RespDTO.onSuc(flag);
+    }
+
+
+    @ApiOperation(value = "批量删除关系",
+            notes = "")
+    @RequestMapping(value = "/deleteRelationship",method = RequestMethod.DELETE)
+    @ResponseBody
+    public RespDTO<Boolean> deleteRelationship(@RequestBody List<RelationshipVO> relationshipList) {
+
+        Boolean flag = relationshipService.deleteRelationship(relationshipList);
+        return RespDTO.onSuc(flag);
+    }
+
+
+    @ApiOperation(value = "根据名称模糊查询关系",
+            notes = "")
+    @RequestMapping(value = "/findRelationshipType",method = RequestMethod.GET)
+    @ResponseBody
+    public RespDTO<List<String>> findRelationshipType(String relationshipType) {
+
+        List<String> relationshipTypeList = relationshipService.findRelationshipType(relationshipType);
+        return RespDTO.onSuc(relationshipTypeList);
+    }
+
+    @ApiOperation(value = "批量更新实体名称",
+            notes = "")
+    @RequestMapping(value = "/updateRelationshipType",method = RequestMethod.PUT)
+    @ResponseBody
+    public RespDTO<List<String>> updateRelationshipType(@RequestBody List<UpdateRelationTypeVO> updateRelationshipTypeList) {
+
+        Boolean flag = relationshipService.updateRelationshipType(updateRelationshipTypeList);
+        return RespDTO.onSuc(flag);
+    }
+}

+ 13 - 0
src/main/java/com/qizhen/healsphere/web/vo/CreateEntityVO.java

@@ -0,0 +1,13 @@
+package com.qizhen.healsphere.web.vo;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.Map;
+
+@Getter
+@Setter
+public class CreateEntityVO {
+    private String name;
+    private String label;
+}

+ 14 - 0
src/main/java/com/qizhen/healsphere/web/vo/EntityVO.java

@@ -0,0 +1,14 @@
+package com.qizhen.healsphere.web.vo;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.Map;
+
+@Getter
+@Setter
+public class EntityVO {
+    private Long nodeId;
+    private String label;
+    private Map<String, Object> property;
+}

+ 18 - 0
src/main/java/com/qizhen/healsphere/web/vo/RelationshipVO.java

@@ -0,0 +1,18 @@
+package com.qizhen.healsphere.web.vo;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.Map;
+
+@Getter
+@Setter
+public class RelationshipVO {
+    private Long startId;
+    private Long endId;
+    private String startLabel;
+    private String endLabel;
+    //关系名称
+    private String relationshipType;
+    private Map<String, Object> property;
+}

+ 11 - 0
src/main/java/com/qizhen/healsphere/web/vo/UpdateEntityVO.java

@@ -0,0 +1,11 @@
+package com.qizhen.healsphere.web.vo;
+
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class UpdateEntityVO {
+    private Long noteId;
+    private String name;
+}

+ 11 - 0
src/main/java/com/qizhen/healsphere/web/vo/UpdateRelationTypeVO.java

@@ -0,0 +1,11 @@
+package com.qizhen.healsphere.web.vo;
+
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class UpdateRelationTypeVO {
+    private String oldRelationshipType;
+    private String newRelationshipType;
+}