deason před 6 roky
rodič
revize
2cf2a3c99f

+ 1 - 7
examcloud-core-print-dao/src/main/java/cn/com/qmth/examcloud/core/print/repository/CourseStatisticRepository.java

@@ -27,13 +27,7 @@ public interface CourseStatisticRepository extends JpaRepository<CourseStatistic
     @Transactional
     @Modifying
     @Query("UPDATE CourseStatistic SET totalStudent=:totalStudent WHERE orgId=:orgId AND examId=:examId AND courseId=:courseId AND paperType=:paperType")
-    int updateTotalStudentByOrgExamCourse(@Param("orgId") Long orgId, @Param("examId") Long examId, @Param("courseId") Long courseId, @Param("paperType") String paperType,
-                                          @Param("totalStudent") Integer totalStudent);
-
-    @Transactional
-    @Modifying
-    @Query("UPDATE CourseStatistic SET paperStatus=:paperStatus WHERE orgId=:orgId AND examId=:examId AND courseId=:courseId")
-    int updatePaperStatusByOrgExamCourse(@Param("orgId") Long orgId, @Param("examId") Long examId, @Param("courseId") Long courseId, @Param("paperStatus") Integer paperStatus);
+    int updateTotalStudentByOrgExamCourse(@Param("orgId") Long orgId, @Param("examId") Long examId, @Param("courseId") Long courseId, @Param("paperType") String paperType, @Param("totalStudent") Integer totalStudent);
 
     int countByOrgIdAndExamId(Long orgId, Long examId);
 

+ 15 - 21
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/StatisticService.java

@@ -10,7 +10,6 @@ package cn.com.qmth.examcloud.core.print.service;
 import cn.com.qmth.examcloud.core.print.service.bean.common.ExamCourseInfo;
 import cn.com.qmth.examcloud.core.print.service.bean.common.ExamInfo;
 import cn.com.qmth.examcloud.core.print.service.bean.examquestionstructure.ExamQuestionStructureInfo;
-import cn.com.qmth.examcloud.examwork.api.bean.ExamPaperTypeRelation;
 
 import java.util.List;
 
