haogh 1 月之前
父節點
當前提交
265f4d5ddc

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

@@ -96,10 +96,4 @@ public interface CacheConstants {
      */
     String LOCK_ARRANGE_EXAM = "arrange_exam:%s";
 
-    /**
-     * 考点时段操作锁
-     * $lock:exam_site_time_period:{examSiteId}
-     */
-    String LOCK_EXAM_SITE_TIME_PERIOD = "exam_site_time_period:%s";
-
 }

+ 24 - 30
src/main/java/com/qmth/exam/reserve/service/impl/ExamRoomServiceImpl.java

@@ -82,6 +82,11 @@ public class ExamRoomServiceImpl extends ServiceImpl<ExamRoomDao, ExamRoomEntity
             log.warn("[考场保存]系统自动预约中,不允许修改考场!lockKey:{}", CacheConstants.LOCK_AUTO_APPLY);
             throw new StatusException("系统正在自动预约中,不允许修改考场");
         }
+        String lockKey = String.format(CacheConstants.LOCK_EXAM_SITE_CAPACITY, req.getExamSiteId());
+        if (concurrentService.isLocked(lockKey)) {
+            log.warn("[考场保存]考点剩余可约数量更新中,不允许考生操作修改!lockKey:{}", lockKey);
+            throw new StatusException("系统正在更新可预约数量,不允许修改考场");
+        }
 
         checkExamRoom(req);
         ExamRoomEntity examRoomEntity = new ExamRoomEntity();
@@ -97,7 +102,6 @@ public class ExamRoomServiceImpl extends ServiceImpl<ExamRoomDao, ExamRoomEntity
         } else {
             beforeUpdateRoom = getById(req.getId());
             updateById(examRoomEntity);
-//            refreshApplyCountCache(examRoomEntity.getId(), examRoomEntity.getExamSiteId(), beforeUpdateRoom.getCapacity(), examRoomEntity.getCapacity());
         }
         //更新考点容量和教学点容量
         updateExamSiteAndTeachingCapacity(req.getExamSiteId());
@@ -122,8 +126,13 @@ public class ExamRoomServiceImpl extends ServiceImpl<ExamRoomDao, ExamRoomEntity
             log.warn("[考场禁用/启用]系统自动预约中,不允许修改考场!lockKey:{}", CacheConstants.LOCK_AUTO_APPLY);
             throw new StatusException("系统正在自动预约中,不允许启用/禁用考场");
         }
-
         ExamRoomEntity examRoom = getById(id);
+        String lockKey = String.format(CacheConstants.LOCK_EXAM_SITE_CAPACITY, examRoom.getExamSiteId());
+        if (concurrentService.isLocked(lockKey)) {
+            log.warn("[考场保存]考点剩余可约数量更新中,不允许考生操作修改!lockKey:{}", lockKey);
+            throw new StatusException("系统正在更新可预约数量,不允许启用/禁用考场");
+        }
+
         //考点容量
         ExamSiteEntity examSite = examSiteService.getById(examRoom.getExamSiteId());
         int oldCapacity = examSite.getCapacity();
