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