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;
/**
*
* 课程学习学习日志表 服务实现类
*
*
* @author kzy
* @since 2024-06-06 13:53:17
*/
@Service
public class ExStudentStudyServiceImpl extends ServiceImpl 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 selectStudyByPhaseAndStundentId(Long phaseId,Long studentId) {
ExCoursePhase coursePhase=coursePhaseMapper.selectById(phaseId);
Long courseId=coursePhase.getCourseId();
List studentStudyList= baseMapper.selectStudyByPhaseAndStudentId(phaseId,courseId,studentId);
Map studentStudyMap=studentStudyList.stream().collect(Collectors.toMap(ExStudentStudy::getPeriodId, item->item));
List courseChapterList=courseChapterService.selectChapterByCourseId(courseId);
List studentStudyVOList=courseChapterList.stream().map(item ->{
StudentStudyVO studentStudyVO=new StudentStudyVO();
studentStudyVO.setChapterId(item.getId());
studentStudyVO.setCourseId(item.getCourseId());
studentStudyVO.setChapterName(item.getName());
List 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);
}
}