Browse Source

代考列表逻辑

wangwei 5 years ago
parent
commit
a3329c1fa4

+ 0 - 17
examcloud-core-examwork-api-provider/src/main/java/cn/com/qmth/examcloud/core/examwork/api/provider/ExamCloudServiceProvider.java

@@ -231,23 +231,6 @@ public class ExamCloudServiceProvider extends ControllerSupport implements ExamC
 		bean.setRootOrgId(exam.getRootOrgId());
 		bean.setExamLimit(exam.getExamLimit());
 
-		Long orgId = req.getOrgId();
-		if (null != orgId) {
-			ExamSpecialSettingsEntity examOrgEntity = examSpecialSettingsRepo
-					.findByExamIdAndOrgIdAndCourseIdIsNullAndStudentIdIsNull(exam.getId(), orgId);
-			if (null != examOrgEntity) {
-				if (null != examOrgEntity.getBeginTime()) {
-					bean.setBeginTime(examOrgEntity.getBeginTime());
-				}
-				if (null != examOrgEntity.getEndTime()) {
-					bean.setEndTime(examOrgEntity.getEndTime());
-				}
-				if (null != examOrgEntity.getExamLimit()) {
-					bean.setExamLimit(examOrgEntity.getExamLimit());
-				}
-			}
-		}
-
 		return examResp;
 	}
 

+ 14 - 1
examcloud-core-examwork-dao/src/main/java/cn/com/qmth/examcloud/core/examwork/dao/ExamSpecialSettingsRepo.java

@@ -30,11 +30,24 @@ public interface ExamSpecialSettingsRepo
 	List<ExamSpecialSettingsEntity> findAllByExamIdAndCourseIdIsNullAndStudentIdIsNullAndOrgIdIsNotNull(
 			Long examId);
 
+	void deleteByExamIdAndOrgIdIsNotNullAndCourseIdIsNullAndStudentIdIsNull(Long examId);
+
 	@Transactional
 	@Modifying
 	@Query("update ExamSpecialSettingsEntity set examEnable = :examEnable where examId = :examId")
 	void updateExamEnableByExamId(@Param("examId") long examId,
 			@Param("examEnable") Boolean examEnable);
 
-	void deleteByExamIdAndOrgIdIsNotNullAndCourseIdIsNullAndStudentIdIsNull(Long examId);
+	@Transactional
+	@Modifying
+	@Query("update ExamSpecialSettingsEntity set specialSettingsEnabled = :specialSettingsEnabled where examId = :examId")
+	void updateSpecialSettingsEnabledByExamId(@Param("examId") long examId,
+			@Param("specialSettingsEnabled") Boolean specialSettingsEnabled);
+
+	@Transactional
+	@Modifying
+	@Query("update ExamSpecialSettingsEntity set specialSettingsType = :specialSettingsType where examId = :examId")
+	void updateSpecialSettingsTypeByExamId(@Param("examId") long examId,
+			@Param("specialSettingsType") String specialSettingsType);
+
 }

+ 34 - 1
examcloud-core-examwork-dao/src/main/java/cn/com/qmth/examcloud/core/examwork/dao/entity/ExamSpecialSettingsEntity.java

@@ -14,6 +14,7 @@ import javax.persistence.Table;
 
 import org.springframework.format.annotation.DateTimeFormat;
 
+import cn.com.qmth.examcloud.api.commons.enums.ExamSpecialSettingsType;
 import cn.com.qmth.examcloud.api.commons.enums.ExamType;
 import cn.com.qmth.examcloud.web.jpa.JpaEntity;
 
@@ -34,7 +35,10 @@ import cn.com.qmth.examcloud.web.jpa.JpaEntity;
 		@Index(name = "IDX_E_E_S_S_001001", columnList = "examId,courseId,orgId,studentId", unique = true),
 		@Index(name = "IDX_E_E_S_S_001002", columnList = "examId,courseId", unique = false),
 		@Index(name = "IDX_E_E_S_S_001003", columnList = "examId,orgId", unique = false),
