Browse Source

重考次数加锁

wangliang 1 year ago
parent
commit
063689513e

+ 45 - 39
themis-admin/src/main/java/com/qmth/themis/admin/api/TEExamReexamController.java

@@ -100,36 +100,36 @@ public class TEExamReexamController {
             List<TEExamReexamAuditing> teExamReexamAuditingList = new ArrayList<>();
             List<TEExamStudent> teExamStudentList = teExamStudentService.listByIds(examStudentIdList);
             for (TEExamStudent teExamStudent : teExamStudentList) {
-                if (redisUtil.lock(SystemConstant.REDIS_LOCK_EXAM_STUDENT_PREFIX + teExamStudent.getId(), SystemConstant.REDIS_LOCK_REEXAM_EXAM_STUDENT_TIME_OUT)) {
-                    try {
-                        ExamCacheBean examCacheBean = teExamService.getExamCacheBean(teExamStudent.getExamId());//考试缓存
-                        if (Objects.isNull(examCacheBean)) {
-                            throw new BusinessException("考试批次[" + examCacheBean + "]不存在");
-                        }
-                        reexamAuditing = examCacheBean.getReexamAuditing();
-                        status = Objects.isNull(reexamAuditing) || reexamAuditing.intValue() == 0 ? 0 : 1;
-                        TEExamReexam teExamReexam = new TEExamReexam(teExamStudent.getExamId(), teExamStudent.getExamActivityId(), teExamStudent.getId(), model, reason, status, Objects.isNull(mapParameter.get(SystemConstant.REMARK)) ? null : String.valueOf(mapParameter.get(SystemConstant.REMARK)));
-                        teExamReexam.setCreateId(tbUser.getId());
-                        if (Objects.nonNull(status) && status.intValue() == 1) {
-                            //这里查询该机构下所有为管理员角色的账号
-                            List<TBUser> tbUserList = tbUserRoleService.userQueryByRole(tbUser.getOrgId(), RoleEnum.ADMIN.name());
-                            if (Objects.nonNull(tbUserList) && tbUserList.size() > 0) {
-                                for (TBUser t : tbUserList) {
-                                    TEExamReexamAuditing teExamReexamAuditing = new TEExamReexamAuditing(teExamReexam.getId(), t.getId());
-                                    teExamReexamAuditingList.add(teExamReexamAuditing);
+                if (Objects.nonNull(teExamStudent)) {
+                    if (redisUtil.lock(SystemConstant.REDIS_LOCK_EXAM_STUDENT_PREFIX + teExamStudent.getId(), SystemConstant.REDIS_LOCK_REEXAM_EXAM_STUDENT_TIME_OUT)) {
+                        try {
+                            ExamCacheBean examCacheBean = teExamService.getExamCacheBean(teExamStudent.getExamId());//考试缓存
+                            if (Objects.isNull(examCacheBean)) {
+                                throw new BusinessException("考试批次[" + examCacheBean + "]不存在");
+                            }
+                            reexamAuditing = examCacheBean.getReexamAuditing();
+                            status = Objects.isNull(reexamAuditing) || reexamAuditing.intValue() == 0 ? 0 : 1;
+                            TEExamReexam teExamReexam = new TEExamReexam(teExamStudent.getExamId(), teExamStudent.getExamActivityId(), teExamStudent.getId(), model, reason, status, Objects.isNull(mapParameter.get(SystemConstant.REMARK)) ? null : String.valueOf(mapParameter.get(SystemConstant.REMARK)));
+                            teExamReexam.setCreateId(tbUser.getId());
+                            if (Objects.nonNull(status) && status.intValue() == 1) {
+                                //这里查询该机构下所有为管理员角色的账号
+                                List<TBUser> tbUserList = tbUserRoleService.userQueryByRole(tbUser.getOrgId(), RoleEnum.ADMIN.name());
+                                if (Objects.nonNull(tbUserList) && tbUserList.size() > 0) {
+                                    for (TBUser t : tbUserList) {
+                                        TEExamReexamAuditing teExamReexamAuditing = new TEExamReexamAuditing(teExamReexam.getId(), t.getId());
+                                        teExamReexamAuditingList.add(teExamReexamAuditing);
+                                    }
                                 }
+                            } else if (Objects.nonNull(status) && status.intValue() == 0) {//无需审核时考生已考次数-1
+                                examStudentIdNotAuditingList.add(teExamStudent.getId());
+                                teExamReexam.setAuditingId(tbUser.getId());
+                                teExamReexam.setAuditingStatus(0);
+                                teExamReexam.setAuditingTime(System.currentTimeMillis());
+                                teExamReexam.setAuditingSuggest("无需审核");
+                                teExamReexam.setUpdateId(tbUser.getId());
                             }
-                        } else if (Objects.nonNull(status) && status.intValue() == 0) {//无需审核时考生已考次数-1
-                            examStudentIdNotAuditingList.add(teExamStudent.getId());
-                            teExamReexam.setAuditingId(tbUser.getId());
-                            teExamReexam.setAuditingStatus(0);
-                            teExamReexam.setAuditingTime(System.currentTimeMillis());
-                            teExamReexam.setAuditingSuggest("无需审核");
-                            teExamReexam.setUpdateId(tbUser.getId());
-                        }
-                        teExamReexamService.save(teExamReexam);
-                    } finally {
-                        if (Objects.nonNull(teExamStudent)) {
+                            teExamReexamService.save(teExamReexam);
+                        } finally {
                             redisUtil.releaseLock(SystemConstant.REDIS_LOCK_EXAM_STUDENT_PREFIX + teExamStudent.getId());
                         }
                     }
@@ -142,18 +142,24 @@ public class TEExamReexamController {
                 examStudentIdNotAuditingList.forEach(s -> {
                     ExamStudentCacheBean examStudentCacheBean = teExamStudentService.getExamStudentCacheBean(s);
                     if (Objects.nonNull(examStudentCacheBean)) {
-                        Integer alreadyExamCount = Objects.nonNull(examStudentCacheBean.getAlreadyExamCount()) ? examStudentCacheBean.getAlreadyExamCount() : 0;
-                        if (alreadyExamCount > 0) {
-                            alreadyExamCount = alreadyExamCount - 1;
-                            ExamCacheBean ec = teExamService.getExamCacheBean(examStudentCacheBean.getExamId());//考试缓存
-                            if (alreadyExamCount.intValue() >= ec.getExamCount().intValue()) {
-                                alreadyExamCount = 0;
+                        if (redisUtil.lock(SystemConstant.REDIS_LOCK_EXAM_STUDENT_NOT_AUDITING_PREFIX + s, SystemConstant.REDIS_LOCK_REEXAM_EXAM_STUDENT_TIME_OUT)) {
+                            try {
+                                Integer alreadyExamCount = Objects.nonNull(examStudentCacheBean.getAlreadyExamCount()) ? examStudentCacheBean.getAlreadyExamCount() : 0;
+                                if (alreadyExamCount > 0) {
+                                    alreadyExamCount = alreadyExamCount - 1;
+                                    ExamCacheBean ec = teExamService.getExamCacheBean(examStudentCacheBean.getExamId());//考试缓存
+                                    if (alreadyExamCount.intValue() >= ec.getExamCount().intValue()) {
+                                        alreadyExamCount = 0;
+                                    }
+                                    UpdateWrapper<TEExamStudent> teExamStudentUpdateWrapper = new UpdateWrapper<>();
+                                    teExamStudentUpdateWrapper.lambda().set(TEExamStudent::getAlreadyExamCount, alreadyExamCount)
+                                            .eq(TEExamStudent::getId, s);
+                                    teExamStudentService.update(teExamStudentUpdateWrapper);
+                                    teExamStudentService.updateExamStudentCacheBean(s);
+                                }
+                            } finally {
+                                redisUtil.releaseLock(SystemConstant.REDIS_LOCK_EXAM_STUDENT_NOT_AUDITING_PREFIX + s);
                             }
-                            UpdateWrapper<TEExamStudent> teExamStudentUpdateWrapper = new UpdateWrapper<>();
-                            teExamStudentUpdateWrapper.lambda().set(TEExamStudent::getAlreadyExamCount, alreadyExamCount)
-                                    .eq(TEExamStudent::getId, s);
-                            teExamStudentService.update(teExamStudentUpdateWrapper);
-                            teExamStudentService.updateExamStudentCacheBean(s);
                         }
                     }
                 });

+ 2 - 0
themis-business/src/main/java/com/qmth/themis/business/constant/SystemConstant.java

@@ -446,6 +446,8 @@ public class SystemConstant {
     //考生锁
     public static final String REDIS_LOCK_EXAM_STUDENT_PREFIX = "lock:student:student_id_";
 
+    public static final String REDIS_LOCK_EXAM_STUDENT_NOT_AUDITING_PREFIX = "lock:student:not_auditing_student_id_";
+
     //考试批次code锁
     public static final String REDIS_LOCK_EXAM_ACTIVITY_CODE_PREFIX = "lock:exam:activity:code:exam_id_";