deason há 6 anos atrás
pai
commit
c2496f0380

+ 5 - 0
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/jpa/SqlWrapper.java

@@ -29,6 +29,11 @@ public class SqlWrapper {
         return this;
     }
 
+    public SqlWrapper append(Number str) {
+        sql.append(str);
+        return this;
+    }
+
     public SqlWrapper as(String name) {
         sql.append(AS).append(name);
         return this;

+ 2 - 1
examcloud-core-print-dao/src/main/java/cn/com/qmth/examcloud/core/print/entity/CourseStatistic.java

@@ -17,7 +17,8 @@ import javax.persistence.*;
 @Entity
 @Table(name = "ec_prt_course_statistic", indexes = {
         @Index(name = "INDEX_PRT_COURSE_STATISTIC_01", columnList = "orgId"),
-        @Index(name = "INDEX_PRT_COURSE_STATISTIC_02", columnList = "examId")})
+        @Index(name = "INDEX_PRT_COURSE_STATISTIC_02", columnList = "examId"),
+        @Index(name = "INDEX_PRT_COURSE_STATISTIC_03", columnList = "courseId")})
 public class CourseStatistic extends IdEntity {
     /**
      * 学校机构ID

+ 5 - 1
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/CommonService.java

@@ -46,6 +46,10 @@ public class CommonService {
         return jdbcTemplate.query(sql.build(), new BeanPropertyRowMapper(ExamInfo.class));
     }
 
+    /**
+     * 获取考试所有的开考课程列表
+     * 含试卷类型、考生数量
+     */
     public List<ExamCourseInfo> getExamCourseList(Long orgId, Long examId) {
         SqlWrapper sql = new SqlWrapper()
                 .select("root_org_id orgId,exam_id,course_id,course_code,course_name,paper_type,count(course_code) totalStudent")
@@ -54,7 +58,7 @@ public class CommonService {
                 .eq("root_org_id", orgId)
                 .and().eq("exam_id", examId)
                 .groupBy("course_id,paper_type");
-        log.debug(sql.build());
+        //log.debug(sql.build());
         return jdbcTemplate.query(sql.build(), new BeanPropertyRowMapper(ExamCourseInfo.class));
     }
 

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

@@ -10,9 +10,11 @@ package cn.com.qmth.examcloud.core.print.service.impl;
 import cn.com.qmth.examcloud.core.print.common.jpa.OrderBuilder;
 import cn.com.qmth.examcloud.core.print.common.jpa.SearchBuilder;
 import cn.com.qmth.examcloud.core.print.common.jpa.SpecUtils;
+import cn.com.qmth.examcloud.core.print.common.jpa.SqlWrapper;
 import cn.com.qmth.examcloud.core.print.common.utils.Check;
 import cn.com.qmth.examcloud.core.print.entity.CourseStatistic;
 import cn.com.qmth.examcloud.core.print.enums.ExamType;
+import cn.com.qmth.examcloud.core.print.enums.PaperBindingStatus;
 import cn.com.qmth.examcloud.core.print.repository.CourseStatisticRepository;
 import cn.com.qmth.examcloud.core.print.service.CommonService;
 import cn.com.qmth.examcloud.core.print.service.CourseStatisticService;
@@ -28,6 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.domain.Specification;
+import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
@@ -43,6 +46,8 @@ public class CourseStatisticServiceImpl implements CourseStatisticService {
     private CourseStatisticRepository courseStatisticsRepository;
     @Autowired
     private CommonService commonService;
+    @Autowired
+    private JdbcTemplate jdbcTemplate;
 
     @Override
     public Page<CourseStatisticInfo> getCourseStatisticList(CourseStatisticQuery query) {
@@ -76,22 +81,78 @@ public class CourseStatisticServiceImpl implements CourseStatisticService {
     @Override
     public void initCourseStatistic(Long orgId, Long examId, Long[] courseIds) {
         List<ExamCourseInfo> examCourses = commonService.getExamCourseList(orgId, examId);
-        //todo
-        System.out.println(examCourses.size());
+        this.syncCourseStatisticList(examCourses);
     }
 
     @Override
     public void initAllCourseStatistic() {
-        //获取所有"传统"考试列表
         log.debug("initAllCourseStatistic...");
         List<ExamInfo> exams = commonService.getExamList(ExamType.TRADITION.name());
         if (exams != null && !exams.isEmpty()) {
             for (ExamInfo info : exams) {
                 List<ExamCourseInfo> examCourses = commonService.getExamCourseList(info.getOrgId(), info.getExamId());
-                //todo
-                System.out.println(examCourses.size());
+                this.syncCourseStatisticList(examCourses);
+            }
+        }
+    }
+
+    private void syncCourseStatisticList(List<ExamCourseInfo> examCourses) {
+        if (examCourses == null || examCourses.isEmpty()) {
+            return;
+        }
+        for (ExamCourseInfo info : examCourses) {
+            if (info.getCourseId() == null) {
+                continue;
+            }
+            boolean isExist = this.isExistCourseStatistic(info);
+            if (isExist) {
+                this.updateCourseStatistic(info);
+            } 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 = courseStatisticsRepository.count(spec);
+        if (total > 0) {
+            return true;
+        }
+        return false;
+    }
+
+    private void addCourseStatistic(ExamCourseInfo info) {
+        CourseStatistic statistic = new CourseStatistic();
+        statistic.setOrgId(info.getOrgId());
+        statistic.setExamId(info.getExamId());
+        statistic.setCourseId(info.getCourseId());
+        statistic.setCourseCode(info.getCourseCode());
+        statistic.setCourseName(info.getCourseName());
+        statistic.setPaperType(info.getPaperType());
+        statistic.setTotalStudent(info.getTotalStudent());
+        statistic.setPaperBindingStatus(PaperBindingStatus.无.getIndex());
+        courseStatisticsRepository.save(statistic);
+    }
+
+    private void updateCourseStatistic(ExamCourseInfo info) {
+        //仅更新total_student、course_name字段
+        SqlWrapper sql = new SqlWrapper()
+                .update("ec_prt_course_statistic")
+                .set()
+                .append("total_student = '").append(info.getTotalStudent()).append("'")
+                .append(",course_name = '").append(info.getCourseName()).append("'")
+                .where()
+                .eq("org_id", info.getOrgId())
+                .and().eq("exam_id", info.getExamId())
+                .and().eq("course_id", info.getCourseId())
+                .and().eq("paper_type", info.getPaperType());
+        jdbcTemplate.update(sql.build());
+    }
+
 }

+ 1 - 1
examcloud-core-print-starter/src/test/java/cn/com/qmth/examcloud/core/print/test/CourseStatisticServiceTest.java

@@ -40,7 +40,7 @@ public class CourseStatisticServiceTest extends BaseTest {
         Long examId = 1L;
         Long[] courseIds = {1L};
         courseStatisticService.initCourseStatistic(orgId, examId, courseIds);
-        courseStatisticService.initAllCourseStatistic();
+        //courseStatisticService.initAllCourseStatistic();
     }
 
 }