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

}
相关推荐
evans在进步17 分钟前
Spring AI 从入门到实战:用 Java 实现大模型对话与 Tool Calling
java·人工智能·spring
evans在进步23 分钟前
LeetCode 189 轮转数组:三次反转原地解决,图解 Java 实现
java·算法·leetcode
风123456789~1 小时前
【Oracle专栏】ORA-02069: global_names 参数
数据库·oracle
ningmengjing_1 小时前
Redis 从入门到实战:Python操作全攻略
数据库·redis·python
我命由我123451 小时前
Jetpack Compose - MaterialExpressiveTheme 与 MaterialTheme、ColorScheme
android·java·开发语言·java-ee·kotlin·android jetpack·android runtime
晚安code2 小时前
Java 异常与日志规范:错误码、异常处理、日志规约一次讲清
java
深海呐2 小时前
仓颉语言是ArkTs的上层语言吗?就像kotlin和Java
java·开发语言·kotlin·仓颉
隔窗听雨眠2 小时前
OceanBase旁路导入与自增主键冲突深度解析:原理、排查与解决方案
java·服务器·数据库
ZCBUS实时计算2 小时前
政务海量分库分表汇聚实战:基于 3 节点 ZCBUS 集群完成医保 10TB 数据实时整合
大数据·数据库·数据仓库·sql·dba·etl·政务
名不经传的养虾人2 小时前
从0到1:企业级AI项目迭代日记 Vol.81|工具调用前,先过一道审批门
数据库·人工智能·ai编程·ai工作流·企业ai