项目复习(1)

第一天

==和equals

为什么userId为2的时候判断相等可以成立,而userId是129的时候判断相等不成立呢?

这是因为userId是Long类型包装类。包装类为了提高性能,减少内存占用,采用了享元模式,提前将-128~127之间的Long包装类提前创建出来,共享使用。

因此只要大小范围在者之间的数字,只要值相同,使用的都是享元模式中提供的同一个对象。杰克的id是2,恰好在范围内;而萝丝的id是129,刚好超过了这个范围。这就导致了杰克可以删除自己订单,而萝丝无法删除的现象。

第二天

1.添加课程到课表

2.分页查询我的课程

⭐Mybatis-Plus的分页查询

java 复制代码
// select * from learning_lesson where user_id = #{userId} order by latest_learn_time
Page<LearningLesson> page = lambdaQuery()
        .eq(LearningLesson::getUserId, userId) // where user_id = #{userId}
        .page(query.toMpPage("latest_learn_time", false));

⭐list集合装成map

可以防止双重for循环取list的集合

用map可以for循环一个集合,然后集合里面直接拿map的键值

java 复制代码
//把cinfos转成map--方便查询课程名字courseName;课程封面courseCoverUrl 课程章节数量sections
Map<Long, CourseSimpleInfoDTO> cInfoMap = cinfos.stream()
    .collect(Collectors.toMap(CourseSimpleInfoDTO::getId, c -> c));

3.查询我正在学习的课程

Mybatis-Plus的自定义语句和按照什么排序

java 复制代码
//2.查询正在学习的课程---按照lastest_learn_time排序,取第一个
//select * from learning_lesson
//where user_id = #{userId} and status = 1 order by latest_learn_time limit 1
LearningLesson lesson = this.lambdaQuery()
                        .eq(LearningLesson::getUserId, userId)
                        .eq(LearningLesson::getStatus,LEARNING)
                        .orderByDesc(LearningLesson::getLatestLearnTime)
                        .last("limit 1")
                        .one();

第三天

1.创建学习计划

⭐2.查询我的学习计划

java 复制代码
查询本周学习计划总数--本周计算学习小节量
//select sum(week_frep) ad plansTotal from learning_lesson 
//where user_id = #{userId} 
//and status in(0,1) 
//and plan_status = 1
QueryWrapper<LearningLesson> wrapper = new QueryWrapper<>();
wrapper.select("sum(week_freq) as plansTotal");
wrapper.eq("user_id",userId);
wrapper.in("status", LEARNING,NOT_BEGIN);
wrapper.eq("plan_status", PLAN_RUNNING);
//Map-->key = plansTotal;value= [sum(week_freq)]---是BigDecimal类型
Map<String, Object> map = this.getMap(wrapper);

3.查询学习记录

⭐4.提交学习记录

逻辑很重要

java 复制代码
//1.获取用户id
Long userId = UserContext.getUser();
//2.判断是考试还是视频
boolean isFinished = false;//表示是否学完
if (dto.getSectionType() == SectionType.VIDEO) {
    //考试
    isFinished = isExam(userId, dto);
} else if (dto.getSectionType() == SectionType.EXAM) {
    //视频
   
    isFinished = isVideo(userId, dto);
}
//5.更新课表数据
handleLessonData(dto, isFinished);
java 复制代码
/**
 * 处理视频
 *
 * @param userId
 * @param dto
 * @return
 */
