Pārlūkot izejas kodu

异步导出监考待审下一条sql优化

xiatian 3 gadi atpakaļ
vecāks
revīzija
8c888178c6

+ 32 - 10
examcloud-core-oe-admin-service/src/main/java/cn/com/qmth/examcloud/core/oe/admin/service/impl/ExamAuditServiceImpl.java

@@ -10,6 +10,7 @@ package cn.com.qmth.examcloud.core.oe.admin.service.impl;
 import static cn.com.qmth.examcloud.core.oe.admin.service.bean.examaudit.ExamAuditMapper.EXAM_RECORD_DATA_ID;
 
 import java.math.BigInteger;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import java.util.Optional;
@@ -55,6 +56,7 @@ import cn.com.qmth.examcloud.core.oe.admin.dao.entity.ExamFaceLivenessVerifyEnti
 import cn.com.qmth.examcloud.core.oe.admin.dao.entity.ExamRecordDataEntity;
 import cn.com.qmth.examcloud.core.oe.admin.dao.entity.ExamRecordForMarkingEntity;
 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.entity.FaceBiopsyItemEntity;
 import cn.com.qmth.examcloud.core.oe.admin.dao.entity.IllegallyTypeEntity;
@@ -164,7 +166,8 @@ public class ExamAuditServiceImpl implements ExamAuditService {
      */
     private static final String AUDIT_USER_NAME = "SYSTEM";
 
-    @Override
+    @SuppressWarnings({ "deprecation", "unchecked", "rawtypes" })
+	@Override
     public Page<ExamAuditInfo> getExamAuditList(UserDataRules uds,ExamAuditQuery query) {
     	if(uds.getCourseRule().assertEmptyQueryResult()||uds.getOrgRule().assertEmptyQueryResult()) {
     		return Page.empty();
@@ -210,7 +213,7 @@ public class ExamAuditServiceImpl implements ExamAuditService {
             OrgCacheBean orgBean = gainBaseDataService.getOrgBean(examAuditInfo.getOrgId());
             examAuditInfo.setOrgName(orgBean.getName());
 
-            //将违纪类型重赋值为文本,万恶的历史原因,不该这么做的。TODO 待优化
+            //将违纪类型重赋值为文本,万恶的历史原因,不该这么做的。 待优化
             if (StringUtils.isNotEmpty(examAuditInfo.getDisciplineType())) {
                 IllegallyTypeEntity illegallyType = illegallyTypeService.getIllegallyType(orgBean.getRootId(), examAuditInfo.getDisciplineType());
                 examAuditInfo.setDisciplineType(illegallyType.getName());
@@ -235,20 +238,39 @@ public class ExamAuditServiceImpl implements ExamAuditService {
         return new PageImpl<ExamAuditInfo>(list, pageable, totalSize);
     }
 
-    @Override
-    public List<ExamAuditInfo> getExamAudit(UserDataRules uds,ExamAuditQuery query) {
-        Check.isNull(query, "查询参数不能为空!");
-        Check.isNull(query.getExamId(), "请先选择考试批次!");
-
-        SqlWrapper wrapper = getSqlWrapper(uds,query);
+    @SuppressWarnings({ "rawtypes", "deprecation" })
+	private List getExamAuditForAsyncPage(UserDataRules uds,ExamAuditQuery query,int pageNo, int pageSize) {
+    	SqlWrapper wrapper = getSqlWrapper(uds,query);
 
         wrapper.groupBy("record.id ");
         //查询分页记录
         wrapper.orderBy("record.update_time", true);//需求调整20200816:按审核时间排序
         Query dataQuery = entityManager.createNativeQuery(wrapper.build());
         dataQuery.unwrap(NativeQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
-
+        int currentNum = (pageNo - 1) * pageSize;
+        dataQuery.setFirstResult(currentNum);
+        dataQuery.setMaxResults(pageSize);
         List resultList = dataQuery.getResultList();
+        return resultList;
+    }
+    
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+	@Override
+    public List<ExamAuditInfo> getExamAudit(UserDataRules uds,ExamAuditQuery query) {
+        Check.isNull(query, "查询参数不能为空!");
+        Check.isNull(query.getExamId(), "请先选择考试批次!");
+        List resultList = new ArrayList<ExamStudentEntity>();
+        int pageNo = 1;
+        int pageSize = 200;
+        for (; ; ) {
+            List tem = getExamAuditForAsyncPage(uds,query,pageNo, pageSize);
+            if (tem == null || tem.size() == 0) {
+                break;
+            } else {
+                pageNo++;
+                resultList.addAll(tem);
+            }
+        }
 
         List<ExamAuditInfo> list = ExamAuditEntityConvert.of(resultList);
 
@@ -256,7 +278,7 @@ public class ExamAuditServiceImpl implements ExamAuditService {
             OrgCacheBean orgBean = gainBaseDataService.getOrgBean(examAuditInfo.getOrgId());
             examAuditInfo.setOrgName(orgBean.getName());
 
-            //将违纪类型重赋值为文本,万恶的历史原因,不该这么做的。TODO 待优化
+            //将违纪类型重赋值为文本,万恶的历史原因,不该这么做的。 待优化
             if (StringUtils.isNotEmpty(examAuditInfo.getDisciplineType())) {
                 IllegallyTypeEntity illegallyType = illegallyTypeService.getIllegallyType(orgBean.getRootId(), examAuditInfo.getDisciplineType());
                 examAuditInfo.setDisciplineType(illegallyType.getName());

+ 18 - 27
examcloud-core-oe-admin-service/src/main/java/cn/com/qmth/examcloud/core/oe/admin/service/impl/ExamRecordServiceImpl.java

@@ -287,11 +287,13 @@ public class ExamRecordServiceImpl implements ExamRecordService {
 
     @Override
     public Long getExamRecordWaitingAuditNextId(UserDataRules uds,ExamRecordQuery query, Long examRecordDataId, String next) {
+    	//****注意按ID倒序排列的****,上一条,下一条
     	if(uds.getCourseRule().assertEmptyQueryResult()||uds.getOrgRule().assertEmptyQueryResult()) {
     		return null;
     	}
     	Check.isNull(query, "查询参数不能为空!");
         Check.isNull(query.getExamId(), "请先选择考试批次!");
+        Check.isBlank(next, "[next]参数不能为空!");
         //默认条件
         query.setIsWarn(true);
         query.setIsAudit(false);
@@ -299,7 +301,11 @@ public class ExamRecordServiceImpl implements ExamRecordService {
         query.addRecordStatus(ExamRecordStatus.EXAM_OVERDUE.name());
         //查询条件
         StringBuilder sqlBuilder = new StringBuilder();
-        sqlBuilder.append("select record_data.id from ec_oe_exam_record_data record_data where 1 = 1 ");
+        if ("1".equals(next)) {//下一条
+        	sqlBuilder.append("select max(record_data.id) id from ec_oe_exam_record_data record_data where 1 = 1 ");
+        } else if ("0".equals(next)) {//上一条
+        	sqlBuilder.append("select min(record_data.id) id from ec_oe_exam_record_data record_data where 1 = 1 ");
+        }
         sqlBuilder.append(buildExamRecordCommonSelectCondition(uds,query));
         if (query.getIsWarn() != null) {
             //只查有异常未审核
@@ -312,29 +318,13 @@ public class ExamRecordServiceImpl implements ExamRecordService {
             sqlBuilder.append(" and ((record_data.is_warn = 0) OR (record_data.is_warn  = 1 and record_data.is_audit = 0))");
         }
         sqlBuilder.append(" and record_data.is_illegality = 0");
-        if (StringUtils.isBlank(next)) {
-            sqlBuilder.append(" and record_data.id in (select id from ec_oe_exam_record_data where");
-            sqlBuilder.append(" id != ");
-            sqlBuilder.append(examRecordDataId);
-            sqlBuilder.append(" and exam_id=" + query.getExamId());
-            sqlBuilder.append(" ) ");
-            sqlBuilder.append(" order by record_data.id desc");
-        } else if ("1".equals(next)) {
-            sqlBuilder.append(" and record_data.id in (select id from ec_oe_exam_record_data where");
-            sqlBuilder.append(" id < ");
+        if ("1".equals(next)) {//下一条
+            sqlBuilder.append(" and record_data.id < ");
             sqlBuilder.append(examRecordDataId);
-            sqlBuilder.append(" and exam_id=" + query.getExamId());
-            sqlBuilder.append(" ) ");
-            sqlBuilder.append(" order by record_data.id desc");
-        } else if ("0".equals(next)) {
-            sqlBuilder.append(" and record_data.id in (select id from ec_oe_exam_record_data where");
-            sqlBuilder.append(" id > ");
+        } else if ("0".equals(next)) {//上一条
+        	sqlBuilder.append(" and record_data.id > ");
             sqlBuilder.append(examRecordDataId);
-            sqlBuilder.append(" and exam_id=" + query.getExamId());
-            sqlBuilder.append(" ) ");
-            sqlBuilder.append(" order by record_data.id asc");
         }
-        sqlBuilder.append(" limit 1 ");
         List<Long> ids = jdbcTemplate.query(sqlBuilder.toString(), (rs, rowNum) -> rs.getLong("id"));
         return CollectionUtils.isEmpty(ids) ? null : ids.get(0);
     }
@@ -740,13 +730,14 @@ public class ExamRecordServiceImpl implements ExamRecordService {
         query.addRecordStatus(ExamRecordStatus.EXAM_OVERDUE.name());
 
         List<ExamRecordInfo> examRecordDataList = new ArrayList<ExamRecordInfo>();
-        Long startId = 0L;
+        int pageNo = 1;
+        int pageSize = 200;
         for (; ; ) {
-            List<ExamRecordInfo> tem = getExamRecordDetailPageForExport(uds,query, startId);
+            List<ExamRecordInfo> tem = getExamRecordDetailPageForExport(uds,query, pageNo,pageSize);
             if (tem == null || tem.size() == 0) {
                 break;
             } else {
-                startId = tem.get(tem.size() - 1).getId();
+            	pageNo++;
                 examRecordDataList.addAll(tem);
             }
         }
@@ -955,18 +946,18 @@ public class ExamRecordServiceImpl implements ExamRecordService {
 
     }
 
-    private List<ExamRecordInfo> getExamRecordDetailPageForExport(UserDataRules uds,ExamRecordQuery query, Long startId) {
+    private List<ExamRecordInfo> getExamRecordDetailPageForExport(UserDataRules uds,ExamRecordQuery query,int pageNo, int pageSize) {
         StringBuilder sqlBuilder = new StringBuilder();
         sqlBuilder.append(EXAM_RECORD_EXPORT_SQL);
         sqlBuilder.append(buildExamRecordCommonSelectCondition(uds,query));
         sqlBuilder.append(" and ((record_data.is_warn = 0) OR (record_data.is_warn  = 1 and record_data.is_audit = 1))");
-        sqlBuilder.append(" and record_data.id >" + startId);
         sqlBuilder.append(" group by record_data.id ");
         //根据ip查询
         /*if (StringUtils.isNoneBlank(query.getIp())) {
             sqlBuilder.append(" having ip like '%"+query.getIp()+"%' ");
         }*/
-        sqlBuilder.append(" order by record_data.id limit 500");
+        int currentNum = (pageNo - 1) * pageSize;
+        sqlBuilder.append(" order by record_data.id limit " + currentNum + "," + pageSize);
 
         RowMapper<ExamRecordInfo> rowMapper = new BeanPropertyRowMapper<ExamRecordInfo>(ExamRecordInfo.class);
         List<ExamRecordInfo> ret = jdbcTemplate.query(sqlBuilder.toString(), rowMapper);

+ 9 - 8
examcloud-core-oe-admin-service/src/main/java/cn/com/qmth/examcloud/core/oe/admin/service/impl/ExamScoreServiceImpl.java

@@ -70,7 +70,7 @@ import cn.com.qmth.examcloud.web.redis.RedisClient;
  */
 @Service
 public class ExamScoreServiceImpl implements ExamScoreService {
-
+	
     @Autowired
     private ExamScoreRepo examScoreRepo;
 
@@ -191,13 +191,14 @@ public class ExamScoreServiceImpl implements ExamScoreService {
             return getExamStudentInfoListOfScoreExport(uds,query, markingType);
         } else {
             List<ExamStudentEntity> examStudentList = new ArrayList<ExamStudentEntity>();
-            Long startId = 0L;
+            int pageNo = 1;
+            int pageSize = 200;
             for (; ; ) {
-                List<ExamStudentEntity> tem = getExamStudentInfoListOfScoreExportByPage(uds,query, markingType, startId);
+                List<ExamStudentEntity> tem = getExamStudentInfoListOfScoreExportByPage(uds,query, markingType, pageNo,pageSize);
                 if (tem == null || tem.size() == 0) {
                     break;
                 } else {
-                    startId = tem.get(tem.size() - 1).getId();
+                	pageNo++;
                     examStudentList.addAll(tem);
                 }
             }
@@ -218,7 +219,7 @@ public class ExamScoreServiceImpl implements ExamScoreService {
         }
     }
 
-    private List<ExamStudentEntity> getExamStudentInfoListOfScoreExportByPage(UserDataRules uds,ExamScoreQuery query, String markingType, Long startId) {
+    private List<ExamStudentEntity> getExamStudentInfoListOfScoreExportByPage(UserDataRules uds,ExamScoreQuery query, String markingType,int pageNo, int pageSize) {
         //查询条件
         StringBuffer sql = new StringBuffer();
         sql.append("select id,exam_student_id,exam_id,course_id,course_code,course_level,finished,student_id,student_code,student_name,identity_number"
@@ -257,8 +258,8 @@ public class ExamScoreServiceImpl implements ExamScoreService {
         if(uds.getCourseRule().assertNeedQueryRefIds()) {
         	sql.append(" and course_id in (" + StringUtils.join(uds.getCourseRule().getRefIds(), ",")+") ");
         }
-        sql.append(" and id >" + startId);
-        sql.append(" order by id limit 500 ");
+        int currentNum = (pageNo - 1) * pageSize;
+        sql.append(" order by id limit " + currentNum + "," + pageSize);
 
         List<ExamStudentEntity> examStudentList = jdbcTemplate.query(sql.toString(), new RowMapper<ExamStudentEntity>() {
             @Override
@@ -333,7 +334,7 @@ public class ExamScoreServiceImpl implements ExamScoreService {
         if(uds.getCourseRule().assertNeedQueryRefIds()) {
         	sql.append(" and course_id in (" + StringUtils.join(uds.getCourseRule().getRefIds(), ",")+") ");
         }
-        sql.append(" order by update_time desc");
+        sql.append(" order by id");
 
         if (query.getStartLimit() != null && query.getEndLimit() != null) {
             int offset = query.getEndLimit() - query.getStartLimit() + 1;

+ 5 - 3
examcloud-core-oe-admin-service/src/main/java/cn/com/qmth/examcloud/core/oe/admin/service/impl/ExamStudentServiceImpl.java

@@ -341,7 +341,7 @@ public class ExamStudentServiceImpl implements ExamStudentService {
 
         List<ExamStudentEntity> examStudentList = new ArrayList<ExamStudentEntity>();
         int pageNo = 1;
-        int pageSize = 500;
+        int pageSize = 200;
         for (; ; ) {
             List<ExamStudentEntity> tem = getExamStudentInfoListByPage(uds,query, examBean, pageNo, pageSize);
             if (tem == null || tem.size() == 0) {
@@ -829,7 +829,8 @@ public class ExamStudentServiceImpl implements ExamStudentService {
         return jdbcTemplate.queryForObject(sql.toString(), Long.class);
     }
 
-    @Override
+    @SuppressWarnings({ "deprecation", "unchecked", "rawtypes" })
+	@Override
     public ExamStudentFinishedStatistic getExamStudentStatisticByFinished(Long examId, Long examStageId) {
         ExamSettingsCacheBean examBean = ExamCacheTransferHelper.getDefaultCachedExam(examId);
         if (ExamType.ONLINE.name().equals(examBean.getExamType()) ||
@@ -883,7 +884,8 @@ public class ExamStudentServiceImpl implements ExamStudentService {
         }
     }
 
-    @Override
+    @SuppressWarnings({ "deprecation", "unchecked", "unused" })
+	@Override
     public List<ExamStudentOrgStatistic> getExamStudentStatisticByOrg(UserDataRule ud,Long examId, Long examStageId, Long orgId) {
     	if(ud.assertEmptyQueryResult()) {
     		return Lists.newArrayList();