deason 6 years ago
parent
commit
fbdfc1e153

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

@@ -10,9 +10,24 @@ package cn.com.qmth.examcloud.core.print.repository;
 import cn.com.qmth.examcloud.core.print.entity.CourseStatistic;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Modifying;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
 import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.Transactional;
 
 @Repository
 public interface CourseStatisticRepository extends JpaRepository<CourseStatistic, Long>, JpaSpecificationExecutor<CourseStatistic> {
 
+    @Transactional
+    @Modifying
+    @Query("UPDATE CourseStatistic SET courseName=:courseName WHERE courseId=:courseId")
+    int updateCourseNameByCourseId(@Param("courseId") Long courseId, @Param("courseName") String courseName);
+
+    @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);
+
 }

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

@@ -35,6 +35,6 @@ public interface CourseStatisticService {
     /**
      * 同步更新课程名称信息
      */
-    void syncCourseStatisticForCourseName(Long courseId, String courseName);
+    void syncCourseNameByCourseId(Long courseId, String courseName);
 
 }

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

@@ -10,7 +10,6 @@ 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;
@@ -30,7 +29,6 @@ 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;
@@ -46,8 +44,6 @@ public class CourseStatisticServiceImpl implements CourseStatisticService {
     private CourseStatisticRepository courseStatisticsRepository;
     @Autowired
     private CommonService commonService;
-    @Autowired
-    private JdbcTemplate jdbcTemplate;
 
     @Override
     public Page<CourseStatisticInfo> getCourseStatisticList(CourseStatisticQuery query) {
@@ -107,7 +103,7 @@ public class CourseStatisticServiceImpl implements CourseStatisticService {
             }
             boolean isExist = this.isExistCourseStatistic(info);
             if (isExist) {
-                this.syncCourseStatisticForTotalStudent(info);
+                this.syncTotalStudentByOrgExamCourse(info);
             } else {
                 this.addCourseStatistic(info);
             }
@@ -141,30 +137,17 @@ public class CourseStatisticServiceImpl implements CourseStatisticService {
         courseStatisticsRepository.save(statistic);
     }
 
-    private void syncCourseStatisticForTotalStudent(ExamCourseInfo info) {
+    private void syncTotalStudentByOrgExamCourse(ExamCourseInfo info) {
         //仅更新total_student字段
-        SqlWrapper sql = new SqlWrapper()
-                .update("ec_prt_course_statistic")
-                .set().append("total_student = ").append(info.getTotalStudent())
-                .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());
+        courseStatisticsRepository.updateTotalStudentByOrgExamCourse(info.getOrgId(), info.getExamId(), info.getCourseId(), info.getPaperType(), info.getTotalStudent());
     }
 
     @Override
-    public void syncCourseStatisticForCourseName(Long courseId, String courseName) {
+    public void syncCourseNameByCourseId(Long courseId, String courseName) {
         if (courseId == null || StringUtils.isBlank(courseName)) {
             return;
         }
-        SqlWrapper sql = new SqlWrapper()
-                .update("ec_prt_course_statistic")
-                .set().append("course_name = '").append(courseName).append("'")
-                .where()
-                .eq("course_id", courseId);
-        jdbcTemplate.update(sql.build());
+        courseStatisticsRepository.updateCourseNameByCourseId(courseId, courseName);
     }
 
 }

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

@@ -39,12 +39,12 @@ public class CourseStatisticServiceTest extends BaseTest {
         Long orgId = 1L;
         Long examId = 1L;
         Long[] courseIds = {1L};
-        courseStatisticService.initCourseStatistic(orgId, examId, courseIds);
+        //courseStatisticService.initCourseStatistic(orgId, examId, courseIds);
         //courseStatisticService.initAllCourseStatistic();
 
-        Long courseId = 1L;
+        Long courseId = 262L;
         String courseName = "测试课程";
-        courseStatisticService.syncCourseStatisticForCourseName(courseId, courseName);
+        courseStatisticService.syncCourseNameByCourseId(courseId, courseName);
     }
 
 }