Просмотр исходного кода

updateTo 预约记录缓存、队列

deason 1 год назад
Родитель
Сommit
2860fef466

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

@@ -163,7 +163,7 @@ public class ApplyTaskCacheService implements CacheConstants {
             wrapper.eq(StudentApplyEntity::getStudentId, studentId);
             wrapper.eq(StudentApplyEntity::getStudentId, studentId);
             wrapper.eq(StudentApplyEntity::getCancel, Boolean.FALSE);
             wrapper.eq(StudentApplyEntity::getCancel, Boolean.FALSE);
             return studentApplyService.count(wrapper);
             return studentApplyService.count(wrapper);
-        }
+        } // todo不走数据库查询,以redis中数据为准
 
 
         Map<String, ApplyRecordCacheBean> maps = this.getStudentApplyRecords(studentId);
         Map<String, ApplyRecordCacheBean> maps = this.getStudentApplyRecords(studentId);
         int studentApplyFinishCount = 0;
         int studentApplyFinishCount = 0;
@@ -204,7 +204,7 @@ public class ApplyTaskCacheService implements CacheConstants {
                 redisClient.expire(cacheKey, 30, TimeUnit.DAYS);
                 redisClient.expire(cacheKey, 30, TimeUnit.DAYS);
             }
             }
             return maps;
             return maps;
-        }
+        } // todo不走数据库查询,以redis中数据为准
 
 
         return redisClient.getEntriesForHash(cacheKey, ApplyRecordCacheBean.class);
         return redisClient.getEntriesForHash(cacheKey, ApplyRecordCacheBean.class);
     }
     }
@@ -225,7 +225,7 @@ public class ApplyTaskCacheService implements CacheConstants {
         String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, value.getStudentId());
         String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, value.getStudentId());
         String hashKey = String.format("%s_%s", value.getExamSiteId(), value.getTimePeriodId());
         String hashKey = String.format("%s_%s", value.getExamSiteId(), value.getTimePeriodId());
         redisClient.setForHash(cacheKey, hashKey, value);
         redisClient.setForHash(cacheKey, hashKey, value);
-        log.info("SET cacheKey:{} hashKey:{}", cacheKey, hashKey);
+        log.info("SET cacheKey:{} hashKey:{} cancel:{}", cacheKey, hashKey, value.getCancel());
     }
     }
 
 
     /**
     /**

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

@@ -35,7 +35,7 @@ public class StudentApplyRecordJob {
      * 定时将“考生预约记录队列”中的数据保存至数据库
      * 定时将“考生预约记录队列”中的数据保存至数据库
      * 注:每N秒执行一次
      * 注:每N秒执行一次
      */
      */
