xiangpei
2024-07-11 5073a245f53fd5ca936e779be8c6b9b19d42f67d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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.constants.CaffeineConstant;
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.mapper.ClassesUserMapper;
import com.ycl.jxkg.mapper.EducationResourceMapper;
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.*;
 
/**
 * @author:flq
 * @date:2024/6/18 10:53
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class EducationResourceServiceImpl implements EducationResourceService {
 
 
    private final EducationResourceMapper mapper;
    private final SubjectMapper subjectMapper;
    private final WebContext webContext;
    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();
        BeanUtils.copyProperties(form, educationResource);
        educationResource.setContentUrl(JSON.toJSONString(form.getContentUrl()));
        if (!CollectionUtils.isEmpty(form.getAttachment())) {
            educationResource.setAttachment(JSON.toJSONString(form.getAttachment()));
        }
        educationResource.setCreateTime(new Date());
        educationResource.setUpdateTime(new Date());
        educationResource.setCreateUser(webContext.getCurrentUser().getId());
        mapper.add(educationResource);
        return Result.ok("添加成功");
    }
 
    @Override
    public Result update(EducationResourceVO form) {
        if (Objects.isNull(form.getId())) {
            throw new RuntimeException("请选择要修改的数据");
        }
        EducationResource educationResource = new EducationResource();
        BeanUtils.copyProperties(form, educationResource);
        educationResource.setContentUrl(JSON.toJSONString(form.getContentUrl()));
        if (!CollectionUtils.isEmpty(form.getAttachment())) {
            educationResource.setAttachment(JSON.toJSONString(form.getAttachment()));
        } else {
            educationResource.setAttachment("");
        }
        mapper.update(educationResource);
        return Result.ok("修改成功");
    }
 
    @Override
    public Result remove(List<Integer> ids) {
        mapper.remove(ids);
        return Result.ok("删除成功");
    }
 
    @Override
    public Result page(EducationResourceVO query) {
        PageInfo<EducationResourceVO> page = PageHelper.startPage(query.getPageNum(), query.getPageSize()).doSelectPageInfo(() ->
                mapper.page(query));
        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());
    }
 
    @Override
    public Result studentPage(EducationResourceVO query) {
        Integer id = webContext.getCurrentUser().getId();
        List<Integer> classes = classesUserMapper.getClassesByUserId(id);
        if (CollectionUtils.isEmpty(classes)) {
            //学生没有班级
            List<EducationResourceVO> list = new ArrayList<>();
            return Result.ok(list).put("total", 0);
        }
        query.setClassIds(classes);
        PageInfo<EducationResourceVO> page = PageHelper.startPage(query.getPageNum(), query.getPageSize()).doSelectPageInfo(() ->
                mapper.studentPage(query));
        page.getList().stream().forEach(item -> {
            item.setContentUrl(JSON.parseObject(item.getContentUrlString(), EducationResourceVO.UploadFile.class));
            item.setAttachment(JSON.parseArray(item.getAttachmentString(), EducationResourceVO.UploadFile.class));
        });
        return Result.ok(page.getList()).put("total", page.getTotal());
    }
 
    @Override
    public Result typeList() {
        List<Subject> subjects = subjectMapper.allSubject();
        return Result.ok(subjects);
    }
 
    //记录视频时长
    @Override
    public Result recordTime(Integer userId) {
        if (userId == null) {
            throw new RuntimeException("用户id为空");
        }
        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);
        }
        return Result.ok();
    }
}