@@ -264,12 +273,18 @@ public class ExamRoomServiceImpl extends ServiceImpl<ExamRoomDao, ExamRoomEntity
     }
 
     private void saveRoom(ExamRoomEntity room) {
+        String lockKey = String.format(CacheConstants.LOCK_EXAM_SITE_CAPACITY, room.getExamSiteId());
+        if (concurrentService.isLocked(lockKey)) {
+            log.warn("[考场保存]考点剩余可约数量更新中,不允许考生操作修改!lockKey:{}", lockKey);
+            throw new StatusException("系统正在更新可预约数量,不允许导入考场");
+        }
+
         ExamRoomEntity examRoom = getExamRoom(room.getExamSiteId(), room.getCode());
         int oldCapacity = 0;
         ExamSiteEntity examSite;
         if (examRoom != null) {
             examSite = examSiteService.getById(examRoom.getExamSiteId());
-            if(examSite == null) {
+            if (examSite == null) {
                 throw new StatusException("找不到考点");
             }
             oldCapacity = examSite.getCapacity();
@@ -316,34 +331,13 @@ public class ExamRoomServiceImpl extends ServiceImpl<ExamRoomDao, ExamRoomEntity
         Map<Long, TimePeriodExamRoomEntity> timePeriodExamRoomMap = timePeriodExamRooms.stream().collect(
                 Collectors.toMap(TimePeriodExamRoomEntity::getTimePeriodId, Function.identity()));
 
-        String examSiteTimePeriodLockKey = String.format(CacheConstants.LOCK_EXAM_SITE_TIME_PERIOD, examSiteId);
-        RLock examSiteTimePeriodLock = (RLock) concurrentService.getLock(examSiteTimePeriodLockKey);
-        try {
-            if (!examSiteTimePeriodLock.tryLock()) {
-                log.warn("[考场容量更新] 获取锁失败,不能同时修改考点考场容量, lockKey:{}", examSiteTimePeriodLock);
-                throw new StatusException("考场保存失败,请稍后再试");
-            } else {
-                for (TimePeriodEntity timePeriod : timePeriods) {
-                    //刷新缓存
-                    TimePeriodExamRoomEntity timePeriodExamRoomEntity = timePeriodExamRoomMap.get(timePeriod.getId());
-                    if (timePeriodExamRoomEntity == null || timePeriodExamRoomEntity.getEnable()) {
-                        cacheService.refreshApplyAvailableCountCache(examSiteId, timePeriod.getId(), oldCapacity, newCapacity);
-                    }
-                }
-
-            }
-        } catch (Exception e) {
-            log.error("[考场容量更新] 考场保存失败, msg:{}", e.getMessage());
-            throw new StatusException("考场保存失败,请稍后再试");
-        } finally {
-            try {
-                if (examSiteTimePeriodLock.isLocked() && examSiteTimePeriodLock.isHeldByCurrentThread()) {
-                    examSiteTimePeriodLock.unlock();
-                    log.info("[考场容量更新] 解锁成功,lockKey:{}", examSiteTimePeriodLock);
-                }
-            } catch (Exception e) {
-                log.warn(e.getMessage());
+        for (TimePeriodEntity timePeriod : timePeriods) {
+            //刷新缓存
+            TimePeriodExamRoomEntity timePeriodExamRoomEntity = timePeriodExamRoomMap.get(timePeriod.getId());
+            if (timePeriodExamRoomEntity == null || timePeriodExamRoomEntity.getEnable()) {
+                cacheService.refreshApplyAvailableCountCache(examSiteId, timePeriod.getId(), oldCapacity, newCapacity);
             }
+
         }
     }
 

+ 38 - 78
src/main/java/com/qmth/exam/reserve/service/impl/TimePeriodExamRoomServiceImpl.java

@@ -242,6 +242,12 @@ public class TimePeriodExamRoomServiceImpl extends ServiceImpl<TimePeriodExamRoo
                 throw new StatusException("保存失败,未找到考点");
             }
 
+            String lockKey = String.format(CacheConstants.LOCK_EXAM_SITE_CAPACITY, examRoom.getExamSiteId());
+            if (concurrentService.isLocked(lockKey)) {
+                log.warn("[考场保存]考点剩余可约数量更新中,不允许考生操作修改!lockKey:{}", lockKey);
+                throw new StatusException("系统正在更新可预约数量,不允许保存");
+            }
+
             // 获取考场对应的所有时段
             List<TimePeriodExamSiteBean> timePeriodList = timePeriodService.listTimePeriodByExamRoomId(curApplyTask.getTaskId(), examRoomId);
 
@@ -402,91 +408,45 @@ public class TimePeriodExamRoomServiceImpl extends ServiceImpl<TimePeriodExamRoo
     }
 
     private void refreshDisableTimePeriodCache(List<TimePeriodExamRoomEntity> disableList, ExamRoomEntity examRoom) {
-        String examSiteTimePeriodLockKey = String.format(CacheConstants.LOCK_EXAM_SITE_TIME_PERIOD, examRoom.getExamSiteId());
-        RLock examSiteTimePeriodLock = (RLock) concurrentService.getLock(examSiteTimePeriodLockKey);
-
-        try {
-            if (!examSiteTimePeriodLock.tryLock()) {
-                log.warn("[考场排班设置] 获取锁失败,不能同时修改考场排班设置, lockKey:{}", examSiteTimePeriodLock);
-                throw new StatusException("考场排班保存失败,请稍后再试");
-            } else {
-                disableList.stream()
-                        .filter(item -> !item.getEnable())
-                        .forEach(item -> {
-                            // 更新容量缓存,禁用需要减考场容量
-                            applyTaskCacheService.refreshApplyAvailableCountCache(
-                                    examRoom.getExamSiteId(),
-                                    item.getTimePeriodId(),
-                                    examRoom.getCapacity(),
-                                    0
-                            );
-                        });
-            }
-        } catch (Exception e) {
-            log.error("[考场排班设置] 考场排班失败, msg:{}", e.getMessage());
-            throw new StatusException("考场排班保存失败,请稍后再试");
-        } finally {
-            try {
-                if (examSiteTimePeriodLock.isLocked() && examSiteTimePeriodLock.isHeldByCurrentThread()) {
-                    examSiteTimePeriodLock.unlock();
-                    log.info("[考场排班设置] 解锁成功,lockKey:{}", examSiteTimePeriodLock);
-                }
-            } catch (Exception e) {
-                log.warn(e.getMessage());
-            }
-        }
-
+        disableList.stream()
+                .filter(item -> !item.getEnable())
+                .forEach(item -> {
+                    // 更新容量缓存,禁用需要减考场容量
+                    applyTaskCacheService.refreshApplyAvailableCountCache(
+                            examRoom.getExamSiteId(),
+                            item.getTimePeriodId(),
+                            examRoom.getCapacity(),
+                            0
+                    );
+                });
     }
 
     private void refreshUpdatedTimePeriodCache(List<TimePeriodExamRoomEntity> updateList,
             Map<Long, TimePeriodExamSiteBean> timePeriodMap, ExamRoomEntity examRoom) {
-        String examSiteTimePeriodLockKey = String.format(CacheConstants.LOCK_EXAM_SITE_TIME_PERIOD, examRoom.getExamSiteId());
-        RLock examSiteTimePeriodLock = (RLock) concurrentService.getLock(examSiteTimePeriodLockKey);
-
-        try {
-            if (!examSiteTimePeriodLock.tryLock()) {
-                log.warn("[考场排班设置] 获取锁失败,不能同时修改考场排班设置, lockKey:{}", examSiteTimePeriodLock);
-                throw new StatusException("考场排班保存失败,请稍后再试");
-            } else {
-                updateList.forEach(item -> {
-                    TimePeriodExamSiteBean timePeriod = timePeriodMap.get(item.getId());
-                    //由启用变为禁用,缓存容量=-考场的容量
-                    if (timePeriod != null && Boolean.TRUE.equals(timePeriod.getEnable()) && !item.getEnable()) {
-                        // 更新容量缓存
-                        applyTaskCacheService.refreshApplyAvailableCountCache(
-                                examRoom.getExamSiteId(),
-                                timePeriod.getTimePeriodId(),
-                                examRoom.getCapacity(),
-                                0
-                        );
-                    }
-
-                    //由禁用变为启用,缓存容量=+考场的容量
-                    if (timePeriod != null && Boolean.FALSE.equals(timePeriod.getEnable()) && item.getEnable()) {
-                        //更新容量缓存
-                        applyTaskCacheService.refreshApplyAvailableCountCache(
-                                examRoom.getExamSiteId(),
-                                timePeriod.getTimePeriodId(),
-                                0,
-                                examRoom.getCapacity()
-                        );
-                    }
-                });
+        updateList.forEach(item -> {
+            TimePeriodExamSiteBean timePeriod = timePeriodMap.get(item.getId());
+            //由启用变为禁用,缓存容量=-考场的容量
+            if (timePeriod != null && Boolean.TRUE.equals(timePeriod.getEnable()) && !item.getEnable()) {
+                // 更新容量缓存
+                applyTaskCacheService.refreshApplyAvailableCountCache(
+                        examRoom.getExamSiteId(),
+                        timePeriod.getTimePeriodId(),
+                        examRoom.getCapacity(),
+                        0
+                );
             }
 
-        } catch (Exception e) {
-            log.error("[考场排班设置] 考场排班失败, msg:{}", e.getMessage());
-            throw new StatusException("考场排班保存失败,请稍后再试");
-        } finally {
-            try {
-                if (examSiteTimePeriodLock.isLocked() && examSiteTimePeriodLock.isHeldByCurrentThread()) {
-                    examSiteTimePeriodLock.unlock();
-                    log.info("[考场排班设置] 解锁成功,lockKey:{}", examSiteTimePeriodLock);
-                }
-            } catch (Exception e) {
-                log.warn(e.getMessage());
+            //由禁用变为启用,缓存容量=+考场的容量
+            if (timePeriod != null && Boolean.FALSE.equals(timePeriod.getEnable()) && item.getEnable()) {
+                //更新容量缓存
+                applyTaskCacheService.refreshApplyAvailableCountCache(
+                        examRoom.getExamSiteId(),
+                        timePeriod.getTimePeriodId(),
+                        0,
+                        examRoom.getCapacity()
+                );
             }
-        }
+        });
 
     }