-    @Scheduled(fixedDelay = 30000, initialDelay = 30000)
+    @Scheduled(fixedDelay = 10000, initialDelay = 30000)
     public void saveStudentApplyRecordJob() {
     public void saveStudentApplyRecordJob() {
         long start = System.currentTimeMillis();
         long start = System.currentTimeMillis();
 
 
@@ -84,7 +84,7 @@ public class StudentApplyRecordJob {
                 bean.getTimePeriodId(), bean.getCancel());
                 bean.getTimePeriodId(), bean.getCancel());
         if (existEntity != null) {
         if (existEntity != null) {
             if (!existEntity.getCancel()) {
             if (!existEntity.getCancel()) {
-                log.debug("{} 预约已存在,跳过!", msg);
+                log.info("{} 预约已存在,跳过!", msg);
                 return;
                 return;
             }
             }
 
 
@@ -95,7 +95,7 @@ public class StudentApplyRecordJob {
             updateWrapper.set(StudentApplyEntity::getUpdateTime, bean.getOperateTime());
             updateWrapper.set(StudentApplyEntity::getUpdateTime, bean.getOperateTime());
             updateWrapper.eq(StudentApplyEntity::getId, existEntity.getId());
             updateWrapper.eq(StudentApplyEntity::getId, existEntity.getId());
             studentApplyService.update(updateWrapper);
             studentApplyService.update(updateWrapper);
-            log.debug("{} 预约修改!", msg);
+            log.info("{} 预约修改!", msg);
         } else {
         } else {
             // 不存在则新增预约记录
             // 不存在则新增预约记录
             StudentApplyEntity entity = new StudentApplyEntity();
             StudentApplyEntity entity = new StudentApplyEntity();
@@ -107,7 +107,7 @@ public class StudentApplyRecordJob {
             entity.setCreateTime(bean.getOperateTime());
             entity.setCreateTime(bean.getOperateTime());
             entity.setUpdateTime(bean.getOperateTime());
             entity.setUpdateTime(bean.getOperateTime());
             studentApplyService.save(entity);
             studentApplyService.save(entity);
-            log.debug("{} 预约新增!", msg);
+            log.info("{} 预约新增!", msg);
         }
         }
     }
     }
 
 

+ 62 - 33
src/main/java/com/qmth/exam/reserve/service/impl/ExamReserveServiceImpl.java

@@ -6,10 +6,7 @@ import com.qmth.boot.core.concurrent.service.ConcurrentService;
 import com.qmth.boot.core.exception.StatusException;
 import com.qmth.boot.core.exception.StatusException;
 import com.qmth.exam.reserve.bean.Constants;
 import com.qmth.exam.reserve.bean.Constants;
 import com.qmth.exam.reserve.bean.RichTextBean;
 import com.qmth.exam.reserve.bean.RichTextBean;
-import com.qmth.exam.reserve.bean.apply.ApplyTimePeriodResult;
-import com.qmth.exam.reserve.bean.apply.ApplyVO;
-import com.qmth.exam.reserve.bean.apply.TicketInfo;
-import com.qmth.exam.reserve.bean.apply.TimePeriodInfo;
+import com.qmth.exam.reserve.bean.apply.*;
 import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO;
 import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO;
 import com.qmth.exam.reserve.bean.login.LoginUser;
 import com.qmth.exam.reserve.bean.login.LoginUser;
 import com.qmth.exam.reserve.cache.CacheConstants;
 import com.qmth.exam.reserve.cache.CacheConstants;
@@ -121,9 +118,9 @@ public class ExamReserveServiceImpl implements ExamReserveService {
             }
             }
 
 
             // 当前预约时段剩余可约数量
             // 当前预约时段剩余可约数量
-            int availableCount = this.getApplyAvailableCount(examSiteId, timePeriod.getId());
+            int availableCount = this.getApplyAvailableCount(examSiteId, timePeriodId);
             if (availableCount < 1) {
             if (availableCount < 1) {
-                log.warn("预约失败,当前预约时段已约满!examSiteId:{} timePeriodId:{}", examSiteId, timePeriod.getId());
+                log.warn("预约失败,当前预约时段已约满!examSiteId:{} timePeriodId:{}", examSiteId, timePeriodId);
                 throw new StatusException("当前预约时段已约满");
                 throw new StatusException("当前预约时段已约满");
             }
             }
 
 