private boolean isVideo(Long userId, LearningRecordFormDTO dto) {
    //1.先判断是否是第一次学习
    LearningRecord record = this.lambdaQuery()
            .eq(LearningRecord::getUserId, userId)
            .eq(LearningRecord::getLessonId, dto.getLessonId())
            .eq(LearningRecord::getSectionId, dto.getSectionId())
            .one();
    //2.第一次学习--新增学习记录
    if (record == null) {
        //2.第一次学习--新增学习记录
        LearningRecord learningRecord = BeanUtils.copyBean(dto, LearningRecord.class);
        learningRecord.setUserId(userId);
        learningRecord.setFinished(false);
        boolean update = this.save(learningRecord);
        if (!update) {
            throw new DbException("新增学习记录失败");
        }
    }
    //3.不是第一次学习--更新学习记录--moment//finished和finishTime取决与是否第一次学完
    //3.1判断是否第一次学完
    Boolean finished = record.getFinished();//是否学完
    Integer moment = record.getMoment();//视频的当前观看时长,单位秒
    Integer duration = dto.getDuration();//视频总时长
    boolean isFinished = !finished && moment * 2 > duration;//是否第一次学完
    boolean update = this.lambdaUpdate()
                .set(LearningRecord::getMoment, dto.getMoment())
                .set(isFinished, LearningRecord::getFinished, isFinished)
                .set(isFinished, LearningRecord::getFinishTime, dto.getCommitTime())
                .eq(LearningRecord::getUserId, userId)
                .eq(LearningRecord::getLessonId, dto.getLessonId())
                .eq(LearningRecord::getSectionId, dto.getSectionId())
                .update();
    if (!update) {
        throw new DbException("更新学习记录失败");
    }
    return isFinished;
}
java 复制代码
/**
 * 处理考试--是考试默认学完
 *
 * @param userId
 * @param dto
 * @return
 */
public boolean isExam(Long userId, LearningRecordFormDTO dto) {
    //3.1是考试---新增学习记录---learning_record表
    LearningRecord learningRecord = BeanUtils.copyBean(dto, LearningRecord.class);
    learningRecord.setUserId(userId);
    learningRecord.setFinished(true);
    learningRecord.setFinishTime(dto.getCommitTime());
    boolean update = this.save(learningRecord);
    if (!update) {
        throw new DbException("新增学习记录失败");
    }
    return true;
}
java 复制代码
/**
 * 课表数据处理
 * @param dto
 * @param isFinished
 */
private void handleLessonData(LearningRecordFormDTO dto, boolean isFinished) {
    //查询课表
    LearningLesson lesson = lessonService.getById(dto.getLessonId());
    if(lesson==null){
        throw new BadRequestException("课表不存在");
    }
    //1.判断是否第一次学完
    boolean allFinished = false;
    if(isFinished){
        //2远程调用课程服务得到课程消息--小节总数
        CourseFullInfoDTO cinfo = courseClient.getCourseInfoById(lesson.getCourseId(), false, false);
        if(cinfo==null){
            throw new BadRequestException("课程不存在");
        }
        Integer sectionNum = cinfo.getSectionNum();//课程总小节数
        //判断是否全部学完
        Integer learnedSections = lesson.getLearnedSections();//已学完小节数量
        allFinished = learnedSections +1 >= sectionNum;
    }
    //3.1更新课表
    lessonService.lambdaUpdate()
            .set(lesson.getStatus()==LessonStatus.NOT_BEGIN, LearningLesson::getPlanStatus, LessonStatus.LEARNING)
            .set(allFinished,  LearningLesson::getStatus, LessonStatus.FINISHED)
            .set(LearningLesson::getLatestSectionId, dto.getSectionId())
            .set(LearningLesson::getLatestLearnTime, dto.getCommitTime())
            .set(allFinished,LearningLesson::getLearnedSections, lesson.getLearnedSections()+1)
            .update();

}

5.定时任务

1.首先先在启动类加上

@EnableScheduling//开启定时任务

2.方法上加上

java 复制代码
@Scheduled(cron = "0 0/1 * * * ?")//每分钟
public void check() {
    log.info("开始检查课程状态");

}
相关推荐
云动雨颤17 分钟前
Java并发性能优化|读写锁与互斥锁解析
java
ldj202030 分钟前
Centos 安装Jenkins
java·linux
hqxstudying37 分钟前
Intellij IDEA中Maven的使用
java·maven·intellij-idea
SimonKing40 分钟前
拯救大文件上传:一文彻底彻底搞懂秒传、断点续传以及分片上传
java·后端·架构
深栈解码40 分钟前
JUC并发编程 内存布局和对象头
java·后端
北方有星辰zz1 小时前
数据结构:栈
java·开发语言·数据结构
Seven971 小时前
一个static关键字引发的线上故障:深度剖析静态变量与配置热更新的陷阱
java
山野万里__1 小时前
C++与Java内存共享技术:跨平台与跨语言实现指南
android·java·c++·笔记
风象南1 小时前
Spring Shell命令行工具开发实战
java·spring boot·后端
Java技术小馆1 小时前
POST为什么发送两次请求
java·面试·架构