Browse Source

新增统计考试信息缓存

wangliang 1 year ago
parent
commit
71d6600ccc

+ 10 - 3
themis-admin/src/main/java/com/qmth/themis/admin/api/TEExamController.java

@@ -6,7 +6,6 @@ import com.qmth.themis.business.annotation.ApiJsonObject;
 import com.qmth.themis.business.annotation.ApiJsonProperty;
 import com.qmth.themis.business.annotation.ApiJsonProperty;
 import com.qmth.themis.business.annotation.Logs;
 import com.qmth.themis.business.annotation.Logs;
 import com.qmth.themis.business.annotation.RedisLimitAnnotation;
 import com.qmth.themis.business.annotation.RedisLimitAnnotation;
-import com.qmth.themis.business.bean.admin.CountStopBean;
 import com.qmth.themis.business.cache.ExamingDataCacheUtil;
 import com.qmth.themis.business.cache.ExamingDataCacheUtil;
 import com.qmth.themis.business.cache.RedisKeyHelper;
 import com.qmth.themis.business.cache.RedisKeyHelper;
 import com.qmth.themis.business.cache.bean.ExamCacheBean;
 import com.qmth.themis.business.cache.bean.ExamCacheBean;
@@ -37,9 +36,7 @@ import org.springframework.web.bind.annotation.*;
 import javax.annotation.Resource;
 import javax.annotation.Resource;
 import javax.validation.constraints.Max;
 import javax.validation.constraints.Max;
 import javax.validation.constraints.Min;
 import javax.validation.constraints.Min;
-import java.math.BigDecimal;
 import java.util.*;
 import java.util.*;
