deason 1 an în urmă
părinte
comite
6c4955f70c

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

@@ -4,6 +4,7 @@ import com.qmth.exam.reserve.cache.CacheConstants;
 import com.qmth.exam.reserve.cache.RedisClient;
 import com.qmth.exam.reserve.service.ExamSiteService;
 import com.qmth.exam.reserve.service.StudentApplyService;
+import org.redisson.api.RAtomicLong;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -38,6 +39,7 @@ public class ApplyTaskCacheService implements CacheConstants {
         // 当前考点当前时段容量 = 当前考点总容量
         value = examSiteService.countExamSiteCapacityById(examSiteId);
         redisClient.set(cacheKey, value, 1, TimeUnit.DAYS);
+        log.info("{} = {}", cacheKey, value);
 
         return value;
     }
@@ -47,13 +49,16 @@ public class ApplyTaskCacheService implements CacheConstants {
      */
     public int getApplyFinishCount(Long examSiteId, Long timePeriodId) {
         String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
-        Integer value = redisClient.get(cacheKey, Integer.class);
-        if (value != null) {
-            return value;
+        if (redisClient.exist(cacheKey)) {
+            return (int) redisClient.getRedissonClient().getAtomicLong(cacheKey).get();
         }
 
-        value = studentApplyService.countApplyFinishForExamSiteAndTimePeriod(examSiteId, timePeriodId);
-        redisClient.set(cacheKey, value, 1, TimeUnit.DAYS);
+        RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
+
+        int value = studentApplyService.countApplyFinishForExamSiteAndTimePeriod(examSiteId, timePeriodId);
+        atomic.set(value);
+        atomic.expire(30, TimeUnit.DAYS);
+        log.info("{} = {}", cacheKey, value);
 
         return value;
     }
@@ -62,14 +67,18 @@ public class ApplyTaskCacheService implements CacheConstants {
      * 累加 某考点某时段的“已预约数量”
      */
     public void increaseApplyFinishCount(Long examSiteId, Long timePeriodId) {
-        // redisClient.getRedissonClient().getAtomicLong(cacheKey);
+        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);
+        atomic.decrementAndGet();
     }
 
 }

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

@@ -120,7 +120,7 @@ public class ExamReserveServiceImpl implements ExamReserveService {
             if (stu.getApplyNumber() - studentApplyFinishCount < 1) {
                 String msg = "当前学生无剩余可约时段次数,已完成预约" + stu.getApplyNumber() + "次";
                 log.warn(msg);
-                // throw new StatusException(msg);todo
+                throw new StatusException(msg);
             }
 
             // 当前预约时段剩余可约数量