-		@Index(name = "IDX_E_E_S_S_001004", columnList = "examId,studentId", unique = false)})
+		@Index(name = "IDX_E_E_S_S_001004", columnList = "examId,studentId", unique = false),
+		@Index(name = "IDX_E_E_S_S_001005", columnList = "rootOrgId,courseId", unique = false),
+		@Index(name = "IDX_E_E_S_S_001006", columnList = "rootOrgId,orgId", unique = false),
+		@Index(name = "IDX_E_E_S_S_001007", columnList = "rootOrgId,studentId", unique = false)})
 public class ExamSpecialSettingsEntity extends JpaEntity {
 
 	private static final long serialVersionUID = -3335725218626631530L;
@@ -104,6 +108,19 @@ public class ExamSpecialSettingsEntity extends JpaEntity {
 	@Column(nullable = false)
 	private Boolean examLimit;
 
+	/**
+	 * 开启特殊设置
+	 */
+	@Column(nullable = false)
+	private Boolean specialSettingsEnabled;
+
+	/**
+	 * 特殊设置类型
+	 */
+	@Column(nullable = true)
+	@Enumerated(EnumType.STRING)
+	private ExamSpecialSettingsType specialSettingsType;
+
 	/**
 	 * 扩展属性1
 	 */
@@ -217,6 +234,22 @@ public class ExamSpecialSettingsEntity extends JpaEntity {
 		this.examLimit = examLimit;
 	}
 
+	public Boolean getSpecialSettingsEnabled() {
+		return specialSettingsEnabled;
+	}
+
+	public void setSpecialSettingsEnabled(Boolean specialSettingsEnabled) {
+		this.specialSettingsEnabled = specialSettingsEnabled;
+	}
+
+	public ExamSpecialSettingsType getSpecialSettingsType() {
+		return specialSettingsType;
+	}
+
+	public void setSpecialSettingsType(ExamSpecialSettingsType specialSettingsType) {
+		this.specialSettingsType = specialSettingsType;
+	}
+
 	public String getExt1() {
 		return ext1;
 	}

+ 11 - 0
examcloud-core-examwork-service/src/main/java/cn/com/qmth/examcloud/core/examwork/service/impl/ExamServiceImpl.java

@@ -270,6 +270,14 @@ public class ExamServiceImpl implements ExamService {
 		this.saveExamSpecialSettings(examSpecialInfo);
 
 		examSpecialSettingsRepo.updateExamEnableByExamId(saved.getId(), saved.getEnable());
+		examSpecialSettingsRepo.updateSpecialSettingsEnabledByExamId(saved.getId(),
+				saved.getSpecialSettingsEnabled());
+		if (null == saved.getSpecialSettingsType()) {
+			examSpecialSettingsRepo.updateSpecialSettingsTypeByExamId(saved.getId(), null);
+		} else {
+			examSpecialSettingsRepo.updateSpecialSettingsTypeByExamId(saved.getId(),
+					saved.getSpecialSettingsType().name());
+		}
 
 		SyncExamReq req = new SyncExamReq();
 		req.setId(saved.getId());
@@ -505,6 +513,9 @@ public class ExamServiceImpl implements ExamService {
 			examSpecialSettingsEntity.setExamId(examId);
 			examSpecialSettingsEntity.setExamType(examEntity.getExamType());
 			examSpecialSettingsEntity.setExamEnable(examEntity.getEnable());
+			examSpecialSettingsEntity
+					.setSpecialSettingsEnabled(examEntity.getSpecialSettingsEnabled());
+			examSpecialSettingsEntity.setSpecialSettingsType(examEntity.getSpecialSettingsType());
 
 			examSpecialSettingsEntity.setOrgId(orgId);
 			examSpecialSettingsEntity.setCourseId(courseId);

+ 149 - 33
examcloud-core-examwork-service/src/main/java/cn/com/qmth/examcloud/core/examwork/service/impl/OnGoingExamServiceImpl.java

@@ -3,10 +3,10 @@ package cn.com.qmth.examcloud.core.examwork.service.impl;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
-import java.util.Map;
 
 import javax.persistence.criteria.Predicate;
 
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.PageRequest;
@@ -16,14 +16,21 @@ import org.springframework.data.jpa.domain.Specification;
 import org.springframework.stereotype.Service;
 
 import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
 
+import cn.com.qmth.examcloud.api.commons.enums.ExamSpecialSettingsType;
 import cn.com.qmth.examcloud.api.commons.enums.ExamType;
 import cn.com.qmth.examcloud.commons.exception.StatusException;
 import cn.com.qmth.examcloud.core.examwork.dao.ExamSpecialSettingsRepo;
 import cn.com.qmth.examcloud.core.examwork.dao.entity.ExamSpecialSettingsEntity;
 import cn.com.qmth.examcloud.core.examwork.service.OnGoingExamService;
 
+/**
+ * 待考考试服务
+ *
+ * @author WANGWEI
+ * @date 2019年10月25日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
 @Service
 public class OnGoingExamServiceImpl implements OnGoingExamService {
 
@@ -34,62 +41,171 @@ public class OnGoingExamServiceImpl implements OnGoingExamService {
 	public List<ExamSpecialSettingsEntity> getOngoingExamList(Long rootOrgId, String examType,
 			Long orgId, Long studentId) {
 
+		if (null == rootOrgId) {
+			throw new StatusException("008001", "rootOrgId is wrong");
+		}
+
 		ExamType et = null;
 		if (StringUtils.isNotBlank(examType)) {
 			try {
 				et = ExamType.valueOf(examType);
 			} catch (Exception e) {
-				throw new StatusException("002005", "examType is wrong");
+				throw new StatusException("008002", "examType is wrong");
 			}
 		}
 
-		final ExamType type = et;
+		if (null == orgId) {
+			throw new StatusException("008003", "orgId is wrong");
+		}
+
+		if (null == studentId) {
+			throw new StatusException("008004", "studentId is wrong");
+		}
+
+		List<ExamSpecialSettingsEntity> byExam = getByExam(rootOrgId, et);
+		List<ExamSpecialSettingsEntity> byOrg = getByOrg(rootOrgId, et, orgId);
+		List<ExamSpecialSettingsEntity> byStudentId = getByStudentId(rootOrgId, et, studentId);
+
+		if (CollectionUtils.isNotEmpty(byOrg) && CollectionUtils.isNotEmpty(byStudentId)) {
+			throw new StatusException("008005", "data error");
+		}
+
+		List<ExamSpecialSettingsEntity> examList = Lists.newArrayListWithCapacity(0);
+		examList.addAll(byOrg);
+		examList.addAll(byStudentId);
+
+		for (ExamSpecialSettingsEntity cur : byExam) {
+			if (!cur.getSpecialSettingsEnabled()) {
+				examList.add(cur);
+			}
+			ExamSpecialSettingsType specialSettingsType = cur.getSpecialSettingsType();
+			if (null == specialSettingsType) {
+				examList.add(cur);
+			} else if (specialSettingsType.equals(ExamSpecialSettingsType.ORG_BASED)) {
+				ExamSpecialSettingsEntity specialSettings = examSpecialSettingsRepo
+						.findByExamIdAndOrgIdAndCourseIdIsNullAndStudentIdIsNull(cur.getExamId(),
+								orgId);
+				if (null == specialSettings) {
+					examList.add(cur);
+				} else if (null == specialSettings.getBeginTime()
+						&& null == specialSettings.getEndTime()) {
+					examList.add(cur);
+				}
+			} else if (specialSettingsType.equals(ExamSpecialSettingsType.STUDENT_BASED)) {
+				ExamSpecialSettingsEntity specialSettings = examSpecialSettingsRepo
+						.findByExamIdAndStudentIdAndOrgIdIsNullAndCourseIdIsNull(cur.getExamId(),
+								studentId);
+				if (null == specialSettings) {
+					examList.add(cur);
+				} else if (null == specialSettings.getBeginTime()
+						&& null == specialSettings.getEndTime()) {
+					examList.add(cur);
+				}
+			}
+		}
 
+		examList.sort((a, b) -> a.getBeginTime().after(b.getBeginTime()) ? 1 : -1);
+
+		return examList;
+	}
+
+	/**
+	 * 考试设置
+	 *
+	 * @author WANGWEI
+	 * @param rootOrgId
+	 * @param type
+	 * @return
+	 */
+	private List<ExamSpecialSettingsEntity> getByExam(Long rootOrgId, ExamType type) {
 		Specification<ExamSpecialSettingsEntity> specification = (root, query, cb) -> {
 			List<Predicate> predicates = new ArrayList<>();
 			predicates.add(cb.equal(root.get("rootOrgId"), rootOrgId));
 			predicates.add(cb.greaterThan(root.get("endTime"), new Date()));
-			if (null != type) {
-				predicates.add(cb.equal(root.get("examType"), type));
-			}
-
+			predicates.add(cb.equal(root.get("examType"), type));
 			predicates.add(cb.equal(root.get("examEnable"), true));
+
 			predicates.add(cb.isNull(root.get("courseId")));
+			predicates.add(cb.isNull(root.get("orgId")));
+			predicates.add(cb.isNull(root.get("studentId")));
 
-			if (null != orgId) {
-				Predicate pr1 = cb.isNull(root.get("orgId"));
-				Predicate pr2 = cb.equal(root.get("orgId"), orgId);
-				predicates.add(cb.or(pr1, pr2));
-			}
+			return cb.and(predicates.toArray(new Predicate[predicates.size()]));
+		};
+
+		PageRequest pageRequest = PageRequest.of(0, 100, new Sort(Direction.ASC, "beginTime"));
+
+		List<ExamSpecialSettingsEntity> entityList = examSpecialSettingsRepo
+				.findAll(specification, pageRequest).getContent();
+		return entityList;
+	}
+
+	/**
+	 * 机构特殊设置
+	 *
+	 * @author WANGWEI
+	 * @param rootOrgId
+	 * @param type
+	 * @param orgId
+	 * @return
+	 */
+	private List<ExamSpecialSettingsEntity> getByOrg(Long rootOrgId, ExamType type, Long orgId) {
+		Specification<ExamSpecialSettingsEntity> specification = (root, query, cb) -> {
+			List<Predicate> predicates = new ArrayList<>();
+			predicates.add(cb.equal(root.get("rootOrgId"), rootOrgId));
+			predicates.add(cb.greaterThan(root.get("endTime"), new Date()));
+			predicates.add(cb.equal(root.get("examType"), type));
+			predicates.add(cb.equal(root.get("examEnable"), true));
+
+			predicates.add(cb.isNull(root.get("courseId")));
+			predicates.add(cb.isNull(root.get("studentId")));
+			predicates.add(cb.equal(root.get("orgId"), orgId));
+			predicates.add(cb.equal(root.get("specialSettingsEnabled"), true));
+			predicates.add(
+					cb.equal(root.get("specialSettingsType"), ExamSpecialSettingsType.ORG_BASED));
 
 			return cb.and(predicates.toArray(new Predicate[predicates.size()]));
 		};
 
-		PageRequest pageRequest = PageRequest.of(0, 10000, new Sort(Direction.ASC, "beginTime"));
+		PageRequest pageRequest = PageRequest.of(0, 100, new Sort(Direction.ASC, "beginTime"));
 
 		List<ExamSpecialSettingsEntity> entityList = examSpecialSettingsRepo
 				.findAll(specification, pageRequest).getContent();
+		return entityList;
+	}
 
-		List<ExamSpecialSettingsEntity> resultList = Lists.newArrayList();
-		Map<String, ExamSpecialSettingsEntity> examMap = Maps.newHashMap();
-		for (ExamSpecialSettingsEntity cur : entityList) {
-			if (null == cur.getOrgId() && null == cur.getCourseId()) {
-				examMap.put(String.valueOf(cur.getExamId()), cur);
-			}
-			if (null != cur.getBeginTime() && null != cur.getEndTime()) {
-				resultList.add(cur);
-			}
-		}
-		for (ExamSpecialSettingsEntity cur : entityList) {
-			if ((null != cur.getOrgId() || null != cur.getCourseId()) && null != cur.getEndTime()) {
-				ExamSpecialSettingsEntity tmp = examMap.get(String.valueOf(cur.getExamId()));
-				if (null != tmp) {
-					resultList.remove(tmp);
-				}
-			}
-		}
+	/**
+	 * 学生特殊设置
+	 *
+	 * @author WANGWEI
+	 * @param rootOrgId
+	 * @param type
+	 * @param studentId
+	 * @return
+	 */
+	private List<ExamSpecialSettingsEntity> getByStudentId(Long rootOrgId, ExamType type,
+			Long studentId) {
+		Specification<ExamSpecialSettingsEntity> specification = (root, query, cb) -> {
+			List<Predicate> predicates = new ArrayList<>();
+			predicates.add(cb.equal(root.get("rootOrgId"), rootOrgId));
+			predicates.add(cb.greaterThan(root.get("endTime"), new Date()));
+			predicates.add(cb.equal(root.get("examType"), type));
+			predicates.add(cb.equal(root.get("examEnable"), true));
+
+			predicates.add(cb.isNull(root.get("courseId")));
+			predicates.add(cb.isNull(root.get("orgId")));
+			predicates.add(cb.equal(root.get("studentId"), studentId));
+			predicates.add(cb.equal(root.get("specialSettingsEnabled"), true));
+			predicates.add(cb.equal(root.get("specialSettingsType"),
+					ExamSpecialSettingsType.STUDENT_BASED));
+
+			return cb.and(predicates.toArray(new Predicate[predicates.size()]));
+		};
 
-		return resultList;
+		PageRequest pageRequest = PageRequest.of(0, 100, new Sort(Direction.ASC, "beginTime"));
+
+		List<ExamSpecialSettingsEntity> entityList = examSpecialSettingsRepo
+				.findAll(specification, pageRequest).getContent();
+		return entityList;
 	}
 
 }