Просмотр исходного кода

同步考生添加考试控制全局锁,同步数据后,清理考试缓存

lideyin 5 лет назад
Родитель
Сommit
c6f6a5581e

+ 28 - 16
examcloud-core-oe-admin-api-provider/src/main/java/cn/com/qmth/examcloud/core/oe/admin/api/provider/SyncCloudServiceProvider.java

@@ -2,6 +2,8 @@ package cn.com.qmth.examcloud.core.oe.admin.api.provider;
 
 import java.util.List;
 
+import cn.com.qmth.examcloud.support.Constants;
+import cn.com.qmth.examcloud.web.helpers.SequenceLockHelper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -67,23 +69,33 @@ public class SyncCloudServiceProvider  extends ControllerSupport implements Hand
 	@ApiOperation(value = "同步考生")
     @PostMapping("/syncExamStudent")
     public SyncExamStudentResp syncExamStudent(@RequestBody SyncExamStudentReq req) {
-    	if("delete".equals(req.getSyncType())){
-    		ExamStudentEntity examStudent = examStudentRepo.findByExamStudentId(req.getId());
-    		if(examStudent != null && (examStudent.getFinished() == null || !examStudent.getFinished())){
-    			examStudentRepo.delete(examStudent);
-    			examStudentCache.remove(examStudent.getId());
-    		}
-    	}else if("update".equals(req.getSyncType())){
-	        ExamStudentInfo examStudent = ExamStudentBeanConvert.of(req);
-	        List<Long> examStudentIdList = examStudentService.saveExamStudentList(Lists.newArrayList(examStudent));
-	        for (Long cur : examStudentIdList) {
-	        	examStudentCache.refresh(cur);
+		//此锁是为了保证同步考生和交卷后更新考生信息不冲突
+		String sequenceLockKey = Constants.EXAM_CONTROL_LOCK_PREFIX + req.getStudentId();
+
+		try {
+			//添加考试控制全局锁
+			SequenceLockHelper.getLockSimple(sequenceLockKey);
+
+			if("delete".equals(req.getSyncType())){
+				ExamStudentEntity examStudent = examStudentRepo.findByExamStudentId(req.getId());
+				if(examStudent != null && (examStudent.getFinished() == null || !examStudent.getFinished())){
+					examStudentRepo.delete(examStudent);
+					examStudentCache.remove(examStudent.getId());
+				}
+			}else if("update".equals(req.getSyncType())){
+				ExamStudentInfo examStudent = ExamStudentBeanConvert.of(req);
+				List<Long> examStudentIdList = examStudentService.saveExamStudentList(Lists.newArrayList(examStudent));
+				for (Long cur : examStudentIdList) {
+					examStudentCache.refresh(cur);
+				}
 			}
-    	}
-    	
-    	SyncExamStudentResp resp = new SyncExamStudentResp();
-    	return resp;
-    }
+
+			SyncExamStudentResp resp = new SyncExamStudentResp();
+			return resp;
+		} finally {
+			SequenceLockHelper.releaseLockSimple(sequenceLockKey);
+		}
+	}
 
     @ApiOperation(value = "同步课程")
     @PostMapping("/syncCourse")

+ 12 - 5
examcloud-core-oe-admin-api-provider/src/main/java/cn/com/qmth/examcloud/core/oe/admin/api/provider/SyncExamDataCloudServiceProvider.java

@@ -9,6 +9,7 @@ import cn.com.qmth.examcloud.core.oe.admin.dao.*;
 import cn.com.qmth.examcloud.core.oe.admin.dao.entity.*;
 import cn.com.qmth.examcloud.core.oe.admin.dao.enums.*;
 import cn.com.qmth.examcloud.core.oe.admin.service.*;
+import cn.com.qmth.examcloud.core.oe.admin.service.cache.ExamStudentCache;
 import cn.com.qmth.examcloud.question.commons.core.question.AnswerType;
 import cn.com.qmth.examcloud.support.cache.CacheHelper;
 import cn.com.qmth.examcloud.support.cache.bean.CourseCacheBean;
@@ -80,7 +81,8 @@ public class SyncExamDataCloudServiceProvider extends ControllerSupport implemen
     private ExamRecordDataSyncRepo examRecordDataSyncRepo;
     @Autowired
     private ExamScoreRepo examScoreRepo;
-
+    @Autowired
+    private ExamStudentCache examStudentCache;
 
     /**
      * 同步考试记录数据
@@ -151,7 +153,8 @@ public class SyncExamDataCloudServiceProvider extends ControllerSupport implemen
         }
 
         //同步后续处理
-        processAfterSyncExamData(realExamRecordDataId, examScoreId,transitionExamRecordData.getObjectiveScore());
+        processAfterSyncExamData(realExamRecordDataId, examScoreId, transitionExamRecordData.getObjectiveScore(),
+                transitionExamRecordData.getExamStudentId());
 
         return new SyncExamDataResp();
     }
@@ -181,6 +184,7 @@ public class SyncExamDataCloudServiceProvider extends ControllerSupport implemen
      * @param examStudentId
      */
     private void increaseUsedExamTimes(Long examStudentId) {
+        //更新数据库中的已考次数
         ExamStudentEntity examStudentEntity = examStudentRepo.findByExamStudentId(examStudentId);
         if (null == examStudentEntity) {
             throw new StatusException("100101", "考生id不正确");
@@ -193,7 +197,6 @@ public class SyncExamDataCloudServiceProvider extends ControllerSupport implemen
         examStudentEntity.setUsedNum(usedTimes + 1);
 
         examStudentRepo.saveAndFlush(examStudentEntity);
-
     }
 
     /**
@@ -568,15 +571,19 @@ public class SyncExamDataCloudServiceProvider extends ControllerSupport implemen
      * @param realExamRecordDataId
      * @param examScoreId
      */
-    private void processAfterSyncExamData(Long realExamRecordDataId, Long examScoreId, Double objectiveScore) {
+    private void processAfterSyncExamData(Long realExamRecordDataId, Long examScoreId, Double objectiveScore,
+                                          Long examStudentId) {
         // 保存阅卷相关数据
-        examRecordForMarkingService.saveExamRecordForMarking(realExamRecordDataId,objectiveScore);
+        examRecordForMarkingService.saveExamRecordForMarking(realExamRecordDataId, objectiveScore);
 
         // 保存考试分数数据到推分队列
         examScorePushQueueService.saveScoreDataInfoToQueue(realExamRecordDataId, examScoreId);
 
         // 保存考试分数数据到分数获取队列
         examScoreObtainQueueService.saveExamScoreObtainQueue(realExamRecordDataId);
+
+        //刷新考生的缓存
+        examStudentCache.refresh(examStudentId);
     }
 
 }