haogh 8 månader sedan
förälder
incheckning
9651cf4bf3

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

@@ -205,7 +205,8 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
                 // 推送至预约队列
                 boolean pushSuccess = cacheService.pushStudentApplyRecordQueue(bean);
                 if (!pushSuccess) {
-                    throw new RuntimeException("预约消息推送失败,请稍后再试!");
+                    log.error("[cancel] 预约消息推送失败,id:{}", id);
+                    throw new RuntimeException("取消失败,请稍后再试!");
                 }
                 // 保存至预约缓存
                 cacheService.saveStudentApplyRecord(bean);
@@ -425,6 +426,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
             }
 
             //保存数据
+            int successRow = 0;
             for (int i = 0; i < applyList.size(); i++) {
                 StudentImportVO vo = applyList.get(i);
                 try {
@@ -432,9 +434,23 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
                 } catch (StatusException e) {
                     failRecords.add(newError(i + 1, "系统异常"));
                     log.error("[importPreExam] 导入异常", e);
+                    successRow = i;
+                }
+            }
 
+            // 回滚
+            if (CollectionUtils.isNotEmpty(failRecords)) {
+                try {
+                    for (int i = 0; i <= successRow; i++) {
+                        StudentImportVO vo = applyList.get(i);
+                        rollBackStudentApply(vo, user.getId());
+                    }
+                } catch (StatusException e) {
+                    log.error("[importPreExam] 回滚异常", e);
                 }
+                return failRecords;
             }
+
         } catch (StatusException e) {
             log.error("[importPreExam] 导入预考失败,msg:{}", e.getMessage());
             throw new StatusException(e.getMessage());
@@ -452,6 +468,66 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
         return failRecords;
     }
 
+    private void rollBackStudentApply(StudentImportVO vo, Long operateId) {
+        List<AgentAndTimeVO> agentTimeList = vo.getAgentTimeList();
+        String studentApplyLockKey = String.format(CacheConstants.LOCK_STUDENT_APPLY, vo.getStudentId());
+        RLock studentApplyLock = (RLock) concurrentService.getLock(studentApplyLockKey);
+        try {
+            if (!studentApplyLock.tryLock()) {
+                log.warn("[importPreExam] 获取锁失败,考生在同时操作预约,lockKey:{}", studentApplyLockKey);
+            } else {
+                log.warn("[importPreExam] 获取锁成功,lockKey:{}", studentApplyLockKey);
+                for (AgentAndTimeVO agentTimeVO : agentTimeList) {
+                    // 队列bean
+                    ApplyRecordCacheBean bean = new ApplyRecordCacheBean();
+                    bean.setStudentId(vo.getStudentId());
+                    bean.setExamSiteId(agentTimeVO.getAgentId());
+                    bean.setTimePeriodId(agentTimeVO.getTimePeriodId());
+                    bean.setCancel(Boolean.TRUE);
+                    bean.setOperateId(operateId);
+                    bean.setOperateTime(System.currentTimeMillis());
+                    bean.setBizId(cacheService.increaseBizId());
+
+                    // 某考点某时段 归还1个刚才被占数量
+                    cacheService.increaseApplyAvailableCount(bean.getExamSiteId(), bean.getTimePeriodId());
+
+                    // 保存至预约缓存
+                    cacheService.saveStudentApplyRecord(bean);
+
+                    // 数据写入到数据库
+                    StudentApplyEntity entity = new StudentApplyEntity();
+                    entity.setStudentId(vo.getStudentId());
+                    entity.setExamSiteId(agentTimeVO.getAgentId());
+                    entity.setTimePeriodId(agentTimeVO.getTimePeriodId());
+                    entity.setCancel(Boolean.TRUE);
+                    entity.setOperateId(operateId);
+                    // 取消预约
+                    StudentApplyEntity existStudentApply = findStudentApply(entity);
+                    if (existStudentApply != null) {
+                        existStudentApply.setCancel(Boolean.TRUE);
+                        baseMapper.updateById(existStudentApply);
+                    }
+
+                }
+            }
+        } catch (Exception e) {
+            log.error("[importPreExam] 导入预约失败,msg:{}", e.getMessage());
+            throw new StatusException("导入预考失败,错误原因:" + e.getMessage());
+        } finally {
+            try {
+                if (studentApplyLock.isLocked() && studentApplyLock.isHeldByCurrentThread()) {
+                    studentApplyLock.unlock();
+                    log.info("[importPreExam] 解锁成功!lockKey:{}", studentApplyLockKey);
+                }
+            } catch (Exception e) {
+                log.warn(e.getMessage());
+            }
+        }
+
+    }
+
+
+
     private Map<Long, Map<String, TimePeriodEntity>> getTimePeriodExamSiteCache(Long taskId) {
         Map<Long, Map<String, TimePeriodEntity>> timePeriodExamSiteCache = new HashMap<>();
         List<TimePeriodExamSiteBean> timePeriodExamSiteBeans = timePeriodService.listTimePeriodByExamSiteId(taskId, null);
@@ -521,15 +597,34 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
                                 DateUtil.getStartAndEndTime(timePeriod.getStartTime(), timePeriod.getEndTime()));
                         throw new StatusException(message);
                     }
-                    // 推送至预约队列
+                  /*
+                   // 推送至预约队列
                     boolean pushSuccess = cacheService.pushStudentApplyRecordQueue(bean);
                     if (!pushSuccess) {
                         // 推送失败时,归还1个被占数量
                         cacheService.increaseApplyAvailableCount(bean.getExamSiteId(), bean.getTimePeriodId());
                         throw new RuntimeException("预约消息推送失败,请稍后再试!");
-                    }
+                    }*/
+
                     // 保存至预约缓存
                     cacheService.saveStudentApplyRecord(bean);
+
+                    // 数据写入到数据库
+                    StudentApplyEntity entity = new StudentApplyEntity();
+                    entity.setStudentId(vo.getStudentId());
+                    entity.setExamSiteId(agentTimeVO.getAgentId());
+                    entity.setTimePeriodId(agentTimeVO.getTimePeriodId());
+                    entity.setCancel(Boolean.FALSE);
+                    entity.setOperateId(userId);
+                    // 预约是否已经存在
+                    StudentApplyEntity existStudentApply = findStudentApply(entity);
+                    if (existStudentApply != null) {
+                        existStudentApply.setCancel(Boolean.FALSE);
+                        baseMapper.updateById(existStudentApply);
+                    } else {
+                        baseMapper.insert(entity);
+                    }
+
                 }
             }
         } catch (Exception e) {
@@ -547,6 +642,14 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
         }
     }
 
+    private StudentApplyEntity findStudentApply(StudentApplyEntity studentApply) {
+        LambdaQueryWrapper<StudentApplyEntity> lm = new LambdaQueryWrapper<>();
+        lm.eq(StudentApplyEntity::getExamSiteId, studentApply.getExamSiteId());
+        lm.eq(StudentApplyEntity::getTimePeriodId, studentApply.getTimePeriodId());
+        lm.eq(StudentApplyEntity::getStudentId, studentApply.getStudentId());
+        return baseMapper.selectOne(lm);
+    }
+
 
     private void checkStudentTimePeriod(StudentImportVO studentImportVO, int row, int allowCancelDays, List<Map<String, Object>> failRecords) {
         List<AgentAndTimeVO> agentTimeList = studentImportVO.getAgentTimeList();