Browse Source

update apply!!

deason 8 months ago
parent
commit
69ea21d471

+ 77 - 46
src/main/java/com/qmth/exam/reserve/cache/impl/ApplyTaskCacheService.java

@@ -65,6 +65,9 @@ public class ApplyTaskCacheService implements CacheConstants {
         return value;
     }
 
+    /**
+     * 清除当前启用的预约任务缓存
+     */
     public void clearCurrentApplyTaskCache(Long orgId) {
         String cacheKey = String.format(CACHE_CURRENT_APPLY_TASK, orgId);
         redisClient.delete(cacheKey);
@@ -72,7 +75,7 @@ public class ApplyTaskCacheService implements CacheConstants {
     }
 
     /**
-     * 获取某考生的“允许预约时段次数”
+     * 获取某考生的“允许预约时段次数”缓存
      */
     public int getStudentApplyNumber(Long studentId) {
         String cacheKey = String.format(CACHE_STUDENT_APPLY_NUMBER, studentId);
@@ -88,6 +91,9 @@ public class ApplyTaskCacheService implements CacheConstants {
         return value;
     }
 
+    /**
+     * 清除某考生的“允许预约时段次数”缓存
+     */
     public void clearStudentApplyNumberCache(Long studentId) {
         String cacheKey = String.format(CACHE_STUDENT_APPLY_NUMBER, studentId);
         redisClient.delete(cacheKey);
@@ -95,7 +101,7 @@ public class ApplyTaskCacheService implements CacheConstants {
     }
 
     /**
-     * 获取某考点的“可预约总量”
+     * 获取某考点的“可预约总量”缓存
      */
     public int getApplyTotalCount(Long examSiteId) {
         String cacheKey = String.format(CACHE_APPLY_TOTAL, examSiteId);
@@ -111,6 +117,9 @@ public class ApplyTaskCacheService implements CacheConstants {
         return value;
     }
 
+    /**
+     * 清除某考点的“可预约总量”缓存
+     */
     public void clearApplyTotalCountCache(Long examSiteId) {
         String cacheKey = String.format(CACHE_APPLY_TOTAL, examSiteId);
         redisClient.delete(cacheKey);
@@ -118,26 +127,32 @@ public class ApplyTaskCacheService implements CacheConstants {
     }
 
     /**
-     * 获取某考点某时段的“已预约数量”
+     * 获取某考点某时段的“已预约数量”(查数据库)
+     */
+    public int getApplyFinishCountFormDB(Long examSiteId, Long timePeriodId) {
+        return studentApplyService.countApplyFinishForExamSiteAndTimePeriod(examSiteId, timePeriodId);
+    }
+
+    /**
+     * 获取某考点某时段的“已预约数量”缓存
      */
     public int getApplyFinishCount(Long examSiteId, Long timePeriodId) {
-        // String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
-        // if (redisClient.exist(cacheKey)) {
-        //     return (int) redisClient.getRedissonClient().getAtomicLong(cacheKey).get();
-        // }
-        //
-        // RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
-
-        int value = studentApplyService.countApplyFinishForExamSiteAndTimePeriod(examSiteId, timePeriodId);
-        // atomic.set(value);
-        // atomic.expire(CACHE_TIME_OUT_30, TimeUnit.DAYS);
-        // log.info("SET cacheKey:{} value:{}", cacheKey, value);
+        String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
+        if (redisClient.exist(cacheKey)) {
+            return (int) redisClient.getRedissonClient().getAtomicLong(cacheKey).get();
+        }
+
+        RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
+        int value = this.getApplyFinishCountFormDB(examSiteId, timePeriodId);
+        atomic.set(value);
+        atomic.expire(CACHE_TIME_OUT_30, TimeUnit.DAYS);
+        log.info("SET cacheKey:{} value:{}", cacheKey, value);
 
         return value;
     }
 
     /**
-     * 获取当前预约时段剩余可约数量
+     * 获取当前预约时段剩余可约数量缓存
      */
     public int getApplyAvailableCount(Long examSiteId, Long timePeriodId) {
         int totalCount = this.getApplyTotalCount(examSiteId);
@@ -147,7 +162,7 @@ public class ApplyTaskCacheService implements CacheConstants {
     }
 
     /**
-     * 累加 某考点某时段的“已预约数量”
+     * 累加 某考点某时段的“已预约数量”缓存
      */
     public void increaseApplyFinishCount(Long examSiteId, Long timePeriodId) {
         String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
@@ -156,7 +171,7 @@ public class ApplyTaskCacheService implements CacheConstants {
     }
 
     /**
-     * 累减 某考点某时段的“已预约数量”
+     * 累减 某考点某时段的“已预约数量”缓存
      */
     public void decreaseApplyFinishCount(Long examSiteId, Long timePeriodId) {
         String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
@@ -167,24 +182,29 @@ public class ApplyTaskCacheService implements CacheConstants {
     }
 
     /**
-     * 获取某考生的 已完成预约次数
+     * 获取某考生的 已完成预约次数缓存(查数据库)
      */
-    public int getStudentApplyFinishCount(Long studentId) {
-        // Map<String, ApplyRecordCacheBean> maps = this.getStudentApplyRecords(studentId);
-        // int studentApplyFinishCount = 0;
-        // for (ApplyRecordCacheBean bean : maps.values()) {
-        //     if (!bean.getCancel()) {
-        //         studentApplyFinishCount++;
-        //     }
-        // }
-        // return studentApplyFinishCount;
-
+    public int getStudentApplyFinishCountFromDB(Long studentId) {
         LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
         wrapper.eq(StudentApplyEntity::getStudentId, studentId);
         wrapper.eq(StudentApplyEntity::getCancel, Boolean.FALSE);
         return studentApplyService.count(wrapper);
     }
 
+    /**
+     * 获取某考生的 已完成预约次数缓存
+     */
+    public int getStudentApplyFinishCount(Long studentId) {
+        Map<String, ApplyRecordCacheBean> maps = this.getStudentApplyRecords(studentId);
+        int studentApplyFinishCount = 0;
+        for (ApplyRecordCacheBean bean : maps.values()) {
+            if (!bean.getCancel()) {
+                studentApplyFinishCount++;
+            }
+        }
+        return studentApplyFinishCount;
+    }
+
     /**
      * 获取某考生的 所有的“预约记录”缓存
      */
@@ -208,7 +228,7 @@ public class ApplyTaskCacheService implements CacheConstants {
     }
 
     /**
-     * 获取某考生的 所有的“预约记录”
+     * 获取某考生的 所有的“预约记录”(查数据库)
      */
     public Map<String, ApplyRecordCacheBean> getStudentApplyRecordsFromDB(Long studentId) {
         LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
@@ -245,33 +265,44 @@ public class ApplyTaskCacheService implements CacheConstants {
         return redisClient.getForHash(cacheKey, hashKey, ApplyRecordCacheBean.class);
     }
 
+    /**
+     * 获取某考生的 某考点某时段的“预约记录”(查数据库)
+     */
+    public StudentApplyEntity getStudentApplyRecordFormDB(Long studentId, Long examSiteId, Long timePeriodId) {
+        LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(StudentApplyEntity::getExamSiteId, examSiteId);
+        wrapper.eq(StudentApplyEntity::getTimePeriodId, timePeriodId);
+        wrapper.eq(StudentApplyEntity::getStudentId, studentId);
+        return studentApplyService.getOne(wrapper);
+    }
+
     /**
      * 保存某考生的 某考点某时段的“预约记录”缓存
      */
     public void saveStudentApplyRecord(ApplyRecordCacheBean value) {
-        // String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, value.getStudentId());
-        // String hashKey = String.format("%s_%s", value.getExamSiteId(), value.getTimePeriodId());
-        // redisClient.setForHash(cacheKey, hashKey, value);
-        // log.info("SET cacheKey:{} hashKey:{} cancel:{}", cacheKey, hashKey, value.getCancel());
+        String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, value.getStudentId());
+        String hashKey = String.format("%s_%s", value.getExamSiteId(), value.getTimePeriodId());
+        redisClient.setForHash(cacheKey, hashKey, value);
+        log.info("SET cacheKey:{} hashKey:{} cancel:{}", cacheKey, hashKey, value.getCancel());
     }
 
     /**
      * 推送至考生预约记录队列
      */
     public void pushStudentApplyRecordQueue(ApplyRecordCacheBean value) {
-        // if (value == null) {
-        //     return;
-        // }
-        //
-        // RQueue<ApplyRecordCacheBean> queue = redisClient.getRedissonClient()
-        //         .getQueue(QUEUE_STUDENT_APPLY_RECORD);
-        //
-        // boolean success = queue.offer(value);
-        // log.info("{}_{}_{}_{} offerQueue:{}", value.getStudentId(), value.getExamSiteId(),
-        //         value.getTimePeriodId(), value.getCancel(), success);
-        // if (!success) {
-        //     throw new RuntimeException("推送至考生预约记录队列失败");
-        // }
+        if (value == null) {
+            return;
+        }
+
+        RQueue<ApplyRecordCacheBean> queue = redisClient.getRedissonClient()
+                .getQueue(QUEUE_STUDENT_APPLY_RECORD);
+
+        boolean success = queue.offer(value);
+        log.info("{}_{}_{}_{} offerQueue:{}", value.getStudentId(), value.getExamSiteId(),
+                value.getTimePeriodId(), value.getCancel(), success);
+        if (!success) {
+            throw new RuntimeException("推送至考生预约记录队列失败");
+        }
     }
 
     /**

+ 2 - 1
src/main/java/com/qmth/exam/reserve/controller/student/StudentApplyController.java

@@ -75,7 +75,8 @@ public class StudentApplyController extends BaseController {
             throw DefaultExceptionEnum.RATE_LIMITED.exception(Constants.REQUEST_API_BUSY);
         }
 
-        return examReserveService.getStudentApplyList(curLoginStudent(), cancel);
+        // return examReserveService.getStudentApplyList(curLoginStudent(), cancel);
+        return examReserveService.getStudentApplyListFromCache(curLoginStudent(), cancel);
     }
 
     @ApiOperation(value = "获取考生当前进行中的预约列表(首页)")

+ 1 - 1
src/main/java/com/qmth/exam/reserve/job/StudentApplyRecordJob.java

@@ -40,7 +40,7 @@ public class StudentApplyRecordJob {
      * 定时将“考生预约记录队列”中的数据保存至数据库
      * 注:每N秒执行一次
      */
-    // @Scheduled(fixedDelay = 10000, initialDelay = 30000)
+    @Scheduled(fixedDelay = 10000, initialDelay = 30000)
     public void saveStudentApplyRecordJob() {
         long start = System.currentTimeMillis();
 

+ 54 - 58
src/main/java/com/qmth/exam/reserve/service/impl/ExamReserveServiceImpl.java

@@ -157,8 +157,8 @@ public class ExamReserveServiceImpl implements ExamReserveService {
             applyTaskCacheService.increaseApplyFinishCount(examSiteId, timePeriodId);
 
             try {
-                StudentApplyEntity existApply = this.getStudentApply(student.getId(), examSiteId, timePeriodId);
-                // ApplyRecordCacheBean existApply = applyTaskCacheService.getStudentApplyRecord(student.getId(), examSiteId, timePeriodId);
+                // StudentApplyEntity existApply = applyTaskCacheService.getStudentApplyRecordFormDB(student.getId(), examSiteId, timePeriodId);
+                ApplyRecordCacheBean existApply = applyTaskCacheService.getStudentApplyRecord(student.getId(), examSiteId, timePeriodId);
                 if (existApply != null) {
                     if (!existApply.getCancel()) {
                         // 某考点某时段的“已预约数量” 累减1(释放1个被占数量)
@@ -169,36 +169,30 @@ public class ExamReserveServiceImpl implements ExamReserveService {
                     }
 
                     // 存在“已取消”预约记录,则恢复预约
-                    // existApply.setCancel(false);
-                    // existApply.setOperateId(student.getId());
-                    // existApply.setOperateTime(System.currentTimeMillis());
-                    // // 推送至预约队列
-                    // applyTaskCacheService.pushStudentApplyRecordQueue(existApply);
-                    // // 保存至预约缓存
-                    // applyTaskCacheService.saveStudentApplyRecord(existApply);
-
-                    this.updateStudentApplyForCancel(existApply.getId(), false, student.getId());
+                    existApply.setCancel(false);
+                    existApply.setOperateId(student.getId());
+                    existApply.setOperateTime(System.currentTimeMillis());
+                    // 推送至预约队列
+                    applyTaskCacheService.pushStudentApplyRecordQueue(existApply);
+                    // 保存至预约缓存
+                    applyTaskCacheService.saveStudentApplyRecord(existApply);
+
+                    // this.updateStudentApplyForCancel(existApply.getId(), false, student.getId());
                 } else {
                     // 不存在则新增预约记录
-                    // ApplyRecordCacheBean newApply = new ApplyRecordCacheBean();
-                    // newApply.setStudentId(student.getId());
-                    // newApply.setExamSiteId(examSiteId);
-                    // newApply.setTimePeriodId(timePeriodId);
-                    // newApply.setCancel(false);
-                    // newApply.setOperateId(student.getId());
-                    // newApply.setOperateTime(System.currentTimeMillis());
-                    // // 推送至预约队列
-                    // applyTaskCacheService.pushStudentApplyRecordQueue(newApply);
-                    // // 保存至预约缓存
-                    // applyTaskCacheService.saveStudentApplyRecord(newApply);
-
-                    StudentApplyEntity newApply = new StudentApplyEntity();
+                    ApplyRecordCacheBean newApply = new ApplyRecordCacheBean();
                     newApply.setStudentId(student.getId());
                     newApply.setExamSiteId(examSiteId);
                     newApply.setTimePeriodId(timePeriodId);
-                    newApply.setOperateId(student.getId());
                     newApply.setCancel(false);
-                    studentApplyService.save(newApply);
+                    newApply.setOperateId(student.getId());
+                    newApply.setOperateTime(System.currentTimeMillis());
+                    // 推送至预约队列
+                    applyTaskCacheService.pushStudentApplyRecordQueue(newApply);
+                    // 保存至预约缓存
+                    applyTaskCacheService.saveStudentApplyRecord(newApply);
+
+                    // this.addStudentApply(newApply);
                 }
 
                 log.warn("预约成功!studentId:{} examSiteId:{} timePeriodId:{}", student.getId(), examSiteId, timePeriodId);
@@ -244,18 +238,6 @@ public class ExamReserveServiceImpl implements ExamReserveService {
             return;
         }
 
-        StudentApplyEntity existApply = this.getStudentApply(student.getId(), examSiteId, timePeriodId);
-        // ApplyRecordCacheBean existApply = applyTaskCacheService.getStudentApplyRecord(student.getId(), examSiteId, timePeriodId);
-        if (existApply == null) {
-            log.warn("取消预约失败,预约记录不存在!examSiteId:{} timePeriodId:{}", examSiteId, timePeriodId);
-            return;
-        }
-
-        if (existApply.getCancel()) {
-            log.warn("当前时段已取消,忽略重复取消!examSiteId:{} timePeriodId:{}", examSiteId, timePeriodId);
-            return;
-        }
-
         TimePeriodEntity timePeriod = timePeriodService.getById(timePeriodId);
         if (timePeriod == null) {
             log.warn("取消预约失败,预约时段不存在!examSiteId:{} timePeriodId:{}", examSiteId, timePeriodId);
@@ -288,19 +270,30 @@ public class ExamReserveServiceImpl implements ExamReserveService {
                 throw new StatusException(Constants.SYSTEM_BUSY);
             }
 
-            // existApply.setCancel(true);
-            // existApply.setOperateId(student.getId());
-            // existApply.setOperateTime(System.currentTimeMillis());
-            // // 推送至预约队列
-            // applyTaskCacheService.pushStudentApplyRecordQueue(existApply);
-            // // 保存至预约缓存
-            // applyTaskCacheService.saveStudentApplyRecord(existApply);
+            // StudentApplyEntity existApply = applyTaskCacheService.getStudentApplyRecordFormDB(student.getId(), examSiteId, timePeriodId);
+            ApplyRecordCacheBean existApply = applyTaskCacheService.getStudentApplyRecord(student.getId(), examSiteId, timePeriodId);
+            if (existApply == null) {
+                log.warn("取消预约失败,预约记录不存在!examSiteId:{} timePeriodId:{}", examSiteId, timePeriodId);
+                return;
+            }
+
+            if (existApply.getCancel()) {
+                log.warn("当前时段已取消,忽略重复取消!examSiteId:{} timePeriodId:{}", examSiteId, timePeriodId);
+                return;
+            }
 
-            this.updateStudentApplyForCancel(existApply.getId(), true, student.getId());
+            existApply.setCancel(true);
+            existApply.setOperateId(student.getId());
+            existApply.setOperateTime(System.currentTimeMillis());
+            // 推送至预约队列
+            applyTaskCacheService.pushStudentApplyRecordQueue(existApply);
+            // 保存至预约缓存
+            applyTaskCacheService.saveStudentApplyRecord(existApply);
+
+            // this.updateStudentApplyForCancel(existApply.getId(), true, student.getId());
 
             // 某考点某时段的“已预约数量” 累减1
             applyTaskCacheService.decreaseApplyFinishCount(examSiteId, timePeriodId);
-
             log.warn("取消预约成功!studentId:{} examSiteId:{} timePeriodId:{}", student.getId(), examSiteId, timePeriodId);
         } finally {
             try {
@@ -315,15 +308,6 @@ public class ExamReserveServiceImpl implements ExamReserveService {
         }
     }
 
-    private StudentApplyEntity getStudentApply(Long studentId, Long examSiteId, Long timePeriodId) {
-        LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(StudentApplyEntity::getExamSiteId, examSiteId);
-        wrapper.eq(StudentApplyEntity::getTimePeriodId, timePeriodId);
-        wrapper.eq(StudentApplyEntity::getStudentId, studentId);
-        return studentApplyService.getOne(wrapper);
-    }
-
-    // @Transactional
     public void updateStudentApplyForCancel(Long applyId, boolean cancel, Long operateId) {
         LambdaUpdateWrapper<StudentApplyEntity> updateWrapper = new LambdaUpdateWrapper<>();
         updateWrapper.set(StudentApplyEntity::getCancel, cancel);
@@ -333,6 +317,16 @@ public class ExamReserveServiceImpl implements ExamReserveService {
         studentApplyService.update(updateWrapper);
     }
 
+    public void addStudentApply(ApplyRecordCacheBean bean) {
+        StudentApplyEntity newApply = new StudentApplyEntity();
+        newApply.setStudentId(bean.getStudentId());
+        newApply.setExamSiteId(bean.getExamSiteId());
+        newApply.setTimePeriodId(bean.getTimePeriodId());
+        newApply.setOperateId(bean.getStudentId());
+        newApply.setCancel(bean.getCancel());
+        studentApplyService.save(newApply);
+    }
+
     private boolean hasTicketNumber(Long studentId, Long examSiteId, Long timePeriodId) {
         LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
         wrapper.select(StudentApplyEntity::getTicketNumber);//只查询ticketNumber等字段
@@ -460,7 +454,8 @@ public class ExamReserveServiceImpl implements ExamReserveService {
     @Override
     public List<ApplyVO> getStudentApplyListForCurrent(LoginUser student) {
         // 默认过滤掉“已取消预约”的记录
-        List<ApplyVO> list = this.getStudentApplyList(student, false);
+        // List<ApplyVO> list = this.getStudentApplyList(student, false);
+        List<ApplyVO> list = this.getStudentApplyListFromCache(student, false);
 
         Date now = new Date();
         List<ApplyVO> newList = new ArrayList<>();
@@ -582,7 +577,8 @@ public class ExamReserveServiceImpl implements ExamReserveService {
         List<ExamSiteTimePeriodInfo> timePeriods = timePeriodExamSiteService.findExamSiteTimePeriods(periodQuery);
 
         // 获取某考生的 所有的“预约记录”
-        Map<String, ApplyRecordCacheBean> maps = applyTaskCacheService.getStudentApplyRecordsFromDB(student.getId());
+        // Map<String, ApplyRecordCacheBean> maps = applyTaskCacheService.getStudentApplyRecordsFromDB(student.getId());
+        Map<String, ApplyRecordCacheBean> maps = applyTaskCacheService.getStudentApplyRecords(student.getId());
         int studentApplyFinishCount = 0;
         for (ApplyRecordCacheBean bean : maps.values()) {
             if (!bean.getCancel()) {