kongzy
2024-06-24 4910e41f81a85c03a9dfc83f8ec9c1e71c123d49
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
package com.gkhy.exam.system.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.exam.common.constant.CacheConstant;
import com.gkhy.exam.common.enums.ResourceTypeEnum;
import com.gkhy.exam.common.exception.ApiException;
import com.gkhy.exam.common.utils.RedisUtils;
import com.gkhy.exam.system.domain.*;
import com.gkhy.exam.system.domain.vo.StudentStudyPeriodVO;
import com.gkhy.exam.system.domain.vo.StudentStudyVO;
import com.gkhy.exam.system.mapper.ExCoursePhaseMapper;
import com.gkhy.exam.system.mapper.ExResourceMapper;
import com.gkhy.exam.system.mapper.ExStudentStudyMapper;
import com.gkhy.exam.system.service.ExCourseChapterService;
import com.gkhy.exam.system.service.ExStudentStudyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 课程学习学习日志表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-06-06 13:53:17
 */
@Service
public class ExStudentStudyServiceImpl extends ServiceImpl<ExStudentStudyMapper, ExStudentStudy> implements ExStudentStudyService {
    @Autowired
    private ExCourseChapterService courseChapterService;
    @Autowired
    private ExCoursePhaseMapper coursePhaseMapper;
    @Autowired
    private ExResourceMapper resourceMapper;
    @Autowired
    private RedisUtils redisUtils;
 
    @Override
    public Long insertStudentStudy(ExStudentStudy studentStudy) {
        int row=0;
        ExStudentStudy existStudentStudy=baseMapper.selectStudyByObject(studentStudy);
        if(existStudentStudy!=null){
            studentStudy.setId(existStudentStudy.getId());
            row=baseMapper.updateById(existStudentStudy);
        }else{
            row=baseMapper.insert(studentStudy);
        }
        if(row<1){
            throw new ApiException("新增学习记录失败");
        }
        return studentStudy.getId();
    }
 
    @Override
    public int deleteStudentStudyById(Long studyId) {
        return baseMapper.deleteById(studyId);
    }
 
    @Override
    public List<StudentStudyVO> selectStudyByPhaseAndStundentId(Long phaseId,Long studentId) {
        ExCoursePhase coursePhase=coursePhaseMapper.selectById(phaseId);
        Long courseId=coursePhase.getCourseId();
        List<ExStudentStudy> studentStudyList= baseMapper.selectStudyByPhaseAndStudentId(phaseId,courseId,studentId);
        Map<Long,ExStudentStudy> studentStudyMap=studentStudyList.stream().collect(Collectors.toMap(ExStudentStudy::getPeriodId, item->item));
        List<ExCourseChapter> courseChapterList=courseChapterService.selectChapterByCourseId(courseId);
        List<StudentStudyVO> studentStudyVOList=courseChapterList.stream().map(item ->{
            StudentStudyVO studentStudyVO=new StudentStudyVO();
            studentStudyVO.setChapterId(item.getId());
            studentStudyVO.setCourseId(item.getCourseId());
            studentStudyVO.setChapterName(item.getName());
            List<StudentStudyPeriodVO> studentStudyPeriodVOList=new ArrayList<>();
            for(ExCourseChapterPeriod courseChapterPeriod:item.getChapterPeriods()){
                StudentStudyPeriodVO studentStudyPeriodVO=new StudentStudyPeriodVO();
                studentStudyPeriodVO.setPeriodName(courseChapterPeriod.getName());
                studentStudyPeriodVO.setPeriodId(courseChapterPeriod.getId());
                studentStudyPeriodVO.setPeriod(courseChapterPeriod.getPeriod());
                ExStudentStudy exStudentStudy=studentStudyMap.get(courseChapterPeriod.getId());
                if(exStudentStudy!=null){
                    studentStudyPeriodVO.setProgress(exStudentStudy.getProgress());
                    studentStudyPeriodVO.setCreateTime(exStudentStudy.getCreateTime());
                    studentStudyPeriodVO.setUpdateTime(exStudentStudy.getUpdateTime());
                    studentStudyPeriodVO.setStudyId(exStudentStudy.getId());
                }
                studentStudyPeriodVOList.add(studentStudyPeriodVO);
            }
            studentStudyVO.setStudentStudyPeriodVOList(studentStudyPeriodVOList);
            return studentStudyVO;
        }).collect(Collectors.toList());
 
        return studentStudyVOList;
    }
 
    @Override
    public void progress(ExStudentStudy studentStudy) {
        if(studentStudy.getPhaseId()==null||studentStudy.getPeriodId()==null||studentStudy.getStudentId()==null||studentStudy.getResourceId()==null){
            throw new ApiException("参数传参错误");
        }
        ExResource resource=resourceMapper.selectById(studentStudy.getResourceId());
        if (ResourceTypeEnum.AUDIO.getCode().equals(resource.getResourceType()) || ResourceTypeEnum.VIDEO.getCode().equals(resource.getResourceType())) {
            // 音视频处理
            if (new BigDecimal(resource.getResourceLength()).subtract(new BigDecimal(studentStudy.getCurrentDuration())).intValue() < 1) {
                // 学习完成
                 completeStudy(studentStudy);
            }
        } else if (ResourceTypeEnum.DOC.getCode().equals(resource.getResourceType())) {
            // 文档类型处理
            if (studentStudy.getCurrentPage().compareTo(resource.getDocPage()) >= 0) {
                // 学习完成
                 completeStudy(studentStudy);
            }
        }
        redisUtils.set(CacheConstant.STUDY_PROCESS_KEY + studentStudy.getId(), studentStudy, 1, TimeUnit.DAYS);
    }
 
    private void completeStudy(ExStudentStudy studentStudy) {
        studentStudy.setProgress(BigDecimal.valueOf(100));
        // 更新观看记录
        baseMapper.updateById(studentStudy);
    }
}