deason 8 miesięcy temu
rodzic
commit
36e28272fb

+ 0 - 6
src/main/java/com/qmth/exam/reserve/cache/CacheConstants.java

@@ -53,12 +53,6 @@ public interface CacheConstants {
      */
     String CACHE_APPLY_AVAILABLE_COUNT = "$cache:apply_available_count:%s_%s";
 
-    /**
-     * 预约任务 - 某考点某时段的“已预约数量”的缓存
-     * $cache:apply_finish:{examSiteId}_{timePeriodId}
-     */
-    String CACHE_APPLY_FINISH = "$cache:apply_finish:%s_%s";
-
     /**
      * 某考生的“允许预约时段次数”的缓存
      * $cache:student_apply_number:{studentId}

+ 0 - 38
src/main/java/com/qmth/exam/reserve/cache/impl/ApplyTaskCacheService.java

@@ -134,24 +134,6 @@ public class ApplyTaskCacheService implements CacheConstants {
         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 = this.getApplyFinishCountFormDB(examSiteId, timePeriodId);
-        atomic.set(value);
-        atomic.expire(CACHE_TIME_OUT_30, TimeUnit.DAYS);
-        log.info("SET cacheKey:{} value:{}", cacheKey, value);
-
-        return value;
-    }
-
     /**
      * 获取某考点某时段的“剩余可约数量”缓存
      */
@@ -252,26 +234,6 @@ public class ApplyTaskCacheService implements CacheConstants {
         }
     }
 
-    /**
-     * 累加 某考点某时段的“已预约数量”缓存
-     */
-    public void increaseApplyFinishCount(Long examSiteId, Long timePeriodId) {
-        String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
-        RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
-        atomic.incrementAndGet();
-    }
-
-    /**
-     * 累减 某考点某时段的“已预约数量”缓存
-     */
-    public void decreaseApplyFinishCount(Long examSiteId, Long timePeriodId) {
-        String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
-        RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
-        if (atomic.get() > 0) {
-            atomic.decrementAndGet();
-        }
-    }
-
     /**
      * 获取某考生的 已完成预约次数缓存(查数据库)
      */

+ 18 - 18
src/main/java/com/qmth/exam/reserve/service/impl/StudentApplyServiceImpl.java

@@ -100,9 +100,6 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     @Autowired
     private CategoryCacheService categoryCacheService;
 
-    @Autowired
-    private StudentAutoAssignService studentAutoAssignService;
-
     @Autowired
     private MaterialGenerateService materialGenerateService;
 
@@ -115,9 +112,6 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     @Autowired
     private StudentApplyNoFinishExportService studentApplyNoFinishExportService;
 
-    @Autowired
-    private ApplyTaskCacheService applyTaskCacheService;
-
     @Autowired
     private StudentApplyDetailExportService studentApplyDetailExportService;
 
@@ -192,8 +186,8 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
                 log.warn("[cancel] 获取锁失败,同一个考生不允许同时操作取消预约, lockKey:{}", studentApplyLockKey);
                 throw new StatusException(Constants.SYSTEM_BUSY);
             } else {
-
-                /*  //先更新数据库
+                /*
+                //先更新数据库
                 studentApplyEntity.setCancel(Boolean.TRUE);
                 updateById(studentApplyEntity);
                 */
@@ -206,12 +200,12 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
                 bean.setOperateId(user.getId());
                 bean.setOperateTime(System.currentTimeMillis());
 
-                // 推送至预约队列
+                // 推送至预约队列
                 cacheService.pushStudentApplyRecordQueue(bean);
+                // 保存至预约缓存
                 cacheService.saveStudentApplyRecord(bean);
-
-                // 扣减redis中的数量
-                cacheService.decreaseApplyFinishCount(studentApplyEntity.getExamSiteId(), studentApplyEntity.getTimePeriodId());
+                // 某考点某时段的“剩余可约数量”(归还1个被占数量)
+                cacheService.increaseApplyAvailableCount(studentApplyEntity.getExamSiteId(), studentApplyEntity.getTimePeriodId());
             }
         } catch (Exception e) {
             log.error("[cancel] 取消预约失败, msg:{}", e.getMessage());
@@ -259,7 +253,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
             }
             log.warn("[importPreExam] 获取锁成功!lockKey:{}", CacheConstants.LOCK_AUTO_APPLY);
 
