分页查询我的课表

文章目录

概要

需求分析以及接口设计

技术细节

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

效果展示

相关推荐
小猿姐3 小时前
实测对比:哪款开源 Kubernetes MySQL Operator 最值得用?(2026 深度评测)
数据库·mysql·云原生
一灯架构5 小时前
90%的人答错!一文带你彻底搞懂ArrayList
java·后端
倔强的石头_5 小时前
从 “存得下” 到 “算得快”:工业物联网需要新一代时序数据平台
数据库
Y4090016 小时前
【多线程】线程安全(1)
java·开发语言·jvm
TDengine (老段)6 小时前
TDengine IDMP 可视化 —— 分享
大数据·数据库·人工智能·时序数据库·tdengine·涛思数据·时序数据
布局呆星6 小时前
SpringBoot 基础入门
java·spring boot·spring
风吹迎面入袖凉7 小时前
【Redis】Redisson的可重入锁原理
java·redis
GottdesKrieges7 小时前
OceanBase数据库备份配置
数据库·oceanbase
w6100104667 小时前
cka-2026-ConfigMap
java·linux·cka·configmap