fuliqi
2024-10-17 8546b3d285af4235a0ef615a0c6e89486ae2c806
src/main/java/com/ycl/jxkg/service/impl/EducationResourceServiceImpl.java
@@ -5,6 +5,7 @@
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycl.jxkg.base.Result;
import com.ycl.jxkg.constants.CaffeineConstant;
import com.ycl.jxkg.context.WebContext;
import com.ycl.jxkg.domain.entity.EducationResource;
import com.ycl.jxkg.domain.entity.Subject;
@@ -15,15 +16,14 @@
import com.ycl.jxkg.mapper.SubjectMapper;
import com.ycl.jxkg.service.EducationResourceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.*;
/**
 * @author:flq
@@ -31,7 +31,9 @@
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class EducationResourceServiceImpl implements EducationResourceService {
    private final EducationResourceMapper mapper;
    private final SubjectMapper subjectMapper;
@@ -39,6 +41,9 @@
    private final ClassesUserMapper classesUserMapper;
    @Autowired
    private Cache<String, Object> caffeineCache;
    @Value("${spring.config.url}")
    private String url;
    @Override
    public Result add(EducationResourceVO form) {
        EducationResource educationResource = new EducationResource();
@@ -84,6 +89,7 @@
        page.getList().stream().forEach(item -> {
            item.setContentUrl(JSON.parseObject(item.getContentUrlString(), EducationResourceVO.UploadFile.class));
            item.setAttachment(JSON.parseArray(item.getAttachmentString(), EducationResourceVO.UploadFile.class));
            item.setVisitUrl(url + "/api/files/" + item.getContentUrl().getUrl());
        });
        return Result.ok(page.getList()).put("total", page.getTotal());
    }
@@ -103,6 +109,7 @@
        page.getList().stream().forEach(item -> {
            item.setContentUrl(JSON.parseObject(item.getContentUrlString(), EducationResourceVO.UploadFile.class));
            item.setAttachment(JSON.parseArray(item.getAttachmentString(), EducationResourceVO.UploadFile.class));
            item.setVisitUrl(url + "/api/files/" + item.getContentUrl().getUrl());
        });
        return Result.ok(page.getList()).put("total", page.getTotal());
    }
@@ -112,27 +119,32 @@
        List<Subject> subjects = subjectMapper.allSubject();
        return Result.ok(subjects);
    }
    //记录视频时长
    @Override
    public Result recordTime(Integer userId) {
        if (userId ==null){
        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());
        Map<String, StudyRecord> studyMap = (Map<String, StudyRecord>) caffeineCache.getIfPresent(CaffeineConstant.STUDY_RECORD);
        if (!CollectionUtils.isEmpty(studyMap)) {
            StudyRecord studyRecord = studyMap.get(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());
            }
            studyMap.put(userId + "", studyRecord);
            caffeineCache.put(CaffeineConstant.STUDY_RECORD, studyMap);
        }
        caffeineCache.put("STUDENT_" + userId, studyRecord);
        return Result.ok();
    }
}