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 resItemServiceMap = new HashMap(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 query(String type, String learnerId, List targetId) { return queryLst(type, learnerId, targetId); } @Override public List query(String type, List 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 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, Double start, Double 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 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, 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 getSubjectChapterTreeVList(String parentId, String userId) { return dao.getSubjectChapterTreeVList(parentId, userId); } }