deason 1 year ago
parent
commit
fcda0620f0

+ 37 - 26
src/main/java/com/qmth/exam/reserve/cache/impl/ApplyTaskCacheService.java

@@ -159,13 +159,13 @@ public class ApplyTaskCacheService implements CacheConstants {
      * 获取某考生的 已完成预约次数
      */
     public int getStudentApplyFinishCount(Long studentId) {
-        String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, studentId);
+        /*String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, studentId);
         if (!redisClient.exist(cacheKey)) {
             LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
             wrapper.eq(StudentApplyEntity::getStudentId, studentId);
             wrapper.eq(StudentApplyEntity::getCancel, Boolean.FALSE);
             return studentApplyService.count(wrapper);
-        } // todo不走数据库查询,以redis中数据为准
+        }*/
 
         Map<String, ApplyRecordCacheBean> maps = this.getStudentApplyRecords(studentId);
         int studentApplyFinishCount = 0;
@@ -183,34 +183,45 @@ public class ApplyTaskCacheService implements CacheConstants {
     public Map<String, ApplyRecordCacheBean> getStudentApplyRecords(Long studentId) {
         String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, studentId);
         if (!redisClient.exist(cacheKey)) {
-            LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
-            wrapper.eq(StudentApplyEntity::getStudentId, studentId);
-            List<StudentApplyEntity> list = studentApplyService.list(wrapper);
-
-            Map<String, ApplyRecordCacheBean> maps = new HashMap<>();
-            if (CollectionUtils.isNotEmpty(list)) {
-                for (StudentApplyEntity entity : list) {
-                    ApplyRecordCacheBean bean = new ApplyRecordCacheBean();
-                    bean.setStudentId(entity.getStudentId());
-                    bean.setExamSiteId(entity.getExamSiteId());
-                    bean.setTimePeriodId(entity.getTimePeriodId());
-                    bean.setCancel(entity.getCancel());
-                    bean.setOperateId(entity.getOperateId());
-                    bean.setOperateTime(entity.getUpdateTime());
-                    this.saveStudentApplyRecord(bean);
-
-                    String hashKey = String.format("%s_%s", entity.getExamSiteId(), entity.getTimePeriodId());
-                    maps.put(hashKey, bean);
-                }
-
-                redisClient.expire(cacheKey, 30, TimeUnit.DAYS);
-            }
-            return maps;
-        } // todo不走数据库查询,以redis中数据为准
+            return this.initStudentApplyRecords(studentId);
+        }
 
         return redisClient.getEntriesForHash(cacheKey, ApplyRecordCacheBean.class);
     }
 
+    /**
+     * 初始某考生的 所有的“预约记录”缓存
+     */
+    public Map<String, ApplyRecordCacheBean> initStudentApplyRecords(Long studentId) {
+        LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(StudentApplyEntity::getStudentId, studentId);
+        List<StudentApplyEntity> list = studentApplyService.list(wrapper);
+
+        if (CollectionUtils.isEmpty(list)) {
+            return new HashMap<>();
+        }
+
+        Map<String, ApplyRecordCacheBean> maps = new HashMap<>();
+        for (StudentApplyEntity entity : list) {
+            ApplyRecordCacheBean bean = new ApplyRecordCacheBean();
+            bean.setStudentId(entity.getStudentId());
+            bean.setExamSiteId(entity.getExamSiteId());
+            bean.setTimePeriodId(entity.getTimePeriodId());
+            bean.setCancel(entity.getCancel());
+            bean.setOperateId(entity.getOperateId());
+            bean.setOperateTime(entity.getUpdateTime());
+
+            String hashKey = String.format("%s_%s", entity.getExamSiteId(), entity.getTimePeriodId());
+            maps.put(hashKey, bean);
+            this.saveStudentApplyRecord(bean);
+        }
+
+        String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, studentId);
+        redisClient.expire(cacheKey, 30, TimeUnit.DAYS);
+
+        return maps;
+    }
+
     /**
      * 获取某考生的 某考点某时段的“预约记录”缓存
      */