浏览代码

。。。

wangwei 6 年之前
父节点
当前提交
246c8e82c5

+ 77 - 117
examcloud-core-examwork-api-provider/src/main/java/cn/com/qmth/examcloud/core/examwork/api/controller/ExamController.java

@@ -1,7 +1,5 @@
 package cn.com.qmth.examcloud.core.examwork.api.controller;
 package cn.com.qmth.examcloud.core.examwork.api.controller;
 
 
-import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.contains;
-
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.Iterator;
@@ -16,8 +14,6 @@ import javax.servlet.http.HttpServletRequest;
 
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.domain.Example;
-import org.springframework.data.domain.ExampleMatcher;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.domain.Pageable;
@@ -104,13 +100,13 @@ public class ExamController extends ControllerSupport {
 	ExamStudentServiceImpl examStudentService;
 	ExamStudentServiceImpl examStudentService;
 
 
 	@Autowired
 	@Autowired
-	ExamCourseGroupSettingsRepo courseGroupRepo;
+	ExamCourseGroupSettingsRepo examCourseGroupSettingsRepo;
 
 
 	@Autowired
 	@Autowired
 	ExamOrgSettingsRepo examOrgSettingsRepo;
 	ExamOrgSettingsRepo examOrgSettingsRepo;
 
 
 	@Autowired
 	@Autowired
-	ExamCourseGroupRelationRepo courseGroupRelationRepo;
+	ExamCourseGroupRelationRepo examCourseGroupRelationRepo;
 
 
 	@Autowired
 	@Autowired
 	OrgCloudService orgCloudService;
 	OrgCloudService orgCloudService;
@@ -122,40 +118,43 @@ public class ExamController extends ControllerSupport {
 	 * 方法注释
 	 * 方法注释
 	 *
 	 *
 	 * @author WANGWEI
 	 * @author WANGWEI
-	 * @param examCriteria
 	 * @param curPage
 	 * @param curPage
 	 * @param pageSize
 	 * @param pageSize
+	 * @param name
+	 * @param examType
+	 * @param enable
 	 * @return
 	 * @return
 	 */
 	 */
-	@ApiOperation(value = "查询所有考试批次", notes = "分页带查询")
-	@GetMapping("all/{curPage}/{pageSize}")
-	public Page<ExamEntity> getAllExam(@ModelAttribute ExamEntity examCriteria,
-			@PathVariable Integer curPage, @PathVariable Integer pageSize) {
+	@ApiOperation(value = "分页查询考试批次")
+	@GetMapping("queryPage/{curPage}/{pageSize}")
+	public Page<ExamEntity> queryPage(@PathVariable Integer curPage, @PathVariable Integer pageSize,
+			@RequestParam(required = false) String name,
+			@RequestParam(required = false) String examType,
+			@RequestParam(required = false) Boolean enable) {
+
 		User accessUser = getAccessUser();
 		User accessUser = getAccessUser();
-		examCriteria.setRootOrgId(accessUser.getRootOrgId());
 
 
-		ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("name", contains());
-		Example<ExamEntity> examExample = Example.of(examCriteria, exampleMatcher);
-		return examRepo.findAll(examExample,
-				new PageRequest(curPage, pageSize, new Sort(Direction.DESC, "updateTime")));
-	}
+		Specification<ExamEntity> specification = (root, query, cb) -> {
+			List<Predicate> predicates = new ArrayList<>();
+			predicates.add(cb.equal(root.get("rootOrgId"), accessUser.getRootOrgId()));
+			if (StringUtils.isNotBlank(name)) {
+				predicates.add(cb.like(root.get("name"), toSqlSearchPattern(name)));
+			}
+			if (null != enable) {
+				predicates.add(cb.equal(root.get("enable"), enable));
+			}
+			if (StringUtils.isNotBlank(examType)) {
+				predicates.add(cb.equal(root.get("examType"), ExamType.valueOf(examType)));
+			}
 
 
-	/**
-	 * 方法注释
-	 *
-	 * @author WANGWEI
-	 * @param examCriteria
-	 * @return
-	 */
-	@ApiOperation(value = "查询所有考试批次(包含有效)", notes = "不分页带查询")
-	@GetMapping("all")
-	public List<ExamEntity> getEnableExam(@ModelAttribute ExamEntity examCriteria) {
-		User accessUser = getAccessUser();
-		examCriteria.setRootOrgId(accessUser.getRootOrgId());
-		examCriteria.setEnable(true);
-		ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("name", contains());
-		Example<ExamEntity> examExample = Example.of(examCriteria, exampleMatcher);
-		return examRepo.findAll(examExample, new Sort(Direction.DESC, "id"));
+			return cb.and(predicates.toArray(new Predicate[predicates.size()]));
+		};
+
+		PageRequest pageRequest = new PageRequest(curPage, pageSize,
+				new Sort(Direction.DESC, "updateTime"));
+
+		Page<ExamEntity> page = examRepo.findAll(specification, pageRequest);
+		return page;
 	}
 	}
 
 
 	/**
 	/**
@@ -163,86 +162,47 @@ public class ExamController extends ControllerSupport {
 	 *
 	 *
 	 * @author WANGWEI
 	 * @author WANGWEI
 	 * @param name
 	 * @param name
+	 * @param examType
+	 * @param enable
 	 * @return
 	 * @return
 	 */
 	 */
-	@ApiOperation(value = "查询考试批次(包含有效、无效)")
-	@GetMapping("queryByName")
-	public List<ExamEntity> queryByName(@RequestParam(required = false) String name) {
+	@ApiOperation(value = "查询考试批次")
+	@GetMapping("queryByNameLike")
+	public List<ExamEntity> query(@RequestParam(required = true) String name,
+			@RequestParam(required = false) String examType,
+			@RequestParam(required = false) Boolean enable) {
 
 
 		if (StringUtils.isBlank(name)) {
 		if (StringUtils.isBlank(name)) {
 			List<ExamEntity> list = Lists.newArrayList();
 			List<ExamEntity> list = Lists.newArrayList();
 			return list;
 			return list;
 		}
 		}
 
 
+		// 过载保护
+		int total = examRepo.countByNameLike(name);
+		if (total > 1000) {
+			List<ExamEntity> list = Lists.newArrayList();
+			return list;
+		}
+
 		Specification<ExamEntity> specification = (root, query, cb) -> {
 		Specification<ExamEntity> specification = (root, query, cb) -> {
 			List<Predicate> predicates = new ArrayList<>();
 			List<Predicate> predicates = new ArrayList<>();
 			predicates.add(cb.equal(root.get("rootOrgId"), getRootOrgId()));
 			predicates.add(cb.equal(root.get("rootOrgId"), getRootOrgId()));
-			predicates.add(cb.like(root.get("name"), toSqlSearchPattern(name)));
+			if (StringUtils.isNotBlank(name)) {
+				predicates.add(cb.like(root.get("name"), toSqlSearchPattern(name)));
+			}
+			if (null != enable) {
+				predicates.add(cb.equal(root.get("enable"), enable));
+			}
+			if (StringUtils.isNotBlank(examType)) {
+				predicates.add(cb.equal(root.get("examType"), ExamType.valueOf(examType)));
+			}
 
 
 			return cb.and(predicates.toArray(new Predicate[predicates.size()]));
 			return cb.and(predicates.toArray(new Predicate[predicates.size()]));
 		};
 		};
 		Sort sort = new Sort(Direction.DESC, "updateTime");
 		Sort sort = new Sort(Direction.DESC, "updateTime");
-		List<ExamEntity> page = examRepo.findAll(specification, sort);
+		List<ExamEntity> list = examRepo.findAll(specification, sort);
 
 
-		return page;
-	}
-
-	/**
-	 * 方法注释
-	 *
-	 * @author WANGWEI
-	 * @param rootOrgId
-	 * @param examType
-	 * @return
-	 */
-	@ApiOperation(value = "根据机构ID和考试类型查询所有有效考试批次", notes = "根据机构ID和考试类型查询所有有效考试批次")
-	@GetMapping("rootOrgId/{rootOrgId}/{examType}")
-	public List<ExamEntity> getEnableExamByRootOrgId(@PathVariable Long rootOrgId,
-			@PathVariable String examType) {
-		ExamEntity examCriteria = new ExamEntity();
-		examCriteria.setRootOrgId(rootOrgId);
-		if (StringUtils.isNotBlank(examType)) {
-			examCriteria.setExamType(ExamType.valueOf(examType));
-		}
-		examCriteria.setEnable(true);
-
-		ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("name", contains());
-		Example<ExamEntity> examExample = Example.of(examCriteria, exampleMatcher);
-		return examRepo.findAll(examExample, new Sort(Direction.DESC, "id"));
-	}
-
-	/**
-	 * 方法注释
-	 *
-	 * @author WANGWEI
-	 * @param examCriteria
-	 * @return
-	 */
-	@ApiOperation(value = "查询所有考试批次(包含有效无效)", notes = "不分页带查询")
-	@GetMapping("all/both")
-	public List<ExamEntity> getAllExam(@ModelAttribute ExamEntity examCriteria) {
-		examCriteria.setRootOrgId(getRootOrgId());
-		ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("name", contains());
-		Example<ExamEntity> examExample = Example.of(examCriteria, exampleMatcher);
-		return examRepo.findAll(examExample, new Sort(Direction.DESC, "id"));
-	}
-
-	/**
-	 * 方法注释
-	 *
-	 * @author WANGWEI
-	 * @param name
-	 * @return
-	 */
-	@ApiOperation(value = "根据名称查询考试批次")
-	@GetMapping("byName/{name}")
-	public ExamEntity getExamByName(@PathVariable String name) {
-		User accessUser = getAccessUser();
-		ExamEntity one = examRepo.findByNameAndRootOrgId(name, accessUser.getRootOrgId());
-		if (null == one) {
-			throw new StatusException("E-001004", "考试不存在");
-		}
-		return one;
+		return list;
 	}
 	}
 
 
 	/**
 	/**
@@ -463,8 +423,8 @@ public class ExamController extends ControllerSupport {
 
 
 		Pageable pageable = new PageRequest(curPage - 1, pageSize, Sort.Direction.DESC,
 		Pageable pageable = new PageRequest(curPage - 1, pageSize, Sort.Direction.DESC,
 				"updateTime");
 				"updateTime");
-		List<ExamCourseGroupSettingsEntity> groupList = courseGroupRepo.findAllByExamId(examId,
-				pageable);
+		List<ExamCourseGroupSettingsEntity> groupList = examCourseGroupSettingsRepo
+				.findAllByExamId(examId, pageable);
 
 
 		List<ExamCourseGroupDomain> ret = Lists.newArrayList();
 		List<ExamCourseGroupDomain> ret = Lists.newArrayList();
 
 
@@ -482,7 +442,7 @@ public class ExamController extends ControllerSupport {
 			List<Long> courseIdList = Lists.newArrayList();
 			List<Long> courseIdList = Lists.newArrayList();
 			bean.setCourseIdList(courseIdList);
 			bean.setCourseIdList(courseIdList);
 
 
-			List<ExamCourseGroupRelationEntity> relationList = courseGroupRelationRepo
+			List<ExamCourseGroupRelationEntity> relationList = examCourseGroupRelationRepo
 					.findAllByGroupId(bean.getId());
 					.findAllByGroupId(bean.getId());
 
 
 			for (ExamCourseGroupRelationEntity cur : relationList) {
 			for (ExamCourseGroupRelationEntity cur : relationList) {
@@ -508,7 +468,7 @@ public class ExamController extends ControllerSupport {
 	public List<ExamCourseGroupDomain> getCourseGroupListByExamIdAndCourseId(
 	public List<ExamCourseGroupDomain> getCourseGroupListByExamIdAndCourseId(
 			@PathVariable Long examId, @PathVariable Long courseId) {
 			@PathVariable Long examId, @PathVariable Long courseId) {
 
 
-		List<ExamCourseGroupRelationEntity> relationList = courseGroupRelationRepo
+		List<ExamCourseGroupRelationEntity> relationList = examCourseGroupRelationRepo
 				.findAllByCourseIdAndExamId(courseId, examId);
 				.findAllByCourseIdAndExamId(courseId, examId);
 
 
 		List<Long> groupIdList = Lists.newArrayList();
 		List<Long> groupIdList = Lists.newArrayList();
@@ -517,7 +477,7 @@ public class ExamController extends ControllerSupport {
 			groupIdList.add(groupId);
 			groupIdList.add(groupId);
 		}
 		}
 
 
-		List<ExamCourseGroupSettingsEntity> groupList = courseGroupRepo
+		List<ExamCourseGroupSettingsEntity> groupList = examCourseGroupSettingsRepo
 				.findAllByIdInOrderByUpdateTimeDesc(groupIdList);
 				.findAllByIdInOrderByUpdateTimeDesc(groupIdList);
 
 
 		List<ExamCourseGroupDomain> ret = Lists.newArrayList();
 		List<ExamCourseGroupDomain> ret = Lists.newArrayList();
@@ -536,7 +496,7 @@ public class ExamController extends ControllerSupport {
 			List<Long> courseIdList = Lists.newArrayList();
 			List<Long> courseIdList = Lists.newArrayList();
 			bean.setCourseIdList(courseIdList);
 			bean.setCourseIdList(courseIdList);
 
 
-			List<ExamCourseGroupRelationEntity> curRelationList = courseGroupRelationRepo
+			List<ExamCourseGroupRelationEntity> curRelationList = examCourseGroupRelationRepo
 					.findAllByGroupId(bean.getId());
 					.findAllByGroupId(bean.getId());
 
 
 			for (ExamCourseGroupRelationEntity cur : curRelationList) {
 			for (ExamCourseGroupRelationEntity cur : curRelationList) {
@@ -569,12 +529,12 @@ public class ExamController extends ControllerSupport {
 		courseGroup.setName(courseGroupBean.getName());
 		courseGroup.setName(courseGroupBean.getName());
 		courseGroup.setUpdateTime(courseGroupBean.getUpdateTime());
 		courseGroup.setUpdateTime(courseGroupBean.getUpdateTime());
 
 
-		courseGroupRepo.save(courseGroup);
+		examCourseGroupSettingsRepo.save(courseGroup);
 		List<Long> courseIdList = courseGroupBean.getCourseIdList();
 		List<Long> courseIdList = courseGroupBean.getCourseIdList();
 
 
 		List<ExamCourseGroupRelationEntity> relationList = Lists.newArrayList();
 		List<ExamCourseGroupRelationEntity> relationList = Lists.newArrayList();
 
 
-		courseGroupRelationRepo.deleteByGroupId(courseGroup.getId());
+		examCourseGroupRelationRepo.deleteByGroupId(courseGroup.getId());
 
 
 		for (Long cur : courseIdList) {
 		for (Long cur : courseIdList) {
 			ExamCourseGroupRelationEntity relation = new ExamCourseGroupRelationEntity();
 			ExamCourseGroupRelationEntity relation = new ExamCourseGroupRelationEntity();
@@ -583,7 +543,7 @@ public class ExamController extends ControllerSupport {
 			relation.setGroupId(courseGroup.getId());
 			relation.setGroupId(courseGroup.getId());
 			relationList.add(relation);
 			relationList.add(relation);
 		}
 		}
-		courseGroupRelationRepo.save(relationList);
+		examCourseGroupRelationRepo.save(relationList);
 	}
 	}
 
 
 	/**
 	/**
@@ -595,8 +555,8 @@ public class ExamController extends ControllerSupport {
 	@ApiOperation(value = "删除课程组", notes = "")
 	@ApiOperation(value = "删除课程组", notes = "")
 	@DeleteMapping("courseGroup/{id}")
 	@DeleteMapping("courseGroup/{id}")
 	public void deleteCourseGroup(@PathVariable Long id) {
 	public void deleteCourseGroup(@PathVariable Long id) {
-		courseGroupRepo.delete(id);
-		courseGroupRelationRepo.deleteByGroupId(id);
+		examCourseGroupSettingsRepo.delete(id);
+		examCourseGroupRelationRepo.deleteByGroupId(id);
 	}
 	}
 
 
 	/**
 	/**
@@ -609,8 +569,8 @@ public class ExamController extends ControllerSupport {
 	 * @return
 	 * @return
 	 */
 	 */
 	@ApiOperation(value = "查询考试相关的学习中心设置", notes = "")
 	@ApiOperation(value = "查询考试相关的学习中心设置", notes = "")
-	@GetMapping("getExamOrgList/{curPage}/{pageSize}")
-	public PageInfo<ExamOrgSettingsDomain> getExamOrgList(@PathVariable Integer curPage,
+	@GetMapping("getExamOrgSettingsList/{curPage}/{pageSize}")
+	public PageInfo<ExamOrgSettingsDomain> getExamOrgSettingsList(@PathVariable Integer curPage,
 			@PathVariable Integer pageSize, @ModelAttribute ExamOrgSettingsDomain examOrgDomain) {
 			@PathVariable Integer pageSize, @ModelAttribute ExamOrgSettingsDomain examOrgDomain) {
 
 
 		Long examId = examOrgDomain.getExamId();
 		Long examId = examOrgDomain.getExamId();
@@ -721,9 +681,9 @@ public class ExamController extends ControllerSupport {
 	 * @return
 	 * @return
 	 */
 	 */
 	@ApiOperation(value = "新增考试相关的学习中心设置", notes = "")
 	@ApiOperation(value = "新增考试相关的学习中心设置", notes = "")
-	@PostMapping("examOrg")
-	public ExamOrgSettingsEntity addExamOrg(@RequestBody ExamOrgSettingsDomain domain) {
-		return saveExamOrg(domain);
+	@PostMapping("examOrgSettings")
+	public ExamOrgSettingsEntity addExamOrgSettings(@RequestBody ExamOrgSettingsDomain domain) {
+		return saveExamOrgSettings(domain);
 	}
 	}
 
 
 	/**
 	/**
@@ -734,9 +694,9 @@ public class ExamController extends ControllerSupport {
 	 * @return
 	 * @return
 	 */
 	 */
 	@ApiOperation(value = "更新考试相关的学习中心设置", notes = "")
 	@ApiOperation(value = "更新考试相关的学习中心设置", notes = "")
-	@PutMapping("examOrg")
-	public ExamOrgSettingsEntity updateExamOrg(@RequestBody ExamOrgSettingsDomain domain) {
-		return saveExamOrg(domain);
+	@PutMapping("examOrgSettings")
+	public ExamOrgSettingsEntity updateExamOrgSettings(@RequestBody ExamOrgSettingsDomain domain) {
+		return saveExamOrgSettings(domain);
 	}
 	}
 
 
 	/**
 	/**
@@ -746,7 +706,7 @@ public class ExamController extends ControllerSupport {
 	 * @param domain
 	 * @param domain
 	 * @return
 	 * @return
 	 */
 	 */
-	private ExamOrgSettingsEntity saveExamOrg(ExamOrgSettingsDomain domain) {
+	private ExamOrgSettingsEntity saveExamOrgSettings(ExamOrgSettingsDomain domain) {
 		Long examId = domain.getExamId();
 		Long examId = domain.getExamId();
 
 
 		ExamEntity examEntity = examRepo.findOne(examId);
 		ExamEntity examEntity = examRepo.findOne(examId);

+ 2 - 0
examcloud-core-examwork-dao/src/main/java/cn/com/qmth/examcloud/core/examwork/dao/ExamRepo.java

@@ -14,6 +14,8 @@ public interface ExamRepo
 			QueryByExampleExecutor<ExamEntity>,
 			QueryByExampleExecutor<ExamEntity>,
 			JpaSpecificationExecutor<ExamEntity> {
 			JpaSpecificationExecutor<ExamEntity> {
 
 
+	int countByNameLike(String name);
+
 	List<ExamEntity> findByRootOrgId(Long rootOrgId);
 	List<ExamEntity> findByRootOrgId(Long rootOrgId);
 
 
 	List<ExamEntity> findByIdIn(List<Long> ids);
 	List<ExamEntity> findByIdIn(List<Long> ids);