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 2d4b44a..7fcce7d 100644 --- a/src/main/java/com/teaching/backend/controller/Know/KnowController.java +++ b/src/main/java/com/teaching/backend/controller/Know/KnowController.java @@ -50,6 +50,12 @@ public class KnowController { BaseResponse> knowLearnPath(@RequestParam String corseId){ return knowService.knowLearnPath(corseId); } + //生成知识点学习路径 默认从第一个知识点到第最后个知识点 + @ApiOperation(value = "生成知识点学习路径") + @GetMapping("/knowLearnPathGraph") + BaseResponse knowLearnPathGraph(@RequestParam String corseId){ + return knowService.knowLearnPathGraph(corseId); + } //返回depth层知识点 @ApiOperation(value = "返回depth层知识点") @@ -69,8 +75,8 @@ public class KnowController { //返回课程下指定关系的知识图谱 @ApiOperation(value = "返回课程下指定关系的知识图谱") @GetMapping("/getRelsNodesByCourseId") - BaseResponse getRelsNodesByCourseId(@RequestParam String courseId,@RequestParam Listtypes){ - return knowService.getRelsNodesByCourseId(courseId,types); + BaseResponse getRelsNodesByCourseId(@RequestParam String courseId,@RequestParam Integer level,@RequestParam Listtypes){ + return knowService.getRelsNodesByCourseId(courseId, level,types); } //返回知识点的前后知识点 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 e7b4d5d..d35271f 100644 --- a/src/main/java/com/teaching/backend/service/Know/KnowService.java +++ b/src/main/java/com/teaching/backend/service/Know/KnowService.java @@ -23,6 +23,7 @@ public interface KnowService { public BaseResponse getNodeByDepth(long id,Long depth); BaseResponse> knowLearnPath(String corseId); + BaseResponseknowLearnPathGraph(String corseId); BaseResponse updateLinks(List linksList); BaseResponse deleteAllByCourseId(String courseId1); @@ -37,7 +38,7 @@ public interface KnowService { BaseResponse getAllKnowByCourseId(String id); //返回课程下指定关系的知识图谱 - BaseResponse getRelsNodesByCourseId(String courseId,Listtypes); + BaseResponse getRelsNodesByCourseId(String courseId, Integer level,Listtypes); //返回课程下指定关系的知识图谱 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 d4f1a64..b36f89d 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 @@ -436,6 +436,75 @@ public class knowServiceImpl implements KnowService { return ResultUtils.success(knowPathVOList); } + @Override + public BaseResponse knowLearnPathGraph(String corseId) { + + //查询第一个知识点-最后一个知识点 + List knowIds = new ArrayList<>(); + List chapterList = chapterMapper.selectChapterPIDByCourseId(corseId); + for (int i = 0; i < chapterList.size(); i++) { + List chapterList1 = chapterMapper.selectChapterChildrenOrderByNum(chapterList.get(i).getId()); + for (int j = 0; j < chapterList1.size(); j++) { + QueryWrapper queryWrapper = new QueryWrapper(); + queryWrapper.eq("chapterid", chapterList1.get(j).getId()); + List knowtmp = knowtmpMapper.selectList(queryWrapper); + for (int k = 0; k < knowtmp.size(); k++) { + Knowtmp knowtmp1 = knowtmp.get(k); + knowIds.add(knowtmp1.getId()); + } + } + + } + + // 生成学习路径 + String str = "MATCH p = (:Know{knowId:" + knowIds.get(0) + "})-[*]->(:Know{knowId:" + knowIds.get(knowIds.size() - 1) + "}) WITH p, REDUCE(totalWeight = 0.0, r IN relationships(p) | totalWeight + r.weight) AS pathWeight " + + "RETURN nodes(p) as `p`,relationships(p) as `r`,pathWeight ORDER BY pathWeight desc;" ; + Collection> result = neo4jClient.query(str).fetch().all(); + Iterator> iterator = result.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(); + List list1 = (List) element.get("p"); + for (int i = 0; i < list1.size(); i++) { + Node node1 = (Node)list1.get(i); + Long id = node1.id(); + String name = node1.get("name").asString(); + knowVO = new KnowVO(); + knowVO.setId(id); + knowVO.setLabel(name); + knowVO.setColor(colorList[0]); + knowList.add(knowVO); + } + List list2 = (List) element.get("r"); + if (!list2.isEmpty()) { + for (int i = 0; i < list2.size(); i++) { + InternalRelationship e = (InternalRelationship) list2.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("顺序"); + } else if("dependent".equals(e.type())){ + links.setLabel("依赖"); + } + linksList.add(links); + } + } + Double pathWeight = (Double) element.get("pathWeight"); + } + BaseKnowReturn baseKnowReturn = new BaseKnowReturn(knowList, linksList); + return ResultUtils.success(baseKnowReturn); + } + @Override public BaseResponse updateLinks(List linksList) { for (int i = 0; i < linksList.size(); i++) { @@ -763,7 +832,8 @@ public class knowServiceImpl implements KnowService { } @Override - public BaseResponse getRelsNodesByCourseId(String courseId, List types) { + public BaseResponse getRelsNodesByCourseId(String courseId, Integer level,List types) { + String str = "match(n:Know)-[r:"; for (int i = 0; i < types.size(); i++) { if (i == types.size() - 1) { @@ -773,7 +843,11 @@ public class knowServiceImpl implements KnowService { } } - str = str + "]->(p:Know) where n.courseId = '" + courseId + "' return n as `n`,r as `r`,p as `p`,length(r) as `d`"; + if(level == null || level <= 0){ + str = str +"]->(p:Know) where n.courseId = '" + courseId + "' return n as `n`,r as `r`,p as `p`,length(r) as `d`"; + }else{ + str = str +"0.."+level+ "]->(p:Know) where n.courseId = '" + courseId + "' return n as `n`,r as `r`,p as `p`,length(r) as `d`"; + } System.out.println(str); Collection> all = neo4jClient.query(str).fetch().all(); @@ -812,6 +886,8 @@ public class knowServiceImpl implements KnowService { links.setLabel("包含"); } else if ("order".equals(e.type())) { links.setLabel("顺序"); + }else if("dependent".equals(e.type())){ + links.setLabel("依赖"); } linksList.add(links); } @@ -871,6 +947,8 @@ public class knowServiceImpl implements KnowService { links.setLabel("包含"); } else if ("order".equals(e.type())) { links.setLabel("顺序"); + }else if("dependent".equals(e.type())){ + links.setLabel("依赖"); } linksList.add(links); } diff --git a/src/test/java/com/teaching/TeachingBackendApplicationTests.java b/src/test/java/com/teaching/TeachingBackendApplicationTests.java index e0d611b..13f2dfd 100644 --- a/src/test/java/com/teaching/TeachingBackendApplicationTests.java +++ b/src/test/java/com/teaching/TeachingBackendApplicationTests.java @@ -8,156 +8,7 @@ import java.util.*; @SpringBootTest class TeachingBackendApplicationTests { -// Map> userLearningRecords = new HashMap<>(); -public static void main(String[] args) { + public static void main(String[] args) { -// String[] data = {"1", "2", "3", "7", "4", "1-1", "1-5", "1-4", "1-9", "1-2", "3-1", "3-2","1-3","1-6"}; - List linkedList = new ArrayList<>(); - linkedList.add("1"); - linkedList.add("1-1"); - linkedList.add("1-2"); - linkedList.add("1-3"); - linkedList.add("2"); - linkedList.add("2-1"); - linkedList.add("2-2"); - linkedList.add("2-3"); - linkedList.add("3"); - linkedList.add("3-1"); - linkedList.add("3-2"); - linkedList.add("3-3"); - linkedList.add("4"); - linkedList.add("5"); -// String x="1"; -// String x1="1011-2456"; -// String[] split = x1.split("-"); -// System.out.println(split[0]); -// System.out.println(split[1]); -// int i = x.indexOf("-"); -// System.out.println(i); - -// for (String str : data) { -// -// insertInOrder(linkedList, str); -// } - System.out.println("9999"); - System.out.println(linkedList); -// String x="2"; - String x1="2-1"; - - - int count=0,j=0; - List list = new ArrayList<>(); - - - String[] split1 = x1.split("-"); - System.out.println(String.valueOf(Integer.parseInt(split1[0])+1)); - for (int i=0;i linkedList, String newData) { -// ListIterator iterator = linkedList.listIterator(); -// -// while (iterator.hasNext()) { -// String current = iterator.next(); -// -// if (newData.compareTo(current) <= 0) { -// iterator.previous(); // 回到插入位置 -// iterator.add(newData); -// return; -// } -// } -// -// linkedList.addLast(newData); -// } -// private static void deleteEntry(LinkedList linkedList, String target) { -// ListIterator iterator = linkedList.listIterator(); -// -// while (iterator.hasNext()) { -// String current = iterator.next(); -// -// if (current.equals(target)) { -// // 获取target的前缀和序号 -// int dashIndex = target.indexOf("-"); -// if (dashIndex != -1) { -// String prefix = target.substring(0, dashIndex); -// int number = Integer.parseInt(target.substring(dashIndex + 1)); -// -// while (iterator.hasNext()) { -// String next = iterator.next(); -// if (next.startsWith(prefix + "-")) { -// // 更新数据序号 -// int nextNumber = Integer.parseInt(next.substring(dashIndex + 1)); -// String newEntry = prefix + "-" + (nextNumber - 1); -// iterator.remove(); -// iterator.add(newEntry); -// } else { -// iterator.previous(); // 回退到上一个位置 -// break; -// } -// } -// } -// } -// } -// } -// -// private static void printList(List list) { -// for (String str : list) { -// System.out.print(str + " "); -// } -// System.out.println(); -// } - }