Parcourir la source

获取成绩数据列表(分页)接口

deason il y a 5 ans
Parent
commit
40e5704bdb

+ 199 - 57
examcloud-core-oe-admin-api-provider/src/main/java/cn/com/qmth/examcloud/core/oe/admin/api/provider/ExamScoreDataCloudServiceProvider.java

@@ -1,18 +1,5 @@
 package cn.com.qmth.examcloud.core.oe.admin.api.provider;
 
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.commons.lang3.StringUtils;
-import org.json.JSONException;
-import org.json.JSONObject;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.domain.Example;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-
 import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.core.basic.api.CourseCloudService;
 import cn.com.qmth.examcloud.core.basic.api.bean.CourseBean;
@@ -22,31 +9,13 @@ import cn.com.qmth.examcloud.core.oe.admin.api.ExamScoreDataCloudService;
 import cn.com.qmth.examcloud.core.oe.admin.api.bean.ExamScoreDataBean;
 import cn.com.qmth.examcloud.core.oe.admin.api.bean.QueryCapturePhotoBean;
 import cn.com.qmth.examcloud.core.oe.admin.api.bean.ScoreDataBean;
-import cn.com.qmth.examcloud.core.oe.admin.api.request.FindExamScoreDataReq;
-import cn.com.qmth.examcloud.core.oe.admin.api.request.GetAuditDataReq;
-import cn.com.qmth.examcloud.core.oe.admin.api.request.GetFinalScoreDataReq;
-import cn.com.qmth.examcloud.core.oe.admin.api.request.GetScoreDataReq;
-import cn.com.qmth.examcloud.core.oe.admin.api.request.QueryCapturePhotoReq;
-import cn.com.qmth.examcloud.core.oe.admin.api.request.QueryScoreDataReq;
-import cn.com.qmth.examcloud.core.oe.admin.api.response.FindExamScoreDataResp;
-import cn.com.qmth.examcloud.core.oe.admin.api.response.GetAuditDataResp;
-import cn.com.qmth.examcloud.core.oe.admin.api.response.GetFinalScoreDataResp;
-import cn.com.qmth.examcloud.core.oe.admin.api.response.GetScoreDataResp;
-import cn.com.qmth.examcloud.core.oe.admin.api.response.QueryCapturePhotoResp;
-import cn.com.qmth.examcloud.core.oe.admin.api.response.QueryScoreDataResp;
+import cn.com.qmth.examcloud.core.oe.admin.api.request.*;
+import cn.com.qmth.examcloud.core.oe.admin.api.response.*;
 import cn.com.qmth.examcloud.core.oe.admin.base.utils.Check;
-import cn.com.qmth.examcloud.core.oe.admin.dao.ExamAuditRepo;
-import cn.com.qmth.examcloud.core.oe.admin.dao.ExamCaptureRepo;
-import cn.com.qmth.examcloud.core.oe.admin.dao.ExamRecordDataRepo;
-import cn.com.qmth.examcloud.core.oe.admin.dao.ExamScoreRepo;
-import cn.com.qmth.examcloud.core.oe.admin.dao.ExamStudentRepo;
-import cn.com.qmth.examcloud.core.oe.admin.dao.entity.ExamAuditEntity;
-import cn.com.qmth.examcloud.core.oe.admin.dao.entity.ExamCaptureEntity;
-import cn.com.qmth.examcloud.core.oe.admin.dao.entity.ExamRecordDataEntity;
-import cn.com.qmth.examcloud.core.oe.admin.dao.entity.ExamScoreEntity;
-import cn.com.qmth.examcloud.core.oe.admin.dao.entity.ExamStudentEntity;
-import cn.com.qmth.examcloud.core.oe.admin.dao.entity.ExamStudentFinalScoreEntity;
+import cn.com.qmth.examcloud.core.oe.admin.dao.*;
+import cn.com.qmth.examcloud.core.oe.admin.dao.entity.*;
 import cn.com.qmth.examcloud.core.oe.admin.dao.enums.DisciplineType;
+import cn.com.qmth.examcloud.core.oe.admin.dao.enums.ExamRecordStatus;
 import cn.com.qmth.examcloud.core.oe.admin.dao.enums.ExamType;
 import cn.com.qmth.examcloud.core.oe.admin.service.ExamScoreService;
 import cn.com.qmth.examcloud.core.oe.admin.service.ExamStudentFinalScoreService;
