文章目录
概要
需求分析以及接口设计
技术细节
1.Controller层:
java
private final ILearningLessonService iLearningLessonService;
/**
* 分页查询我的课表
* @param pageQuery
* @return
*/
@ApiOperation("分页查询我的课表")
@GetMapping("/page")
public PageDTO<LearningLessonVO> queryMyLessons(PageQuery pageQuery){
return iLearningLessonService.queryMyLessons(pageQuery);
}
2.Service层:
java
/**
* 分页查询我的课表
* @param pageQuery
* @return
*/
public PageDTO<LearningLessonVO> queryMyLessons(PageQuery pageQuery) {
//1.获取用户id
Long userId = UserContext.getUser();
//2.分页查询课程信息
Page<LearningLesson> page = lambdaQuery()
.eq(LearningLesson::getUserId, userId)
.page(pageQuery.toMpPage("latest_learn_time", false));
List<LearningLesson> records = page.getRecords();
if (CollUtils.isEmpty(records)){
return PageDTO.empty(page);
}
//3.将po封装成vo
//3.1远程调用课程服务查询课程信息,获取剩余字段
Set<Long> ids = records.stream().map(LearningLesson::getCourseId).collect(Collectors.toSet());
List<CourseSimpleInfoDTO> courseInfoList = courseClient.getSimpleInfoList(ids);
if (courseInfoList.isEmpty()){
throw new BizIllegalException("课程不存在");
}
//将课程信息转为map,以便循环时一一对应课表的信息
Map<Long, CourseSimpleInfoDTO> cInfoDTOMap = courseInfoList.stream().collect(Collectors.toMap(CourseSimpleInfoDTO::getId, c -> c));
ArrayList<LearningLessonVO> learningLessonVOS = new ArrayList<>();
for (LearningLesson lesson : records) {
//3.2遍历课表循环每个课程进行封装
LearningLessonVO learningLessonVO = BeanUtils.copyBean(lesson, LearningLessonVO.class);
//3.3填充剩余字段
//通过key(课程id)获取到该课程的详细信息
CourseSimpleInfoDTO courseSimpleInfoDTO = cInfoDTOMap.get(lesson.getCourseId());
learningLessonVO.setCourseName(courseSimpleInfoDTO.getName());
learningLessonVO.setCourseCoverUrl(courseSimpleInfoDTO.getCoverUrl());
learningLessonVO.setSections(courseSimpleInfoDTO.getSectionNum());
learningLessonVOS.add(learningLessonVO);
}
//4.返回
return PageDTO.of(page,learningLessonVOS);
}