diff --git a/src/main/java/com/teaching/backend/controller/KnowGraph/KnowController.java b/src/main/java/com/teaching/backend/controller/KnowGraph/KnowController.java new file mode 100644 index 0000000..3d1625c --- /dev/null +++ b/src/main/java/com/teaching/backend/controller/KnowGraph/KnowController.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 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 getKnowAll(@RequestParam Long id) { + BaseKnowReturn baseKnowReturn =knowService.getKnowAll(id); + return ResultUtils.success(baseKnowReturn); + } + + @GetMapping("/KnowById") + public BaseResponse getKnowById(@RequestParam Long id) { + BaseKnowReturn baseKnowReturn =knowService.getKnowById(id); + return ResultUtils.success(baseKnowReturn); + } + +} diff --git a/src/main/java/com/teaching/backend/mapper/KnowGraph/KnowRepository.java b/src/main/java/com/teaching/backend/mapper/KnowGraph/KnowRepository.java new file mode 100644 index 0000000..ea96859 --- /dev/null +++ b/src/main/java/com/teaching/backend/mapper/KnowGraph/KnowRepository.java @@ -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 { + + + @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 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 KnowIds); + + @Query("MATCH(n)-[r:related]->(nn:Know) where ID(nn) = $id RETURN n") + List queryKnowAllKnowById(Long id); + + + + +} diff --git a/src/main/java/com/teaching/backend/model/dto/KnowGraph/BaseKnowReturn.java b/src/main/java/com/teaching/backend/model/dto/KnowGraph/BaseKnowReturn.java new file mode 100644 index 0000000..8dc8bef --- /dev/null +++ b/src/main/java/com/teaching/backend/model/dto/KnowGraph/BaseKnowReturn.java @@ -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 knowList; + + private SetlinksList; +} diff --git a/src/main/java/com/teaching/backend/model/dto/KnowGraph/KnowRequest.java b/src/main/java/com/teaching/backend/model/dto/KnowGraph/KnowRequest.java new file mode 100644 index 0000000..b3e07ae --- /dev/null +++ b/src/main/java/com/teaching/backend/model/dto/KnowGraph/KnowRequest.java @@ -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 resourceList; + + +} + diff --git a/src/main/java/com/teaching/backend/model/dto/KnowGraph/KnowUpdateRequest.java b/src/main/java/com/teaching/backend/model/dto/KnowGraph/KnowUpdateRequest.java new file mode 100644 index 0000000..6e1b20b --- /dev/null +++ b/src/main/java/com/teaching/backend/model/dto/KnowGraph/KnowUpdateRequest.java @@ -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 resourceList; + +} + diff --git a/src/main/java/com/teaching/backend/model/dto/KnowGraph/RelationshipKnowRequest.java b/src/main/java/com/teaching/backend/model/dto/KnowGraph/RelationshipKnowRequest.java new file mode 100644 index 0000000..ff3b1ae --- /dev/null +++ b/src/main/java/com/teaching/backend/model/dto/KnowGraph/RelationshipKnowRequest.java @@ -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 KnowIds; + +} + diff --git a/src/main/java/com/teaching/backend/model/entity/KnowGraph/Know.java b/src/main/java/com/teaching/backend/model/entity/KnowGraph/Know.java new file mode 100644 index 0000000..b3836cb --- /dev/null +++ b/src/main/java/com/teaching/backend/model/entity/KnowGraph/Know.java @@ -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 resourceList; + +} + diff --git a/src/main/java/com/teaching/backend/model/entity/KnowGraph/Links.java b/src/main/java/com/teaching/backend/model/entity/KnowGraph/Links.java new file mode 100644 index 0000000..139c1dd --- /dev/null +++ b/src/main/java/com/teaching/backend/model/entity/KnowGraph/Links.java @@ -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; + + +} diff --git a/src/main/java/com/teaching/backend/model/vo/knowGraph/KnowVO.java b/src/main/java/com/teaching/backend/model/vo/knowGraph/KnowVO.java new file mode 100644 index 0000000..8ab5cc0 --- /dev/null +++ b/src/main/java/com/teaching/backend/model/vo/knowGraph/KnowVO.java @@ -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 resourceList; + +} + diff --git a/src/main/java/com/teaching/backend/service/KnowGraph/KnowService.java b/src/main/java/com/teaching/backend/service/KnowGraph/KnowService.java new file mode 100644 index 0000000..855fc65 --- /dev/null +++ b/src/main/java/com/teaching/backend/service/KnowGraph/KnowService.java @@ -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 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> 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> iterator = all.iterator(); + Set knowList = new HashSet<>(); + SetlinksList = new HashSet<>(); + KnowVO knowVO; + List node2 = new ArrayList<>(); + Links links; + int p = 0; + while (iterator.hasNext()) { + Map 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> 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> iterator = all.iterator(); + Set knowList = new HashSet<>(); + SetlinksList = new HashSet<>(); + KnowVO knowVO; + List node2 = new ArrayList<>(); + Links links; + int p = 0; + while (iterator.hasNext()) { + Map 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; + } + +}