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

}
相关推荐
勤奋的知更鸟6 分钟前
Java编程之原型模式
java·开发语言·原型模式
叶 落16 分钟前
[Java 基础]数组
java·java 基础
KK溜了溜了17 分钟前
JAVA-springboot log日志
java·spring boot·logback
珂朵莉MM19 分钟前
2021 RoboCom 世界机器人开发者大赛-高职组(初赛)解题报告 | 珂学家
java·开发语言·人工智能·算法·职场和发展·机器人
香蕉炒肉34 分钟前
Java优化:双重for循环
java·开发语言
超级小忍36 分钟前
如何配置 MySQL 允许远程连接
数据库·mysql·adb
吹牛不交税1 小时前
sqlsugar WhereIF条件的大于等于和等于查出来的坑
数据库·mysql
黄雪超1 小时前
JVM——打开JVM后门的钥匙:反射机制
java·开发语言·jvm
有梦想的攻城狮1 小时前
spring中的@RabbitListener注解详解
java·后端·spring·rabbitlistener
李斯维1 小时前
循序渐进 Android Binder(二):传递自定义对象和 AIDL 回调
android·java·android studio