-            CurrentApplyTaskVO task = applyTaskCacheService.currentApplyTask(user.getOrgId());
+            CurrentApplyTaskVO task = cacheService.currentApplyTask(user.getOrgId());
             Date openStartTime = new Date(task.getOpenApplyStartTime());
             Date openEndTime = new Date(task.getOpenApplyEndTime());
             Date now = new Date();
@@ -522,11 +516,17 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
                     bean.setOperateId(userId);
                     bean.setOperateTime(System.currentTimeMillis());
 
-                    // 先推送至预约队列
+                    // 某考点某时段的“剩余可约数量”(抢占1个数量)
+                    boolean takeSuccess = cacheService.decreaseApplyAvailableCount(bean.getExamSiteId(), bean.getTimePeriodId());
+                    if (!takeSuccess) {
+                        log.warn("预约失败,当前预约时段已约满!examSiteId:{} timePeriodId:{} studentId:{}",
+                                bean.getExamSiteId(), bean.getTimePeriodId(), bean.getStudentId());
+                        throw new StatusException("当前预约时段已约满");
+                    }
+                    // 推送至预约队列
                     cacheService.pushStudentApplyRecordQueue(bean);
+                    // 保存至预约缓存
                     cacheService.saveStudentApplyRecord(bean);
-                    // redis已预约数量+1
-                    cacheService.increaseApplyFinishCount(agentTimeVO.getAgentId(), agentTimeVO.getTimePeriodId());
                 }
             }
 
@@ -579,7 +579,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
         int studentApplyFinishCount = haveApplyList.size();*/
 
         // 考生已经预约的次数
-        int studentApplyFinishCount = applyTaskCacheService.getStudentApplyFinishCount(studentImportVO.getStudentId());
+        int studentApplyFinishCount = cacheService.getStudentApplyFinishCount(studentImportVO.getStudentId());
         List<AgentAndTimeVO> toBeApplyTimeList = new ArrayList<>();
 
         //考生已完成预约
@@ -1155,7 +1155,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
 
     @Override
     public List<StudentApplyExport> listStudentApplyDetail(Long teachingId) {
-        CurrentApplyTaskVO curApplyTask = applyTaskCacheService.currentApplyTask(null);
+        CurrentApplyTaskVO curApplyTask = cacheService.currentApplyTask(null);
         if (curApplyTask == null) {
             return Collections.emptyList();
         }

+ 9 - 2
src/main/java/com/qmth/exam/reserve/service/impl/StudentAutoAssignServiceImpl.java

@@ -247,10 +247,17 @@ public class StudentAutoAssignServiceImpl extends ServiceImpl<StudentApplyDao, S
                         bean.setOperateId(operateId);
                         bean.setOperateTime(System.currentTimeMillis());
 
-                        // 先推送至预约队列
+                        // 某考点某时段的“剩余可约数量”(抢占1个数量)
+                        boolean takeSuccess = cacheService.decreaseApplyAvailableCount(bean.getExamSiteId(), bean.getTimePeriodId());
+                        if (!takeSuccess) {
+                            log.warn("预约失败,当前预约时段已约满!examSiteId:{} timePeriodId:{} studentId:{}",
+                                    bean.getExamSiteId(), bean.getTimePeriodId(), bean.getStudentId());
+                            throw new StatusException("当前预约时段已约满");
+                        }
+                        // 推送至预约队列
                         cacheService.pushStudentApplyRecordQueue(bean);
+                        // 保存至预约缓存
                         cacheService.saveStudentApplyRecord(bean);
-                        cacheService.increaseApplyFinishCount(siteId, timeId);
 
                         num++;