上传知识图谱crud

master
youahng 4 months ago committed by yh
parent 3b286f4356
commit c9d9ffecb6
  1. 81
      src/main/java/com/teaching/backend/controller/KnowGraph/KnowController.java
  2. 31
      src/main/java/com/teaching/backend/mapper/KnowGraph/KnowRepository.java
  3. 31
      src/main/java/com/teaching/backend/model/dto/KnowGraph/BaseKnowReturn.java
  4. 34
      src/main/java/com/teaching/backend/model/dto/KnowGraph/KnowRequest.java
  5. 37
      src/main/java/com/teaching/backend/model/dto/KnowGraph/KnowUpdateRequest.java
  6. 22
      src/main/java/com/teaching/backend/model/dto/KnowGraph/RelationshipKnowRequest.java
  7. 45
      src/main/java/com/teaching/backend/model/entity/KnowGraph/Know.java
  8. 32
      src/main/java/com/teaching/backend/model/entity/KnowGraph/Links.java
  9. 36
      src/main/java/com/teaching/backend/model/vo/knowGraph/KnowVO.java
  10. 155
      src/main/java/com/teaching/backend/service/KnowGraph/KnowService.java

@ -0,0 +1,81 @@
package com.teaching.backend.controller.KnowGraph;
/**
* @Author:youhang
* @Date:2024-06-09-9:55
* @Description:
*/
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.ResultUtils;
import com.teaching.backend.model.dto.KnowGraph.BaseKnowReturn;
import com.teaching.backend.model.dto.KnowGraph.KnowRequest;
import com.teaching.backend.model.dto.KnowGraph.KnowUpdateRequest;
import com.teaching.backend.model.dto.KnowGraph.RelationshipKnowRequest;
import com.teaching.backend.model.entity.KnowGraph.Know;
import com.teaching.backend.service.KnowGraph.KnowService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/know")
public class KnowController {
private KnowService knowService;
public KnowController(KnowService knowService) {
this.knowService = knowService;
}
//根据 id查询所关联的所有结点
@PostMapping ("/query/{id}")
public List<Know> queryKnowAllKnowById(@PathVariable Long id) {
return knowService.queryKnowAllKnowById(id);
}
//添加知识点
@PostMapping("/add")
public Know createKnow(@RequestBody KnowRequest knowRequest) {
return knowService.createKnow(knowRequest);
}
//修改知识点
@PostMapping ("/update")
public Know updateKnow(@RequestBody KnowUpdateRequest knowUpdateRequest) {
return knowService.updateKnow(knowUpdateRequest);
}
//删除知识点
@GetMapping ("delete/{id}")
public void deleteKnow(@PathVariable Long id) {
knowService.deleteKnow(id);
}
//添加知识点与知识点的关系 related
@PostMapping ("/addKnowRelatedKnow")
public void addKnowRelatedKnow(@RequestBody RelationshipKnowRequest relationshipKnowRequest) {
knowService.addKnowRelatedKnow(relationshipKnowRequest);
}
//添加知识点与知识点的关系 related
@PostMapping ("/addKnowFatherAndSonKnow")
public void addKnowFatherAndSonKnow(@RequestBody RelationshipKnowRequest relationshipKnowRequest) {
knowService.addKnowFatherAndSonKnow(relationshipKnowRequest);
}
@GetMapping("/all")
public BaseResponse<BaseKnowReturn> getKnowAll(@RequestParam Long id) {
BaseKnowReturn baseKnowReturn =knowService.getKnowAll(id);
return ResultUtils.success(baseKnowReturn);
}
@GetMapping("/KnowById")
public BaseResponse<BaseKnowReturn> getKnowById(@RequestParam Long id) {
BaseKnowReturn baseKnowReturn =knowService.getKnowById(id);
return ResultUtils.success(baseKnowReturn);
}
}

@ -0,0 +1,31 @@
package com.teaching.backend.mapper.KnowGraph;
/**
* @Author:youhang
* @Date:2024-06-09-8:59
* @Description:
*/
import com.teaching.backend.model.entity.KnowGraph.Know;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import java.util.List;
public interface KnowRepository extends Neo4jRepository<Know, Long> {
@Query("MATCH (p:Know), (c:Know) WHERE ID(p) = $id AND ID(c) IN $KnowIds CREATE (p)-[:related]->(c),(c)-[:related]->(p)")
void addKnowRelatedKnow(Long id, List<Long> KnowIds);
@Query("MATCH (p:Know), (c:Know) WHERE ID(p) = $id AND ID(c) IN $KnowIds CREATE (p)-[:FatherAndSon]->(c)")
void addKnowFatherAndSonKnow(Long id, List<Long> KnowIds);
@Query("MATCH(n)-[r:related]->(nn:Know) where ID(nn) = $id RETURN n")
List<Know> queryKnowAllKnowById(Long id);
}

