you hang 5 months ago committed by youhang20021127
parent 9701771c45
commit f999d4e04a
  1. 6
      pom.xml
  2. 21
      src/main/java/com/teaching/backend/controller/Know/Edge.java
  3. 38
      src/main/java/com/teaching/backend/controller/Know/KnowChapterVO.java
  4. 56
      src/main/java/com/teaching/backend/controller/Know/KnowController.java
  5. 33
      src/main/java/com/teaching/backend/controller/Know/KnowCourseVO.java
  6. 42
      src/main/java/com/teaching/backend/controller/Know/KnowJson.java
  7. 31
      src/main/java/com/teaching/backend/controller/Know/KnowJsonListVO.java
  8. 9
      src/main/java/com/teaching/backend/controller/Knowtmp/KnowtmpController.java
  9. 6
      src/main/java/com/teaching/backend/controller/resource/ResourceController.java
  10. 8
      src/main/java/com/teaching/backend/mapper/Knowtemp/KnowtmpMapper.java
  11. 18
      src/main/java/com/teaching/backend/mapper/chapter/ChapterMapper.java
  12. 62
      src/main/java/com/teaching/backend/mapper/know/KnowRepository.java
  13. 4
      src/main/java/com/teaching/backend/mapper/resource/ResourcesRepository.java
  14. 15
      src/main/java/com/teaching/backend/service/Know/KnowService.java
  15. 719
      src/main/java/com/teaching/backend/service/impl/know/knowServiceImpl.java
  16. 23
      src/main/java/com/teaching/backend/service/impl/knowtmp/KnowtmpServiceImpl.java
  17. 7
      src/main/java/com/teaching/backend/service/impl/resource/ResourceServiceImpl.java
  18. 4
      src/main/java/com/teaching/backend/service/knowtmp/KnowtmpService.java
  19. 2
      src/main/java/com/teaching/backend/service/resource/ResourceService.java

@ -243,6 +243,12 @@
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
</dependencies>

@ -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 点 - 点
}

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

@ -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<BaseKnowReturn> getTwoLevelKnows(@RequestParam Long id){
return knowService.getTwoLevelKnows(id);
}
//返回3层知识点
@GetMapping("/threeLevelKnows")
BaseResponse<BaseKnowReturn> getThreeLevelKnows(@RequestParam Long id){
return knowService.getThreeLevelKnows(id);
}
//返回4层知识点
@GetMapping("/fourLevelKnows")
BaseResponse<BaseKnowReturn> 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<String> showGraphJson(@RequestParam String courseId){
return knowService.showGraphJson(courseId);
}
// 添加全部知识点
@PostMapping("/addAllKnow")
BaseResponse<String> addAllKnow(@RequestParam String courseId){
@ -42,11 +79,11 @@ public class KnowController {
//===================
// 添加知识点
@PostMapping("/addKnow")
BaseResponse<String> addKnow(@RequestBody KnowRequest knowRequest){
return knowService.addKnow(knowRequest);
}
// // 添加知识点
// @PostMapping("/addKnow")
// BaseResponse<String> addKnow(@RequestBody KnowRequest knowRequest){
// return knowService.addKnow(knowRequest);
// }
// 修改知识点
@PostMapping("/updateKnow")
@ -129,4 +166,13 @@ public class KnowController {
}
//获取二级知识图谱
@GetMapping("/getsecondKnowsById")
BaseResponse<BaseKnowReturn> getsecondKnowsById(@RequestParam Long id){
return knowService.getsecondKnowsById(id);
}
}

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

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

@ -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<KnowJson> knowJsonList;
public List<KnowJson> KnowJsonListVO() {
return knowJsonList;
}
public void setKnowJsonList(List<KnowJson> knowJsonList) {
this.knowJsonList = knowJsonList;
}
public List<KnowJson> getKnowJsonList() {
return this.knowJsonList ;
}
}

