分页查询我的课表

文章目录

概要

需求分析以及接口设计

技术细节

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

效果展示

相关推荐
IT小崔8 小时前
SqlSugar 使用教程
数据库·后端
架构师沉默8 小时前
为什么国外程序员都写独立博客,而国内都在公众号?
java·后端·架构
带刺的坐椅8 小时前
SolonCode v2026.4.1 发布(比 ClaudeCode 简约的编程智能体)
java·ai·llm·agent·solon-ai·claudecode·soloncode
殷紫川8 小时前
从单体到亿级流量:登录功能全场景设计指南,踩过的坑全给你填平了
java
Filwaod8 小时前
Cursor+IDEA开发问题
java·idea·cursor
GIS阵地8 小时前
QgsProviderMetadata 详解(基于 QGIS 3.40.13 API)
数据库·qt·arcgis·oracle·gis·开源软件·qgis
qq_366086228 小时前
sql server OUTER APPLY使用
数据库·sql·mysql
Sunshine for you8 小时前
使用Flask快速搭建轻量级Web应用
jvm·数据库·python
爱丽_8 小时前
Spring 事务:传播行为、失效场景、回滚规则与最佳实践
java·后端·spring
qwehjk20088 小时前
如何从Python初学者进阶为专家?
jvm·数据库·python