goupan
2024-04-03 5506e9a45e717ffcb67ec313b5a4e8206d9b3a39
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package cn.stylefeng.guns.modular.business.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.business.entity.CourseChapter;
import cn.stylefeng.guns.modular.business.mapper.CourseChapterMapper;
import cn.stylefeng.guns.modular.business.service.ICourseChapterService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 章节管理 服务实现类
 * </p>
 *
 * @author goupan
 * @since 2024-01-01
 */
@Service
public class CourseChapterServiceImpl extends ServiceImpl<CourseChapterMapper, CourseChapter> implements ICourseChapterService {
 
    /**
     * 根据课程id获取章节层次信息
     * @param courseId 课程id
     * @return
     */
    @Override
    public List<CourseChapter> getAllByCourseId(Long courseId) {
        List<CourseChapter> courseChapterList = this.list(new LambdaQueryWrapper<CourseChapter>().eq(CourseChapter::getCourseId,courseId).eq(CourseChapter::getIsDelete,false)
                .eq(CourseChapter::getParentId,0L));
        //获取一级章节目录
        if (CollectionUtil.isNotEmpty(courseChapterList)) {
            //获取所有子集章节信息
            List<CourseChapter> courseChapterChildList = this.list(new LambdaQueryWrapper<CourseChapter>().eq(CourseChapter::getCourseId,courseId).eq(CourseChapter::getIsDelete,false)
                    .ne(CourseChapter::getParentId,0L));
            if (CollectionUtil.isNotEmpty(courseChapterChildList)){
                Map<Long,List<CourseChapter>> courseChapterMap = new HashMap<>();
                //处理课程下面非一级的章节内容,形成统一map
                courseChapterChildList.stream().forEach(courseChapter -> {
                    if (ObjectUtil.isNotEmpty(courseChapterMap.get(courseChapter.getParentId()))){
                        List<CourseChapter> courseChapterList1 = courseChapterMap.get(courseChapter.getParentId());
                        courseChapterList1.add(courseChapter);
                    }else{
                        List<CourseChapter> childCourseChapList = new ArrayList<>();
                        childCourseChapList.add(courseChapter);
                        courseChapterMap.put(courseChapter.getParentId(),childCourseChapList);
                    }
                });
 
                if (ObjectUtil.isNotEmpty(courseChapterMap)){
                  return this.getChildCourseChapter(courseChapterMap,courseChapterList);
                }
            }
 
        }
        return courseChapterList;
    }
 
    /**
     * 处理层次章节信息
     * @param courseChapterMap 子集章节map信息
     * @param courseChapterList 高层级章节信息
     * @return
     */
    private List<CourseChapter> getChildCourseChapter(Map<Long,List<CourseChapter>> courseChapterMap,List<CourseChapter> courseChapterList){
        if (ObjectUtil.isNotEmpty(courseChapterMap)){
            courseChapterList.forEach(courseChapter -> {
                List<CourseChapter> childCourseChapterList = courseChapterMap.get(courseChapter.getId());
                if (ObjectUtil.isNotEmpty(childCourseChapterList)){
                    childCourseChapterList = this.getChildCourseChapter(courseChapterMap,childCourseChapterList);
                    courseChapter.setChildList(childCourseChapterList);
                }
 
            });
        }
        return courseChapterList;
    }
}