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

}
相关推荐
金仓拾光集7 小时前
金仓替代MongoDB:安全与性能协同提升——社交用户画像系统的国产化实践
数据库·安全·mongodb·kingbase·kingbasees·数据库平替用金仓·金仓数据库
FinTech老王7 小时前
国产数据库替换MongoDB实战:浙江人民医院电子病历系统国产化升级案例
数据库·mongodb
l1t7 小时前
在Lua用luasql-sqlite3库访问SQLite数据库
数据库·git·sqlite·lua
无敌最俊朗@7 小时前
SQLite 约束 (Constraints) 面试核心知识点
java·开发语言·jvm
2501_938780288 小时前
《轨道交通检测系统中 Qt 与数据库交互的优化方案》
数据库·qt·交互
qqxhb8 小时前
系统架构设计师备考第61天——嵌入式系统架构模式&操作系统&数据库&中间件
数据库·中间件·系统架构·sqlite·dds·层次化(封闭/开放)·递归模式
憨憨崽&8 小时前
C语言、Java、Python 的选择与未来发展以及学习路线
java·c语言·python
SelectDB8 小时前
Apache Doris 数据导入原理与性能优化 | Deep Dive
运维·数据库·数据分析
在坚持一下我可没意见8 小时前
Java 网络编程:TCP 与 UDP 的「通信江湖」(基于UDP回显服务器)
java·服务器·开发语言·tcp/ip·udp·java-ee
少爷晚安。8 小时前
Java零基础学习完整笔记,基于Intellij IDEA开发工具,笔记持续更新中
java·笔记·学习