@ -0,0 +1,31 @@
package com.teaching.backend.model.dto.KnowGraph;
/**
* @Author:youhang
* @Date:2024-07-23-17:30
* @Description:
*/
import com.teaching.backend.model.entity.KnowGraph.Links;
import com.teaching.backend.model.vo.knowGraph.KnowVO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Set;
/**
* @Author:youhang
* @Date:2024-07-22-5:01
* @Description:
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BaseKnowReturn {
private Set<KnowVO> knowList;
private Set<Links>linksList;
}

@ -0,0 +1,34 @@
package com.teaching.backend.model.dto.KnowGraph;
import lombok.Data;
import org.springframework.data.neo4j.core.schema.Property;
import java.io.Serializable;
import java.util.List;
@Data
public class KnowRequest implements Serializable {
/**
* 知识点名称
*/
private String name;
/**
* 信息
*/
private String info;
/**
* 知识点所关联的资源
*/
@Property
private List<Integer> resourceList;
}

@ -0,0 +1,37 @@
package com.teaching.backend.model.dto.KnowGraph;
import lombok.Data;
import org.springframework.data.neo4j.core.schema.Property;
import java.io.Serializable;
import java.util.List;
@Data
public class KnowUpdateRequest implements Serializable {
/**
* id
*/
private Long id;
/**
* 知识点名称
*/
private String name;
/**
* 信息
*/
private String info;
/**
* 知识点所关联的资源
*/
@Property
private List<Integer> resourceList;
}

@ -0,0 +1,22 @@
package com.teaching.backend.model.dto.KnowGraph;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
public class RelationshipKnowRequest implements Serializable {
/**
* knowid
*/
private Long id;
/**
* 课程id
*/
private List<Long> KnowIds;
}

@ -0,0 +1,45 @@
package com.teaching.backend.model.entity.KnowGraph;
import lombok.Data;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Property;
import java.io.Serializable;
import java.util.List;
@Node
@Data
public class Know implements Serializable {
/**
* id
*/
@Id
@GeneratedValue
private Long id;
/**
* 知识点名称
*/
@Property
private String name;
/**
* 信息
*/
@Property
private String info;
/**
* 知识点所关联的资源
*/
@Property
private List<Integer> resourceList;
}

@ -0,0 +1,32 @@
package com.teaching.backend.model.entity.KnowGraph;
import lombok.Data;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Property;
/**
* @Author:youhang
* @Date:2024-07-21-16:32
* @Description:
*/
@Data
@Node
public class Links {
@Id
@GeneratedValue
private Long id;
@Property
private Long source;
@Property
private Long target;
@Property
private String label;
}

@ -0,0 +1,36 @@
package com.teaching.backend.model.vo.knowGraph;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
public class KnowVO implements Serializable {
/**
* id
*/
private Long id;
/**
* 知识点名称
*/
private String label;
/**
* 信息
*/
private Long group;
/**
* 知识点所关联的资源
*/
private List<Integer> resourceList;
}

