From d74ca8b622e10198d09b88a9eeb38c4a7c0c7f55 Mon Sep 17 00:00:00 2001 From: youahng <2998465706@qq.com> Date: Thu, 8 Aug 2024 15:30:13 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=84=E6=BA=90=E6=95=B4=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/Know/KnowController.java | 14 +++ .../controller/Knowtmp/KnowtmpController.java | 2 +- .../ResourceLearningRecordController.java | 1 + .../resource/ResourceController.java | 2 +- .../backend/mapper/know/KnowRepository.java | 22 +++- .../mapper/resource/ResourcesRepository.java | 2 +- .../model/entity/know/KnowChapter.java | 10 +- .../backend/model/entity/know/KnowCourse.java | 8 +- .../backend/service/Know/KnowService.java | 10 ++ .../service/impl/know/knowServiceImpl.java | 118 ++++++++++++++++-- .../impl/knowtmp/KnowtmpServiceImpl.java | 14 ++- .../impl/resource/ResourceServiceImpl.java | 4 +- .../service/knowtmp/KnowtmpService.java | 5 +- .../service/resource/ResourceService.java | 2 +- 14 files changed, 184 insertions(+), 30 deletions(-) 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 ee8b95d..5903b7e 100644 --- a/src/main/java/com/teaching/backend/controller/Know/KnowController.java +++ b/src/main/java/com/teaching/backend/controller/Know/KnowController.java @@ -28,6 +28,20 @@ public class KnowController { @Autowired private KnowService knowService; + // 添加全部知识点 + @PostMapping("/addAllKnow") + BaseResponse addAllKnow(@RequestParam String courseId){ + return knowService.addAllKnow(courseId); + } + + // 添加知识点与资源的关系 + @PostMapping("/addRelationship/Withresources") + BaseResponse addRelationshipWithresources(@RequestParam Long id,@RequestParam Long resourceId){ + return knowService.addRelationshipWithresources(id,resourceId); + } + + //=================== + // 添加知识点 @PostMapping("/addKnow") BaseResponse addKnow(@RequestBody KnowRequest knowRequest){ 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 dbb122a..85ab55d 100644 --- a/src/main/java/com/teaching/backend/controller/Knowtmp/KnowtmpController.java +++ b/src/main/java/com/teaching/backend/controller/Knowtmp/KnowtmpController.java @@ -50,7 +50,7 @@ public class KnowtmpController { } //查询通过chapterId List @GetMapping("/queryKnow") - BaseResponse> queryByChapterId(Long chapterId){ + List queryByChapterId(Long chapterId){ return knowtmpService.queryByChapterId(chapterId); } } diff --git a/src/main/java/com/teaching/backend/controller/records/ResourceLearningRecordController.java b/src/main/java/com/teaching/backend/controller/records/ResourceLearningRecordController.java index 29a3aca..4758229 100644 --- a/src/main/java/com/teaching/backend/controller/records/ResourceLearningRecordController.java +++ b/src/main/java/com/teaching/backend/controller/records/ResourceLearningRecordController.java @@ -25,6 +25,7 @@ //@RestController //@RequestMapping("/api/resourcelearningrecords") //public class ResourceLearningRecordController { + // @Resource // private ResourceLearningRecordServiceImpl resourceLearningRecordService; // @Resource 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 71b0884..2633406 100644 --- a/src/main/java/com/teaching/backend/controller/resource/ResourceController.java +++ b/src/main/java/com/teaching/backend/controller/resource/ResourceController.java @@ -32,7 +32,7 @@ public class ResourceController { //添加知识点 @PostMapping("/upload") @ResponseBody - public BaseResponse uploadFile(@RequestPart("file") MultipartFile file) { + public BaseResponse uploadFile(@RequestPart("file") MultipartFile file) { return resourceService.upload(file); } 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 9871a0d..b1c5d8d 100644 --- a/src/main/java/com/teaching/backend/mapper/know/KnowRepository.java +++ b/src/main/java/com/teaching/backend/mapper/know/KnowRepository.java @@ -14,12 +14,15 @@ import com.teaching.backend.model.entity.resource.Resources; import org.springframework.data.neo4j.repository.Neo4jRepository; import org.springframework.data.neo4j.repository.query.Query; +import java.util.List; import java.util.Set; public interface KnowRepository extends Neo4jRepository { - @Query("CREATE (n:Know {name: $name,info:$info}) return count(n)") - int addKnow(String name,String info); + //====================== + + @Query("CREATE (n:Know {name: $name,info:$info}) return n;") + Know addKnow(String name,String info); @Query("MATCH ID(p:Know) = $id SET p.name = $name set p.info= $info RETURN count(p);") @@ -63,4 +66,19 @@ public interface KnowRepository extends Neo4jRepository { @Query("MATCH (p:Know)-[:FatherAndSon]->(n:Know)-[:resources]->(m:Resources) WHERE ID(p) = $id RETURN m") 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 {chapterId: $chapterId1}), (c:Know {chapterId: $chapterId2}) CREATE (c)-[r:FatherAndSon]->(p) return count(r);") + int addFatherAndSonWithChapter(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), (c:Resources) WHERE ID(p) = $id AND ID(c) = $ResourcesId CREATE (p)-[r:resources]->(c) return count(r)") + int addResourceResourcesKnow(Long id, Long ResourcesId); + } 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 16286ed..94889d6 100644 --- a/src/main/java/com/teaching/backend/mapper/resource/ResourcesRepository.java +++ b/src/main/java/com/teaching/backend/mapper/resource/ResourcesRepository.java @@ -38,7 +38,7 @@ public interface ResourcesRepository extends Neo4jRepository { Set queryResourcesByCourseId(String courseId); //查询章节下资源 - @Query("MATCH p = (a:Know)-[rels*]-(b:Resources) WHERE a.chapterId = chapterId and ANY(rel IN rels WHERE TYPE(rel) = 'resources') RETURN b") + @Query("MATCH p = (a:Know)-[rels*]-(b:Resources) WHERE a.chapterId = $chapterId and ANY(rel IN rels WHERE TYPE(rel) = 'resources') RETURN b") Set queryResourcesByChapterId(Long chapterId); //查询二级节点下资源 diff --git a/src/main/java/com/teaching/backend/model/entity/know/KnowChapter.java b/src/main/java/com/teaching/backend/model/entity/know/KnowChapter.java index 07e330a..b4759c5 100644 --- a/src/main/java/com/teaching/backend/model/entity/know/KnowChapter.java +++ b/src/main/java/com/teaching/backend/model/entity/know/KnowChapter.java @@ -8,7 +8,7 @@ import org.springframework.data.neo4j.core.schema.Property; import java.io.Serializable; -@Node + @Data public class KnowChapter implements Serializable { @@ -16,26 +16,26 @@ public class KnowChapter implements Serializable { /** * id */ - @Id - @GeneratedValue + private Long id; /** * 章节id */ + private Long chapterId; /** * 知识点名称 */ - @Property + private String name; /** * 信息 */ - @Property + private String info; diff --git a/src/main/java/com/teaching/backend/model/entity/know/KnowCourse.java b/src/main/java/com/teaching/backend/model/entity/know/KnowCourse.java index f85d1d2..56399af 100644 --- a/src/main/java/com/teaching/backend/model/entity/know/KnowCourse.java +++ b/src/main/java/com/teaching/backend/model/entity/know/KnowCourse.java @@ -8,7 +8,6 @@ import org.springframework.data.neo4j.core.schema.Property; import java.io.Serializable; -@Node @Data public class KnowCourse implements Serializable { @@ -16,26 +15,25 @@ public class KnowCourse implements Serializable { /** * id */ - @Id - @GeneratedValue + private Long id; /** * 课程id */ + private String courseId; /** * 知识点名称 */ - @Property + private String name; /** * 信息 */ - @Property private String info; 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 14cdb91..41c54eb 100644 --- a/src/main/java/com/teaching/backend/service/Know/KnowService.java +++ b/src/main/java/com/teaching/backend/service/Know/KnowService.java @@ -7,6 +7,7 @@ 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 com.teaching.backend.model.vo.knowGraph.KnowVO1; +import org.springframework.web.bind.annotation.RequestParam; import java.util.Set; @@ -17,6 +18,15 @@ import java.util.Set; */ public interface KnowService { + //通过courseId 添加所有节点 + BaseResponse addAllKnow(String courseId); + + BaseResponse addRelationshipWithresources(Long id, Long resourceId); + + + + //===================== + // 添加知识点 BaseResponse addKnow(KnowRequest knowRequest); 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 ae58db6..8277c60 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,27 +1,36 @@ package com.teaching.backend.service.impl.know; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.teaching.backend.common.BaseResponse; import com.teaching.backend.common.ErrorCode; import com.teaching.backend.common.ResultUtils; +import com.teaching.backend.mapper.chapter.ChapterMapper; +import com.teaching.backend.mapper.courses.CoursesMapper; import com.teaching.backend.mapper.know.KnowRepository; import com.teaching.backend.model.dto.Know.*; +import com.teaching.backend.model.entity.chapter.Chapter; +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.know.Links; +import com.teaching.backend.model.entity.knowtmp.Knowtmp; import com.teaching.backend.model.entity.resource.Resources; import com.teaching.backend.model.vo.knowGraph.KnowVO; import com.teaching.backend.model.vo.knowGraph.KnowVO1; import com.teaching.backend.service.Know.KnowService; +import com.teaching.backend.service.knowtmp.KnowtmpService; import org.neo4j.driver.internal.InternalRelationship; import org.neo4j.driver.types.Node; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.neo4j.core.Neo4jClient; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.*; +import java.util.stream.Collectors; /** * @Author:youhang @@ -34,15 +43,108 @@ public class knowServiceImpl implements KnowService { @Autowired private KnowRepository knowRepository; + + @Autowired + private ChapterMapper chapterMapper; + + @Autowired + private KnowtmpService knowtmpService; + + + + @Autowired + CoursesMapper coursesMapper; + @Resource private Neo4jClient neo4jClient; + + 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 addAllKnow(String courseId) { + try { + addAllKnowTransation(courseId) ; + }catch (Exception e){ + e.printStackTrace(); + return ResultUtils.error(ErrorCode.OPERATION_ERROR,e.getMessage()); + } + //6.返回成功信息,关闭事务 + return ResultUtils.success("添加成功"); + } + + @Transactional + // todo 事务没有正确使用 + public void addAllKnowTransation(String courseId) throws Exception{ + // 2.从课程表查出课程节点 并添加到 neo + Courses courses = coursesMapper.selectById(courseId); + int f = knowRepository.addCourse(courseId,courses.getName(),courses.getDescription()); + if(f <= 0){ + throw new RuntimeException("添加课程失败"); + } + // 3 从章节表查出章节节点 并添加到 neo + List chapterList = chapterMapper.selectSectionsByCourseId(courseId); + for (int i = 0; i < chapterList.size(); i++) { + Chapter chapter = chapterList.get(i); + f =knowRepository.addChapter(chapter.getId(),chapter.getName(),chapter.getContent()); + if(f <= 0){ + 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("添加知识点-章节失败"); + } + } + } + + 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("添加章节-章节关系失败"); + } + + } + +// 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("添加章节-知识点关系失败"); +// } + + + } + @Override public BaseResponse addKnow(KnowRequest knowRequest) { Know know = new Know(); BeanUtils.copyProperties(knowRequest,know); - int f = knowRepository.addKnow(know.getName(),know.getInfo()); - if(f > 0){ + Know know1 = knowRepository.addKnow(know.getName(),know.getInfo()); + if(know1 != null){ return ResultUtils.success("添加知识点成功"); }else{ return ResultUtils.error(ErrorCode.OPERATION_ERROR,"添加知识点失败"); @@ -85,8 +187,8 @@ public class knowServiceImpl implements KnowService { public BaseResponse addCourse(KnowCourseCreateRequest knowCourseCreateRequest) { KnowCourse knowCourse = new KnowCourse(); BeanUtils.copyProperties(knowCourseCreateRequest,knowCourse); - int f = knowRepository.addCourse(knowCourse.getCourseId(),knowCourse.getName(),knowCourse.getInfo()); - if(f > 0){ + int f = knowRepository.addCourse(knowCourse.getCourseId(),knowCourse.getName(),knowCourse.getInfo()); + if(f >0){ return ResultUtils.success("添加知识点成功"); }else{ return ResultUtils.error(ErrorCode.OPERATION_ERROR,"添加知识点失败"); @@ -130,11 +232,11 @@ public class knowServiceImpl implements KnowService { public BaseResponse addChapter(KnowChapterCreateRequest knowChapterCreateRequest) { KnowChapter knowChapter = new KnowChapter(); BeanUtils.copyProperties(knowChapterCreateRequest,knowChapter); - int f = knowRepository.addChapter(knowChapter.getChapterId(),knowChapter.getName(),knowChapter.getInfo()); - if(f > 0){ - return ResultUtils.success("添加知识点成功"); + int f = knowRepository.addChapter(knowChapter.getChapterId(),knowChapter.getName(),knowChapter.getInfo()); + if(f>0){ + return ResultUtils.success("添加章节成功"); }else{ - return ResultUtils.error(ErrorCode.OPERATION_ERROR,"添加知识点失败"); + return ResultUtils.error(ErrorCode.OPERATION_ERROR,"添加章节失败"); } } 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 8165dae..ab78ee0 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 @@ -69,11 +69,19 @@ public class KnowtmpServiceImpl implements KnowtmpService { } @Override - public BaseResponse> queryByChapterId(Long chapterId) { + public List queryByChapterId(Long chapterId) { Knowtmp knowtmp = new Knowtmp(); - final QueryWrapper knowtmpQueryWrapper = new QueryWrapper<>(); + QueryWrapper knowtmpQueryWrapper = new QueryWrapper<>(); knowtmpQueryWrapper.eq("chapterId",chapterId); List knowtmps = knowtmpMapper.selectList(knowtmpQueryWrapper); - return ResultUtils.success(knowtmps); + return knowtmps; + } + + @Override + public List queryByChapterIdList(List chapterIdList) { + QueryWrapper knowtmpQueryWrapper = new QueryWrapper<>(); + knowtmpQueryWrapper.in("chapterId",chapterIdList); + List knowtmps = knowtmpMapper.selectList(knowtmpQueryWrapper); + 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 b8ba096..f59cab9 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 @@ -67,7 +67,7 @@ public class ResourceServiceImpl implements ResourceService { private Neo4jClient neo4jClient; @Override - public BaseResponse upload(MultipartFile file) { + public BaseResponse upload(MultipartFile file) { ResourceUploadDto resourceUploadDto = minioUtils.upload(file); if (resourceUploadDto == null) { return ResultUtils.error(ErrorCode.OPERATION_ERROR, "上传失败"); @@ -83,7 +83,7 @@ public class ResourceServiceImpl implements ResourceService { resource.setUrl(url); resourcesRepository.save(resource); - return ResultUtils.success(resourceUploadDto); + return ResultUtils.success(resource); } @Override 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 193fe44..e8ab344 100644 --- a/src/main/java/com/teaching/backend/service/knowtmp/KnowtmpService.java +++ b/src/main/java/com/teaching/backend/service/knowtmp/KnowtmpService.java @@ -26,6 +26,9 @@ public interface KnowtmpService { BaseResponse query(Long id); //查询通过chapterId List - BaseResponse> queryByChapterId(Long chapterId); + List queryByChapterId(Long chapterId); + + //查询通过chapterId List + List queryByChapterIdList(List chapterIdList); } 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 a916e24..c8c5fab 100644 --- a/src/main/java/com/teaching/backend/service/resource/ResourceService.java +++ b/src/main/java/com/teaching/backend/service/resource/ResourceService.java @@ -20,7 +20,7 @@ import java.util.Set; public interface ResourceService { - BaseResponse upload(MultipartFile file) ; + BaseResponse upload(MultipartFile file) ; BaseResponse delete(String filename) ;