Selaa lähdekoodia

缓存 和新增分析课程删除考生相关数据错误

caozixuan 2 vuotta sitten
vanhempi
commit
e1930d4df9

+ 2 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/contant/SystemConstant.java

@@ -173,6 +173,8 @@ public class SystemConstant {
     public static final int REDIS_MQ_MAX_RECONSUME = 5;
     public static final String MQ_TOPIC_BUFFER_LIST = "mq:topic:buffer:list";
     public static final String REDIS_LOCK_BATCH_NO_PREFIX = "redis:lock:batchNo:";//
+    public static final String REDIS_LOCK_GRADE_COURSE_DELETE_PREFIX = "redis:lock:courseInfo:";
+    public static final String REDIS_LOCK_GRADE_EXAM_DELETE_PREFIX = "redis:lock:exam:";
 
     /**
      * redis lock

+ 7 - 7
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/CommonCacheService.java

@@ -307,33 +307,33 @@ public interface CommonCacheService {
      * 删除院长缓存
      *
      * @param schoolId
-     * @param semester
+     * @param semesterId
      * @param examId
      * @param collegeId
      */
-    public void removeCollegeDeanCache(Long schoolId, String semester, Long examId, Long collegeId);
+    public void removeCollegeDeanCache(Long schoolId, Long semesterId, Long examId, Long collegeId);
 
     /**
-     * 删除学院学科缓存
+     * 删除考察院长课程详情报告
      *
      * @param schoolId
-     * @param semester
+     * @param semesterId
      * @param examId
      * @param courseCode
      * @param collegeId
      */
-    public void removeCollegeCourseCache(Long schoolId, String semester, Long examId, String courseCode, Long collegeId);
+    public void removeCollegeCourseCache(Long schoolId, Long semesterId, Long examId, String courseCode, Long collegeId);
 
     /**
      * 删除开课课程考试总览图表缓存
      *
      * @param schoolId
-     * @param semester
+     * @param semesterId
      * @param examId
      * @param courseCode
      * @param collegeId
      */
-    public void removeSurveyTeacherViewCache(Long schoolId, String semester, Long examId, String courseCode, Long collegeId);
+    public void removeSurveyTeacherViewCache(Long schoolId, Long semesterId, Long examId, String courseCode, Long collegeId);
 
     /**
      * 添加鉴权缓存

+ 10 - 10
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/impl/CommonCacheServiceImpl.java

@@ -498,13 +498,13 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * 删除院长缓存
      *
      * @param schoolId
-     * @param semester
+     * @param semesterId
      * @param examId
      * @param collegeId
      */
     @Override
-    @CacheEvict(value = SystemConstant.COLLEGE_DEAN_REPORT, key = "#schoolId + '-' + #semester + '-' + #examId + '-' +#collegeId")
-    public void removeCollegeDeanCache(Long schoolId, String semester, Long examId, Long collegeId) {
+    @CacheEvict(value = SystemConstant.COLLEGE_DEAN_REPORT, key = "#schoolId + '-' + #semesterId + '-' + #examId + '-' +#collegeId")
+    public void removeCollegeDeanCache(Long schoolId, Long semesterId, Long examId, Long collegeId) {
 
     }
 
@@ -512,29 +512,29 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      * 删除学院学科缓存
      *
      * @param schoolId
-     * @param semester
+     * @param semesterId
      * @param examId
      * @param courseCode
      * @param collegeId
      */
     @Override
-    @CacheEvict(value = SystemConstant.COLLEGE_COURSE_REPORT, key = "#schoolId + '-' + #semester + '-' + #examId + '-' + #courseCode + '-' + #collegeId")
-    public void removeCollegeCourseCache(Long schoolId, String semester, Long examId, String courseCode, Long collegeId) {
+    @CacheEvict(value = SystemConstant.COLLEGE_COURSE_REPORT, key = "#schoolId + '-' + #semesterId + '-' + #examId + '-' + #courseCode + '-' + #collegeId")
+    public void removeCollegeCourseCache(Long schoolId, Long semesterId, Long examId, String courseCode, Long collegeId) {
 
     }
 
     /**
-     * 删除开课课程考试总览图表缓存
+     * 删除开课课程考试分析图表缓存
      *
      * @param schoolId
-     * @param semester
+     * @param semesterId
      * @param examId
      * @param courseCode
      * @param collegeId
      */
     @Override
-    @CacheEvict(value = SystemConstant.SURVEY_TEACHER_VIEW_REPORT, key = "#schoolId + '-' + #semester + '-' + #examId + '-' + #courseCode + '-' + #collegeId")
-    public void removeSurveyTeacherViewCache(Long schoolId, String semester, Long examId, String courseCode, Long collegeId) {
+    @CacheEvict(value = SystemConstant.SURVEY_TEACHER_VIEW_REPORT, key = "#schoolId + '-' + #semesterId + '-' + #examId + '-' + #courseCode + '-' + #collegeId")
+    public void removeSurveyTeacherViewCache(Long schoolId, Long semesterId, Long examId, String courseCode, Long collegeId) {
 
     }
 

+ 8 - 0
teachcloud-report-business/src/main/java/com/qmth/teachcloud/report/business/service/AnalyzeForReportService.java

@@ -225,4 +225,12 @@ public interface AnalyzeForReportService {
      * @param courseCode 课程代码
      */
     void normalAssignScore(Long examId, Long schoolId, String courseCode);
+
+    /**
+     * 统一清除redis缓存
+     * @param schoolId 学校id
+     * @param examId 考试id
+     * @param courseCode 课程编号
+     */
+    void removeRedisCache(Long schoolId,Long examId,String courseCode);
 }

+ 2 - 0
teachcloud-report-business/src/main/java/com/qmth/teachcloud/report/business/service/impl/AnalyzeDataCalculateServiceImpl.java

@@ -112,6 +112,8 @@ public class AnalyzeDataCalculateServiceImpl implements AnalyzeDataCalculateServ
                 }
                 // 计算完成
                 analyzeForReportService.updateCoursePublishStatus(examId, courseCode, PublishStatusEnum.UN_PUBLISH);
+                // 清除该分析课程缓存
+                analyzeForReportService.removeRedisCache(schoolId, examId, courseCode);
                 this.submitTBSyncTask(tbSyncTask, totalTask, currentTask, courseCode, GradeAnalyzePaperStatusEnum.FINISH_CALCULATE);
                 calculateTaskTemplate.updateProgress(calculateParams);
             } catch (Exception e) {

+ 1 - 0
teachcloud-report-business/src/main/java/com/qmth/teachcloud/report/business/service/impl/AnalyzeDataGetAndEditServiceImpl.java

@@ -762,6 +762,7 @@ public class AnalyzeDataGetAndEditServiceImpl implements AnalyzeDataGetAndEditSe
         Set<Long> examStudentRemoveIdSet = new HashSet<>();
         for (String s : courseInfoList) {
             Set<Long> ids = tbExamStudentService.list(new QueryWrapper<TBExamStudent>().lambda()
+                            .eq(TBExamStudent::getExamId,examId)
                             .eq(TBExamStudent::getCourseCode, s))
                     .stream().map(TBExamStudent::getId).collect(Collectors.toSet());
             examStudentRemoveIdSet.addAll(ids);

+ 39 - 3
teachcloud-report-business/src/main/java/com/qmth/teachcloud/report/business/service/impl/AnalyzeForReportServiceImpl.java

@@ -3,12 +3,11 @@ package com.qmth.teachcloud.report.business.service.impl;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.qmth.teachcloud.common.contant.SpringContextHolder;
 import com.qmth.teachcloud.common.contant.SystemConstant;
+import com.qmth.teachcloud.common.entity.BasicSemester;
 import com.qmth.teachcloud.common.entity.SysOrg;
 import com.qmth.teachcloud.common.enums.AssignEnum;
 import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
-import com.qmth.teachcloud.common.service.BasicCourseService;
-import com.qmth.teachcloud.common.service.SysOrgService;
-import com.qmth.teachcloud.common.service.SysUserService;
+import com.qmth.teachcloud.common.service.*;
 import com.qmth.teachcloud.report.business.bean.dto.AnswerDetailBean;
 import com.qmth.teachcloud.report.business.bean.dto.query.BasicAnswerDto;
 import com.qmth.teachcloud.report.business.bean.dto.query.BasicExamRecordDto;
@@ -110,6 +109,11 @@ public class AnalyzeForReportServiceImpl implements AnalyzeForReportService {
     @Resource
     private TAExamCourseTeacherCollegePaperStructService taExamCourseTeacherCollegePaperStructService;
 
+    @Resource
+    private CommonCacheService commonCacheService;
+    @Resource
+    private BasicSemesterService basicSemesterService;
+
 
     @Transactional(rollbackFor = Exception.class)
     @Override
@@ -2020,6 +2024,8 @@ public class AnalyzeForReportServiceImpl implements AnalyzeForReportService {
             }
             throw ExceptionResultEnum.ERROR.exception("【" + status + "】状态的课程无法进行" + "【" + statusDesc + "】");
         }
+        // 发布或取消发布清除缓存
+        this.removeRedisCache(tbExamCourse.getSchoolId(),examId,courseCode);
         tbExamCourseService.updateById(tbExamCourse);
     }
 
@@ -2089,6 +2095,36 @@ public class AnalyzeForReportServiceImpl implements AnalyzeForReportService {
         }
     }
 
+    @Override
+    public void removeRedisCache(Long schoolId, Long examId, String courseCode) {
+        Long semesterId = tbExamService.getById(examId).getSemesterId();
+        List<TAExamCourseRecord> datasource = taExamCourseRecordService.list(new QueryWrapper<TAExamCourseRecord>()
+                .lambda()
+                .eq(TAExamCourseRecord::getSchoolId, schoolId)
+                .eq(TAExamCourseRecord::getExamId, examId)
+                .eq(TAExamCourseRecord::getCourseCode, courseCode));
+
+
+        // 清除学生个人报告缓存
+        List<String> studentCodeList = datasource.stream().map(TAExamCourseRecord::getStudentCode).distinct().collect(Collectors.toList());
+        for (String studentCode : studentCodeList) {
+            commonCacheService.removeExamStudentReportCache(schoolId, studentCode, String.valueOf(examId), courseCode);
+        }
+
+        // 考察院长报告总览和详情缓存
+        List<Long> inspectCollegeIdList = datasource.stream().map(TAExamCourseRecord::getInspectCollegeId).distinct().collect(Collectors.toList());
+        for (Long inspectCollegeId : inspectCollegeIdList) {
+            commonCacheService.removeCollegeDeanCache(schoolId,semesterId,examId,inspectCollegeId);
+            commonCacheService.removeCollegeCourseCache(schoolId,semesterId,examId,courseCode,inspectCollegeId);
+        }
+
+        // 开课课程考试分析缓存清除
+        List<Long> teachCollegeIdList = datasource.stream().map(TAExamCourseRecord::getTeachCollegeId).distinct().collect(Collectors.toList());
+        for (Long teachCollegeId : teachCollegeIdList) {
+            commonCacheService.removeSurveyTeacherViewCache(schoolId,semesterId,examId,courseCode,teachCollegeId);
+        }
+    }
+
 
     private void verifyComputing(Long examId, String courseCode) {
         List<String> effectiveCourseCodeList = tbExamCourseService.findEffectiveByExamId(examId, courseCode);

+ 47 - 16
teachcloud-report-business/src/main/java/com/qmth/teachcloud/report/business/service/impl/TBExamCourseDeleteServiceImpl.java

@@ -1,10 +1,14 @@
 package com.qmth.teachcloud.report.business.service.impl;
 
+import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.qmth.teachcloud.common.bean.params.TBExamCourseDeleteParams;
+import com.qmth.teachcloud.common.contant.SystemConstant;
 import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
+import com.qmth.teachcloud.common.util.RedisUtil;
 import com.qmth.teachcloud.report.business.entity.*;
 import com.qmth.teachcloud.report.business.service.*;
+import org.apache.commons.codec.digest.DigestUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -89,6 +93,9 @@ public class TBExamCourseDeleteServiceImpl implements TBExamCourseDeleteService
     @Resource
     AnalyzeForReportService analyzeForReportService;
 
+    @Resource
+    RedisUtil redisUtil;
+
     @Transactional(rollbackFor = Exception.class)
     @Override
     public void deleteBasicTableData(Long schoolId, Long examId, String courseCode) {
@@ -292,24 +299,48 @@ public class TBExamCourseDeleteServiceImpl implements TBExamCourseDeleteService
 
     @Transactional(rollbackFor = Exception.class)
     @Override
-    public synchronized void deleteTBExamCourse(List<TBExamCourseDeleteParams> tbExamCourseDeleteParamsList) {
-        for (TBExamCourseDeleteParams tbExamCourseDeleteParams : tbExamCourseDeleteParamsList) {
-            Long schoolId = tbExamCourseDeleteParams.getSchoolId();
-            Long examId = tbExamCourseDeleteParams.getExamId();
-            String courseCode = tbExamCourseDeleteParams.getCourseCode();
-            if (tbExamCourseService.list(new QueryWrapper<TBExamCourse>()
-                    .lambda()
-                    .eq(TBExamCourse::getSchoolId,schoolId)
-                    .eq(TBExamCourse::getExamId,examId)
-                    .eq(TBExamCourse::getCourseCode,courseCode)).size() > 0){
-                // 教研分析有这个课程再删除,没有不删直接返回
-                if (tbExamCourseService.verifyExamCourseCantDelete(schoolId, examId, courseCode)) {
-                    throw ExceptionResultEnum.ERROR.exception("考试id为【" + examId + "】,课程编号为【" + courseCode + "】的分析课程当前不能被删除");
+    public void deleteTBExamCourse(List<TBExamCourseDeleteParams> tbExamCourseDeleteParamsList) {
+        List<Long> schoolIdList = tbExamCourseDeleteParamsList.stream().map(TBExamCourseDeleteParams::getSchoolId).distinct().collect(Collectors.toList());
+        if (schoolIdList.size() != 1){
+            throw ExceptionResultEnum.ERROR.exception("学校信息异常");
+        }
+        Long lockSchoolId = schoolIdList.get(0);
+
+        List<Long> examIdList = tbExamCourseDeleteParamsList.stream().map(TBExamCourseDeleteParams::getExamId).distinct().collect(Collectors.toList());
+        if (examIdList.size() != 1){
+            throw ExceptionResultEnum.ERROR.exception("不能删除不同考试下的分析课程");
+        }
+        Long lockExamId = examIdList.get(0);
+        List<String> courseCodeList = tbExamCourseDeleteParamsList.stream().map(TBExamCourseDeleteParams::getCourseCode).distinct().sorted().collect(Collectors.toList());
+        String lockCourseCode = DigestUtils.md5Hex(JSON.toJSONString(courseCodeList));
+        String lockKey = SystemConstant.REDIS_LOCK_GRADE_COURSE_DELETE_PREFIX + lockSchoolId + "-" + lockExamId + "-" + lockCourseCode;
+
+        boolean lock = redisUtil.lock(lockKey, SystemConstant.REDIS_LOCK_BATCH_NO_TIME_OUT);
+        if (!lock) {
+            throw ExceptionResultEnum.ERROR.exception("正在删除数据,请稍候再试!");
+        }
+        try {
+            for (TBExamCourseDeleteParams tbExamCourseDeleteParams : tbExamCourseDeleteParamsList) {
+                Long schoolId = tbExamCourseDeleteParams.getSchoolId();
+                Long examId = tbExamCourseDeleteParams.getExamId();
+                String courseCode = tbExamCourseDeleteParams.getCourseCode();
+                if (tbExamCourseService.list(new QueryWrapper<TBExamCourse>()
+                        .lambda()
+                        .eq(TBExamCourse::getSchoolId,schoolId)
+                        .eq(TBExamCourse::getExamId,examId)
+                        .eq(TBExamCourse::getCourseCode,courseCode)).size() > 0){
+                    // 教研分析有这个课程再删除,没有不删直接返回
+                    if (tbExamCourseService.verifyExamCourseCantDelete(schoolId, examId, courseCode)) {
+                        throw ExceptionResultEnum.ERROR.exception("考试id为【" + examId + "】,课程编号为【" + courseCode + "】的分析课程当前不能被删除");
+                    }
+                    this.deleteBasicTableData(schoolId, examId, courseCode);
+                    this.deleteAnalyzeTableData(schoolId, examId, courseCode);
+                    this.repeatCalculateTotalTableData(schoolId, examId, courseCode);
                 }
-                this.deleteBasicTableData(schoolId, examId, courseCode);
-                this.deleteAnalyzeTableData(schoolId, examId, courseCode);
-                this.repeatCalculateTotalTableData(schoolId, examId, courseCode);
             }
+        } finally {
+            redisUtil.releaseLock(lockKey);
         }
+
     }
 }

+ 25 - 12
teachcloud-report-business/src/main/java/com/qmth/teachcloud/report/business/service/impl/TBExamServiceImpl.java

@@ -7,6 +7,7 @@ import com.qmth.teachcloud.common.entity.BasicSemester;
 import com.qmth.teachcloud.common.enums.ExamModelEnum;
 import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
 import com.qmth.teachcloud.common.service.BasicSemesterService;
+import com.qmth.teachcloud.common.util.RedisUtil;
 import com.qmth.teachcloud.report.business.bean.params.TBExamDeleteParam;
 import com.qmth.teachcloud.report.business.bean.params.TBExamParam;
 import com.qmth.teachcloud.report.business.entity.TBCloudExam;
@@ -40,6 +41,8 @@ public class TBExamServiceImpl extends ServiceImpl<TBExamMapper, TBExam> impleme
     private BasicSemesterService basicSemesterService;
     @Resource
     private TBExamCourseService tbExamCourseService;
+    @Resource
+    private RedisUtil redisUtil;
 
     @Override
     public TBExam findByCloudExamId(Long cloudExamId) {
@@ -107,22 +110,32 @@ public class TBExamServiceImpl extends ServiceImpl<TBExamMapper, TBExam> impleme
 
     @Transactional(rollbackFor = Exception.class)
     @Override
-    public synchronized Boolean deleteTBExam(TBExamDeleteParam tbExamDeleteParam) {
+    public Boolean deleteTBExam(TBExamDeleteParam tbExamDeleteParam) {
         Long examId = tbExamDeleteParam.getId();
-        if (SystemConstant.longNotNull(examId)){
+        if (SystemConstant.longNotNull(examId)) {
             Long schoolId = tbExamDeleteParam.getSchoolId();
-            if (!SystemConstant.longNotNull(schoolId)) {
-                throw ExceptionResultEnum.ERROR.exception("缺少参数学校id");
-            }
-            TBExam tbExam = this.getById(examId);
-            if (Objects.isNull(tbExam)){
-                throw ExceptionResultEnum.ERROR.exception("未找到考试信息");
+            String lockKey = SystemConstant.REDIS_LOCK_GRADE_EXAM_DELETE_PREFIX + Math.abs(schoolId.toString().hashCode() + examId.toString().hashCode());
+            boolean lock = redisUtil.lock(lockKey, SystemConstant.REDIS_LOCK_BATCH_NO_TIME_OUT);
+            if (!lock) {
+                throw ExceptionResultEnum.ERROR.exception("正在删除数据,请稍候再试!");
             }
-            List<TBExamCourse> tbExamCourseList = tbExamCourseService.list(new QueryWrapper<TBExamCourse>().lambda().eq(TBExamCourse::getSchoolId,schoolId).eq(TBExamCourse::getExamId, examId));
-            if (tbExamCourseList.size() > 0){
-                throw ExceptionResultEnum.ERROR.exception("考试下还有分析课程不能删除");
+            try {
+                if (!SystemConstant.longNotNull(schoolId)) {
+                    throw ExceptionResultEnum.ERROR.exception("缺少参数学校id");
+                }
+                TBExam tbExam = this.getById(examId);
+                if (Objects.isNull(tbExam)) {
+                    throw ExceptionResultEnum.ERROR.exception("未找到考试信息");
+                }
+                List<TBExamCourse> tbExamCourseList = tbExamCourseService.list(new QueryWrapper<TBExamCourse>().lambda().eq(TBExamCourse::getSchoolId, schoolId).eq(TBExamCourse::getExamId, examId));
+                if (tbExamCourseList.size() > 0) {
+                    throw ExceptionResultEnum.ERROR.exception("考试下还有分析课程不能删除");
+                }
+                this.removeById(examId);
+            } finally {
+                redisUtil.releaseLock(lockKey);
             }
-            this.removeById(examId);
+
         }
         return true;
     }

+ 1 - 9
teachcloud-report-business/src/main/java/com/qmth/teachcloud/report/business/templete/strategy/CalculateTaskTemplate.java

@@ -15,6 +15,7 @@ import com.qmth.teachcloud.common.service.CommonCacheService;
 import com.qmth.teachcloud.common.util.*;
 import com.qmth.teachcloud.report.business.bean.params.CalculateParams;
 import com.qmth.teachcloud.report.business.entity.TBSyncTask;
+import com.qmth.teachcloud.report.business.service.AnalyzeForReportService;
 import com.qmth.teachcloud.report.business.service.TBSyncTaskService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -172,7 +173,6 @@ public abstract class CalculateTaskTemplate {
     public void start(CalculateParams calculateParams) throws IOException {
         RedisUtil redisUtil = SpringContextHolder.getBean(RedisUtil.class);
         String source;
-        String exception = null;
         if (Objects.nonNull(calculateParams.getTbSyncTask())) {
             source = calculateParams.getTbSyncTask().getSource();
         } else {
@@ -188,7 +188,6 @@ public abstract class CalculateTaskTemplate {
             calculate(calculateParams);
             finished(calculateParams);
         } catch (Exception e) {
-            exception = e.getMessage();
             log.error(SystemConstant.LOG_ERROR, e);
             if (Objects.nonNull(calculateParams.getTbSyncTask())) {
                 calculateParams.getTbSyncTask().setSummary(e.getMessage());
@@ -200,13 +199,6 @@ public abstract class CalculateTaskTemplate {
                 ResultUtil.error(e.getMessage());
             }
         } finally {
-            if (Objects.isNull(exception)) {
-                CommonCacheService commonCacheService = SpringContextHolder.getBean(CommonCacheService.class);
-                commonCacheService.removeExamStudentReportCache();
-                commonCacheService.removeCollegeDeanCache();
-                commonCacheService.removeCollegeCourseCache();
-                commonCacheService.removeSurveyTeacherViewCache();
-            }
             redisUtil.releaseLock(SystemConstant.REDIS_LOCK_CALCULATE_PREFIX + source);
         }
     }

+ 1 - 1
teachcloud-report-business/src/main/resources/mapper/TBExamCourseMapper.xml

@@ -17,7 +17,7 @@
         </choose>
         <if test="filter != null and filter == true">
             join t_b_exam_course tbec on
-            tbec.course_code = bc.code
+            tbec.course_code = bc.code and tbec.exam_id = tbs.exam_id
         </if>
         <where>
             <if test="examId != null and examId != ''">