From f999d4e04a14398f9cc7cf53d034fc1f66218508 Mon Sep 17 00:00:00 2001 From: you hang <2998465706@qq.com> Date: Wed, 21 Aug 2024 14:46:36 +0800 Subject: [PATCH] 111 --- pom.xml | 6 + .../backend/controller/Know/Edge.java | 21 + .../controller/Know/KnowChapterVO.java | 38 + .../controller/Know/KnowController.java | 56 +- .../backend/controller/Know/KnowCourseVO.java | 33 + .../backend/controller/Know/KnowJson.java | 42 + .../controller/Know/KnowJsonListVO.java | 31 + .../controller/Knowtmp/KnowtmpController.java | 9 + .../resource/ResourceController.java | 6 + .../mapper/Knowtemp/KnowtmpMapper.java | 8 + .../backend/mapper/chapter/ChapterMapper.java | 18 + .../backend/mapper/know/KnowRepository.java | 62 +- .../mapper/resource/ResourcesRepository.java | 4 + .../backend/service/Know/KnowService.java | 15 +- .../service/impl/know/knowServiceImpl.java | 729 +++++++++++++++--- .../impl/knowtmp/KnowtmpServiceImpl.java | 23 +- .../impl/resource/ResourceServiceImpl.java | 7 + .../service/knowtmp/KnowtmpService.java | 4 + .../service/resource/ResourceService.java | 2 + 19 files changed, 995 insertions(+), 119 deletions(-) create mode 100644 src/main/java/com/teaching/backend/controller/Know/Edge.java create mode 100644 src/main/java/com/teaching/backend/controller/Know/KnowChapterVO.java create mode 100644 src/main/java/com/teaching/backend/controller/Know/KnowCourseVO.java create mode 100644 src/main/java/com/teaching/backend/controller/Know/KnowJson.java create mode 100644 src/main/java/com/teaching/backend/controller/Know/KnowJsonListVO.java diff --git a/pom.xml b/pom.xml index f3f4d71..1f52a11 100644 --- a/pom.xml +++ b/pom.xml @@ -243,6 +243,12 @@ httpclient 4.5.6 + + com.google.code.gson + gson + 2.8.9 + + diff --git a/src/main/java/com/teaching/backend/controller/Know/Edge.java b/src/main/java/com/teaching/backend/controller/Know/Edge.java new file mode 100644 index 0000000..12d70cc --- /dev/null +++ b/src/main/java/com/teaching/backend/controller/Know/Edge.java @@ -0,0 +1,21 @@ +package com.teaching.backend.controller.Know; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @Author:youhang + * @Date:2024-08-19-15:41 + * @Description: + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Edge { + private String source; + private String target; + private String type; + private Integer sta; // 1 课程-章节 2 章节 - 章节 3 章节 - 知识点 4 点 - 点 + +} diff --git a/src/main/java/com/teaching/backend/controller/Know/KnowChapterVO.java b/src/main/java/com/teaching/backend/controller/Know/KnowChapterVO.java new file mode 100644 index 0000000..1c7c577 --- /dev/null +++ b/src/main/java/com/teaching/backend/controller/Know/KnowChapterVO.java @@ -0,0 +1,38 @@ +package com.teaching.backend.controller.Know; + +import lombok.Data; + +import java.io.Serializable; + + +@Data +public class KnowChapterVO implements Serializable { + + + /** + * 章节id + */ + + private Long chapterId; + + + /** + * 知识点名称 + */ + + private String name; + + /** + * 信息 + */ + + private String info; + /** + * 学时 + */ + + private double hour; + + +} + diff --git a/src/main/java/com/teaching/backend/controller/Know/KnowController.java b/src/main/java/com/teaching/backend/controller/Know/KnowController.java index c508fc2..cd67565 100644 --- a/src/main/java/com/teaching/backend/controller/Know/KnowController.java +++ b/src/main/java/com/teaching/backend/controller/Know/KnowController.java @@ -7,6 +7,7 @@ package com.teaching.backend.controller.Know; */ import com.teaching.backend.common.BaseResponse; +import com.teaching.backend.mapper.know.KnowRepository; import com.teaching.backend.model.dto.Know.*; import com.teaching.backend.model.entity.know.Know; import com.teaching.backend.model.entity.know.KnowChapter; @@ -28,6 +29,42 @@ public class KnowController { @Autowired private KnowService knowService; + @Autowired + private KnowRepository knowRepository; + + + + + //返回2层知识点 + @GetMapping("/twoLevelKnows") + BaseResponse getTwoLevelKnows(@RequestParam Long id){ + return knowService.getTwoLevelKnows(id); + } + //返回3层知识点 + @GetMapping("/threeLevelKnows") + BaseResponse getThreeLevelKnows(@RequestParam Long id){ + return knowService.getThreeLevelKnows(id); + } + //返回4层知识点 + @GetMapping("/fourLevelKnows") + BaseResponse getFourLevelKnows(@RequestParam Long id){ + return knowService.getFourLevelKnows(id); + } + + + //通过courseId 显示图的JSON + @GetMapping("/test") + void test(@RequestParam String courseId){ + int res = knowRepository.addCourseRelationship(courseId,932L,"related"); + } + + //通过courseId 显示图的JSON + @GetMapping("/showGraphJson") + BaseResponse showGraphJson(@RequestParam String courseId){ + return knowService.showGraphJson(courseId); + } + + // 添加全部知识点 @PostMapping("/addAllKnow") BaseResponse addAllKnow(@RequestParam String courseId){ @@ -42,11 +79,11 @@ public class KnowController { //=================== - // 添加知识点 - @PostMapping("/addKnow") - BaseResponse addKnow(@RequestBody KnowRequest knowRequest){ - return knowService.addKnow(knowRequest); - } +// // 添加知识点 +// @PostMapping("/addKnow") +// BaseResponse addKnow(@RequestBody KnowRequest knowRequest){ +// return knowService.addKnow(knowRequest); +// } // 修改知识点 @PostMapping("/updateKnow") @@ -129,4 +166,13 @@ public class KnowController { } + //获取二级知识图谱 + @GetMapping("/getsecondKnowsById") + BaseResponse getsecondKnowsById(@RequestParam Long id){ + return knowService.getsecondKnowsById(id); + } + + + + } diff --git a/src/main/java/com/teaching/backend/controller/Know/KnowCourseVO.java b/src/main/java/com/teaching/backend/controller/Know/KnowCourseVO.java new file mode 100644 index 0000000..eff546c --- /dev/null +++ b/src/main/java/com/teaching/backend/controller/Know/KnowCourseVO.java @@ -0,0 +1,33 @@ +package com.teaching.backend.controller.Know; + +import lombok.Data; + +import java.io.Serializable; + +@Data +public class KnowCourseVO implements Serializable { + + + /** + * 课程id + */ + + private String courseId; + + + /** + * 知识点名称 + */ + + private String name; + + /** + * 信息 + */ + private String info; + private double hour; + + + +} + diff --git a/src/main/java/com/teaching/backend/controller/Know/KnowJson.java b/src/main/java/com/teaching/backend/controller/Know/KnowJson.java new file mode 100644 index 0000000..09e4911 --- /dev/null +++ b/src/main/java/com/teaching/backend/controller/Know/KnowJson.java @@ -0,0 +1,42 @@ +package com.teaching.backend.controller.Know; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class KnowJson implements Serializable { + + + /** + * mysql 中 知识点id + */ + + private Long knowId; + + + /** + * 知识点名称 + */ + + private String name; + + /** + * 信息 + */ + + private String info; + /** + * 学时 + */ + + private double hour; + + +} + diff --git a/src/main/java/com/teaching/backend/controller/Know/KnowJsonListVO.java b/src/main/java/com/teaching/backend/controller/Know/KnowJsonListVO.java new file mode 100644 index 0000000..421fea8 --- /dev/null +++ b/src/main/java/com/teaching/backend/controller/Know/KnowJsonListVO.java @@ -0,0 +1,31 @@ +package com.teaching.backend.controller.Know; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.util.List; + + + +public class KnowJsonListVO implements Serializable { + + private List knowJsonList; + public List KnowJsonListVO() { + return knowJsonList; + } + + public void setKnowJsonList(List knowJsonList) { + this.knowJsonList = knowJsonList; + } + + public List getKnowJsonList() { + return this.knowJsonList ; + } + + + + +} + diff --git a/src/main/java/com/teaching/backend/controller/Knowtmp/KnowtmpController.java b/src/main/java/com/teaching/backend/controller/Knowtmp/KnowtmpController.java index 85ab55d..c71c1a8 100644 --- a/src/main/java/com/teaching/backend/controller/Knowtmp/KnowtmpController.java +++ b/src/main/java/com/teaching/backend/controller/Knowtmp/KnowtmpController.java @@ -1,6 +1,7 @@ package com.teaching.backend.controller.Knowtmp; import com.teaching.backend.common.BaseResponse; +import com.teaching.backend.model.dto.Know.BaseKnowReturn; import com.teaching.backend.model.dto.Knowtmp.KnowtempAdd; import com.teaching.backend.model.dto.Knowtmp.KnowtempUpdate; import com.teaching.backend.model.entity.knowtmp.Knowtmp; @@ -22,6 +23,14 @@ public class KnowtmpController { @Autowired private KnowtmpService knowtmpService; + //返回课程下所有知识点 + @GetMapping("/getAllKnows") + BaseResponse getAllKnows(@RequestParam String courseId){ + return knowtmpService.getAllKnows(courseId); + } + + + // 添加知识点 @PostMapping("/addKnow") diff --git a/src/main/java/com/teaching/backend/controller/resource/ResourceController.java b/src/main/java/com/teaching/backend/controller/resource/ResourceController.java index 29cdfd4..9c45bd9 100644 --- a/src/main/java/com/teaching/backend/controller/resource/ResourceController.java +++ b/src/main/java/com/teaching/backend/controller/resource/ResourceController.java @@ -47,6 +47,7 @@ public class ResourceController { public ResponseEntity readFile(@RequestParam String filename) { return resourceService.readFile(filename); } + @GetMapping("/addRelationship/BetweenCourseAndResources") public BaseResponse addResourcesAndKnowByCourseId(@RequestParam String courseId, @RequestParam List resourcesIds){ return resourceService.addResourcesAndKnowByCourseId(courseId,resourcesIds); @@ -77,6 +78,11 @@ public class ResourceController { BaseResponse> queryBesidesKnowToResources(@RequestParam Long KnowId){ return resourceService.queryBesidesKnowToResources(KnowId); } + //查询知识点下资源 + @GetMapping("/queryResourcesByKnowId") + BaseResponse> queryResourcesByKnowId(@RequestParam Long KnowId){ + return resourceService.queryResourcesByKnowId(KnowId); + } diff --git a/src/main/java/com/teaching/backend/mapper/Knowtemp/KnowtmpMapper.java b/src/main/java/com/teaching/backend/mapper/Knowtemp/KnowtmpMapper.java index 5aee0cb..fd799d1 100644 --- a/src/main/java/com/teaching/backend/mapper/Knowtemp/KnowtmpMapper.java +++ b/src/main/java/com/teaching/backend/mapper/Knowtemp/KnowtmpMapper.java @@ -2,7 +2,11 @@ package com.teaching.backend.mapper.Knowtemp; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.teaching.backend.model.entity.knowtmp.Knowtmp; +import io.lettuce.core.dynamic.annotation.Param; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Select; + +import java.util.List; /** @@ -11,6 +15,10 @@ import org.apache.ibatis.annotations.Mapper; @Mapper public interface KnowtmpMapper extends BaseMapper { + @Select("SELECT * FROM Knowtmp WHERE chapterid = #{chapterId} ORDER BY ordernum") + List queryIdByChapterId(@Param("chapterid") Long chapterId); + + } diff --git a/src/main/java/com/teaching/backend/mapper/chapter/ChapterMapper.java b/src/main/java/com/teaching/backend/mapper/chapter/ChapterMapper.java index 01859f9..1866cc8 100644 --- a/src/main/java/com/teaching/backend/mapper/chapter/ChapterMapper.java +++ b/src/main/java/com/teaching/backend/mapper/chapter/ChapterMapper.java @@ -21,6 +21,24 @@ public interface ChapterMapper extends BaseMapper { @Select("SELECT * FROM chapter WHERE course_id = #{courseId}") List selectSectionsByCourseId(@Param("courseId") String courseId); + @Select("SELECT id FROM chapter WHERE course_id = #{courseId}") + List selectChapterIdsByCourseId(@Param("courseId") String courseId); + + + @Select("SELECT * FROM chapter WHERE course_id = #{courseId} ORDER BY order_num") + List selectSectionsOrderByNumByCourseId(@Param("courseId") String courseId); + + @Select("SELECT * FROM chapter WHERE course_id = #{courseId} AND pid = 0 ORDER BY order_num") + List selectChapterPIDByCourseId(@Param("courseId") String courseId); + + @Select("SELECT * FROM chapter WHERE pid = #{parentId} ORDER BY order_num") + List selectChapterChildrenOrderByNum(@Param("parentId") Long parentId); + + + @Select("SELECT id FROM chapter WHERE pid = #{chapterId} ORDER BY order_num") + List selectSectionsOrderByNumByChapterId(@Param("pid") Integer chapterId); + + @Select("SELECT * FROM course_chapter WHERE course_id = #{courseId} AND (pid = #{parentId} )") List selectSectionsWithChildren(@Param("courseId") String courseId, @Param("parentId") String parentId); @Select("SELECT * FROM chapter WHERE id = #{excelId}") diff --git a/src/main/java/com/teaching/backend/mapper/know/KnowRepository.java b/src/main/java/com/teaching/backend/mapper/know/KnowRepository.java index e58df2f..958a8ac 100644 --- a/src/main/java/com/teaching/backend/mapper/know/KnowRepository.java +++ b/src/main/java/com/teaching/backend/mapper/know/KnowRepository.java @@ -7,22 +7,31 @@ package com.teaching.backend.mapper.know; */ +import com.teaching.backend.model.entity.courses.Courses; import com.teaching.backend.model.entity.know.Know; import com.teaching.backend.model.entity.know.KnowChapter; import com.teaching.backend.model.entity.know.KnowCourse; import com.teaching.backend.model.entity.resource.Resources; import org.springframework.data.neo4j.repository.Neo4jRepository; import org.springframework.data.neo4j.repository.query.Query; +import org.springframework.data.repository.query.Param; import java.util.List; import java.util.Set; public interface KnowRepository extends Neo4jRepository { + + + @Query("MATCH (p:Know)-[:FatherAndSon]->(n:Know) WHERE p.chapterId = $chapterId RETURN p.id ORDER BY ordernum;") + ListqueryKnowIdByChapterId(Long chapterId); + + + //====================== - @Query("CREATE (n:Know {name: $name,info:$info}) return n;") - Know addKnow(String name,String info); + @Query("CREATE (n:Know {knowId: $KnowId,name: $name,info:$info,hour:$hour}) return ID(n);") + Long addKnow(Long KnowId,String name,String info,double hour); @Query("MATCH ID(p:Know) = $id SET p.name = $name set p.info= $info RETURN count(p);") @@ -36,8 +45,8 @@ public interface KnowRepository extends Neo4jRepository { //============================= - @Query("CREATE (n:Know {name: $name, courseId: $courseId,info:$info,hour:$hour}) return count(n)") - int addCourse(String courseId, String name, String info,double hour); + @Query("CREATE (n:Know {name: $name, courseId: $courseId,info:$info,hour:$hour}) return ID(n) as id") + Long addCourse(String courseId, String name, String info, double hour); @Query("MATCH (p:Know {courseId:$courseId }) SET p.name = $name set p.info= $info RETURN count(p);") int updateCourse(String courseId, String name, String info); @@ -50,8 +59,8 @@ public interface KnowRepository extends Neo4jRepository { //==================== - @Query("CREATE (n:Know {name: $name, chapterId:$chapterId,info:$info,hour:$hour}) return count(n)") - int addChapter(Long chapterId, String name, String info,double hour); + @Query("CREATE (n:Know {name: $name, chapterId:$chapterId,info:$info,hour:$hour}) return ID(n)") + Long addChapter(Long chapterId, String name, String info,double hour); @Query("MATCH (p:Know {chapterId:$chapterId }) SET p.name = $name set p.info= $info RETURN count(p);") int updateChapter(Long chapterId, String name, String info); @@ -67,18 +76,47 @@ public interface KnowRepository extends Neo4jRepository { Set queryBesidesKnowToResources(Long id); //建立关系 课程 - 章节 - @Query("MATCH (p:Know {courseId: $courseId}), (c:Know {chapterId: $chapterId}) CREATE (p)-[r:FatherAndSon]->(c) return count(r);") - int addFatherAndSonWithCourse(String courseId,Long chapterId); + @Query("MATCH (p:Know {courseId: $courseId}), (c:Know {chapterId: $chapterId}) CREATE (p)-[r:contain]->(c) return count(r);") + int addContainWithCourse(String courseId,Long chapterId); //建立关系 章节与章节 - @Query("MATCH (p:Know {chapterId: $chapterId1}), (c:Know {chapterId: $chapterId2}) CREATE (c)-[r:FatherAndSon]->(p) return count(r);") - int addFatherAndSonWithChapter(Long chapterId1,Long chapterId2); + @Query("MATCH (p:Know {chapterId: $chapterId1}), (c:Know {chapterId: $chapterId2}) CREATE (c)-[r:contain]->(p) return count(r);") + int addContainWithChapter(Long chapterId1,Long chapterId2); //建立关系 章节与知识点 - @Query("MATCH (p:Know), (j:Know) WHERE p.chapterId= $chapterId AND ID(j) = $id CREATE (p)-[r:FatherAndSon]->(j) return count(r);") - int addFatherAndSonWithKnow(Long id,Long chapterId); + @Query("MATCH (p:Know), (j:Know) WHERE p.chapterId= $chapterId AND ID(j) = $id CREATE (p)-[r:contain]->(j) return count(r);") + int addContainWithKnow(Long id,Long chapterId); @Query("MATCH (p:Know), (c:Resources) WHERE ID(p) = $id AND ID(c) = $ResourcesId CREATE (p)-[r:resources]->(c) return count(r)") int addResourceResourcesKnow(Long id, Long ResourcesId); + + //建立关系 章节与章节 顺序 Subsequent + @Query("MATCH (p:Know), (j:Know) WHERE ID(p) = $chapterId1 AND ID(j)= $chapterId2 CREATE (p)-[r:Subsequent]->(j) return count(r);") + int addSubsequentByChapterId(Long chapterId1,Long chapterId2); + + //建立关系 章节与章节 顺序 Subsequent + @Query("MATCH (p:Know), (j:Know) WHERE ID(p)= $id1 AND ID(j)= $id2 CREATE (p)-[r:order]->(j) return count(r);") + int addOrderById(Long id1,Long id2); + + //========== + //建立关系 all -课程 + @Query("MATCH (p:Know {courseId: $courseId}), (j:Know {chapterId: $chapterId}) CREATE (p)-[r:顺序]->(j) return count(p);") + int addCourseRelationship(String courseId,Long chapterId,String type); + + //建立关系 all -章节 + @Query("MATCH (p:Know), (j:Know) WHERE p.chapterId= $chapterId1 AND ID(j)= $chapterId2 CREATE (p)-[r: "+"$type"+"]->(j) return count(r);") + int addChapterRelationship(Long chapterId1, Long chapterId2, @Param("type")String type); + + //建立关系 all -节 - 点 + @Query("MATCH (p:Know), (j:Know) WHERE p.chapterId= $chapterId AND j.knowId= $knowId CREATE (p)-[r: $type]->(j) return count(r);") + int addChapterToKnowRelationship(Long chapterId,Long knowId,String type); + + + + //建立关系 all - 知识点 + @Query("MATCH (p:Know), (j:Know) WHERE p.knowId= $knowId1 AND j.knowId = $knowId2 CREATE (p)-[r: $type]->(j) return count(r);") + int addKnowRelationship(Long knowId1,Long knowId2,String type); + + } diff --git a/src/main/java/com/teaching/backend/mapper/resource/ResourcesRepository.java b/src/main/java/com/teaching/backend/mapper/resource/ResourcesRepository.java index ec4ad2e..256b73c 100644 --- a/src/main/java/com/teaching/backend/mapper/resource/ResourcesRepository.java +++ b/src/main/java/com/teaching/backend/mapper/resource/ResourcesRepository.java @@ -45,6 +45,10 @@ public interface ResourcesRepository extends Neo4jRepository { @Query("MATCh (p:Know)-[*0..1]->(n:Know)-[:resources]->(m:Resources) WHERE ID(p) = $KnowId RETURN m") Set queryBesidesKnowToResources(Long KnowId); + // + @Query("MATCH (p)-[r:resources]->(c) WHERE ID(p) = $KnowId RETURN c") + Set queryResourcesByKnowId(Long KnowId); + diff --git a/src/main/java/com/teaching/backend/service/Know/KnowService.java b/src/main/java/com/teaching/backend/service/Know/KnowService.java index f50b2be..064bbc5 100644 --- a/src/main/java/com/teaching/backend/service/Know/KnowService.java +++ b/src/main/java/com/teaching/backend/service/Know/KnowService.java @@ -18,6 +18,14 @@ import java.util.Set; */ public interface KnowService { + BaseResponse getTwoLevelKnows(Long id); + BaseResponse getThreeLevelKnows(Long id); + BaseResponse getFourLevelKnows(Long id); + + //通过courseId 显示图的JSON + BaseResponse showGraphJson(String courseId); + + //通过courseId 添加所有节点 BaseResponse addAllKnow(String courseId); @@ -27,8 +35,8 @@ public interface KnowService { //===================== - // 添加知识点 - BaseResponse addKnow(KnowRequest knowRequest); +// // 添加知识点 +// BaseResponse addKnow(KnowRequest knowRequest); // 修改知识点 BaseResponse updateKnow(KnowUpdateRequest knowUpdateRequest); @@ -73,6 +81,9 @@ public interface KnowService { //获取课程下的所有知识点 BaseResponse getAllKnowByCourseId(String id); + //获取课程下的所有知识点 + BaseResponse getsecondKnowsById(Long id); + //获取课程下的所有资源 BaseResponse> getAllResourcesByCourseId(String id); diff --git a/src/main/java/com/teaching/backend/service/impl/know/knowServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/know/knowServiceImpl.java index c86cbca..8744014 100644 --- a/src/main/java/com/teaching/backend/service/impl/know/knowServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/know/knowServiceImpl.java @@ -1,9 +1,25 @@ package com.teaching.backend.service.impl.know; +import cn.hutool.json.JSONArray; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; import com.teaching.backend.common.BaseResponse; import com.teaching.backend.common.ErrorCode; import com.teaching.backend.common.ResultUtils; +import com.teaching.backend.controller.Know.Edge; +import com.teaching.backend.controller.Know.KnowJson; +import com.teaching.backend.controller.Know.KnowJsonListVO; +import com.teaching.backend.mapper.Knowtemp.KnowtmpMapper; import com.teaching.backend.mapper.chapter.ChapterMapper; import com.teaching.backend.mapper.courses.CoursesMapper; import com.teaching.backend.mapper.know.KnowRepository; @@ -29,6 +45,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.io.IOException; import java.util.*; import java.util.stream.Collectors; @@ -43,6 +60,9 @@ public class knowServiceImpl implements KnowService { @Autowired private KnowRepository knowRepository; + @Autowired + private KnowtmpMapper knowtmpMapper; + @Autowired private ChapterMapper chapterMapper; @@ -51,7 +71,6 @@ public class knowServiceImpl implements KnowService { private KnowtmpService knowtmpService; - @Autowired CoursesMapper coursesMapper; @@ -59,107 +78,563 @@ public class knowServiceImpl implements KnowService { private Neo4jClient neo4jClient; - public BaseResponse addRelationshipWithresources(Long id, Long resourceId){ - int f = knowRepository.addResourceResourcesKnow(id,resourceId); - if(f <= 0){ + public BaseResponse addRelationshipWithresources(Long id, Long resourceId) { + int f = knowRepository.addResourceResourcesKnow(id, resourceId); + if (f <= 0) { throw new RuntimeException("添加关系失败"); } return ResultUtils.success("添加关系成功"); } + @Override + public BaseResponse getTwoLevelKnows(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(); + + Map colorChoose = new + HashMap<>(); + String color[] = new String[10]; + String[] colorList = {"#91CC75", "#5470C6", "#FAC858", "#EE6666", "#73C0DE", "#EA7CCC", "#5577FF", "#5577FF", "#9DBFFF", "#78A7FF"}; + for (int i = 0; i < 10; i++) { + colorChoose.put((long) i, colorList[i]); + } + + Iterator> iterator = all.iterator(); + Set knowList = new HashSet<>(); + Set linksList = new HashSet<>(); + KnowVO knowVO; + List node2 = new ArrayList<>(); + Links links; + int p = 0; + while (iterator.hasNext()) { + Map element = iterator.next(); + knowVO = new KnowVO(); + Node node1 = (Node) element.get("p"); + + Long group = (Long) element.get("d"); + knowVO.setColor(colorChoose.get(group)); + + Long id1 = node1.id(); + String name1 = node1.get("name").asString(); + knowVO.setId(id1); + knowVO.setLabel(name1); + + 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()); + if ("contain".equals(e.type())) { + links.setLabel("包含"); + } else if ("order".equals(e.type())) { + links.setLabel("顺序"); + } + linksList.add(links); + } + + + } + BaseKnowReturn baseKnowReturn = new BaseKnowReturn(knowList, linksList); + return ResultUtils.success(baseKnowReturn); + } + + @Override + public BaseResponse getThreeLevelKnows(Long id) { + Collection> all = + neo4jClient.query("match(n:Know)-[r*0..3]->(p:Know) where ID(n) = " + id + " return n as `n`,r as `r`,p as `p`,length(r) as `d`").fetch().all(); + + Map colorChoose = new + HashMap<>(); + String color[] = new String[10]; + String[] colorList = {"#91CC75", "#5470C6", "#FAC858", "#EE6666", "#73C0DE", "#EA7CCC", "#5577FF", "#5577FF", "#9DBFFF", "#78A7FF"}; + for (int i = 0; i < 10; i++) { + colorChoose.put((long) i, colorList[i]); + } + + Iterator> iterator = all.iterator(); + Set knowList = new HashSet<>(); + Set linksList = new HashSet<>(); + KnowVO knowVO; + List node2 = new ArrayList<>(); + Links links; + int p = 0; + while (iterator.hasNext()) { + Map element = iterator.next(); + knowVO = new KnowVO(); + Node node1 = (Node) element.get("p"); + + Long group = (Long) element.get("d"); + knowVO.setColor(colorChoose.get(group)); + + Long id1 = node1.id(); + String name1 = node1.get("name").asString(); + knowVO.setId(id1); + knowVO.setLabel(name1); + + 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()); + if ("contain".equals(e.type())) { + links.setLabel("包含"); + } else if ("order".equals(e.type())) { + links.setLabel("顺序"); + } + linksList.add(links); + } + + + } + BaseKnowReturn baseKnowReturn = new BaseKnowReturn(knowList, linksList); + return ResultUtils.success(baseKnowReturn); + } + + @Override + public BaseResponse getFourLevelKnows(Long id) { + Collection> all = + neo4jClient.query("match(n:Know)-[r*0..4]->(p:Know) where ID(n) = " + id + " return n as `n`,r as `r`,p as `p`,length(r) as `d`").fetch().all(); + + Map colorChoose = new + HashMap<>(); + String color[] = new String[10]; + String[] colorList = {"#91CC75", "#5470C6", "#FAC858", "#EE6666", "#73C0DE", "#EA7CCC", "#5577FF", "#5577FF", "#9DBFFF", "#78A7FF"}; + for (int i = 0; i < 10; i++) { + colorChoose.put((long) i, colorList[i]); + } + + Iterator> iterator = all.iterator(); + Set knowList = new HashSet<>(); + Set linksList = new HashSet<>(); + KnowVO knowVO; + List node2 = new ArrayList<>(); + Links links; + int p = 0; + while (iterator.hasNext()) { + Map element = iterator.next(); + knowVO = new KnowVO(); + Node node1 = (Node) element.get("p"); + + Long group = (Long) element.get("d"); + knowVO.setColor(colorChoose.get(group)); + + Long id1 = node1.id(); + String name1 = node1.get("name").asString(); + knowVO.setId(id1); + knowVO.setLabel(name1); + + 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()); + if ("contain".equals(e.type())) { + links.setLabel("包含"); + } else if ("order".equals(e.type())) { + links.setLabel("顺序"); + } + linksList.add(links); + } + + + } + BaseKnowReturn baseKnowReturn = new BaseKnowReturn(knowList, linksList); + return ResultUtils.success(baseKnowReturn); + } + + @Override + public BaseResponse showGraphJson(String courseId) { + return addInformation(courseId); + } + @Override public BaseResponse addAllKnow(String courseId) { try { - addAllKnowTransation(courseId) ; - }catch (Exception e){ + addAllKnowTransation(courseId); + } catch (Exception e) { e.printStackTrace(); - return ResultUtils.error(ErrorCode.OPERATION_ERROR,e.getMessage()); + return ResultUtils.error(ErrorCode.OPERATION_ERROR, e.getMessage()); } //6.返回成功信息,关闭事务 return ResultUtils.success("添加成功"); } + private BaseResponse addInformation(String courseId) { + + // 创建 JSON 对象数组 + JSONArray jsonArrayKnowNode = JSONUtil.createArray(); + JSONArray jsonArrayCourseNode = JSONUtil.createArray(); + JSONArray jsonArrayChapterNode = JSONUtil.createArray(); + + + //查询数据库-课程 + Courses courses = coursesMapper.selectById(courseId); + JSONObject obj1 = JSONUtil.createObj(); + obj1.put("courseId", courseId); + obj1.put("name", courses.getName()); + obj1.put("info", courses.getDescription()); + obj1.put("hour", courses.getClasshours()); + jsonArrayCourseNode.add(obj1); + + //查询数据库-章-节 + List chapterList = new ArrayList<>(); + List chapterListPID = chapterMapper.selectChapterPIDByCourseId(courseId); + List chapterOneIds = new ArrayList<>(); + List chapterTwoIds = new ArrayList<>(); + for (int i = 0; i < chapterListPID.size(); i++) { + chapterList.add(chapterListPID.get(i)); + chapterOneIds.add(chapterListPID.get(i).getId()); + List chapterListSecond = chapterMapper.selectChapterChildrenOrderByNum(chapterListPID.get(i).getId()); + for (int j = 0; j < chapterListSecond.size(); j++) { + chapterList.add(chapterListSecond.get(j)); + chapterTwoIds.add(chapterListSecond.get(j).getId()); + } + } + for (int i = 0; i < chapterList.size(); i++) { + Chapter chapter = chapterList.get(i); + JSONObject obj2 = JSONUtil.createObj(); + obj2.put("chapterId", chapter.getId()); + obj2.put("name", chapter.getName()); + obj2.put("info", chapter.getContent()); + obj2.put("hour", chapter.getTotalClassHours()); + jsonArrayChapterNode.add(obj2); + } + + //查询数据库-知识点 + for (int i = 0; i < chapterList.size(); i++) { + Chapter chapter = chapterList.get(i); + if (chapter.getPid() != 0) { + List knowlist = knowtmpService.queryByChapterId(chapter.getId()); + for (int j = 0; j < knowlist.size(); j++) { + Knowtmp knowtmp = knowlist.get(j); + JSONObject obj3 = JSONUtil.createObj(); + obj3.put("knowId", knowtmp.getId()); + obj3.put("name", knowtmp.getName()); + obj3.put("info", knowtmp.getInfo()); + obj3.put("hour", knowtmp.getHour()); + jsonArrayKnowNode.add(obj3); + } + } + + } + + + // =======创建关系 + JSONArray edgesArray = JSONUtil.createArray(); + + + // 创建边列表 + // 课程-章节 父子 + for (int i = 0; i < chapterListPID.size(); i++) { + Chapter chapter = chapterListPID.get(i); + JSONObject edge = JSONUtil.createObj(); + edge.put("source", courses.getId()); + edge.put("target", chapter.getId()); + edge.put("type", "FatherAndSon"); + edge.put("sta", 1); + edgesArray.add(edge); + } + //章-节 父子 + for (int i = 0; i < chapterListPID.size(); i++) { + List chapterListSecond = chapterMapper.selectChapterChildrenOrderByNum(chapterListPID.get(i).getId()); + for (int j = 0; j < chapterListSecond.size(); j++) { + JSONObject edge = JSONUtil.createObj(); + edge.put("source", chapterListPID.get(i).getId()); + edge.put("target", chapterListSecond.get(j).getId()); + edge.put("type", "FatherAndSon"); + edge.put("sta", 2); + edgesArray.add(edge); + } + } + //节与点父子 + List knowIds = new ArrayList<>(); + for (int i = 0; i < chapterTwoIds.size(); i++) { + List knowlist = knowtmpService.queryByChapterId(chapterTwoIds.get(i)); + for (int j = 0; j < knowlist.size(); j++) { + Knowtmp knowtmp = knowlist.get(j); + JSONObject edge = JSONUtil.createObj(); + edge.put("source", chapterTwoIds.get(i)); + edge.put("target", knowtmp.getId()); + edge.put("type", "FatherAndSon"); + edge.put("sta", 3); + edgesArray.add(edge); + knowIds.add(knowtmp.getId()); + } + } + + + //章-章 顺序 + for (int i = 0; i < chapterOneIds.size() - 1; i++) { + JSONObject edge = JSONUtil.createObj(); + edge.put("source", chapterOneIds.get(i)); + edge.put("target", chapterOneIds.get(i + 1)); + edge.put("type", "Subsequence"); + edge.put("sta", 2); + edgesArray.add(edge); + } + + + //节与节 顺序 + for (int i = 0; i < chapterTwoIds.size() - 1; i++) { + JSONObject edge = JSONUtil.createObj(); + edge.put("source", chapterTwoIds.get(i)); + edge.put("target", chapterTwoIds.get(i + 1)); + edge.put("type", "Subsequence"); + edge.put("sta", 2); + edgesArray.add(edge); + } + + + //点-点 顺序 + for (int i = 0; i < knowIds.size() - 1; i++) { + JSONObject edge = JSONUtil.createObj(); + edge.put("source", knowIds.get(i)); + edge.put("target", knowIds.get(i + 1)); + edge.put("type", "Subsequence"); + edge.put("sta", 4); + edgesArray.add(edge); + } + String jsonNodeKnow = jsonArrayKnowNode.toString(); + String jsonNodeChapter = jsonArrayChapterNode.toString(); + String jsonNodeCourse = jsonArrayCourseNode.toString(); + String jsonEdges = edgesArray.toString(); + + System.out.println("json node " + jsonNodeKnow); + System.out.println("json jsonNodeChapter " + jsonNodeChapter); + System.out.println("json edge " + jsonEdges); + return generateGraph(jsonNodeCourse, jsonNodeChapter, jsonNodeKnow, jsonEdges); + //将json保存在mysql表中 + + } + + //todo 根据json串生成 图谱 保证事务 + private BaseResponse generateGraph(String jsonNodeCourse, String jsonNodeChapter, String jsonNodeKnow, String jsonEdges) { + //插入图 + JSONArray jsonArrayCourse = JSONUtil.parseArray(jsonNodeCourse); + for (Object obj : jsonArrayCourse) { + JSONObject jsonObject = (JSONObject) obj; + String courseId = jsonObject.getStr("courseId"); + String name = jsonObject.getStr("name"); + String info = jsonObject.getStr("info"); + Integer hour = jsonObject.getInt("hour"); + //在图数据库中插入 + Long res = knowRepository.addCourse(courseId, name, info, hour); + if (res == null) { + throw new RuntimeException("添加课程失败"); + } + + } + //插入章节 + JSONArray jsonArray = JSONUtil.parseArray(jsonNodeChapter); + for (Object obj : jsonArray) { + JSONObject jsonObject = (JSONObject) obj; + Long chapterId = jsonObject.getLong("chapterId"); + String name = jsonObject.getStr("name"); + String info = jsonObject.getStr("info"); + double hour = jsonObject.getDouble("hour"); + //在图数据库中插入 + Long res = knowRepository.addChapter(chapterId, name, info, hour); + if (res == null) { + throw new RuntimeException("添加章节失败"); + } + + } + //插入知识点 + JSONArray jsonArrayKnow = JSONUtil.parseArray(jsonNodeKnow); + for (Object obj : jsonArrayKnow) { + JSONObject jsonObject = (JSONObject) obj; + Long knowId = jsonObject.getLong("knowId"); + String name = jsonObject.getStr("name"); + String info = jsonObject.getStr("info"); + double hour = jsonObject.getDouble("hour"); + //在图数据库中插入 + Long res = knowRepository.addKnow(knowId, name, info, hour); + if (res == null) { + throw new RuntimeException("添加知识点失败"); + } + + } + + //插入关系 + JSONArray jsonArrayEdges = JSONUtil.parseArray(jsonEdges); + for (Object obj : jsonArrayEdges) { + JSONObject jsonObject = (JSONObject) obj; + String source = jsonObject.getStr("source"); + String target = jsonObject.getStr("target"); + String type = jsonObject.getStr("type"); + Integer sta = jsonObject.getInt("sta"); + //在图数据库中插入 + if (sta != null && sta == 1) { + Long target1 = Long.parseLong(target); + int res = knowRepository.addCourseRelationship(source, target1, type); + if (res <= 0) { + throw new RuntimeException("添加关系 1 失败"); + } + } else if (sta != null && sta == 2) { + Long source1 = Long.parseLong(source); + Long target1 = Long.parseLong(target); + int res = knowRepository.addChapterRelationship(source1, target1, type); + if (res <= 0) { + throw new RuntimeException("添加关系 2 失败"); + } + + } else if (sta != null && sta == 3) { + Long source1 = Long.parseLong(source); + Long target1 = Long.parseLong(target); + int res = knowRepository.addChapterToKnowRelationship(source1, target1, type); + if (res <= 0) { + throw new RuntimeException("添加关系 13失败"); + } + } else if (sta != null && sta == 4) { + Long source1 = Long.parseLong(source); + Long target1 = Long.parseLong(target); + int res = knowRepository.addKnowRelationship(source1, target1, type); + if (res <= 0) { + throw new RuntimeException("添加关系 4 失败"); + } + } else { + throw new RuntimeException("sta 错误"); + } + + } + return ResultUtils.success("添加节点关系成功"); + + + } + + @Transactional - // todo 事务没有正确使用 - public void addAllKnowTransation(String courseId) throws Exception{ + public void addAllKnowTransation(String courseId) throws Exception { + int f; + //验证章节是否存在 + //验证知识点是否存在,避免重复添加 + //先添加节点 + //后赋值关系 + + // 2.从课程表查出课程节点 并添加到 neo Courses courses = coursesMapper.selectById(courseId); - int f = knowRepository.addCourse(courseId,courses.getName(),courses.getDescription(),courses.getClasshours()); - if(f <= 0){ + Long KnowCourseId = knowRepository.addCourse(courseId, courses.getName(), courses.getDescription(), courses.getClasshours()); + + if (KnowCourseId == null) { throw new RuntimeException("添加课程失败"); } // 3 从章节表查出章节节点 并添加到 neo - List chapterList = chapterMapper.selectSectionsByCourseId(courseId); + List chapterList = new ArrayList<>(); + List chapterListPID = chapterMapper.selectChapterPIDByCourseId(courseId); + for (int i = 0; i < chapterListPID.size(); i++) { + chapterList.add(chapterListPID.get(i)); + List chapterListSecond = chapterMapper.selectChapterChildrenOrderByNum(chapterListPID.get(i).getId()); + for (int j = 0; j < chapterListSecond.size(); j++) { + chapterList.add(chapterListSecond.get(j)); + } + } + //添加节点 + List chapterIds = new ArrayList<>(); + List chapterOneIds = new ArrayList<>(); + List chapterTwoIds = new ArrayList<>(); for (int i = 0; i < chapterList.size(); i++) { Chapter chapter = chapterList.get(i); - f =knowRepository.addChapter(chapter.getId(),chapter.getName(),chapter.getContent(),chapter.getTotalClassHours()); - if(f <= 0){ + Long KnowChapterId = knowRepository.addChapter(chapter.getId(), chapter.getName(), chapter.getContent(), chapter.getTotalClassHours()); + if (chapter.getPid() == 0) { + chapterOneIds.add(KnowChapterId); + } else { + chapterTwoIds.add(KnowChapterId); + } + chapterIds.add(chapter.getId()); + if (KnowChapterId == null) { throw new RuntimeException("添加章节失败"); } } - List idList = chapterList.stream() - .map(menu -> menu.getId()) - .collect(Collectors.toList()); //获取chapterId 查询知识点 返回知识点集合 - List knowlist = knowtmpService.queryByChapterIdList(idList); - for (int i = 0; i < idList.size(); i++) { - List knowlist1 = knowtmpService.queryByChapterId(idList.get(i)); - for (int j = 0; j < knowlist1.size(); j++) { - Know know = knowRepository.addKnow(knowlist1.get(j).getName(),knowlist1.get(j).getInfo()); - if(know == null){ - throw new RuntimeException("添加知识点失败"); - } - f = knowRepository.addFatherAndSonWithKnow(know.getId(),idList.get(i)); - if(f <= 0){ - throw new RuntimeException("添加知识点-章节失败"); + List knowIds = new ArrayList<>(); + for (int i = 0; i < chapterList.size(); i++) { + Chapter chapter = chapterList.get(i); + if (chapter.getPid() != 0) { + List knowlist = knowtmpService.queryByChapterId(chapter.getId()); + for (int j = 0; j < knowlist.size(); j++) { + Long knowId = knowRepository.addKnow(knowlist.get(j).getId(), knowlist.get(j).getName(), knowlist.get(j).getInfo(), knowlist.get(j).getHour()); + knowIds.add(knowId); + if (knowId == null) { + throw new RuntimeException("添加知识点失败"); + } + //添加知识点关系 + f = knowRepository.addContainWithKnow(knowId, chapterIds.get(i)); + if (f <= 0) { + throw new RuntimeException("添加知识点-章节失败关系失败"); + } } } + } for (int i = 0; i < chapterList.size(); i++) { Chapter chapter = chapterList.get(i); - if(chapter.getPid()== 0){ - f = knowRepository.addFatherAndSonWithCourse(chapter.getCourseId(),chapter.getId()); - if(f <= 0) throw new RuntimeException("添加课程-章节关系失败"); - }else{ - f = knowRepository.addFatherAndSonWithChapter(chapter.getId(),chapter.getPid()); - if(f <= 0) throw new RuntimeException("添加章节-章节关系失败"); + if (chapter.getPid() == 0) { + f = knowRepository.addContainWithCourse(chapter.getCourseId(), chapter.getId()); + if (f <= 0) throw new RuntimeException("添加课程-章节关系失败"); + } else { + f = knowRepository.addContainWithChapter(chapter.getId(), chapter.getPid()); + if (f <= 0) throw new RuntimeException("添加章节-章节关系失败"); } } -// for (int j = 0; j < knowlist.size(); j++) { -// Knowtmp knowtmp = knowlist.get(j); -// f = knowRepository.addFatherAndSonWithKnow(knowtmp.getId(),knowtmp.getChapterid()); -// if(f <= 0) throw new RuntimeException("添加章节-知识点关系失败"); -// } - + //添加顺序关系 - } + //给章添加顺序关系 + for (int i = 0; i < chapterOneIds.size() - 1; i++) { + f = knowRepository.addOrderById(chapterOneIds.get(i), chapterOneIds.get(i + 1)); + if (f <= 0) throw new RuntimeException("添加章-章 顺序关系失败"); + } - @Override - public BaseResponse addKnow(KnowRequest knowRequest) { - Know know = new Know(); - BeanUtils.copyProperties(knowRequest,know); - Know know1 = knowRepository.addKnow(know.getName(),know.getInfo()); - if(know1 != null){ - return ResultUtils.success("添加知识点成功"); - }else{ - return ResultUtils.error(ErrorCode.OPERATION_ERROR,"添加知识点失败"); + //给节添加顺序关系 + for (int i = 0; i < chapterTwoIds.size() - 1; i++) { + f = knowRepository.addOrderById(chapterTwoIds.get(i), chapterTwoIds.get(i + 1)); + if (f <= 0) throw new RuntimeException("添加节-节 顺序关系失败"); + } + //给知识点添加顺序关系 + for (int i = 0; i < knowIds.size() - 1; i++) { + f = knowRepository.addOrderById(knowIds.get(i), knowIds.get(i + 1)); + if (f <= 0) throw new RuntimeException("添加知识点-知识点 顺序关系失败"); } } +// @Override +// public BaseResponse addKnow(KnowRequest knowRequest) { +// Know know = new Know(); +// BeanUtils.copyProperties(knowRequest,know); +// Long know1 = knowRepository.addKnow(know.getName(),know.getInfo()); +// if(know1 != null){ +// return ResultUtils.success("添加知识点成功"); +// }else{ +// return ResultUtils.error(ErrorCode.OPERATION_ERROR,"添加知识点失败"); +// } +// } + @Override public BaseResponse updateKnow(KnowUpdateRequest knowUpdateRequest) { Know know = new Know(); - BeanUtils.copyProperties(knowUpdateRequest,know); - int f = knowRepository.updateKnow(know.getId(),know.getName(),know.getInfo()); - if(f > 0){ + BeanUtils.copyProperties(knowUpdateRequest, know); + int f = knowRepository.updateKnow(know.getId(), know.getName(), know.getInfo()); + if (f > 0) { return ResultUtils.success("修改知识点成功"); - }else{ - return ResultUtils.error(ErrorCode.OPERATION_ERROR,"修改知识点失败"); + } else { + return ResultUtils.error(ErrorCode.OPERATION_ERROR, "修改知识点失败"); } } @@ -167,10 +642,10 @@ public class knowServiceImpl implements KnowService { public BaseResponse deleteKnow(Long id) { // todo 判断知识点存在否 int f = knowRepository.deleteKnow(id); - if(f > 0){ + if (f > 0) { return ResultUtils.success("删除知识点成功"); - }else{ - return ResultUtils.error(ErrorCode.OPERATION_ERROR,"删除知识点失败"); + } else { + return ResultUtils.error(ErrorCode.OPERATION_ERROR, "删除知识点失败"); } } @@ -186,24 +661,24 @@ public class knowServiceImpl implements KnowService { @Override public BaseResponse addCourse(KnowCourseCreateRequest knowCourseCreateRequest) { KnowCourse knowCourse = new KnowCourse(); - BeanUtils.copyProperties(knowCourseCreateRequest,knowCourse); - int f = knowRepository.addCourse(knowCourse.getCourseId(),knowCourse.getName(),knowCourse.getInfo(),knowCourse.getHour()); - if(f >0){ + BeanUtils.copyProperties(knowCourseCreateRequest, knowCourse); + Long knowCourse1 = knowRepository.addCourse(knowCourse.getCourseId(), knowCourse.getName(), knowCourse.getInfo(), knowCourse.getHour()); + if (knowCourse1 != null) { return ResultUtils.success("添加知识点成功"); - }else{ - return ResultUtils.error(ErrorCode.OPERATION_ERROR,"添加知识点失败"); + } else { + return ResultUtils.error(ErrorCode.OPERATION_ERROR, "添加知识点失败"); } } @Override public BaseResponse updateCourse(KnowCourseCreateRequest knowCourseCreateRequest) { KnowCourse knowCourse = new KnowCourse(); - BeanUtils.copyProperties(knowCourseCreateRequest,knowCourse); - int f = knowRepository.updateCourse(knowCourse.getCourseId(),knowCourse.getName(),knowCourse.getInfo()); - if(f > 0){ + BeanUtils.copyProperties(knowCourseCreateRequest, knowCourse); + int f = knowRepository.updateCourse(knowCourse.getCourseId(), knowCourse.getName(), knowCourse.getInfo()); + if (f > 0) { return ResultUtils.success("修改知识点成功"); - }else{ - return ResultUtils.error(ErrorCode.OPERATION_ERROR,"修改知识点失败"); + } else { + return ResultUtils.error(ErrorCode.OPERATION_ERROR, "修改知识点失败"); } } @@ -211,10 +686,10 @@ public class knowServiceImpl implements KnowService { public BaseResponse deleteCourse(String courseId) { int f = knowRepository.deleteCourse(courseId); - if(f > 0){ + if (f > 0) { return ResultUtils.success("删除知识点成功"); - }else{ - return ResultUtils.error(ErrorCode.OPERATION_ERROR,"删除知识点失败"); + } else { + return ResultUtils.error(ErrorCode.OPERATION_ERROR, "删除知识点失败"); } } @@ -227,44 +702,43 @@ public class knowServiceImpl implements KnowService { //========================= - @Override public BaseResponse addChapter(KnowChapterCreateRequest knowChapterCreateRequest) { KnowChapter knowChapter = new KnowChapter(); - BeanUtils.copyProperties(knowChapterCreateRequest,knowChapter); - int f = knowRepository.addChapter(knowChapter.getChapterId(),knowChapter.getName(),knowChapter.getInfo(),knowChapter.getHour()); - if(f>0){ + BeanUtils.copyProperties(knowChapterCreateRequest, knowChapter); + Long f = knowRepository.addChapter(knowChapter.getChapterId(), knowChapter.getName(), knowChapter.getInfo(), knowChapter.getHour()); + if (f > 0) { return ResultUtils.success("添加章节成功"); - }else{ - return ResultUtils.error(ErrorCode.OPERATION_ERROR,"添加章节失败"); + } else { + return ResultUtils.error(ErrorCode.OPERATION_ERROR, "添加章节失败"); } } @Override public BaseResponse updateChapter(KnowChapterCreateRequest knowChapterCreateRequest) { KnowChapter knowChapter = new KnowChapter(); - BeanUtils.copyProperties(knowChapterCreateRequest,knowChapter); - int f = knowRepository.updateChapter(knowChapter.getChapterId(),knowChapter.getName(),knowChapter.getInfo()); - if(f > 0){ + BeanUtils.copyProperties(knowChapterCreateRequest, knowChapter); + int f = knowRepository.updateChapter(knowChapter.getChapterId(), knowChapter.getName(), knowChapter.getInfo()); + if (f > 0) { return ResultUtils.success("修改知识点成功"); - }else{ - return ResultUtils.error(ErrorCode.OPERATION_ERROR,"修改知识点失败"); + } else { + return ResultUtils.error(ErrorCode.OPERATION_ERROR, "修改知识点失败"); } } @Override public BaseResponse deleteChapter(Long chapterId) { int f = knowRepository.deleteChapter(chapterId); - if(f > 0){ + if (f > 0) { return ResultUtils.success("删除知识点成功"); - }else{ - return ResultUtils.error(ErrorCode.OPERATION_ERROR,"删除知识点失败"); + } else { + return ResultUtils.error(ErrorCode.OPERATION_ERROR, "删除知识点失败"); } } @Override public BaseResponse queryChapter(Long chapterId) { - KnowChapter knowChapter = knowRepository.queryChapter(chapterId); + KnowChapter knowChapter = knowRepository.queryChapter(chapterId); return ResultUtils.success(knowChapter); } @@ -274,18 +748,18 @@ public class knowServiceImpl implements KnowService { @Override public BaseResponse getAllKnowByCourseId(String id) { Collection> all = - neo4jClient.query( "match(n:Know)-[r*0..]->(p:Know) where n.courseId = '"+id+"' return n as `n`,r as `r`,p as `p`,length(r) as `d`").fetch().all(); + neo4jClient.query("match(n:Know)-[r:contain*0..4]->(p:Know) where n.courseId = '" + id + "' return n as `n`,r as `r`,p as `p`,length(r) as `d`").fetch().all(); - MapcolorChoose = new HashMap<>(); + Map colorChoose = new HashMap<>(); String color[] = new String[10]; String[] colorList = {"#91CC75", "#5470C6", "#FAC858", "#EE6666", "#73C0DE", "#EA7CCC", "#5577FF", "#5577FF", "#9DBFFF", "#78A7FF"}; for (int i = 0; i < 10; i++) { - colorChoose.put((long) i,colorList[i]); + colorChoose.put((long) i, colorList[i]); } Iterator> iterator = all.iterator(); Set knowList = new HashSet<>(); - SetlinksList = new HashSet<>(); + Set linksList = new HashSet<>(); KnowVO knowVO; List node2 = new ArrayList<>(); Links links; @@ -296,14 +770,73 @@ public class knowServiceImpl implements KnowService { Node node1 = (Node) element.get("p"); Long group = (Long) element.get("d"); - knowVO.setColor(colorChoose.get(group)); +// knowVO.setColor(colorChoose.get(group)); + if (node1 != null) { + Long id1 = node1.id(); + String name1 = node1.get("name").asString(); + knowVO.setId(id1); + knowVO.setLabel(name1); + knowList.add(knowVO); + knowVO.setColor(colorChoose.get(group)); + + } + + node2 = (List) element.get("r"); + if (!node2.isEmpty()) { + 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()); + if ("contain".equals(e.type())) { + links.setLabel("包含"); + } else if ("order".equals(e.type())) { + links.setLabel("顺序"); + } + linksList.add(links); + } + } + + } + + BaseKnowReturn baseKnowReturn = new BaseKnowReturn(knowList, linksList); + return ResultUtils.success(baseKnowReturn); + } + + @Override + public BaseResponse getsecondKnowsById(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(); + + Map colorChoose = new HashMap<>(); + String color[] = new String[10]; + String[] colorList = {"#91CC75", "#5470C6", "#FAC858", "#EE6666", "#73C0DE", "#EA7CCC", "#5577FF", "#5577FF", "#9DBFFF", "#78A7FF"}; + for (int i = 0; i < 10; i++) { + colorChoose.put((long) i, colorList[i]); + } + + Iterator> iterator = all.iterator(); + Set knowList = new HashSet<>(); + Set linksList = new HashSet<>(); + KnowVO knowVO; + List node2 = new ArrayList<>(); + Links links; + int p = 0; + while (iterator.hasNext()) { + Map element = iterator.next(); + knowVO = new KnowVO(); + Node node1 = (Node) element.get("p"); + + Long group = (Long) element.get("d"); Long id1 = node1.id(); + String name1 = node1.get("name").asString(); knowVO.setId(id1); knowVO.setLabel(name1); + knowVO.setColor(colorChoose.get(group)); - knowList.add(knowVO); node2 = (List) element.get("r"); for (int i = 0; i < node2.size(); i++) { @@ -312,13 +845,18 @@ public class knowServiceImpl implements KnowService { links.setId(e.id()); links.setSource(e.startNodeId()); links.setTarget(e.endNodeId()); - links.setLabel(e.type()); + if ("contain".equals(e.type())) { + links.setLabel("包含"); + } else if ("order".equals(e.type())) { + links.setLabel("顺序"); + } linksList.add(links); } } - BaseKnowReturn baseKnowReturn = new BaseKnowReturn(knowList,linksList); + + BaseKnowReturn baseKnowReturn = new BaseKnowReturn(knowList, linksList); return ResultUtils.success(baseKnowReturn); } @@ -326,7 +864,7 @@ public class knowServiceImpl implements KnowService { @Override public BaseResponse> getAllResourcesByCourseId(String id) { Collection> all = - neo4jClient.query( "match(n:Know)-[r*0..]->(p:Know) where n.courseId = '"+id+"' return p as `p`").fetch().all(); + neo4jClient.query("match(n:Know)-[r*0..]->(p:Know) where n.courseId = '" + id + "' return p as `p`").fetch().all(); Iterator> iterator = all.iterator(); Set knowList = new HashSet<>(); KnowVO1 knowVO; @@ -344,5 +882,4 @@ public class knowServiceImpl implements KnowService { } - } diff --git a/src/main/java/com/teaching/backend/service/impl/knowtmp/KnowtmpServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/knowtmp/KnowtmpServiceImpl.java index ab78ee0..968c903 100644 --- a/src/main/java/com/teaching/backend/service/impl/knowtmp/KnowtmpServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/knowtmp/KnowtmpServiceImpl.java @@ -5,8 +5,10 @@ import com.teaching.backend.common.BaseResponse; import com.teaching.backend.common.ErrorCode; import com.teaching.backend.common.ResultUtils; import com.teaching.backend.mapper.Knowtemp.KnowtmpMapper; +import com.teaching.backend.mapper.chapter.ChapterMapper; import com.teaching.backend.model.dto.Knowtmp.KnowtempAdd; import com.teaching.backend.model.dto.Knowtmp.KnowtempUpdate; +import com.teaching.backend.model.entity.chapter.Chapter; import com.teaching.backend.model.entity.knowtmp.Knowtmp; import com.teaching.backend.service.knowtmp.KnowtmpService; import org.checkerframework.checker.units.qual.A; @@ -14,6 +16,7 @@ import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; /** @@ -24,9 +27,24 @@ import java.util.List; @Service public class KnowtmpServiceImpl implements KnowtmpService { + @Autowired + private ChapterMapper chapterMapper; + + @Autowired private KnowtmpMapper knowtmpMapper; + @Override + public BaseResponse getAllKnows(String courseId) { + // 3 从章节表查出章节节点 并添加到 neo + List chapterIds = chapterMapper.selectChapterIdsByCourseId(courseId); + //获取chapterId 查询知识点 返回知识点集合 + QueryWrapper objectQueryWrapper = new QueryWrapper<>(); + objectQueryWrapper.in("chapterid",chapterIds); + Long count = knowtmpMapper.selectCount(objectQueryWrapper); + return ResultUtils.success(count); + } + @Override public BaseResponse add(KnowtempAdd knowtempAdd) { Knowtmp knowtmp = new Knowtmp(); @@ -70,10 +88,7 @@ public class KnowtmpServiceImpl implements KnowtmpService { @Override public List queryByChapterId(Long chapterId) { - Knowtmp knowtmp = new Knowtmp(); - QueryWrapper knowtmpQueryWrapper = new QueryWrapper<>(); - knowtmpQueryWrapper.eq("chapterId",chapterId); - List knowtmps = knowtmpMapper.selectList(knowtmpQueryWrapper); + List knowtmps = knowtmpMapper.queryIdByChapterId(chapterId); return knowtmps; } diff --git a/src/main/java/com/teaching/backend/service/impl/resource/ResourceServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/resource/ResourceServiceImpl.java index 53378a0..a8708af 100644 --- a/src/main/java/com/teaching/backend/service/impl/resource/ResourceServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/resource/ResourceServiceImpl.java @@ -164,6 +164,13 @@ public class ResourceServiceImpl implements ResourceService { return ResultUtils.success(resourcesSet); } + @Override + public BaseResponse> queryResourcesByKnowId(Long KnowId) { + // todo 判空,查询 + SetresourcesSet ; + resourcesSet = resourcesRepository.queryResourcesByKnowId(KnowId); + return ResultUtils.success(resourcesSet); + } } diff --git a/src/main/java/com/teaching/backend/service/knowtmp/KnowtmpService.java b/src/main/java/com/teaching/backend/service/knowtmp/KnowtmpService.java index e8ab344..3468a16 100644 --- a/src/main/java/com/teaching/backend/service/knowtmp/KnowtmpService.java +++ b/src/main/java/com/teaching/backend/service/knowtmp/KnowtmpService.java @@ -13,6 +13,10 @@ import java.util.List; * @Description: */ public interface KnowtmpService { + //查询 + BaseResponse getAllKnows(String courseId); + + // 添加知识点 BaseResponse add(KnowtempAdd knowtempAdd); diff --git a/src/main/java/com/teaching/backend/service/resource/ResourceService.java b/src/main/java/com/teaching/backend/service/resource/ResourceService.java index d44a7ba..570a74e 100644 --- a/src/main/java/com/teaching/backend/service/resource/ResourceService.java +++ b/src/main/java/com/teaching/backend/service/resource/ResourceService.java @@ -40,5 +40,7 @@ public interface ResourceService { //查询二级节点下资源 BaseResponse> queryBesidesKnowToResources(Long KnowId); + BaseResponse> queryResourcesByKnowId(Long KnowId); + }