@@ -63,15 +32,31 @@ import cn.com.qmth.examcloud.web.helpers.GlobalHelper;
 import cn.com.qmth.examcloud.web.support.ControllerSupport;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Example;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.jpa.domain.Specification;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.persistence.criteria.Predicate;
+import java.text.SimpleDateFormat;
+import java.util.*;
+import java.util.stream.Collectors;
 
 @Api(tags = "考试分数信息相关接口")
 @RestController
 @RequestMapping("${$rmp.cloud.oe}/examScoreData")
 public class ExamScoreDataCloudServiceProvider extends ControllerSupport implements ExamScoreDataCloudService {
 
-    /**
-     *
-     */
     private static final long serialVersionUID = 8280332746440316604L;
 
     @Autowired
@@ -287,35 +272,34 @@ public class ExamScoreDataCloudServiceProvider extends ControllerSupport impleme
     @ApiOperation(value = "按考试名称,rootOrgId,courseCode,身份证号或学号查询成绩信息")
     @PostMapping("/queryScoreData")
     public QueryScoreDataResp queryScoreData(@RequestBody QueryScoreDataReq req) {
-        if (StringUtils.isBlank(req.getExamName())) {
+        /*if (StringUtils.isBlank(req.getExamName())) {
             throw new StatusException("OE-ADMIN-1000005", "考试名称不能为空");
+        }*/
+        if (req.getRootOrgId() == null) {
+            throw new StatusException("OE-ADMIN-1000005", "rootOrgId不能为空");
         }
         if (StringUtils.isBlank(req.getCourseCode())) {
             throw new StatusException("OE-ADMIN-1000005", "课程code不能为空");
         }
-        if (req.getRootOrgId() == null) {
-            throw new StatusException("OE-ADMIN-1000005", "rootOrgId不能为空");
-        }
         if (StringUtils.isBlank(req.getIdentityNumber()) && StringUtils.isBlank(req.getStudentCode())) {
             throw new StatusException("OE-ADMIN-1000005", "身份证号和学号不能都为空");
         }
-        GetExamReq getExamReq = new GetExamReq();
-        getExamReq.setRootOrgId(req.getRootOrgId());
+
+        GetExamReq examReq = new GetExamReq();
+        examReq.setRootOrgId(req.getRootOrgId());
         //如果考试代码为空,则将考试名称作为考试代码
         if (StringUtils.isEmpty(req.getExamCode())) {
-            getExamReq.setCode(req.getExamName());
+            examReq.setCode(req.getExamName());
         } else {
-            getExamReq.setCode(req.getExamCode());
+            examReq.setCode(req.getExamCode());
         }
+        if (StringUtils.isBlank(examReq.getCode())) {
+            throw new StatusException("OE-ADMIN-1000005", "考试代码不能为空");
+        }
+        GetExamResp examResp = examCloudService.getExam(examReq);
+        ExamBean examBean = examResp.getExamBean();
 
-        GetExamResp getExamResp = examCloudService.getExam(getExamReq);
-        ExamBean examBean = getExamResp.getExamBean();
-
-        GetCourseReq getCourseReq = new GetCourseReq();
-        getCourseReq.setRootOrgId(req.getRootOrgId());
-        getCourseReq.setCode(req.getCourseCode());
-        GetCourseResp getCourseResp = courseCloudService.getCourse(getCourseReq);
-        CourseBean courseBean = getCourseResp.getCourseBean();
+        CourseBean courseBean = this.queryCourse(req.getRootOrgId(), null, req.getCourseCode());
 
         ExamRecordDataEntity selectCondition = new ExamRecordDataEntity();
         selectCondition.setExamId(examBean.getId());
@@ -326,9 +310,11 @@ public class ExamScoreDataCloudServiceProvider extends ControllerSupport impleme
         if (StringUtils.isNotBlank(req.getStudentCode())) {
             selectCondition.setStudentCode(req.getStudentCode());
         }
+
         //查询考试记录
-        List<ScoreDataBean> scoreDataBeanList = new ArrayList<ScoreDataBean>();
         List<ExamRecordDataEntity> examRecordList = examRecordDataRepo.findAll(Example.of(selectCondition));
+
+        List<ScoreDataBean> scoreDataBeanList = new ArrayList<>();
         for (ExamRecordDataEntity examRecordData : examRecordList) {
             ExamScoreEntity examScore = examScoreRepo.findByExamRecordDataId(examRecordData.getId());
             if (examScore == null) {
@@ -345,18 +331,174 @@ public class ExamScoreDataCloudServiceProvider extends ControllerSupport impleme
             scoreDataBean.setCourseName(courseBean.getName());
             scoreDataBean.setStartTime(examRecordData.getStartTime());
             scoreDataBean.setEndTime(examRecordData.getEndTime());
-            scoreDataBean.setTotalScore(examScore.getTotalScore());
 
             scoreDataBean.setIsWarn(examRecordData.getIsWarn());
             scoreDataBean.setIsAudit(examRecordData.getIsAudit());
             scoreDataBean.setIsIllegality(examRecordData.getIsIllegality());
+            scoreDataBean.setTotalScore(examScore.getTotalScore());
             scoreDataBeanList.add(scoreDataBean);
         }
+
         QueryScoreDataResp queryScoreDataResp = new QueryScoreDataResp();
         queryScoreDataResp.setScoreDataBeanList(scoreDataBeanList);
         return queryScoreDataResp;
     }
 
+    @Override
+    @ApiOperation(value = "获取成绩数据列表(分页)")
+    @PostMapping("/queryScoreList")
+    public QueryScoreListResp queryScoreList(@RequestBody QueryScoreListReq req) {
+        if (req.getRootOrgId() == null) {
+            throw new StatusException("OE-ADMIN-1001005", "rootOrgId不能为空");
+        }
+
+        if (req.getPageNo() == null || req.getPageNo() < 1) {
+            req.setPageNo(1);// 第几页,起始为1
+        }
+        if (req.getPageSize() == null || req.getPageSize() < 1) {
+            req.setPageSize(10);// 每页条数,默认值
+        }
+        if (req.getPageSize() > 100) {
+            req.setPageSize(100);// 每页条数,限制最大100条
+        }
+
+        final String pattern = "yyyy-MM-dd HH:mm:ss";
+        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
+
+        final Date queryStartTime;
+        try {
+            if (StringUtils.isNotBlank(req.getQueryStartTime())) {
+                queryStartTime = sdf.parse(req.getQueryStartTime());
+            } else {
+                queryStartTime = null;
+            }
+        } catch (Exception e) {
+            throw new StatusException("OE-ADMIN-1001005", "queryStartTime值有误,格式:" + pattern);
+        }
+
+        final Date queryEndTime;
+        try {
+            if (StringUtils.isNotBlank(req.getQueryEndTime())) {
+                queryEndTime = sdf.parse(req.getQueryEndTime());
+            } else {
+                queryEndTime = null;
+            }
+        } catch (Exception e) {
+            throw new StatusException("OE-ADMIN-1001005", "queryEndTime值有误,格式:" + pattern);
+        }
+
+        GetExamReq examReq = new GetExamReq();
+        examReq.setRootOrgId(req.getRootOrgId());
+        if (StringUtils.isEmpty(req.getExamCode())) {
+            // 如果考试代码为空,则将考试名称作为考试代码
+            examReq.setCode(req.getExamName());
+        } else {
+            examReq.setCode(req.getExamCode());
+        }
+        if (StringUtils.isBlank(examReq.getCode())) {
+            throw new StatusException("OE-ADMIN-1001005", "考试代码不能为空");
+        }
+        GetExamResp examResp = examCloudService.getExam(examReq);
+        final Long examId = examResp.getExamBean().getId();
+
+        final CourseBean queryCourse;
+        if (StringUtils.isNotBlank(req.getCourseCode())) {
+            queryCourse = this.queryCourse(req.getRootOrgId(), null, req.getCourseCode());
+        } else {
+            queryCourse = null;
+        }
+
+        Specification<ExamRecordDataEntity> spec = (Specification<ExamRecordDataEntity>) (root, query, builder) -> {
+            List<Predicate> predicates = new ArrayList<>();
+            predicates.add(builder.equal(root.get("examId"), examId));
+
+            if (queryCourse != null) {
+                predicates.add(builder.equal(root.get("courseId"), queryCourse.getId()));
+            }
+
+            predicates.add(builder.notEqual(root.get("examRecordStatus"), ExamRecordStatus.EXAM_INVALID));
+
+            if (queryStartTime != null) {
+                predicates.add(builder.greaterThanOrEqualTo(root.get("updateTime"), queryStartTime));
+            }
+
+            if (queryEndTime != null) {
+                predicates.add(builder.lessThanOrEqualTo(root.get("updateTime"), queryEndTime));
+            }
+
+            return builder.and(predicates.toArray(new Predicate[predicates.size()]));
+        };
+
+        Pageable pageable = PageRequest.of(req.getPageNo() - 1, req.getPageSize());
+        Page<ExamRecordDataEntity> page = examRecordDataRepo.findAll(spec, pageable);
+        List<ExamRecordDataEntity> examRecordDataEntities = page.getContent();
+
+        QueryScoreListResp queryResp = new QueryScoreListResp();
+        queryResp.setTotalElements(page.getTotalElements());
+        queryResp.setTotalPages(page.getTotalPages());
+        List<ScoreDataBean> scoreList = new ArrayList<>();
+        queryResp.setScoreList(scoreList);
+
+        if (CollectionUtils.isEmpty(examRecordDataEntities)) {
+            return queryResp;
+        }
+
+        List<Long> examRecordDataIds = examRecordDataEntities.stream().map(ExamRecordDataEntity::getId).collect(Collectors.toList());
+        List<ExamScoreEntity> examScoreEntities = examScoreRepo.findByExamRecordDataIdIn(examRecordDataIds);
+        if (CollectionUtils.isEmpty(examScoreEntities)) {
+            log.info("examScoreEntities size is empty. ");
+            return queryResp;
+        }
+
+        log.info("examRecordData size is " + examRecordDataEntities.size() + ", examScore size is " + examScoreEntities.size());
+        Map<Long, ExamScoreEntity> examScoreMaps = examScoreEntities.stream().collect(Collectors.toMap(ExamScoreEntity::getExamRecordDataId, v -> v, (k, v) -> v));
+
+        Map<Long, CourseBean> courseMaps = new HashMap<>();
+        for (ExamRecordDataEntity examRecordData : examRecordDataEntities) {
+            ExamScoreEntity examScore = examScoreMaps.get(examRecordData.getId());
+            if (examScore == null) {
+                log.debug("examSore not exist. examRecordDataId = " + examRecordData.getId());
+                continue;
+            }
+
+            ScoreDataBean score = new ScoreDataBean();
+            if (queryCourse != null) {
+                score.setCourseCode(queryCourse.getCode());
+                score.setCourseName(queryCourse.getName());
+            } else {
+                CourseBean course = courseMaps.get(examRecordData.getCourseId());
+                if (course == null) {
+                    course = this.queryCourse(examRecordData.getRootOrgId(), examRecordData.getCourseId(), null);
+                    courseMaps.put(course.getId(), course);
+                }
+                score.setCourseCode(course.getCode());
+                score.setCourseName(course.getName());
+            }
+            score.setStartTime(examRecordData.getStartTime());
+            score.setEndTime(examRecordData.getEndTime());
+            score.setExamRecordDataId(examRecordData.getId());
+            score.setStudentCode(examRecordData.getStudentCode());
+            score.setStudentName(examRecordData.getStudentName());
+            score.setIdentityNumber(examRecordData.getIdentityNumber());
+            score.setIsIllegality(examRecordData.getIsIllegality());
+            score.setIsWarn(examRecordData.getIsWarn());
+            score.setIsAudit(examRecordData.getIsAudit());
+            score.setTotalScore(examScore.getTotalScore());
+            scoreList.add(score);
+        }
+
+        return queryResp;
+    }
+
+    private CourseBean queryCourse(Long rootOrgId, Long courseId, String courseCode) {
+        GetCourseReq courseReq = new GetCourseReq();
+        courseReq.setRootOrgId(rootOrgId);
+        courseReq.setId(courseId);
+        courseReq.setCode(courseCode);
+        GetCourseResp courseResp = courseCloudService.getCourse(courseReq);
+        return courseResp.getCourseBean();
+    }
+
     @ApiOperation(value = "按考试信息,rootOrgId,courseCode,身份证号或学号查询最终成绩信息")
     @PostMapping("/getFinalScoreData")
     @Override