haogh 4 weeks ago
parent
commit
7a8173d58e

+ 105 - 0
src/main/java/com/qmth/exam/reserve/service/impl/ExamRoomServiceImpl.java

@@ -11,18 +11,23 @@ import com.qmth.boot.core.exception.StatusException;
 import com.qmth.boot.tools.excel.ExcelReader;
 import com.qmth.boot.tools.excel.enums.ExcelType;
 import com.qmth.boot.tools.excel.model.DataMap;
+import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO;
 import com.qmth.exam.reserve.bean.login.LoginUser;
+import com.qmth.exam.reserve.bean.org.OrgInfo;
 import com.qmth.exam.reserve.bean.room.ExamRoomReq;
 import com.qmth.exam.reserve.bean.room.ExamRoomSaveReq;
 import com.qmth.exam.reserve.bean.room.ExamRoomVO;
+import com.qmth.exam.reserve.bean.timeperiod.TimePeriodExamSiteBean;
 import com.qmth.exam.reserve.cache.CacheConstants;
 import com.qmth.exam.reserve.cache.impl.ApplyTaskCacheService;
+import com.qmth.exam.reserve.cache.impl.OrgCacheService;
 import com.qmth.exam.reserve.dao.ExamRoomDao;
 import com.qmth.exam.reserve.entity.ExamRoomEntity;
 import com.qmth.exam.reserve.entity.ExamSiteEntity;
 import com.qmth.exam.reserve.entity.TimePeriodEntity;
 import com.qmth.exam.reserve.entity.TimePeriodExamRoomEntity;
 import com.qmth.exam.reserve.service.*;
+import com.qmth.exam.reserve.util.DateUtil;
 import com.qmth.exam.reserve.util.PageUtil;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -65,6 +70,9 @@ public class ExamRoomServiceImpl extends ServiceImpl<ExamRoomDao, ExamRoomEntity
     @Autowired
     private ConcurrentService concurrentService;
 
+    @Autowired
+    private OrgCacheService orgCacheService;
+
 
     @Override
     public PageResult<ExamRoomVO> pageExamRoom(ExamRoomReq req) {
@@ -106,6 +114,11 @@ public class ExamRoomServiceImpl extends ServiceImpl<ExamRoomDao, ExamRoomEntity
             save(examRoomEntity);
         } else {
             beforeUpdateRoom = getById(req.getId());
+
+            if (req.getCapacity() != null && beforeUpdateRoom.getCapacity() != null && beforeUpdateRoom.getCapacity() > req.getCapacity()) {
+                checkCapacity(beforeUpdateRoom, beforeUpdateRoom.getCapacity() - req.getCapacity());
+            }
+
             updateById(examRoomEntity);
         }
         //更新考点容量和教学点容量
@@ -123,6 +136,87 @@ public class ExamRoomServiceImpl extends ServiceImpl<ExamRoomDao, ExamRoomEntity
         doRefreshApplyCountCache(examRoomEntity.getId(), examRoomEntity.getExamSiteId(), oldCapacity, newCapacity);
     }
 