@@ -131,29 +128,41 @@ public class ExamReserveServiceImpl implements ExamReserveService {
             applyTaskCacheService.increaseApplyFinishCount(examSiteId, timePeriodId);
             applyTaskCacheService.increaseApplyFinishCount(examSiteId, timePeriodId);
 
 
             try {
             try {
-                StudentApplyEntity existEntity = this.getStudentApply(student.getId(), examSiteId, timePeriodId);
-                if (existEntity != null) {
-                    if (!existEntity.getCancel()) {
+                // StudentApplyEntity existApply = this.getStudentApply(student.getId(), examSiteId, timePeriodId);
+                ApplyRecordCacheBean existApply = applyTaskCacheService.getStudentApplyRecord(student.getId(), examSiteId, timePeriodId);
+                if (existApply != null) {
+                    if (!existApply.getCancel()) {
                         // 某考点某时段的“已预约数量” 累减1(释放1个被占数量)
                         // 某考点某时段的“已预约数量” 累减1(释放1个被占数量)
                         applyTaskCacheService.decreaseApplyFinishCount(examSiteId, timePeriodId);
                         applyTaskCacheService.decreaseApplyFinishCount(examSiteId, timePeriodId);
 
 
-                        log.warn("预约失败,当前时段已预约,请勿重复预约!applyId:{}", existEntity.getId());
+                        log.warn("预约失败,当前时段已预约,请勿重复预约!examSiteId:{} timePeriodId:{}", examSiteId, timePeriodId);
                         throw new StatusException("当前时段已预约,请勿重复预约");
                         throw new StatusException("当前时段已预约,请勿重复预约");
                     }
                     }
 
 
                     // 存在“已取消”预约记录,则恢复预约
                     // 存在“已取消”预约记录,则恢复预约
-                    this.updateStudentApplyForCancel(existEntity.getId(), false, student.getId());
+                    existApply.setCancel(false);
+                    existApply.setOperateId(student.getId());
+                    existApply.setOperateTime(System.currentTimeMillis());
+                    applyTaskCacheService.saveStudentApplyRecord(existApply);
+
+                    // 推送至预约队列
+                    applyTaskCacheService.pushStudentApplyRecordQueue(existApply);
                 } else {
                 } else {
                     // 不存在则新增预约记录
                     // 不存在则新增预约记录
-                    StudentApplyEntity entity = new StudentApplyEntity();
-                    entity.setStudentId(student.getId());
-                    entity.setExamSiteId(examSiteId);
-                    entity.setTimePeriodId(timePeriodId);
-                    entity.setOperateId(student.getId());
-                    entity.setCancel(false);
-                    studentApplyService.save(entity);
-                    log.warn("新增考生预约记录!applyId:{} examSiteId:{} timePeriodId:{}", entity.getId(), examSiteId, timePeriodId);
+                    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.saveStudentApplyRecord(newApply);
+
+                    // 推送至预约队列
+                    applyTaskCacheService.pushStudentApplyRecordQueue(newApply);
                 }
                 }
+
+                log.warn("预约成功!studentId:{} examSiteId:{} timePeriodId:{}", student.getId(), examSiteId, timePeriodId);
             } catch (Exception e) {
             } catch (Exception e) {
                 if (e instanceof StatusException) {
                 if (e instanceof StatusException) {
                     throw e;
                     throw e;
@@ -161,7 +170,7 @@ public class ExamReserveServiceImpl implements ExamReserveService {
 
 
                 // 异常时,释放1个被占数量
                 // 异常时,释放1个被占数量
                 applyTaskCacheService.decreaseApplyFinishCount(examSiteId, timePeriodId);
                 applyTaskCacheService.decreaseApplyFinishCount(examSiteId, timePeriodId);
-                log.error("预约结果保存失败!examSiteId:{} timePeriodId:{} err:{}", examSiteId, timePeriodId, e.getMessage());
+                log.error("预约异常!examSiteId:{} timePeriodId:{} err:{}", examSiteId, timePeriodId, e.getMessage());
                 throw new StatusException("预约失败,请稍后再试!", e);
                 throw new StatusException("预约失败,请稍后再试!", e);
             }
             }
         } finally {
         } finally {
@@ -193,14 +202,15 @@ public class ExamReserveServiceImpl implements ExamReserveService {
             return;
             return;
         }
         }
 
 
-        StudentApplyEntity studentApply = this.getStudentApply(student.getId(), examSiteId, timePeriodId);
-        if (studentApply == null) {
+        // 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);
             log.warn("取消预约失败,预约记录不存在!examSiteId:{} timePeriodId:{}", examSiteId, timePeriodId);
             return;
             return;
         }
         }
 
 
-        if (studentApply.getCancel()) {
-            log.warn("当前时段已取消预约,忽略重复取消!examSiteId:{} timePeriodId:{}", examSiteId, timePeriodId);
+        if (existApply.getCancel()) {
+            log.warn("取消预约失败,当前时段已取消,忽略重复取消!examSiteId:{} timePeriodId:{}", examSiteId, timePeriodId);
             return;
             return;
         }
         }
 
 
@@ -225,7 +235,7 @@ public class ExamReserveServiceImpl implements ExamReserveService {
         Lock studentApplyLock = concurrentService.getLock(studentApplyLockKey);
         Lock studentApplyLock = concurrentService.getLock(studentApplyLockKey);
         try {
         try {
             if (!studentApplyLock.tryLock()) {
             if (!studentApplyLock.tryLock()) {
-                log.warn("获取锁失败,不允许同一个考生同时操作预约!lockKey:{}", studentApplyLockKey);
+                log.warn("获取锁失败,同一个考生不允许同时操作预约!lockKey:{}", studentApplyLockKey);
                 throw new StatusException(Constants.SYSTEM_BUSY);
                 throw new StatusException(Constants.SYSTEM_BUSY);
             }
             }
 
 
@@ -235,12 +245,19 @@ public class ExamReserveServiceImpl implements ExamReserveService {
                 throw new StatusException(Constants.SYSTEM_BUSY);
                 throw new StatusException(Constants.SYSTEM_BUSY);
             }
             }
 
 
-            this.updateStudentApplyForCancel(studentApply.getId(), true, student.getId());
+            existApply.setCancel(true);
+            existApply.setOperateId(student.getId());
+            existApply.setOperateTime(System.currentTimeMillis());
+            applyTaskCacheService.saveStudentApplyRecord(existApply);
+
+            // 推送至预约队列
+            applyTaskCacheService.pushStudentApplyRecordQueue(existApply);
 
 
             // 某考点某时段的“已预约数量” 累减1
             // 某考点某时段的“已预约数量” 累减1
-            applyTaskCacheService.decreaseApplyFinishCount(studentApply.getExamSiteId(), studentApply.getTimePeriodId());
+            applyTaskCacheService.decreaseApplyFinishCount(examSiteId, timePeriodId);
 
 
             // todo 取消预约事件
             // todo 取消预约事件
+            log.warn("取消预约成功!studentId:{} examSiteId:{} timePeriodId:{}", student.getId(), examSiteId, timePeriodId);
         } finally {
         } finally {
             try {
             try {
                 studentApplyLock.unlock();
                 studentApplyLock.unlock();
@@ -417,6 +434,19 @@ public class ExamReserveServiceImpl implements ExamReserveService {
         wrapper.between(TimePeriodEntity::getStartTime, startTime.getTime(), endTime.getTime());
         wrapper.between(TimePeriodEntity::getStartTime, startTime.getTime(), endTime.getTime());
         List<TimePeriodEntity> timePeriods = timePeriodService.list(wrapper);
         List<TimePeriodEntity> timePeriods = timePeriodService.list(wrapper);
 
 
+        // 获取某考生的 所有的“预约记录”
+        Map<String, ApplyRecordCacheBean> maps = applyTaskCacheService.getStudentApplyRecords(student.getId());
+        int studentApplyFinishCount = 0;
+        for (ApplyRecordCacheBean bean : maps.values()) {
+            if (!bean.getCancel()) {
+                studentApplyFinishCount++;
+            }
+        }
+
+        // 考生剩余预约次数 = 允许预约次数 - 已完成预约次数
+        int studentApplyNumber = applyTaskCacheService.getStudentApplyNumber(student.getId());
+        int unApplyNumber = studentApplyNumber - studentApplyFinishCount;
+
         List<TimePeriodInfo> periodList = new ArrayList<>();
         List<TimePeriodInfo> periodList = new ArrayList<>();
         if (CollectionUtils.isNotEmpty(timePeriods)) {
         if (CollectionUtils.isNotEmpty(timePeriods)) {
             for (TimePeriodEntity timePeriod : timePeriods) {
             for (TimePeriodEntity timePeriod : timePeriods) {
@@ -433,17 +463,16 @@ public class ExamReserveServiceImpl implements ExamReserveService {
                     info.setStatus(ApplyStatus.AVAILABLE);
                     info.setStatus(ApplyStatus.AVAILABLE);
                 }
                 }
 
 
-                // todo 已约
+                String hashKey = String.format("%s_%s", examSiteId, timePeriod.getId());
+                ApplyRecordCacheBean existApply = maps.get(hashKey);
+                if (existApply != null && !existApply.getCancel()) {
+                    info.setStatus(ApplyStatus.FINISHED);
+                }
 
 
                 periodList.add(info);
                 periodList.add(info);
             }
             }
         }
         }
 
 
-        // 考生剩余预约次数 = 允许预约次数 - 已完成预约次数
-        int studentApplyNumber = applyTaskCacheService.getStudentApplyNumber(student.getId());
-        int studentApplyFinishCount = applyTaskCacheService.getStudentApplyFinishCount(student.getId());
-        int unApplyNumber = studentApplyNumber - studentApplyFinishCount;
-
         ApplyTimePeriodResult result = new ApplyTimePeriodResult();
         ApplyTimePeriodResult result = new ApplyTimePeriodResult();
         result.setUnApplyNumber(Math.max(unApplyNumber, 0));
         result.setUnApplyNumber(Math.max(unApplyNumber, 0));
         result.setPeriodList(periodList);
         result.setPeriodList(periodList);