@ -0,0 +1,155 @@
package com.teaching.backend.service.KnowGraph;
import com.teaching.backend.mapper.KnowGraph.KnowRepository;
import com.teaching.backend.model.dto.KnowGraph.BaseKnowReturn;
import com.teaching.backend.model.dto.KnowGraph.KnowRequest;
import com.teaching.backend.model.dto.KnowGraph.KnowUpdateRequest;
import com.teaching.backend.model.dto.KnowGraph.RelationshipKnowRequest;
import com.teaching.backend.model.entity.KnowGraph.Know;
import com.teaching.backend.model.entity.KnowGraph.Links;
import com.teaching.backend.model.vo.knowGraph.KnowVO;
import org.neo4j.driver.internal.InternalRelationship;
import org.neo4j.driver.types.Node;
import org.springframework.beans.BeanUtils;
import org.springframework.data.neo4j.core.Neo4jClient;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
/**
* @Author:youhang
* @Date:2024-07-21-14:46
* @Description:
*/
@Service
public class KnowService {
private KnowRepository knowRepository;
@Resource
private Neo4jClient neo4jClient;
public KnowService(KnowRepository knowRepository) {
this.knowRepository = knowRepository;
}
public List<Know> queryKnowAllKnowById(Long id) {
return knowRepository.queryKnowAllKnowById(id);
}
public Know createKnow(KnowRequest knowRequest ) {
Know know = new Know();
BeanUtils.copyProperties(knowRequest,know);
return knowRepository.save(know);
}
public void deleteKnow(Long id) {
knowRepository.deleteById(id);
}
public Know updateKnow(KnowUpdateRequest knowUpdateRequest ) {
Know know = new Know();
know = knowRepository.findById(knowUpdateRequest.getId()).orElseThrow(() -> new RuntimeException("知识点 not found"));
BeanUtils.copyProperties(knowUpdateRequest,know);
return knowRepository.save(know);
}
public void addKnowRelatedKnow( RelationshipKnowRequest relationshipKnowRequest) {
knowRepository.addKnowRelatedKnow(relationshipKnowRequest.getId(),relationshipKnowRequest.getKnowIds());
}
public void addKnowFatherAndSonKnow( RelationshipKnowRequest relationshipKnowRequest) {
knowRepository.addKnowFatherAndSonKnow(relationshipKnowRequest.getId(),relationshipKnowRequest.getKnowIds());
}
public BaseKnowReturn getKnowAll(Long id) {
Collection<Map<String, Object>> all =
neo4jClient.query( "match(n:Know)-[r*0..]->(p:Know) where ID(n) = "+id+" return n as `n`,r as `r`,p as `p`,length(r) as `d`").fetch().all();
Iterator<Map<String, Object>> iterator = all.iterator();
Set<KnowVO> knowList = new HashSet<>();
Set<Links>linksList = new HashSet<>();
KnowVO knowVO;
List node2 = new ArrayList<>();
Links links;
int p = 0;
while (iterator.hasNext()) {
Map<String, Object> element = iterator.next();
Long group = (Long) element.get("d");
knowVO = new KnowVO();
Node node1 = (Node) element.get("p");
Long id1 = node1.id();
String name1 = node1.get("name").asString();
// String info1 = node1.get("info").asString();
knowVO.setId(id1);
knowVO.setLabel(name1);
knowVO.setGroup(group);
knowList.add(knowVO);
node2 = (List) element.get("r");
for (int i = 0; i < node2.size(); i++) {
InternalRelationship e = (InternalRelationship) node2.get(i);
links = new Links();
links.setId(e.id());
links.setSource(e.startNodeId());
links.setTarget(e.endNodeId());
links.setLabel(e.type());
linksList.add(links);
}
}
BaseKnowReturn baseKnowReturn = new BaseKnowReturn(knowList,linksList);
System.out.println(baseKnowReturn);
return baseKnowReturn;
}
public BaseKnowReturn getKnowById(Long id) {
Collection<Map<String, Object>> all =
neo4jClient.query( "match(n:Know)-[r*0..2]->(p:Know) where ID(n) = "+id+" return n as `n`,r as `r`,p as `p`,length(r) as `d`").fetch().all();
Iterator<Map<String, Object>> iterator = all.iterator();
Set<KnowVO> knowList = new HashSet<>();
Set<Links>linksList = new HashSet<>();
KnowVO knowVO;
List node2 = new ArrayList<>();
Links links;
int p = 0;
while (iterator.hasNext()) {
Map<String, Object> element = iterator.next();
Long group = (Long) element.get("d");
knowVO = new KnowVO();
Node node1 = (Node) element.get("p");
Long id1 = node1.id();
String name1 = node1.get("name").asString();
// String info1 = node1.get("info").asString();
knowVO.setId(id1);
knowVO.setLabel(name1);
knowVO.setGroup(group);
knowList.add(knowVO);
node2 = (List) element.get("r");
for (int i = 0; i < node2.size(); i++) {
InternalRelationship e = (InternalRelationship) node2.get(i);
links = new Links();
links.setId(e.id());
links.setSource(e.startNodeId());
links.setTarget(e.endNodeId());
links.setLabel(e.type());
linksList.add(links);
}
}
BaseKnowReturn baseKnowReturn = new BaseKnowReturn(knowList,linksList);
System.out.println(baseKnowReturn);
return baseKnowReturn;
}
}
Loading…
Cancel
Save