+    private void checkCapacity(ExamRoomEntity beforeUpdateRoom, Integer decreaseCapacity) {
+        if (beforeUpdateRoom == null || beforeUpdateRoom.getExamSiteId() == null) {
+            throw new StatusException("考场信息不完整");
+        }
+
+        OrgInfo org = orgCacheService.currentOrg();
+        if (org == null) {
+            throw new StatusException("当前机构不存在");
+        }
+
+        CurrentApplyTaskVO curApplyTask = cacheService.currentApplyTask(org.getOrgId());
+        if (curApplyTask == null) {
+            return;
+        }
+
+        List<TimePeriodExamSiteBean> timePeriodExamSiteBeans = timePeriodService.listTimePeriodByTask(curApplyTask.getTaskId());
+        if (CollectionUtils.isEmpty(timePeriodExamSiteBeans)) {
+            return;
+        }
+
+        //过滤已不可取消的时段
+        timePeriodExamSiteBeans = filterTimePeriod(timePeriodExamSiteBeans, curApplyTask);
+        if (CollectionUtils.isEmpty(timePeriodExamSiteBeans)) {
+            return;
+        }
+
+        //获取更新前的时段容量
+        Map<Long, Integer> oldCapacityMap = new HashMap<>();
+        for (TimePeriodExamSiteBean timePeriod : timePeriodExamSiteBeans) {
+            Long periodId = timePeriod.getTimePeriodId();
+            int capacity = examSiteService.getExamSiteTimePeriodCapacity(beforeUpdateRoom.getExamSiteId(), periodId);
+            oldCapacityMap.put(periodId, capacity);
+        }
+
+        for (TimePeriodExamSiteBean bean : timePeriodExamSiteBeans) {
+            Long periodId = bean.getTimePeriodId();
+            //剩余的容量
+            int availableCount = cacheService.getApplyAvailableCount(beforeUpdateRoom.getExamSiteId(), periodId);
+            //修改前的容量
+            int oldCount = oldCapacityMap.getOrDefault(periodId, 0);
+            //已预约的容量
+            int haveApplyCount = oldCount - availableCount;
+            //即将更新之后的剩余容量
+            int remainCount = oldCount - decreaseCapacity;
+            if (haveApplyCount > remainCount) {
+                TimePeriodEntity timePeriod = timePeriodService.getById(periodId);
+                if (timePeriod == null) {
+                    continue;
+                }
+                String dateStr = DateUtil.getShortDateByLongTime(timePeriod.getStartTime());
+                String timeStr = DateUtil.getStartToEndTime(timePeriod.getStartTime(), timePeriod.getEndTime());
+                String errorMessage = String.format("时段:%s %s,已预约%d人,当前已预约人数超出剩余容量%d人,无法缩小考场容量。", dateStr, timeStr,
+                        haveApplyCount, remainCount);
+                log.error(errorMessage);
+                throw new StatusException(errorMessage);
+            }
+        }
+    }
+
+    private List<TimePeriodExamSiteBean> filterTimePeriod(List<TimePeriodExamSiteBean> timePeriodExamRoomList, CurrentApplyTaskVO curApplyTask) {
+        List<TimePeriodExamSiteBean> resultList = new ArrayList<>();
+        if (CollectionUtils.isNotEmpty(timePeriodExamRoomList)) {
+            Long longToday = DateUtil.getLongTimeByDate(DateUtil.formatShortSplitDateString(new Date()) + " 00:00:00");
+            Date today = new Date(longToday);
+            Date otherDay = DateUtil.addValues(today, Calendar.DAY_OF_MONTH, curApplyTask.getAllowApplyCancelDays());
+            long longOtherDay = DateUtil.getLongTimeByDate(DateUtil.formatShortSplitDateString(otherDay) + " 23:59:59");
+            // 当前时间
+            long now = System.currentTimeMillis();
+
+            for (TimePeriodExamSiteBean bean : timePeriodExamRoomList) {
+                if (isTimeEditable(now, bean.getEndTime(), longOtherDay, bean.getStartTime())) {
+                    resultList.add(bean);
+                }
+            }
+        }
+        return resultList;
+    }
+
+    private boolean isTimeEditable(long now, long endTime, long longOtherDay, long startTime) {
+        return !(endTime < now || startTime < longOtherDay);
+    }
 
 
     @Transactional
@@ -143,6 +237,11 @@ public class ExamRoomServiceImpl extends ServiceImpl<ExamRoomDao, ExamRoomEntity
         ExamSiteEntity examSite = examSiteService.getById(examRoom.getExamSiteId());
         int oldCapacity = examSite.getCapacity();
 
+        //禁用的时候,判断容量
+        if (!enable) {
+            checkCapacity(examRoom, examRoom.getCapacity());
+        }
+
         LambdaUpdateWrapper<ExamRoomEntity> wrapper = new LambdaUpdateWrapper<>();
         wrapper.set(ExamRoomEntity::getEnable, enable);
         wrapper.eq(ExamRoomEntity::getId, id);
@@ -296,6 +395,12 @@ public class ExamRoomServiceImpl extends ServiceImpl<ExamRoomDao, ExamRoomEntity
 
         ExamRoomEntity examRoom = getExamRoom(room.getExamSiteId(), room.getCode());
         if (examRoom != null) {
+
+            // 校验容量
+            if (examRoom.getCapacity() != null && room.getCapacity() != null && examRoom.getCapacity() > room.getCapacity()) {
+                checkCapacity(examRoom, examRoom.getCapacity() - room.getCapacity());
+            }
+
             roomId = examRoom.getId();
             examRoom.setCapacity(room.getCapacity());
             LambdaUpdateWrapper<ExamRoomEntity> wrapper = new LambdaUpdateWrapper<>();