@@ -20,30 +19,15 @@ import java.util.List;
  */
 public interface StatisticService {
 
-    /**
-     * 获取考试所有开考的课程列表(含试卷类型、考生数量)
-     */
-    List<ExamCourseInfo> findExamCourses(Long orgId, Long examId);
-
-    /**
-     * 获取考试所有开考的课程列表(含试卷类型、考生数量)
-     */
-    List<ExamCourseInfo> findExamCourses(Long orgId, Long examId, Long courseId, String paperType);
-
-    /**
-     * 获取试卷结构信息
-     */
-    ExamQuestionStructureInfo findStructureByPaperId(Long examId, String paperId);
-
     /**
      * 获取某考试的课程和试卷类型列表
      */
-    List<ExamPaperTypeRelation> findExamCourseAndPaperTypes(Long orgId, Long examId);
+    List<ExamCourseInfo> findExamCourseAndPaperTypes(Long orgId, Long examId);
 
     /**
-     * 获取考试的课程数量
+     * 获取考试的实际课程数量
      */
-    int countExamTotalCourse(List<ExamPaperTypeRelation> coursePaperTypes);
+    int countExamTotalCourse(List<ExamCourseInfo> coursePaperTypes);
 
     /**
      * 获取考试的试卷袋列表
@@ -63,7 +47,12 @@ public interface StatisticService {
     /**
      * 通过考试获取考生的数量(人科次)
      */
-    int countExamTotalStudent(Long examId);
+    int countExamTotalStudentByExamId(Long examId);
+
+    /**
+     * 通过课程和试卷类型获取考生的数量
+     */
+    int countExamTotalStudentByCourseIdAndPaperType(Long examId, Long courseId, String paperType);
 
     /**
      * 通过试卷袋编号获取考生的数量
@@ -81,7 +70,12 @@ public interface StatisticService {
     int countExamTotalStudentBySite(Long examId, String site);
 
     /**
-     * 获取"传统"考试列表
+     * 获取考试的试卷试题结构信息
+     */
+    ExamQuestionStructureInfo findStructureByPaperId(Long examId, String paperId);
+
+    /**
+     * 获取传统考试列表
      */
     List<ExamInfo> findTraditionExams();
 

+ 11 - 0
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/bean/common/ExamCourseInfo.java

@@ -47,6 +47,17 @@ public class ExamCourseInfo implements Serializable {
      */
     private Integer totalStudent;
 
+    public ExamCourseInfo(Long orgId, Long examId, Long courseId, String paperType) {
+        this.orgId = orgId;
+        this.examId = examId;
+        this.courseId = courseId;
+        this.paperType = paperType;
+    }
+
+    public ExamCourseInfo() {
+
+    }
+
     public Long getOrgId() {
         return orgId;
     }

+ 55 - 28
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/impl/CourseStatisticServiceImpl.java

@@ -32,7 +32,10 @@ import org.springframework.jdbc.core.BeanPropertyRowMapper;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @author: fengdesheng
@@ -110,18 +113,23 @@ public class CourseStatisticServiceImpl implements CourseStatisticService {
 
         List<CourseStatisticRefreshReq.Course> courses = req.getCourses();
         if (courses == null || courses.size() == 0) {
-            //课程ID和试卷类型未指定时,则不统计
+            //未选择课程时,则跳过统计
             return;
         }
 
+        //获取当前考试下已有的课程统计信息
+        Map<String, CourseStatistic> oldCourseStatisticMaps = this.getCourseStatisticMaps(req.getOrgId(), req.getExamId());
+
+        //待刷新的课程列表
+        List<ExamCourseInfo> examCourses = new ArrayList<>();
         for (CourseStatisticRefreshReq.Course course : courses) {
             if (course.getCourseId() == null || StringUtils.isBlank(course.getPaperType())) {
                 continue;
             }
-            //按课程和试卷类型逐个更新统计信息
-            List<ExamCourseInfo> examCourses = statisticService.findExamCourses(req.getOrgId(), req.getExamId(), course.getCourseId(), course.getPaperType());//todo
-            this.syncCourseStatisticList(examCourses);
+            examCourses.add(new ExamCourseInfo(req.getOrgId(), req.getExamId(), course.getCourseId(), course.getPaperType()));
         }
+
+        this.syncCourseStatistics(examCourses, oldCourseStatisticMaps);
     }
 
     @Override
@@ -134,43 +142,44 @@ public class CourseStatisticServiceImpl implements CourseStatisticService {
             return;
         }
 
-        //todo 优化
-        for (ExamInfo info : exams) {
-            List<ExamCourseInfo> examCourses = statisticService.findExamCourses(info.getOrgId(), info.getExamId());//todo
-            this.syncCourseStatisticList(examCourses);
+        for (ExamInfo exam : exams) {
+            //获取当前考试下已有的课程统计信息
+            Map<String, CourseStatistic> oldCourseStatisticMaps = this.getCourseStatisticMaps(exam.getOrgId(), exam.getExamId());
+
+            //获取某考试的课程和试卷类型列表
+            List<ExamCourseInfo> examCourses = statisticService.findExamCourseAndPaperTypes(exam.getOrgId(), exam.getExamId());
+
+            this.syncCourseStatistics(examCourses, oldCourseStatisticMaps);
         }
     }
 
-    private void syncCourseStatisticList(List<ExamCourseInfo> examCourses) {
+    private void syncCourseStatistics(List<ExamCourseInfo> examCourses, Map<String, CourseStatistic> oldCourseStatisticMaps) {
         if (examCourses == null || examCourses.isEmpty()) {
             return;
         }
+        //按课程和试卷类型逐个更新统计信息
         for (ExamCourseInfo info : examCourses) {
-            if (info.getCourseId() == null) {
-                continue;
-            }
+            //Key: examId_courseId_paperType
+            String key = info.getExamId() + "_" + info.getCourseId() + "_" + info.getPaperType();
 
-            boolean isExist = this.isExistCourseStatistic(info);
-            if (isExist) {
-                this.syncTotalStudentByOrgExamCourse(info);
+            //获取当前考生的数量
+            int totalStudent = statisticService.countExamTotalStudentByCourseIdAndPaperType(info.getExamId(), info.getCourseId(), info.getPaperType());
+            info.setTotalStudent(totalStudent);
+
+            if (oldCourseStatisticMaps.containsKey(key)) {
+                //已存在则更新
+                this.updateTotalStudentByOrgExamCourse(info);
+                oldCourseStatisticMaps.remove(key);
             } else {
+                //不存在则新增
                 this.addCourseStatistic(info);
             }
         }
-    }
 
-    private boolean isExistCourseStatistic(ExamCourseInfo info) {
-        SearchBuilder searches = new SearchBuilder()
-                .eq("orgId", info.getOrgId())
-                .eq("examId", info.getExamId())
-                .eq("courseId", info.getCourseId())
-                .eq("paperType", info.getPaperType());
-        Specification<CourseStatistic> spec = SpecUtils.buildSearchers(CourseStatistic.class, searches.build());
-        long total = courseStatisticRepository.count(spec);
-        if (total > 0) {
-            return true;
+        //清除已不存在的考试课程
+        if (oldCourseStatisticMaps.size() > 0) {
+            courseStatisticRepository.delete(oldCourseStatisticMaps.values());
         }
-        return false;
     }
 
     private void addCourseStatistic(ExamCourseInfo info) {
@@ -186,7 +195,7 @@ public class CourseStatisticServiceImpl implements CourseStatisticService {
         courseStatisticRepository.save(statistic);
     }
 
-    private void syncTotalStudentByOrgExamCourse(ExamCourseInfo info) {
+    private void updateTotalStudentByOrgExamCourse(ExamCourseInfo info) {
         if (info == null || info.getTotalStudent() == null) {
             return;
         }
@@ -194,4 +203,22 @@ public class CourseStatisticServiceImpl implements CourseStatisticService {
         courseStatisticRepository.updateTotalStudentByOrgExamCourse(info.getOrgId(), info.getExamId(), info.getCourseId(), info.getPaperType(), info.getTotalStudent());
     }
 
+    private Map<String, CourseStatistic> getCourseStatisticMaps(Long orgId, Long examId) {
+        SearchBuilder searches = new SearchBuilder()
+                .eq("orgId", orgId)
+                .eq("examId", examId);
+        Specification<CourseStatistic> spec = SpecUtils.buildSearchers(CourseStatistic.class, searches.build());
+        List<CourseStatistic> list = courseStatisticRepository.findAll(spec);
+
+        Map<String, CourseStatistic> maps = new HashMap<>();
+        if (list != null && !list.isEmpty()) {
+            for (CourseStatistic statistic : list) {
+                //Key: examId_courseId_paperType
+                String key = statistic.getExamId() + "_" + statistic.getCourseId() + "_" + statistic.getPaperType();
+                maps.put(key, statistic);
+            }
+        }
+        return maps;
+    }
+
 }

+ 3 - 3
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/impl/PrintingProjectStatisticServiceImpl.java

@@ -19,11 +19,11 @@ import cn.com.qmth.examcloud.core.print.service.CourseStatisticService;
 import cn.com.qmth.examcloud.core.print.service.PrintingProjectService;
 import cn.com.qmth.examcloud.core.print.service.PrintingProjectStatisticService;
 import cn.com.qmth.examcloud.core.print.service.StatisticService;
+import cn.com.qmth.examcloud.core.print.service.bean.common.ExamCourseInfo;
 import cn.com.qmth.examcloud.core.print.service.bean.coursestatistic.CourseStatisticLessInfo;
 import cn.com.qmth.examcloud.core.print.service.bean.printingproject.PrintingProjectLessInfo;
 import cn.com.qmth.examcloud.core.print.service.bean.printingprojectstatistic.PrintingProjectStatisticConvert;
 import cn.com.qmth.examcloud.core.print.service.bean.printingprojectstatistic.PrintingProjectStatisticInfo;
-import cn.com.qmth.examcloud.examwork.api.bean.ExamPaperTypeRelation;
 import com.google.common.collect.Lists;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -105,7 +105,7 @@ public class PrintingProjectStatisticServiceImpl implements PrintingProjectStati
             }
 
             //获取某考试的课程和试卷类型列表
-            List<ExamPaperTypeRelation> coursePaperTypes = statisticService.findExamCourseAndPaperTypes(project.getOrgId(), project.getExamId());
+            List<ExamCourseInfo> coursePaperTypes = statisticService.findExamCourseAndPaperTypes(project.getOrgId(), project.getExamId());
 
             //获取某考试的试卷袋列表
             List<String> packageCodes = statisticService.findExamPackageCodes(project.getOrgId(), project.getExamId());
@@ -117,7 +117,7 @@ public class PrintingProjectStatisticServiceImpl implements PrintingProjectStati
             ProjectBackupSetting backupSetting = projectBackupSettingRepository.findByProjectId(project.getProjectId());
 
             //人科次(考生的数量)
-            final int totalStudent = statisticService.countExamTotalStudent(project.getExamId());
+            final int totalStudent = statisticService.countExamTotalStudentByExamId(project.getExamId());
 
             //课程数量(课程代码的数量)
             final int totalCourse = statisticService.countExamTotalCourse(coursePaperTypes);

+ 31 - 41
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/impl/StatisticServiceImpl.java

@@ -23,7 +23,6 @@ import cn.com.qmth.examcloud.examwork.api.response.CountExamStudentResp;
 import cn.com.qmth.examcloud.examwork.api.response.GetExamCoursePaperTypeListResp;
 import cn.com.qmth.examcloud.examwork.api.response.GetExamOrgListResp;
 import cn.com.qmth.examcloud.examwork.api.response.GetExamStudentPropertyValueListResp;
-import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -48,40 +47,8 @@ public class StatisticServiceImpl implements StatisticService {
     @Autowired
     private JdbcTemplate jdbcTemplate;
 
-    @Override
-    public List<ExamCourseInfo> findExamCourses(Long orgId, Long examId) {
-        return this.findExamCourses(orgId, examId, null, null);
-    }
-
-    @Override
-    public List<ExamCourseInfo> findExamCourses(Long orgId, Long examId, Long courseId, String paperType) {
-        //暂时直接查库,待“考务”接口提供后改为通过接口获取数据
-        SqlWrapper sql = new SqlWrapper()
-                .select("root_org_id orgId,exam_id,course_id,course_code,course_name,paper_type,count(course_code) totalStudent")
-                .from("ec_e_exam_student") //old ecs_exam_student
-                .where()
-                .eq("root_org_id", orgId)
-                .and().eq("exam_id", examId);
-        if (courseId != null) {
-            sql.and().eq("course_id", courseId);
-        }
-        if (StringUtils.isNotBlank(paperType)) {
-            sql.and().eq("paper_type", paperType);
-        }
-        sql.groupBy("course_id,paper_type");
-        return jdbcTemplate.query(sql.build(), new BeanPropertyRowMapper(ExamCourseInfo.class));
-    }
-
-    @Override
-    public ExamQuestionStructureInfo findStructureByPaperId(Long examId, String paperId) {
-        ExamQuestionStructureInfo info = new ExamQuestionStructureInfo();
-        //todo
-        return info;
-    }
-
-    public List<ExamPaperTypeRelation> findExamCourseAndPaperTypes(Long orgId, Long examId) {
-        //获取某考试的课程和试卷类型列表
-        List<ExamPaperTypeRelation> coursePaperTypes = new ArrayList<>();
+    public List<ExamCourseInfo> findExamCourseAndPaperTypes(Long orgId, Long examId) {
+        List<ExamCourseInfo> coursePaperTypes = new ArrayList<>();
         GetExamCoursePaperTypeListReq req = new GetExamCoursePaperTypeListReq();
         req.setRootOrgId(orgId);
         req.setExamId(examId);
@@ -93,7 +60,11 @@ public class StatisticServiceImpl implements StatisticService {
             if (list == null || list.isEmpty()) {
                 break;
             }
-            coursePaperTypes.addAll(list);
+
+            for (ExamPaperTypeRelation relation : list) {
+                coursePaperTypes.add(new ExamCourseInfo(orgId, relation.getExamId(), relation.getCourseId(), relation.getPaperType()));
+            }
+
             if (start.equals(resp.getNext())) {
                 break;
             } else {
@@ -104,11 +75,14 @@ public class StatisticServiceImpl implements StatisticService {
     }
 
     @Override
-    public int countExamTotalCourse(List<ExamPaperTypeRelation> coursePaperTypes) {
-        //获取实际课程数量
+    public int countExamTotalCourse(List<ExamCourseInfo> coursePaperTypes) {
+        if (coursePaperTypes == null || coursePaperTypes.isEmpty()) {
+            return 0;
+        }
+        //获取考试的实际课程数量
         Set<Long> ids = new HashSet<>();
-        for (ExamPaperTypeRelation relation : coursePaperTypes) {
-            ids.add(relation.getCourseId());
+        for (ExamCourseInfo info : coursePaperTypes) {
+            ids.add(info.getCourseId());
         }
         return ids.size();
     }
@@ -153,9 +127,18 @@ public class StatisticServiceImpl implements StatisticService {
     }
 
     @Override
-    public int countExamTotalStudent(Long examId) {
+    public int countExamTotalStudentByExamId(Long examId) {
+        CountExamStudentReq req = new CountExamStudentReq();
+        req.setExamId(examId);
+        return this.countExamTotalStudent(req);
+    }
+
+    @Override
+    public int countExamTotalStudentByCourseIdAndPaperType(Long examId, Long courseId, String paperType) {
         CountExamStudentReq req = new CountExamStudentReq();
         req.setExamId(examId);
+        req.setCourseId(courseId);
+        req.setPaperType(paperType);
         return this.countExamTotalStudent(req);
     }
 
@@ -209,6 +192,13 @@ public class StatisticServiceImpl implements StatisticService {
         return properties;
     }
 
+    @Override
+    public ExamQuestionStructureInfo findStructureByPaperId(Long examId, String paperId) {
+        ExamQuestionStructureInfo info = new ExamQuestionStructureInfo();
+        //todo
+        return info;
+    }
+
     @Override
     public List<ExamInfo> findTraditionExams() {
         SqlWrapper sql = new SqlWrapper()