|
@@ -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;
|
|
|
+ }
|
|
|
+
|
|
|
}
|