fuliqi
2024-07-04 c0792dcbc27d0580d9ed0e7aa3cf34c96c651840
src/main/java/com/ycl/jxkg/service/impl/EducationResourceServiceImpl.java
@@ -1,20 +1,22 @@
package com.ycl.jxkg.service.impl;
import com.alibaba.fastjson.JSON;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycl.jxkg.base.Result;
import com.ycl.jxkg.context.WebContext;
import com.ycl.jxkg.domain.entity.EducationResource;
import com.ycl.jxkg.domain.entity.Subject;
import com.ycl.jxkg.domain.entity.StudyRecord;
import com.ycl.jxkg.domain.vo.admin.education.EducationResourceVO;
import com.ycl.jxkg.domain.vo.student.education.StudentOnlineVO;
import com.ycl.jxkg.mapper.ClassesUserMapper;
import com.ycl.jxkg.mapper.EducationResourceMapper;
import com.ycl.jxkg.mapper.SubjectMapper;
import com.ycl.jxkg.service.EducationResourceService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
@@ -35,7 +37,8 @@
    private final SubjectMapper subjectMapper;
    private final WebContext webContext;
    private final ClassesUserMapper classesUserMapper;
    @Autowired
    private Cache<String, Object> caffeineCache;
    @Override
    public Result add(EducationResourceVO form) {
        EducationResource educationResource = new EducationResource();
@@ -109,4 +112,27 @@
        List<Subject> subjects = subjectMapper.allSubject();
        return Result.ok(subjects);
    }
    //记录视频时长
    @Override
    public Result recordTime(Integer userId) {
        if (userId ==null){
            throw new RuntimeException("用户id为空");
        }
        StudyRecord studyRecord = (StudyRecord) caffeineCache.getIfPresent("STUDENT_"+userId);
        if (studyRecord != null) {
            //存在缓存
            Date lastTime = studyRecord.getLastTime();
            Date now = new Date();
            studyRecord.setStudyTime(studyRecord.getStudyTime()+(now.getTime()-lastTime.getTime())/1000);
            studyRecord.setLastTime(now);
        }else {
            //不存在缓存
            studyRecord = new StudyRecord();
            studyRecord.setStudentId(userId);
            studyRecord.setStudyTime(0L);
            studyRecord.setLastTime(new Date());
        }
        caffeineCache.put("STUDENT_" + userId, studyRecord);
        return Result.ok();
    }
}