天机学堂—学习辅助功能(含场景问答和作业)

我的课表

需求分析

原型图

管理后台

用户端

流程图

数据设计

接口设计

  1. 支付成功报名课程后, 加入到我的课表(MQ)
  2. 分页查询我的课表
  3. 查询我正在学习的课程
  4. 根据id查询指定课程的学习状态
  5. 删除课表中的某课程

代码实现

数据表设计

添加课程到课表(非标准接口)

需求:用户购买/报名课程后,交易服务会通过MQ通知学习服务,学习服务将课程加入用户课表中

接口设计

首先写Listener,定义好RabbitMQ,对DTO数据校验后调用lessonService

@Component
@RequiredArgsConstructor
@Slf4j
public class LessonChangeListener {private final ILearningLessonService lessonService;@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "learning.lesson.pay.queue", durable = "true"),exchange = @Exchange(name = MqConstants.Exchange.ORDER_EXCHANGE, type = ExchangeTypes.TOPIC),key = MqConstants.Key.ORDER_PAY_KEY))public void listenLessonPay(OrderBasicDTO dto) {//1.非空判断if(dto==null || dto.getOrderId()==null || CollUtils.isEmpty(dto.getCourseIds())){log.error("接收到MQ的消息有误,订单数据为空");return;}//2.调用业务lessonService.addLesson(dto);}}
@Service
@RequiredArgsConstructor
public class LearningLessonServiceImpl extends ServiceImpl<LearningLessonMapper, LearningLesson> implements ILearningLessonService {private final CourseClient courseClient;@Overridepublic void addLesson(OrderBasicDTO dto) {//1.远程调用tj-course微服务,根据课程id查询出课程信息(课程有效期)List<Long> courseIds = dto.getCourseIds();List<CourseSimpleInfoDTO> courseList = courseClient.getSimpleInfoList(courseIds);if (CollUtils.isEmpty(courseList)) {throw new BadRequestException("课程不存在");}//2.遍历课程信息,封装LearningLesson(用户id,课程id,过期时间)List<LearningLesson> lessonList = new ArrayList<>();for (CourseSimpleInfoDTO course : courseList) {LearningLesson lesson = new LearningLesson();//2.1 填充userId和courseIdlesson.setUserId(dto.getUserId());lesson.setCourseId(course.getId());//2.2 算出过期时间lesson.setExpireTime(LocalDateTime.now().plusMonths(course.getValidDuration())); lessonList.add(lesson);}//3.批量保存saveBatch(lessonList);}
}

Q1: 过期时间怎么计算?

加入课表的时间(LocalDateTime.now())+课程有效期

Q2: 课程信息是怎么获得的?

远程调用课程微服务tj-course,根据课程id查询课程信息

Q3: 当前添加课表的业务是怎么保证幂等性?

当前业务靠MySQL,在DDL中把(user id ,course id)设置唯一约束

通用做法:在DTO中找个唯一标识,判断Redis里面是否有

分页查询我的课表

需求: 在个人中心-我的课程页面,可以分页查询当前用户的课表及学习状态信息。

接口设计

分页用到的类如下

PageQuery

public class PageQuery {public static final Integer DEFAULT_PAGE_SIZE = 20;public static final Integer DEFAULT_PAGE_NUM = 1;@ApiModelProperty(value = "页码", example = "1")@Min(value = 1, message = "页码不能小于1")private Integer pageNo = DEFAULT_PAGE_NUM;@ApiModelProperty(value = "每页大小", example = "5")@Min(value = 1, message = "每页查询数量不能小于1")private Integer pageSize = DEFAULT_PAGE_SIZE;@ApiModelProperty(value = "是否升序", example = "true")private Boolean isAsc = true;@ApiModelProperty(value = "排序字段", example = "id")private String sortBy;
}

返回的分页结果PageDTO

public class PageDTO<T> {@ApiModelProperty("总条数")protected Long total;@ApiModelProperty("总页码数")protected Long pages;@ApiModelProperty("当前页数据")protected List<T> list;
}

接口

@RestController
@RequestMapping("/lessons")
@Api(tags = "我的课表相关的接口")
@RequiredArgsConstructor
public class LearningLessonController {private final ILearningLessonService lessonService;@GetMapping("/page")@ApiOperation("分页查询我的课表")public PageDTO<LearningLessonVO> queryMyLesson(@Validated PageQuery query){return lessonService.queryMyLesson(query);}}
    /*** 分页查询我的课表** @param query* @return*/@Overridepublic PageDTO<LearningLessonVO> queryMyLesson(PageQuery query) {// 1. 分页查询出当前用户的课表信息Long userId = UserContext.getUser();Page<LearningLesson> pageResult = lambdaQuery().eq(LearningLesson::getUserId, userId).page(query.toMpPageDefaultSortByCreateTimeDesc());List<LearningLesson> lessonList = pageResult.getRecords();if (CollUtils.isEmpty(lessonList)) {return PageDTO.empty(pageResult);}// 2. 根据courseId查询出课程信息List<Long> cIds = lessonList.stream().map(LearningLesson::getCourseId).collect(Collectors.toList());List<CourseSimpleInfoDTO> courseList = courseClient.getSimpleInfoList(cIds);if (CollUtils.isEmpty(courseList)) {throw new BadRequestException("课程信息不存在");}Map<Long, CourseSimpleInfoDTO> courseMap = courseList.stream().collect(Collectors.toMap(CourseSimpleInfoDTO::getId, c -> c));// 3. 遍历课表List,封装LearningLessonVOList<LearningLessonVO> voList = new ArrayList<>();for (LearningLesson lesson : lessonList) {LearningLessonVO vo = BeanUtils.copyBean(lesson, LearningLessonVO.class);//封装课程信息CourseSimpleInfoDTO course = courseMap.get(lesson.getCourseId());if (course!=null){vo.setCourseName(course.getName());vo.setCourseCoverUrl(course.getCoverUrl());vo.setSections(course.getSectionNum());}voList.add(vo);}// 4. 封装PageDTOreturn PageDTO.of(pageResult,voList);}

Q1: 为什么把courseList转成了courseMap

Map中Key是课程id,Value是课程对象,从而可以通过课程id拿到课程对象                                                            

查询最近正在学习的课程

需求: 在首页、个人中心-课程表页,需要查询并展示当前用户最近一次学习的课程

接口设计

这次代码是个标准的三层都写案例了

@GetMapping("/now")
@ApiOperation("查询当前用户正在学习的课程")
public LearningLessonVO queryCurrent() {return lessonService.queryCurrent();
}
    /*** 查询当前用户正在学习的课程** @return*/@Overridepublic LearningLessonVO queryCurrent() {//1.获得当前用户IdLong userId = UserContext.getUser();//2.查询课表,当前用户正在学习的课程 SELECT * FROM   learning_lesson WHERE user_id =2 AND status  = 1 ORDER BY latest_learn_time  DESC  limit 0,1;LearningLesson lesson = getBaseMapper().queryCurrent(userId);if(lesson == null){return null;}LearningLessonVO vo = BeanUtils.copyBean(lesson, LearningLessonVO.class);//3.根据课程id查询出课程信息CourseFullInfoDTO course = courseClient.getCourseInfoById(lesson.getCourseId(), false, false);if(course == null){throw new BadRequestException("课程不存在");}vo.setCourseName(course.getName());vo.setCourseCoverUrl(course.getCoverUrl());vo.setSections(course.getSectionNum());//4.统计课程中的课程Integer courseAmount = lambdaQuery().eq(LearningLesson::getUserId, userId).count();vo.setCourseAmount(courseAmount);//5.根据最近学习的章节id查询章节信息List<CataSimpleInfoDTO> catalogueList = catalogueClient.batchQueryCatalogue(List.of(lesson.getLatestSectionId()));if(!CollUtils.isEmpty(catalogueList)){CataSimpleInfoDTO cata = catalogueList.get(0);vo.setLatestSectionIndex(cata.getCIndex());vo.setLatestSectionName(cata.getName());}return vo;}
public interface LearningLessonMapper extends BaseMapper<LearningLesson> {@Select("SELECT * FROM learning_lesson WHERE user_id = #{userId} AND status=1 ORDER BY latest_learn_time DESC limit 0,1")LearningLesson queryCurrent(@Param("userId") Long userId);
}

根据id查询指定课程的学习状态

需求: 在课程详情页需要查询用户是否购买了指定课程,如果购买了则要返回学习状态信息

接口设计

代码最简单的一集

@GetMapping("/{courseId}")
@ApiOperation("根据课程id查询课程状态")
public LearningLessonVO queryByCourseId(@PathVariable("courseId") Long courseId) {return lessonService.queryByCourseId(courseId);
}
    /*** 根据课程id查询出课程状态** @param courseId* @return*/@Overridepublic LearningLessonVO queryByCourseId(Long courseId) {// 1. 根据用户id和课程id查询出课表LearningLessonLearningLesson lesson = lambdaQuery().eq(LearningLesson::getUserId, UserContext.getUser()).eq(LearningLesson::getCourseId, courseId).one();if (lesson == null) {return null;}// 2. 根据课程id查询出课程信息CourseFullInfoDTO course = courseClient.getCourseInfoById(courseId, false, false);if(course==null){throw new BadRequestException("课程不存在");}// 3. 封装voLearningLessonVO vo = BeanUtils.copyBean(lesson, LearningLessonVO.class);vo.setCourseName(course.getName());vo.setCourseCoverUrl(course.getCoverUrl());vo.setSections(course.getSectionNum());return vo;}

Q1: 为什么不能直接使用courseId查询课表?

还需要userid的约束

检查课程是否有效(作业)

这是一个微服务内部接口,当用户学习课程时,可能需要播放课程视频。此时提供视频播放功能的媒资系统就需要校验用户是否有播放视频的资格。所以,开发媒资服务(tj-media)的同事就请你提供这样一个接口。

    @ApiOperation("校验当前课程是否已经报名")@GetMapping("/{courseId}/valid")public Long isLessonValid(@ApiParam(value = "课程id" ,example = "1") @PathVariable("courseId") Long courseId){return lessonService.isLessonValid(courseId);}
    @Overridepublic Long isLessonValid(Long courseId) {// 1.获取登录用户Long userId = UserContext.getUser();if (userId == null) {return null;}// 2.查询课程LearningLesson lesson = lambdaQuery().eq(LearningLesson::getUserId, UserContext.getUser()).eq(LearningLesson::getCourseId, courseId).one();if (lesson == null) {return null;}return lesson.getId();}

删除课表中课程(作业)

删除课表中的课程有两种场景:

  • 用户直接删除已失效的课程
  • 用户退款后触发课表自动删除
    @DeleteMapping("/{courseId}")@ApiOperation("删除指定课程信息")public void deleteCourseFromLesson(@ApiParam(value = "课程id" ,example = "1") @PathVariable("courseId") Long courseId) {lessonService.deleteCourseFromLesson(null, courseId);}
@Overridepublic void deleteCourseFromLesson(Long userId, Long courseId) {// 1.获取当前登录用户if (userId == null) {userId = UserContext.getUser();}// 2.删除课程remove(buildUserIdAndCourseIdWrapper(userId, courseId));}private LambdaQueryWrapper<LearningLesson> buildUserIdAndCourseIdWrapper(Long userId, Long courseId) {LambdaQueryWrapper<LearningLesson> queryWrapper = new QueryWrapper<LearningLesson>().lambda().eq(LearningLesson::getUserId, userId).eq(LearningLesson::getCourseId, courseId);return queryWrapper;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/690852.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【深耕 Python】Quantum Computing 量子计算机(5)量子物理概念(二)

写在前面 往期量子计算机博客&#xff1a; 【深耕 Python】Quantum Computing 量子计算机&#xff08;1&#xff09;图像绘制基础 【深耕 Python】Quantum Computing 量子计算机&#xff08;2&#xff09;绘制电子运动平面波 【深耕 Python】Quantum Computing 量子计算机&…

数据增强,迁移学习,Resnet分类实战

目录 1. 数据增强&#xff08;Data Augmentation&#xff09; 2. 迁移学习 3. 模型保存 4. 102种类花分类实战 1. 数据集 2.导入包 3. 数据读取与预处理操作 4. Datasets制作输入数据 5.将标签的名字读出 6.展示原始数据 7.加载models中提供的模型 8.初始化…

德国Dürr杜尔机器人维修技巧分析

在工业生产中&#xff0c;杜尔工业机器人因其高效、精准和稳定性而备受青睐。然而&#xff0c;即便是最精良的设备&#xff0c;也难免会出现Drr机械手故障。 一、传感器故障 1. 视觉传感器故障&#xff1a;可能导致机器人无法正确识别目标物&#xff0c;影响工作效率。解决方法…

力扣每日一题124:二叉树中的最大路径和

题目 困难 二叉树中的 路径 被定义为一条节点序列&#xff0c;序列中每对相邻节点之间都存在一条边。同一个节点在一条路径序列中 至多出现一次 。该路径 至少包含一个 节点&#xff0c;且不一定经过根节点。 路径和 是路径中各节点值的总和。 给你一个二叉树的根节点 root…

Apache ECharts

Apache ECharts介绍&#xff1a; Apache ECharts 是一款基于 Javascript 的数据可视化图表库&#xff0c;提供直观&#xff0c;生动&#xff0c;可交互&#xff0c;可个性化定制的数据可视化图表。 官网地址&#xff1a;https://echarts.apache.org/zh/index.html Apache ECh…

基于STC12C5A60S2系列1T 8051单片机实现一主单片机与一从单片机进行双向串口通信功能

基于STC12C5A60S2系列1T 8051单片机实现一主单片机与一从单片机进行双向串口通信功能 STC12C5A60S2系列1T 8051单片机管脚图STC12C5A60S2系列1T 8051单片机串口通信介绍STC12C5A60S2系列1T 8051单片机串口通信的结构基于STC12C5A60S2系列1T 8051单片机串口通信的特殊功能寄存器…

Django项目运行报错:ModuleNotFoundError: No module named ‘MySQLdb‘

解决方法&#xff1a; 在__init__.py文件下&#xff0c;新增下面这段代码 import pymysql pymysql.install_as_MySQLdb() 注意&#xff1a;确保你的 python 有下载 pymysql 库&#xff0c;没有的话可以使用 pip install pymysql安装 原理&#xff1a;用pymysql来代替mysqlL…

深度学习:基于人工神经网络ANN的降雨预测

前言 系列专栏:【深度学习&#xff1a;算法项目实战】✨︎ 本专栏涉及创建深度学习模型、处理非结构化数据以及指导复杂的模型&#xff0c;如卷积神经网络(CNN)、递归神经网络 (RNN)&#xff0c;包括长短期记忆 (LSTM) 、门控循环单元 (GRU)、自动编码器 (AE)、受限玻尔兹曼机(…

docker搭建redis6.0(docker rundocker compose演示)

文章讲了&#xff1a;docker下搭建redis6.0.20遇到一些问题&#xff0c;以及解决后的最佳实践方案 文章实现了&#xff1a; docker run搭建redisdocker compose搭建redis 搭建一个redis’的过程中遇到很多问题&#xff0c;先简单说一下搭建的顺序 找一个redis.conf文件&…

LangChain:大模型框架的深度解析与应用探索

在数字化的时代浪潮中&#xff0c;人工智能技术正以前所未有的速度蓬勃发展&#xff0c;而大模型作为其中的翘楚&#xff0c;以生成式对话技术逐渐成为推动行业乃至整个社会进步的核心力量。再往近一点来说&#xff0c;在公司&#xff0c;不少产品都戴上了人工智能的帽子&#…

maven mirrorOf的作用

在工作中遇到了一个问题导致依赖下载不了&#xff0c;最后发现是mirror的问题&#xff0c;决定好好去看一下mirror的配置&#xff0c;以及mirrorOf的作用&#xff0c;以前都是直接复制过来使用&#xff0c;看了之后才明白什么意思。 过程 如果你设置了镜像&#xff0c;镜像会匹…

Doris【部署 01】Linux部署MPP数据库Doris稳定版(下载+安装+连接+测试)

本次安装测试的为稳定版2.0.8官方文档 https://doris.apache.org/zh-CN/docs/2.0/get-starting/quick-start 这个简短的指南将告诉你如何下载 Doris 最新稳定版本&#xff0c;在单节点上安装并运行它&#xff0c;包括创建数据库、数据表、导入数据及查询等。 Linux部署稳定版Do…