派生自 projectDept/qhighschool

EricsHu
2022-12-08 6ca7ca9feb1cc4a35cab3a3af8dddfae2580bddc
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
package com.qxueyou.scc.admin.progress.service.impl;
 
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.PostConstruct;
 
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
 
import com.qxueyou.scc.admin.progress.dao.ProgressDAO;
import com.qxueyou.scc.admin.progress.model.Progress;
import com.qxueyou.scc.admin.progress.model.view.SubjectProgressTreeV;
import com.qxueyou.scc.admin.progress.service.IDetailProgressService;
import com.qxueyou.scc.admin.progress.service.IProgressService;
import com.qxueyou.scc.base.model.Result;
import com.qxueyou.scc.base.service.ICacheService;
import com.qxueyou.scc.base.service.impl.CommonAppService;
import com.qxueyou.scc.base.util.ClientUtils;
import com.qxueyou.scc.base.util.CollectionUtils;
import com.qxueyou.scc.base.util.TraceUtils;
 
@Service
public class ProgressService extends CommonAppService implements IProgressService {
    public static final String SUBJECT_PROGRESS_STATISTIC = "subject_progress_statistic";
 
    private final Logger log = LogManager.getLogger(ProgressService.class);
 
    @Autowired
    ICacheService cacheService;
 
    @Autowired
    ProgressDAO dao;
 
    /**
     * 各类型课件进度服务实现
     */
    private Map<String, IDetailProgressService> resItemServiceMap = new HashMap<String, IDetailProgressService>(6);
 
    @Autowired
    private ApplicationContext appContext;
 
    @PostConstruct
    private void init() {
        addProgressService(appContext.getBean("LectureProgressService", IDetailProgressService.class));
        addProgressService(appContext.getBean("ChapterProgressService", IDetailProgressService.class));
        addProgressService(appContext.getBean("SubjectProgressService", IDetailProgressService.class));
        addProgressService(appContext.getBean("ClassProgressService", IDetailProgressService.class));
    }
 
    private void addProgressService(IDetailProgressService service) {
        resItemServiceMap.put(service.getType(), service);
    }
 
    @Override
    public List<Progress> query(String type, String learnerId, List<String> targetId) {
        return queryLst(type, learnerId, targetId);
    }
 
    @Override
    public List<Progress> query(String type, List<String> learnerId, String targetId) {
        return queryLst(type, learnerId, targetId);
    }
 
    @Override
    public Progress query(String type, String targetId, String learnerId) {
        String hql = "from Progress where type=:type and learnerId=:learnerId and targetId=:targetId";
        return findUniqueByHql(hql,
                CollectionUtils.newObjectMap("type", type, "learnerId", learnerId, "targetId", targetId));
    }
 
    private List<Progress> queryLst(String type, Object learnerId, Object targetId) {
        String hql = "from Progress where type=:type and learnerId=:learnerId and targetId=:targetId";
        return findByComplexHql(hql,
                CollectionUtils.newObjectMap("type", type, "learnerId", learnerId, "targetId", targetId),
                Progress.class);
    }
 
    @Override
    public Result addProgress(String type, String targetId, int start, int end, String userId) {
        String userId_ = StringUtils.isBlank(userId) ? ClientUtils.getUserId() : userId;
        return resItemServiceMap.get(type).addProgress(targetId, userId_, start, end);
    }
 
    @Scheduled(cron = "0/2 * * * * ?")
    protected void doTimer() {
        String nodeIdOrUserId = cacheService.lstLeftPop(SUBJECT_PROGRESS_STATISTIC);
 
        if (StringUtils.isBlank(nodeIdOrUserId)) {
            return;
        }
        String[] ids = nodeIdOrUserId.split(",");
        String nodeId = ids[0];
        String userId = ids[1];
        SubjectProgressTreeV subjectProgressTreeV = null;
        try {
 
            subjectProgressTreeV = dao.getSubjectProgressTreeV(nodeId, userId);
            Map<String, Object> itemProgress = dao.getItemProgress(nodeId, userId);
            Progress progress = dao.getProgressObject(nodeId, userId);
            Double progressPercent = Double.valueOf(itemProgress.get("progressPercent").toString());
            BigDecimal progressValue = new BigDecimal(itemProgress.get("progressValue").toString());
            if (progress == null) {
                String nodeType = subjectProgressTreeV.getNodeType();
                this.addProgress(nodeType, nodeId, 0, 0, userId);
                cacheService.lstRightPush(SUBJECT_PROGRESS_STATISTIC, nodeIdOrUserId);
                return;
            }
            TraceUtils.setUpdateTrace(progress);
            progress.setProgressPercent(progressPercent);
            progress.setProgressValue(progressValue);
            save(progress);
 
        } catch (Exception e) {
            log.error("保存异常", e);
            return;
        }
 
        if (StringUtils.isNotBlank(subjectProgressTreeV.getParentId())) {
            cacheService.lstRightPush(SUBJECT_PROGRESS_STATISTIC, subjectProgressTreeV.getParentId() + "," + userId);
        }
 
    }
 
    @Override
    public List<SubjectProgressTreeV> getSubjectChapterTreeVList(String parentId, String userId) {
        return dao.getSubjectChapterTreeVList(parentId, userId);
    }
 
}