|
|
@@ -85,24 +85,44 @@ public class CourseChapterController {
|
|
|
param.clear();
|
|
|
param.put("chapterIdList", chapterIdList);
|
|
|
List<Map<String,Object>> classHourList =classHourService.queryAllList(param);
|
|
|
+ // 1. 准备结果:为每个章节添加classHours字段
|
|
|
+ for (Map<String, Object> chapter : chapterList) {
|
|
|
+ if (chapter == null || chapter.get("id") == null) continue;
|
|
|
|
|
|
- // 2. 按chapterId分组课程
|
|
|
- Map<String, List<Map<String, Object>>> classHourByChapter = classHourList.stream()
|
|
|
- .filter(Objects::nonNull)
|
|
|
- .filter(classHour -> classHour.get("chapterId") != null)
|
|
|
- .collect(Collectors.groupingBy(
|
|
|
- classHour -> classHour.get("chapterId").toString()
|
|
|
- ));
|
|
|
-
|
|
|
- // 3. 为每个章节添加对应的课程列表
|
|
|
- chapterList.stream()
|
|
|
- .filter(Objects::nonNull)
|
|
|
- .filter(chapter -> chapter.get("id") != null)
|
|
|
- .forEach(chapter -> {
|
|
|
- String chapterId = chapter.get("id").toString();
|
|
|
- chapter.put("classHours",
|
|
|
- classHourByChapter.getOrDefault(chapterId, Collections.emptyList()));
|
|
|
- });
|
|
|
+ String chapterId = chapter.get("id").toString();
|
|
|
+ List<Map<String, Object>> chapterClassHours = new ArrayList<>();
|
|
|
+
|
|
|
+ // 2. 倒序遍历课程列表(关键点!)
|
|
|
+ for (int i = classHourList.size() - 1; i >= 0; i--) {
|
|
|
+ Map<String, Object> classHour = classHourList.get(i);
|
|
|
+ if (classHour == null || classHour.get("chapterId") == null) continue;
|
|
|
+
|
|
|
+ // 3. 匹配当前章节
|
|
|
+ if (chapterId.equals(classHour.get("chapterId").toString())) {
|
|
|
+ chapterClassHours.add(classHour); // 添加到章节
|
|
|
+ classHourList.remove(i); // 从原列表删除(安全操作)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ chapter.put("classHours", chapterClassHours);
|
|
|
+ }
|
|
|
+// //按chapterId分组课程
|
|
|
+// Map<String, List<Map<String, Object>>> classHourByChapter = classHourList.stream()
|
|
|
+// .filter(Objects::nonNull)
|
|
|
+// .filter(classHour -> classHour.get("chapterId") != null)
|
|
|
+// .collect(Collectors.groupingBy(
|
|
|
+// classHour -> classHour.get("chapterId").toString()
|
|
|
+// ));
|
|
|
+//
|
|
|
+// //为每个章节添加对应的课程列表
|
|
|
+// chapterList.stream()
|
|
|
+// .filter(Objects::nonNull)
|
|
|
+// .filter(chapter -> chapter.get("id") != null)
|
|
|
+// .forEach(chapter -> {
|
|
|
+// String chapterId = chapter.get("id").toString();
|
|
|
+// chapter.put("classHours",
|
|
|
+// classHourByChapter.getOrDefault(chapterId, Collections.emptyList()));
|
|
|
+// });
|
|
|
|
|
|
return CommonResult.data(chapterPage);
|
|
|
}
|
|
|
@@ -128,24 +148,44 @@ public class CourseChapterController {
|
|
|
param.clear();
|
|
|
param.put("chapterIdList", chapterIdList);
|
|
|
List<Map<String,Object>> classHourList =classHourService.queryAllList(param);
|
|
|
+ // 1. 准备结果:为每个章节添加classHours字段
|
|
|
+ for (Map<String, Object> chapter : chapterList) {
|
|
|
+ if (chapter == null || chapter.get("id") == null) continue;
|
|
|
+
|
|
|
+ String chapterId = chapter.get("id").toString();
|
|
|
+ List<Map<String, Object>> chapterClassHours = new ArrayList<>();
|
|
|
+
|
|
|
+ // 2. 倒序遍历课程列表(关键点!)
|
|
|
+ for (int i = classHourList.size() - 1; i >= 0; i--) {
|
|
|
+ Map<String, Object> classHour = classHourList.get(i);
|
|
|
+ if (classHour == null || classHour.get("chapterId") == null) continue;
|
|
|
+
|
|
|
+ // 3. 匹配当前章节
|
|
|
+ if (chapterId.equals(classHour.get("chapterId").toString())) {
|
|
|
+ chapterClassHours.add(classHour); // 添加到章节
|
|
|
+ classHourList.remove(i); // 从原列表删除(安全操作)
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- // 2. 按chapterId分组课程
|
|
|
- Map<String, List<Map<String, Object>>> classHourByChapter = classHourList.stream()
|
|
|
- .filter(Objects::nonNull)
|
|
|
- .filter(classHour -> classHour.get("chapterId") != null)
|
|
|
- .collect(Collectors.groupingBy(
|
|
|
- classHour -> classHour.get("chapterId").toString()
|
|
|
- ));
|
|
|
-
|
|
|
- // 3. 为每个章节添加对应的课程列表
|
|
|
- chapterList.stream()
|
|
|
- .filter(Objects::nonNull)
|
|
|
- .filter(chapter -> chapter.get("id") != null)
|
|
|
- .forEach(chapter -> {
|
|
|
- String chapterId = chapter.get("id").toString();
|
|
|
- chapter.put("classHours",
|
|
|
- classHourByChapter.getOrDefault(chapterId, Collections.emptyList()));
|
|
|
- });
|
|
|
+ chapter.put("classHours", chapterClassHours);
|
|
|
+ }
|
|
|
+// //按chapterId分组课程
|
|
|
+// Map<String, List<Map<String, Object>>> classHourByChapter = classHourList.stream()
|
|
|
+// .filter(Objects::nonNull)
|
|
|
+// .filter(classHour -> classHour.get("chapterId") != null)
|
|
|
+// .collect(Collectors.groupingBy(
|
|
|
+// classHour -> classHour.get("chapterId").toString()
|
|
|
+// ));
|
|
|
+//
|
|
|
+// //为每个章节添加对应的课程列表
|
|
|
+// chapterList.stream()
|
|
|
+// .filter(Objects::nonNull)
|
|
|
+// .filter(chapter -> chapter.get("id") != null)
|
|
|
+// .forEach(chapter -> {
|
|
|
+// String chapterId = chapter.get("id").toString();
|
|
|
+// chapter.put("classHours",
|
|
|
+// classHourByChapter.getOrDefault(chapterId, Collections.emptyList()));
|
|
|
+// });
|
|
|
return CommonResult.data(chapterList);
|
|
|
}
|
|
|
|