ApiUserStudyBiz.java 4.0 KB
Newer Older
F
修改  
fengyw 已提交
1 2
package com.roncoo.education.course.service.api.biz;

F
修改  
fengyw 已提交
3 4 5
import cn.hutool.core.util.ObjectUtil;
import com.roncoo.education.common.cache.CacheRedis;
import com.roncoo.education.common.core.base.Result;
F
fengyw 已提交
6
import com.roncoo.education.common.core.enums.ResourceTypeEnum;
F
修改  
fengyw 已提交
7
import com.roncoo.education.common.core.tools.Constants;
F
修改  
fengyw 已提交
8
import com.roncoo.education.common.service.BaseBiz;
F
修改  
fengyw 已提交
9
import com.roncoo.education.course.dao.ResourceDao;
F
修改  
fengyw 已提交
10
import com.roncoo.education.course.dao.UserStudyDao;
F
修改  
fengyw 已提交
11 12 13
import com.roncoo.education.course.dao.impl.mapper.entity.Resource;
import com.roncoo.education.course.dao.impl.mapper.entity.UserStudy;
import com.roncoo.education.course.service.auth.req.AuthUserStudyReq;
F
修改  
fengyw 已提交
14 15 16 17
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
F
修改  
fengyw 已提交
18 19
import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;
F
修改  
fengyw 已提交
20 21 22 23 24 25 26 27 28 29 30 31

/**
 * API-课程用户学习日志
 *
 * @author wujing
 */
@Component
@RequiredArgsConstructor
public class ApiUserStudyBiz extends BaseBiz {

    @NotNull
    private final UserStudyDao dao;
F
修改  
fengyw 已提交
32 33 34 35 36 37 38 39 40 41 42
    @NotNull
    private final ResourceDao resourceDao;
    @NotNull
    private final CacheRedis cacheRedis;

    public Result<String> study(AuthUserStudyReq req) {
        // 资源信息
        Resource resource = getByResource(req);
        if (ObjectUtil.isEmpty(resource)) {
            Result.error("resourceId不正确");
        }
F
fengyw 已提交
43
        req.setResourceType(resource.getResourceType());
F
fengyw 已提交
44 45 46
        if (ResourceTypeEnum.AUDIO.getCode().equals(resource.getResourceType()) || ResourceTypeEnum.VIDEO.getCode().equals(resource.getResourceType())) {
            // 音视频处理
            if (new BigDecimal(resource.getVideoLength()).subtract(req.getCurrentDuration()).intValue() < 1) {
F
fengyw 已提交
47 48
                // 学习完成
                return completeStudy(req);
F
修改  
fengyw 已提交
49
            }
F
fengyw 已提交
50 51
            // 没观看完成,进度存入redis,如没看完,定时任务处理
            req.setTotalDuration(new BigDecimal(resource.getVideoLength()));
F
fengyw 已提交
52

F
fengyw 已提交
53 54
        } else if (ResourceTypeEnum.DOC.getCode().equals(resource.getResourceType())) {
            // 文档类型处理
F
fengyw 已提交
55 56 57 58 59 60
            if (req.getCurrentPage().compareTo(resource.getDocPage()) >= 0) {
                // 学习完成
                return completeStudy(req);
            }
            // 没学习完成,进度存入redis,如没学习完,定时任务处理
            req.setTotalPage(resource.getDocPage());
F
修改  
fengyw 已提交
61
        }
F
fengyw 已提交
62
        cacheRedis.set(Constants.RedisPre.PROGRESS + req.getStudyId(), req, 1, TimeUnit.DAYS);
F
修改  
fengyw 已提交
63 64 65
        return Result.success("学习中");
    }

F
fengyw 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79
    private Result<String> completeStudy(AuthUserStudyReq req) {
        UserStudy userStudy = getUserStudy(req);
        if (ObjectUtil.isEmpty(userStudy)) {
            return Result.error("studyId不正确");
        }
        userStudy.setProgress(BigDecimal.valueOf(100));
        // 更新观看记录
        dao.updateById(userStudy);
        // 清空缓存
        cacheRedis.delete(Constants.RedisPre.USER_STUDY + req.getStudyId());
        cacheRedis.delete(Constants.RedisPre.PROGRESS + req.getStudyId());
        return Result.success("OK");
    }

F
修改  
fengyw 已提交
80 81 82 83 84 85 86 87
    private Resource getByResource(AuthUserStudyReq req) {
        Resource resource = cacheRedis.getByJson(Constants.RedisPre.RESOURCE + req.getResourceId(), Resource.class);
        if (ObjectUtil.isEmpty(resource)) {
            resource = resourceDao.getById(req.getResourceId());
            cacheRedis.set(Constants.RedisPre.RESOURCE + req.getResourceId(), resource, 1, TimeUnit.HOURS);
        }
        return resource;
    }
F
修改  
fengyw 已提交
88

F
修改  
fengyw 已提交
89 90 91 92 93 94 95 96
    private UserStudy getUserStudy(AuthUserStudyReq req) {
        UserStudy userStudy = cacheRedis.getByJson(Constants.RedisPre.USER_STUDY + req.getStudyId(), UserStudy.class);
        if (ObjectUtil.isEmpty(userStudy)) {
            userStudy = dao.getById(req.getStudyId());
            cacheRedis.set(Constants.RedisPre.USER_STUDY + req.getStudyId(), userStudy, 1, TimeUnit.HOURS);
        }
        return userStudy;
    }
F
修改  
fengyw 已提交
97
}