项目复习(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("开始检查课程状态");

}
相关推荐
Tisfy1 小时前
LeetCode 1927.求和游戏:抵消+看最值
java·leetcode·游戏·题解·博弈论
Elastic 中国社区官方博客5 小时前
搜索倍增器:推动收入、生产力和 AI 实现规模化
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
保定公民7 小时前
达梦数据库存储过程中的数组类型详解:基础数组与记录数组的差异化应用
数据库·达梦·存储过程·达梦数据库·dm8·dm
秋饼8 小时前
JDK 27 企业级 AI 服务落地实战:G1 默认、紧凑对象头、后量子 TLS 与 JFR 脱敏全解析
java·ai·技术分享·后端开发
梦Arrebol8 小时前
Redis 内容及相关实验
数据库·redis
卓怡学长8 小时前
w176基于SpringBoot的医院管理系统
java·spring boot·spring·maven·intellij-idea
lemon_sjdk8 小时前
ObjectProperty
java·开发语言·算法
大模型码小白8 小时前
Spring AI Tool 实现自然语言操作 MySQL 数据库详解
服务器·开发语言·数据库·人工智能·python·mysql·spring
hh9508 小时前
Agent Plan x DeepSeek Harness:Agent 供应链安全与第三方插件审计
java·开发语言·安全·agent plan·adg成都社区·adg社区
吴声子夜歌8 小时前
Java面试——HBase原理及应用
java·面试·hbase