-import java.util.concurrent.atomic.AtomicReference;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
 /**
 /**
@@ -226,6 +223,16 @@ public class TEExamController {
                 teExamActivityService.saveOrUpdate(teExamActivity);
                 teExamActivityService.saveOrUpdate(teExamActivity);
                 teExamActivityService.updateExamActivityCacheBean(teExamActivity.getId());
                 teExamActivityService.updateExamActivityCacheBean(teExamActivity.getId());
             }
             }
+//            if (teExam.getEnable().intValue() == 0 || teExam.getEndTime().longValue() <= System.currentTimeMillis()) {
+//                themisCacheService.removeTodayExamCache(teExam.getId().toString());
+//                themisCacheService.removeTodayExamListCache(teExam.getId());
+//            } else {
+            themisCacheService.updateTodayExamListCache(teExam.getId());
+            List<TEExamActivity> teExamActivityList = teExamActivityService.list(new QueryWrapper<TEExamActivity>().lambda().eq(TEExamActivity::getExamId, teExam.getId()).eq(TEExamActivity::getEnable, 1));
+            for (TEExamActivity t : teExamActivityList) {
+                themisCacheService.updateTodayExamCache(t.getExamId().toString(), t.getId().toString());
+            }
+//            }
         } catch (Exception e) {
         } catch (Exception e) {
             log.error(SystemConstant.LOG_ERROR, e);
             log.error(SystemConstant.LOG_ERROR, e);
             if (Objects.nonNull(teExam) && redisUtil.lock(SystemConstant.REDIS_LOCK_EXAM_ACTIVITY_CODE_PREFIX + teExam.getId(), SystemConstant.REDIS_LOCK_EXAM_ACTIVITY_CODE_TIME_OUT)) {
             if (Objects.nonNull(teExam) && redisUtil.lock(SystemConstant.REDIS_LOCK_EXAM_ACTIVITY_CODE_PREFIX + teExam.getId(), SystemConstant.REDIS_LOCK_EXAM_ACTIVITY_CODE_TIME_OUT)) {

+ 8 - 0
themis-business/src/main/java/com/qmth/themis/business/service/impl/TEExamActivityServiceImpl.java

@@ -83,6 +83,9 @@ public class TEExamActivityServiceImpl extends ServiceImpl<TEExamActivityMapper,
     @Resource
     @Resource
     TOeExamRecordService tOeExamRecordService;
     TOeExamRecordService tOeExamRecordService;
 
 
+    @Resource
+    ThemisCacheService themisCacheService;
+
     /**
     /**
      * 表是否存在
      * 表是否存在
      *
      *
@@ -411,6 +414,11 @@ public class TEExamActivityServiceImpl extends ServiceImpl<TEExamActivityMapper,
             teExamActivityService.saveOrUpdateBatch(teExamActivityList);
             teExamActivityService.saveOrUpdateBatch(teExamActivityList);
 
 
             for (TEExamActivity ac : teExamActivityList) {
             for (TEExamActivity ac : teExamActivityList) {
+//                if (ac.getEnable().intValue() == 0 || ac.getFinishTime().longValue() <= System.currentTimeMillis()) {
+//                    themisCacheService.removeTodayExamCache(ac.getExamId().toString(), ac.getId().toString());
+//                } else {
+                themisCacheService.updateTodayExamCache(ac.getExamId().toString(), ac.getId().toString());
+//                }
                 teExamActivityService.updateExamActivityCacheBean(ac.getId());
                 teExamActivityService.updateExamActivityCacheBean(ac.getId());
             }
             }
 
 

+ 12 - 5
themis-business/src/main/java/com/qmth/themis/business/service/impl/ThemisCacheServiceImpl.java

@@ -2,6 +2,7 @@ package com.qmth.themis.business.service.impl;
 
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.qmth.themis.business.bean.admin.ExamActivityTodayBean;
 import com.qmth.themis.business.bean.admin.ExamActivityTodayBean;
+import com.qmth.themis.business.cache.bean.ExamCacheBean;
 import com.qmth.themis.business.constant.SpringContextHolder;
 import com.qmth.themis.business.constant.SpringContextHolder;
 import com.qmth.themis.business.constant.SystemConstant;
 import com.qmth.themis.business.constant.SystemConstant;
 import com.qmth.themis.business.dto.AuthDto;
 import com.qmth.themis.business.dto.AuthDto;
@@ -90,6 +91,9 @@ public class ThemisCacheServiceImpl implements ThemisCacheService {
     @Resource
     @Resource
     TEExamSummaryService teExamSummaryService;
     TEExamSummaryService teExamSummaryService;
 
 
+    @Resource
+    TEExamService teExamService;
+
     /**
     /**
      * 添加机构缓存
      * 添加机构缓存
      *
      *
@@ -606,14 +610,17 @@ public class ThemisCacheServiceImpl implements ThemisCacheService {
             SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             Long startTime = sdf2.parse(today + " 00:00:00").getTime();
             Long startTime = sdf2.parse(today + " 00:00:00").getTime();
             Long endTime = sdf2.parse(today + " 23:59:59").getTime();
             Long endTime = sdf2.parse(today + " 23:59:59").getTime();
-            List<TEExamActivity> teExamActivityList = teExamActivityService.list(new QueryWrapper<TEExamActivity>().lambda().ge(TEExamActivity::getStartTime, startTime).le(TEExamActivity::getStartTime, endTime));
+            List<TEExamActivity> teExamActivityList = teExamActivityService.list(new QueryWrapper<TEExamActivity>().lambda().ge(TEExamActivity::getStartTime, startTime).le(TEExamActivity::getStartTime, endTime).eq(TEExamActivity::getEnable, 1));
             if (!CollectionUtils.isEmpty(teExamActivityList)) {
             if (!CollectionUtils.isEmpty(teExamActivityList)) {
                 LinkedMultiValueMap<Long, ExamActivityTodayBean> examActivityMap = new LinkedMultiValueMap<>();
                 LinkedMultiValueMap<Long, ExamActivityTodayBean> examActivityMap = new LinkedMultiValueMap<>();
                 for (TEExamActivity t : teExamActivityList) {
                 for (TEExamActivity t : teExamActivityList) {
-                    List<TEExamStudent> teExamStudentList = teExamStudentService.list(new QueryWrapper<TEExamStudent>().lambda().eq(TEExamStudent::getExamId, t.getExamId()).eq(TEExamStudent::getExamActivityId, t.getId()));
-                    if (!CollectionUtils.isEmpty(teExamStudentList)) {
-                        Set<String> roomCodeSet = teExamStudentList.stream().map(s -> s.getRoomCode()).collect(Collectors.toSet());
-                        examActivityMap.add(t.getExamId(), new ExamActivityTodayBean(t.getId(), roomCodeSet));
+                    ExamCacheBean examCacheBean = teExamService.getExamCacheBean(t.getExamId());
+                    if (Objects.nonNull(examCacheBean.getEnable()) && examCacheBean.getEnable().intValue() == 1) {
+                        List<TEExamStudent> teExamStudentList = teExamStudentService.list(new QueryWrapper<TEExamStudent>().lambda().eq(TEExamStudent::getExamId, t.getExamId()).eq(TEExamStudent::getExamActivityId, t.getId()));
+                        if (!CollectionUtils.isEmpty(teExamStudentList)) {
+                            Set<String> roomCodeSet = teExamStudentList.stream().map(s -> s.getRoomCode()).collect(Collectors.toSet());
+                            examActivityMap.add(t.getExamId(), new ExamActivityTodayBean(t.getId(), roomCodeSet));
+                        }
                     }
                     }
                 }
                 }
                 examActivityMap.forEach((k, v) -> {
                 examActivityMap.forEach((k, v) -> {

+ 45 - 3
themis-task/src/main/java/com/qmth/themis/task/quartz/ExamSummaryJob.java

@@ -1,5 +1,11 @@
 package com.qmth.themis.task.quartz;
 package com.qmth.themis.task.quartz;
 
 
+import com.qmth.themis.business.cache.bean.ExamActivityCacheBean;
+import com.qmth.themis.business.cache.bean.ExamCacheBean;
+import com.qmth.themis.business.constant.SystemConstant;
+import com.qmth.themis.business.enums.ExamModeEnum;
+import com.qmth.themis.business.service.TEExamActivityService;
+import com.qmth.themis.business.service.TEExamService;
 import com.qmth.themis.business.service.TEExamSummaryService;
 import com.qmth.themis.business.service.TEExamSummaryService;
 import com.qmth.themis.business.service.ThemisCacheService;
 import com.qmth.themis.business.service.ThemisCacheService;
 import org.quartz.JobExecutionContext;
 import org.quartz.JobExecutionContext;
@@ -12,6 +18,7 @@ import org.springframework.util.CollectionUtils;
 
 
 import javax.annotation.Resource;
 import javax.annotation.Resource;
 import java.util.Map;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Set;
 import java.util.Set;
 
 
 /**
 /**
@@ -31,16 +38,51 @@ public class ExamSummaryJob extends QuartzJobBean {
     @Resource
     @Resource
     TEExamSummaryService teExamSummaryService;
     TEExamSummaryService teExamSummaryService;
 
 
+    @Resource
+    TEExamService teExamService;
+
+    @Resource
+    TEExamActivityService teExamActivityService;
+
     @Override
     @Override
     protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
     protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
         log.info("ExamSummaryJob进来了,context:{}", context);
         log.info("ExamSummaryJob进来了,context:{}", context);
         Set<Long> examIdSet = themisCacheService.getTodayExamListCache();
         Set<Long> examIdSet = themisCacheService.getTodayExamListCache();
         if (!CollectionUtils.isEmpty(examIdSet)) {
         if (!CollectionUtils.isEmpty(examIdSet)) {
             for (Long l : examIdSet) {
             for (Long l : examIdSet) {
+                ExamCacheBean examCacheBean = teExamService.getExamCacheBean(l);
+                if (examCacheBean.getEnable().intValue() == 0 || examCacheBean.getEndTime().longValue() <= System.currentTimeMillis()) {
+                    themisCacheService.removeTodayExamCache(l.toString());
+                    themisCacheService.removeTodayExamListCache(l);
+                }
                 Map<String, Set<String>> map = themisCacheService.getTodayExamCache(l.toString());
                 Map<String, Set<String>> map = themisCacheService.getTodayExamCache(l.toString());
-                map.forEach((k, v) -> {
-                    teExamSummaryService.examSummary(l, Long.parseLong(k), v);
-                });
+                if (!CollectionUtils.isEmpty(map)) {
+                    map.forEach((k, v) -> {
+                        //换算开始时间和结束时间
+                        Long startTime = null, endTime = null;
+                        ExamActivityCacheBean ac = teExamActivityService.getExamActivityCacheBean(Long.parseLong(k));
+                        if (ExamModeEnum.ANYTIME.equals(examCacheBean.getMode())) {
+                            startTime = ac.getStartTime() - (ac.getPrepareSeconds() * 1000);
+                            endTime = ac.getFinishTime();
+                            endTime = endTime + (1000 * 60 * 5);//额外加5分钟,有可能后台交卷未完成
+                        } else {
+                            startTime = ac.getStartTime() - (ac.getPrepareSeconds() * 1000);
+                            Integer openingSecondsTemp = ac.getOpeningSeconds();
+                            openingSecondsTemp = Objects.nonNull(openingSecondsTemp) && openingSecondsTemp.intValue() == 0 ? SystemConstant.DEFAULT_OPENING_SECONDS : openingSecondsTemp;
+                            endTime = ac.getStartTime() + (openingSecondsTemp * 1000);
+                            endTime = endTime + (1000 * 60 * 5);//额外加5分钟,有可能后台交卷未完成
+                        }
+                        long timestamp = System.currentTimeMillis();
+                        if (ac.getEnable().intValue() == 1 && startTime <= timestamp && endTime >= timestamp) {
+                            teExamSummaryService.examSummary(l, Long.parseLong(k), v);
+                        } else {
+                            themisCacheService.removeTodayExamCache(l.toString(), k);
+                        }
+                    });
+                } else {
+                    themisCacheService.removeTodayExamCache(l.toString());
+                    themisCacheService.removeTodayExamListCache(l);
+                }
             }
             }
         }
         }
     }
     }

+ 23 - 4
themis-task/src/main/java/com/qmth/themis/task/quartz/MqActivityJob.java

@@ -1,6 +1,8 @@
 package com.qmth.themis.task.quartz;
 package com.qmth.themis.task.quartz;
 
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.qmth.themis.business.cache.bean.ExamActivityCacheBean;
+import com.qmth.themis.business.cache.bean.ExamCacheBean;
 import com.qmth.themis.business.cache.bean.ExamStudentCacheBean;
 import com.qmth.themis.business.cache.bean.ExamStudentCacheBean;
 import com.qmth.themis.business.constant.SystemConstant;
 import com.qmth.themis.business.constant.SystemConstant;
 import com.qmth.themis.business.dto.cache.TEStudentCacheDto;
 import com.qmth.themis.business.dto.cache.TEStudentCacheDto;
@@ -18,10 +20,7 @@ import org.springframework.scheduling.quartz.QuartzJobBean;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.CollectionUtils;
 
 
 import javax.annotation.Resource;
 import javax.annotation.Resource;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
-import java.util.Set;
+import java.util.*;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
 /**
 /**
@@ -61,6 +60,26 @@ public class MqActivityJob extends QuartzJobBean {
     protected void executeInternal(JobExecutionContext context) {
     protected void executeInternal(JobExecutionContext context) {
         log.info("mq_activity_job进来了,context:{}", context);
         log.info("mq_activity_job进来了,context:{}", context);
         themisCacheService.setTodayExamCache();
         themisCacheService.setTodayExamCache();
+        //每天凌晨清理考试缓存数据
+        Set<Long> examIdSet = themisCacheService.getTodayExamListCache();
+        for (Long l : examIdSet) {
+            ExamCacheBean examCacheBean = teExamService.getExamCacheBean(l);
+            if (examCacheBean.getEnable().intValue() == 0 || examCacheBean.getEndTime().longValue() <= System.currentTimeMillis()) {
+                themisCacheService.removeTodayExamCache(l.toString());
+                themisCacheService.removeTodayExamListCache(l);
+            }
+            Map<String, Set<String>> map = themisCacheService.getTodayExamCache(l.toString());
+            if (!CollectionUtils.isEmpty(map)) {
+                map.forEach((k, v) -> {
+                    ExamActivityCacheBean examActivityCacheBean = teExamActivityService.getExamActivityCacheBean(Long.parseLong(k));
+                    if (examActivityCacheBean.getEnable().intValue() == 0 || examActivityCacheBean.getFinishTime().longValue() <= System.currentTimeMillis()) {
+                        themisCacheService.removeTodayExamCache(l.toString(), k);
+                    }
+                });
+            } else {
+                themisCacheService.removeTodayExamCache(l.toString());
+            }
+        }
         Long finishTime = System.currentTimeMillis();
         Long finishTime = System.currentTimeMillis();
         QueryWrapper<TEExam> teExamQueryWrapper = new QueryWrapper<>();
         QueryWrapper<TEExam> teExamQueryWrapper = new QueryWrapper<>();
         teExamQueryWrapper.lambda().eq(TEExam::getEnable, 1)
         teExamQueryWrapper.lambda().eq(TEExam::getEnable, 1)