@ -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<Long> getAllKnows(@RequestParam String courseId){
return knowtmpService.getAllKnows(courseId);
}
// 添加知识点
@PostMapping("/addKnow")

@ -47,6 +47,7 @@ public class ResourceController {
public ResponseEntity<InputStreamResource> readFile(@RequestParam String filename) {
return resourceService.readFile(filename);
}
@GetMapping("/addRelationship/BetweenCourseAndResources")
public BaseResponse<String> addResourcesAndKnowByCourseId(@RequestParam String courseId, @RequestParam List<Long> resourcesIds){
return resourceService.addResourcesAndKnowByCourseId(courseId,resourcesIds);
@ -77,6 +78,11 @@ public class ResourceController {
BaseResponse<Set<Resources>> queryBesidesKnowToResources(@RequestParam Long KnowId){
return resourceService.queryBesidesKnowToResources(KnowId);
}
//查询知识点下资源
@GetMapping("/queryResourcesByKnowId")
BaseResponse<Set<Resources>> queryResourcesByKnowId(@RequestParam Long KnowId){
return resourceService.queryResourcesByKnowId(KnowId);
}

@ -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<Knowtmp> {
@Select("SELECT * FROM Knowtmp WHERE chapterid = #{chapterId} ORDER BY ordernum")
List<Knowtmp> queryIdByChapterId(@Param("chapterid") Long chapterId);
}

@ -21,6 +21,24 @@ public interface ChapterMapper extends BaseMapper<Chapter> {
@Select("SELECT * FROM chapter WHERE course_id = #{courseId}")
List<Chapter> selectSectionsByCourseId(@Param("courseId") String courseId);
@Select("SELECT id FROM chapter WHERE course_id = #{courseId}")
List<Long> selectChapterIdsByCourseId(@Param("courseId") String courseId);
@Select("SELECT * FROM chapter WHERE course_id = #{courseId} ORDER BY order_num")
List<Chapter> selectSectionsOrderByNumByCourseId(@Param("courseId") String courseId);
@Select("SELECT * FROM chapter WHERE course_id = #{courseId} AND pid = 0 ORDER BY order_num")
List<Chapter> selectChapterPIDByCourseId(@Param("courseId") String courseId);
@Select("SELECT * FROM chapter WHERE pid = #{parentId} ORDER BY order_num")
List<Chapter> selectChapterChildrenOrderByNum(@Param("parentId") Long parentId);
@Select("SELECT id FROM chapter WHERE pid = #{chapterId} ORDER BY order_num")
List<Integer> selectSectionsOrderByNumByChapterId(@Param("pid") Integer chapterId);
@Select("SELECT * FROM course_chapter WHERE course_id = #{courseId} AND (pid = #{parentId} )")
List<Chapter> selectSectionsWithChildren(@Param("courseId") String courseId, @Param("parentId") String parentId);
@Select("SELECT * FROM chapter WHERE id = #{excelId}")

@ -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<Know, Long> {
@Query("MATCH (p:Know)-[:FatherAndSon]->(n:Know) WHERE p.chapterId = $chapterId RETURN p.id ORDER BY ordernum;")
List<Long>queryKnowIdByChapterId(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<Know, Long> {
//=============================
@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<Know, Long> {
//====================
@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<Know, Long> {
Set<Resources> 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);
}

@ -45,6 +45,10 @@ public interface ResourcesRepository extends Neo4jRepository<Resources, Long> {
@Query("MATCh (p:Know)-[*0..1]->(n:Know)-[:resources]->(m:Resources) WHERE ID(p) = $KnowId RETURN m")
Set<Resources> queryBesidesKnowToResources(Long KnowId);
//
@Query("MATCH (p)-[r:resources]->(c) WHERE ID(p) = $KnowId RETURN c")
Set<Resources> queryResourcesByKnowId(Long KnowId);

@ -18,6 +18,14 @@ import java.util.Set;
*/
public interface KnowService {
BaseResponse<BaseKnowReturn> getTwoLevelKnows(Long id);
BaseResponse<BaseKnowReturn> getThreeLevelKnows(Long id);
BaseResponse<BaseKnowReturn> getFourLevelKnows(Long id);
//通过courseId 显示图的JSON
BaseResponse<String> showGraphJson(String courseId);
//通过courseId 添加所有节点
BaseResponse<String> addAllKnow(String courseId);
@ -27,8 +35,8 @@ public interface KnowService {
//=====================
// 添加知识点
BaseResponse<String> addKnow(KnowRequest knowRequest);
// // 添加知识点
// BaseResponse<String> addKnow(KnowRequest knowRequest);
// 修改知识点
BaseResponse<String> updateKnow(KnowUpdateRequest knowUpdateRequest);
@ -73,6 +81,9 @@ public interface KnowService {
//获取课程下的所有知识点
BaseResponse<BaseKnowReturn> getAllKnowByCourseId(String id);
//获取课程下的所有知识点
BaseResponse<BaseKnowReturn> getsecondKnowsById(Long id);
//获取课程下的所有资源
BaseResponse<Set<KnowVO1>> getAllResourcesByCourseId(String id);

@ -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<String> addRelationshipWithresources(Long id, Long resourceId){
int f = knowRepository.addResourceResourcesKnow(id,resourceId);
if(f <= 0){
public BaseResponse<String> addRelationshipWithresources(Long id, Long resourceId) {
int f = knowRepository.addResourceResourcesKnow(id, resourceId);
if (f <= 0) {
throw new RuntimeException("添加关系失败");
}
return ResultUtils.success("添加关系成功");
}
@Override
public BaseResponse<BaseKnowReturn> getTwoLevelKnows(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();
Map<Long, String> 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<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();
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<BaseKnowReturn> getThreeLevelKnows(Long id) {
Collection<Map<String, Object>> 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<Long, String> 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<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();
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<BaseKnowReturn> getFourLevelKnows(Long id) {
Collection<Map<String, Object>> 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<Long, String> 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<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();
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<String> showGraphJson(String courseId) {
return addInformation(courseId);
}
@Override
public BaseResponse<String> 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<String> 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<Chapter> chapterList = new ArrayList<>();
List<Chapter> chapterListPID = chapterMapper.selectChapterPIDByCourseId(courseId);
List<Long> chapterOneIds = new ArrayList<>();
List<Long> chapterTwoIds = new ArrayList<>();
for (int i = 0; i < chapterListPID.size(); i++) {
chapterList.add(chapterListPID.get(i));
chapterOneIds.add(chapterListPID.get(i).getId());
List<Chapter> 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<Knowtmp> 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<Chapter> 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<Long> knowIds = new ArrayList<>();
for (int i = 0; i < chapterTwoIds.size(); i++) {
List<Knowtmp> 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<String> 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<Chapter> chapterList = chapterMapper.selectSectionsByCourseId(courseId);
List<Chapter> chapterList = new ArrayList<>();
List<Chapter> chapterListPID = chapterMapper.selectChapterPIDByCourseId(courseId);
for (int i = 0; i < chapterListPID.size(); i++) {
chapterList.add(chapterListPID.get(i));
List<Chapter> chapterListSecond = chapterMapper.selectChapterChildrenOrderByNum(chapterListPID.get(i).getId());
for (int j = 0; j < chapterListSecond.size(); j++) {
chapterList.add(chapterListSecond.get(j));
}
}
//添加节点
List<Long> chapterIds = new ArrayList<>();
List<Long> chapterOneIds = new ArrayList<>();
List<Long> 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<Long> idList = chapterList.stream()
.map(menu -> menu.getId())
.collect(Collectors.toList());
//获取chapterId 查询知识点 返回知识点集合
List<Knowtmp> knowlist = knowtmpService.queryByChapterIdList(idList);
for (int i = 0; i < idList.size(); i++) {
List<Knowtmp> 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){
List<Long> knowIds = new ArrayList<>();
for (int i = 0; i < chapterList.size(); i++) {
Chapter chapter = chapterList.get(i);
if (chapter.getPid() != 0) {
List<Knowtmp> 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.addFatherAndSonWithKnow(know.getId(),idList.get(i));
if(f <= 0){
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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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,38 +702,37 @@ public class knowServiceImpl implements KnowService {
//=========================
@Override
public BaseResponse<String> 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<String> 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<String> 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, "删除知识点失败");
}
}
@ -274,18 +748,18 @@ public class knowServiceImpl implements KnowService {
@Override
public BaseResponse<BaseKnowReturn> getAllKnowByCourseId(String id) {
Collection<Map<String, Object>> 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();
Map<Long,String>colorChoose = new HashMap<>();
Map<Long, String> 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<Map<String, Object>> iterator = all.iterator();
Set<KnowVO> knowList = new HashSet<>();
Set<Links>linksList = new HashSet<>();
Set<Links> 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));
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<BaseKnowReturn> getsecondKnowsById(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();
Map<Long, String> 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<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();
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<Set<KnowVO1>> getAllResourcesByCourseId(String id) {
Collection<Map<String, Object>> 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<Map<String, Object>> iterator = all.iterator();
Set<KnowVO1> knowList = new HashSet<>();
KnowVO1 knowVO;
@ -344,5 +882,4 @@ public class knowServiceImpl implements KnowService {
}
}

@ -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<Long> getAllKnows(String courseId) {
// 3 从章节表查出章节节点 并添加到 neo
List<Long> chapterIds = chapterMapper.selectChapterIdsByCourseId(courseId);
//获取chapterId 查询知识点 返回知识点集合
QueryWrapper<Knowtmp> objectQueryWrapper = new QueryWrapper<>();
objectQueryWrapper.in("chapterid",chapterIds);
Long count = knowtmpMapper.selectCount(objectQueryWrapper);
return ResultUtils.success(count);
}
@Override
public BaseResponse<String> add(KnowtempAdd knowtempAdd) {
Knowtmp knowtmp = new Knowtmp();
@ -70,10 +88,7 @@ public class KnowtmpServiceImpl implements KnowtmpService {
@Override
public List<Knowtmp> queryByChapterId(Long chapterId) {
Knowtmp knowtmp = new Knowtmp();
QueryWrapper<Knowtmp> knowtmpQueryWrapper = new QueryWrapper<>();
knowtmpQueryWrapper.eq("chapterId",chapterId);
List<Knowtmp> knowtmps = knowtmpMapper.selectList(knowtmpQueryWrapper);
List<Knowtmp> knowtmps = knowtmpMapper.queryIdByChapterId(chapterId);
return knowtmps;
}

@ -164,6 +164,13 @@ public class ResourceServiceImpl implements ResourceService {
return ResultUtils.success(resourcesSet);
}
@Override
public BaseResponse<Set<Resources>> queryResourcesByKnowId(Long KnowId) {
// todo 判空,查询
Set<Resources>resourcesSet ;
resourcesSet = resourcesRepository.queryResourcesByKnowId(KnowId);
return ResultUtils.success(resourcesSet);
}
}

@ -13,6 +13,10 @@ import java.util.List;
* @Description:
*/
public interface KnowtmpService {
//查询
BaseResponse<Long> getAllKnows(String courseId);
// 添加知识点
BaseResponse<String> add(KnowtempAdd knowtempAdd);

@ -40,5 +40,7 @@ public interface ResourceService {
//查询二级节点下资源
BaseResponse<Set<Resources>> queryBesidesKnowToResources(Long KnowId);
BaseResponse<Set<Resources>> queryResourcesByKnowId(Long KnowId);
}

Loading…
Cancel
Save