分页查询我的课表

文章目录

概要

需求分析以及接口设计

技术细节

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

效果展示

相关推荐
.Cnn27 分钟前
Redis基础day02
java·数据库·redis
能有时光29 分钟前
[MAF的Agent管道详解-07]利用AIAgent中间件构建Agent管道
java·数据库
什巳31 分钟前
JAVA练习277- 找到字符串中所有字母异位
java·开发语言·算法·leetcode
迷枫71236 分钟前
简单sql与dm功能实验记录
数据库·sql·oracle
我登哥MVP41 分钟前
HDFS硬核拆解-读写Pipeline与Java实战
java·hadoop·hdfs·云原生·云计算
小则又沐风a1 小时前
深入理解文件系统(二)
java·服务器·前端
jojo甜在心1 小时前
(开源)宠物商城系统 全栈开发实录 采用 Java + SpringBoot + Vue + MySQL
java·开源·宠物
蚁库1 小时前
Oracle 19c 文件系统安装实战教程:从规划到日常管理完整流程
数据库·oracle
枫叶丹41 小时前
Loop Engineering:从提示词到循环系统
数据库·redis·缓存·loop
zhanghaofaowhrql1 小时前
Spring Data JPA Repository 详解:从基础到高级用法
java·数据库·sql