heheng
6 天以前 81791ded0d0bf0a452dcc3a96f25ea3bb12ebfcb
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
package com.gkhy.exam.framework.job;
 
import cn.hutool.core.util.ObjectUtil;
import com.gkhy.exam.common.constant.CacheConstant;
import com.gkhy.exam.common.enums.ResourceTypeEnum;
import com.gkhy.exam.common.utils.RedisUtils;
import com.gkhy.exam.system.domain.ExResource;
import com.gkhy.exam.system.domain.ExStudentStudy;
import com.gkhy.exam.system.mapper.ExResourceMapper;
import com.gkhy.exam.system.mapper.ExStudentStudyMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.math.BigDecimal;
import java.util.Set;
 
/**
 * 学习进度job
 */
@Slf4j
@Component
public class UserStudyJob {
    @Autowired
    private ExResourceMapper resourceMapper;
    @Autowired
    private ExStudentStudyMapper studentStudyMapper;
    @Autowired
    private RedisUtils redisUtils;
 
    /**
     * 建议:每10秒执行一次
     */
    @Scheduled(cron = "*/10 * * * * ?")
    public void doprogress(){
        try {
            Set<String> keys = redisUtils.keys(CacheConstant.STUDY_PROCESS_KEY + "*");
            if (ObjectUtil.isNotEmpty(keys)) {
                for (String key : keys) {
                    ExStudentStudy studentStudy = (ExStudentStudy) redisUtils.get(key);
                    ExResource resource = resourceMapper.selectById(studentStudy.getResourceId());
                    if (ResourceTypeEnum.VIDEO.getCode().equals(resource.getResourceType()) || ResourceTypeEnum.AUDIO.getCode().equals(resource.getResourceType())) {
                        studentStudy.setProgress(new BigDecimal(studentStudy.getCurrentDuration()).divide(new BigDecimal(resource.getResourceLength()), BigDecimal.ROUND_CEILING).multiply(BigDecimal.valueOf(100)));
                    } else {
                        studentStudy.setProgress(BigDecimal.valueOf(studentStudy.getCurrentPage()).divide(BigDecimal.valueOf(resource.getDocPage()).multiply(BigDecimal.valueOf(100))));
                    }
                    studentStudyMapper.updateById(studentStudy);
                    redisUtils.del(key);
                }
            }
        }catch (Exception e){
            log.error("UserStudyJob doprogress error:{}",e.getMessage());
        }
 
    }
 
    /**
     * 统计学员学习总进度
     */
    public void staticProgress(){
 
    }
}