deason 1 an în urmă
părinte
comite
57434e2db6

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

@@ -14,7 +14,6 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
-import org.springframework.transaction.annotation.Transactional;
 
 import java.util.concurrent.locks.Lock;
 
@@ -36,7 +35,7 @@ public class StudentApplyRecordJob {
      * 定时将“考生预约记录队列”中的数据保存至数据库
      * 注:每N秒执行一次
      */
-    // @Scheduled(fixedDelay = 30000, initialDelay = 30000)
+    @Scheduled(fixedDelay = 30000, initialDelay = 30000)
     public void saveStudentApplyRecordJob() {
         long start = System.currentTimeMillis();
 
@@ -73,16 +72,19 @@ public class StudentApplyRecordJob {
         }
     }
 
-    @Transactional
+    // @Transactional
     public void saveOrUpdate(ApplyRecordCacheBean bean) {
         LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(StudentApplyEntity::getStudentId, bean.getStudentId());
         wrapper.eq(StudentApplyEntity::getExamSiteId, bean.getExamSiteId());
         wrapper.eq(StudentApplyEntity::getTimePeriodId, bean.getTimePeriodId());
+        wrapper.eq(StudentApplyEntity::getStudentId, bean.getStudentId());
         StudentApplyEntity existEntity = studentApplyService.getOne(wrapper);
+
+        String msg = String.format("%s_%s_%s_%s", bean.getStudentId(), bean.getExamSiteId(),
+                bean.getTimePeriodId(), bean.getCancel());
         if (existEntity != null) {
             if (!existEntity.getCancel()) {
-                log.debug("{}_{}_{}_{} db exist", bean.getStudentId(), bean.getExamSiteId(), bean.getTimePeriodId(), bean.getCancel());
+                log.debug("{} 预约已存在,跳过!", msg);
                 return;
             }
 
@@ -93,7 +95,7 @@ public class StudentApplyRecordJob {
             updateWrapper.set(StudentApplyEntity::getUpdateTime, bean.getOperateTime());
             updateWrapper.eq(StudentApplyEntity::getId, existEntity.getId());
             studentApplyService.update(updateWrapper);
-            log.debug("{}_{}_{}_{} db update", bean.getStudentId(), bean.getExamSiteId(), bean.getTimePeriodId(), bean.getCancel());
+            log.debug("{} 预约修改!", msg);
         } else {
             // 不存在则新增预约记录
             StudentApplyEntity entity = new StudentApplyEntity();
@@ -105,7 +107,7 @@ public class StudentApplyRecordJob {
             entity.setCreateTime(bean.getOperateTime());
             entity.setUpdateTime(bean.getOperateTime());
             studentApplyService.save(entity);
-            log.debug("{}_{}_{}_{} db insert", bean.getStudentId(), bean.getExamSiteId(), bean.getTimePeriodId(), bean.getCancel());
+            log.debug("{} 预约新增!", msg);
         }
     }
 

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

@@ -100,7 +100,7 @@ public class ExamReserveServiceImpl implements ExamReserveService {
         Lock studentApplyLock = concurrentService.getLock(studentApplyLockKey);
         try {
             if (!studentApplyLock.tryLock()) {
-                log.warn("获取锁失败,不允许同一个考生同时操作预约!lockKey:{}", studentApplyLockKey);
+                log.warn("获取锁失败,同一个考生不允许同时操作预约!lockKey:{}", studentApplyLockKey);
                 throw new StatusException(Constants.SYSTEM_BUSY);
             }
 
@@ -131,11 +131,7 @@ public class ExamReserveServiceImpl implements ExamReserveService {
             applyTaskCacheService.increaseApplyFinishCount(examSiteId, timePeriodId);
 
             try {
-                LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
-                wrapper.eq(StudentApplyEntity::getStudentId, student.getId());
-                wrapper.eq(StudentApplyEntity::getExamSiteId, examSiteId);
-                wrapper.eq(StudentApplyEntity::getTimePeriodId, timePeriodId);
-                StudentApplyEntity existEntity = studentApplyService.getOne(wrapper);
+                StudentApplyEntity existEntity = this.getStudentApply(student.getId(), examSiteId, timePeriodId);
                 if (existEntity != null) {
                     if (!existEntity.getCancel()) {
                         // 某考点某时段的“已预约数量” 累减1(释放1个被占数量)
@@ -197,11 +193,7 @@ public class ExamReserveServiceImpl implements ExamReserveService {
             return;
         }
 
-        LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(StudentApplyEntity::getExamSiteId, examSiteId);
-        wrapper.eq(StudentApplyEntity::getTimePeriodId, timePeriodId);
-        wrapper.eq(StudentApplyEntity::getStudentId, student.getId());
-        StudentApplyEntity studentApply = studentApplyService.getOne(wrapper);
+        StudentApplyEntity studentApply = this.getStudentApply(student.getId(), examSiteId, timePeriodId);
         if (studentApply == null) {
             log.warn("取消预约失败,预约记录不存在!examSiteId:{} timePeriodId:{}", examSiteId, timePeriodId);
             return;
@@ -258,6 +250,14 @@ public class ExamReserveServiceImpl implements ExamReserveService {
         }
     }
 
+    private StudentApplyEntity getStudentApply(Long studentId, Long examSiteId, Long timePeriodId) {
+        LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(StudentApplyEntity::getExamSiteId, examSiteId);
+        wrapper.eq(StudentApplyEntity::getTimePeriodId, timePeriodId);
+        wrapper.eq(StudentApplyEntity::getStudentId, studentId);
+        return studentApplyService.getOne(wrapper);
+    }
+
     private void updateStudentApplyForCancel(Long applyId, boolean cancel, Long operateId) {
         LambdaUpdateWrapper<StudentApplyEntity> updateWrapper = new LambdaUpdateWrapper<>();
         updateWrapper.set(StudentApplyEntity::getCancel, cancel);