wangwei 7 ani în urmă
părinte
comite
a60793fd06

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

@@ -22,6 +22,7 @@ import org.springframework.data.jpa.domain.Specification;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.DeleteMapping;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.ModelAttribute;
@@ -58,6 +59,7 @@ import io.swagger.annotations.ApiOperation;
 /**
  * 考试服务API Created by songyue on 17/1/13.
  */
+@Transactional
 @RestController
 @RequestMapping("${$rmp.ctr.examwork}/exam")
 public class ExamController extends ControllerSupport {
@@ -234,19 +236,20 @@ public class ExamController extends ControllerSupport {
 		return ret;
 	}
 
+	/**
+	 * 方法注释
+	 *
+	 * @author WANGWEI
+	 * @param id
+	 * @return
+	 */
 	@ApiOperation(value = "按ID删除考试批次", notes = "删除")
 	@DeleteMapping("{id}")
-	public ResponseEntity deleteExam(@PathVariable String id) {
-		try {
-			List<Long> examIds = Stream.of(id.split(",")).map(s -> Long.parseLong(s.trim()))
-					.collect(Collectors.toList());
-			examService.deleteExam(examIds);
-			return new ResponseEntity(HttpStatus.OK);
-		} catch (Exception e) {
-			log.error("删除失败:", e);
-			return new ResponseEntity(new ErrorMsg(e.getMessage()),
-					HttpStatus.INTERNAL_SERVER_ERROR);
-		}
+	public List<Long> deleteExam(@PathVariable String id) {
+		List<Long> examIds = Stream.of(id.split(",")).map(s -> Long.parseLong(s.trim()))
+				.collect(Collectors.toList());
+		examService.deleteExam(examIds);
+		return examIds;
 	}
 
 	@ApiOperation(value = "启用考试", notes = "启用考试")

+ 162 - 158
examcloud-core-examwork-service/src/main/java/cn/com/qmth/examcloud/core/examwork/service/impl/ExamService.java

@@ -6,16 +6,6 @@ import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 
-import cn.com.qmth.examcloud.common.dto.core.Org;
-import cn.com.qmth.examcloud.commons.web.security.entity.AccessUser;
-import cn.com.qmth.examcloud.core.examwork.dao.ExamOrgTimeRepo;
-import cn.com.qmth.examcloud.core.examwork.dao.ExamRepo;
-import cn.com.qmth.examcloud.core.examwork.dao.entity.Exam;
-import cn.com.qmth.examcloud.core.examwork.dao.entity.ExamOrgTime;
-import cn.com.qmth.examcloud.core.examwork.dao.enums.ExamType;
-import cn.com.qmth.examcloud.core.examwork.service.mq.DataSendService;
-import cn.com.qmth.examcloud.core.examwork.service.rpc.OrgService;
-
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Example;
 import org.springframework.data.domain.ExampleMatcher;
@@ -26,157 +16,171 @@ import org.springframework.data.domain.Sort.Direction;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import cn.com.qmth.examcloud.common.dto.core.Org;
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
+import cn.com.qmth.examcloud.core.examwork.dao.ExamOrgTimeRepo;
+import cn.com.qmth.examcloud.core.examwork.dao.ExamRepo;
+import cn.com.qmth.examcloud.core.examwork.dao.entity.Exam;
+import cn.com.qmth.examcloud.core.examwork.dao.entity.ExamOrgTime;
+import cn.com.qmth.examcloud.core.examwork.dao.enums.ExamType;
+import cn.com.qmth.examcloud.core.examwork.service.mq.DataSendService;
+import cn.com.qmth.examcloud.core.examwork.service.rpc.OrgService;
+
 /**
- * 考试批次服务类
- * Created by songyue on 17/1/13.
+ * 考试批次服务类 Created by songyue on 17/1/13.
  */
 @Service
 public class ExamService {
-    @Autowired
-    ExamRepo examRepo;
-
-    @Autowired
-    ExamStudentService examStudentService;
-
-    @Autowired
-    ExamOrgTimeRepo examOrgTimeRepo;
-
-    @Autowired
-    OrgService orgService;
-    
-    @Autowired
-    DataSendService dataSendService;
-
-    /**
-     * 获取所有考试批次(分页)
-     * @param examCriteria
-     * @param pageable
-     * @return
-     */
-    public Page<Exam> getAllExam(Exam examCriteria, Pageable pageable){
-        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
-                .withMatcher("name",contains());
-        Example<Exam> examExample = Example.of(examCriteria, exampleMatcher);
-        return examRepo.findAll(examExample,pageable);
-    }
-
-    /**
-     * 获取所有考试批次
-     * @param examCriteria
-     * @return 
-     * @return
-     */
-    public List<Exam> getAllExam(Exam examCriteria){
-    	ExampleMatcher exampleMatcher = ExampleMatcher.matching()
-                .withMatcher("name",contains());
-        Example<Exam> examExample = Example.of(examCriteria,exampleMatcher);
-        return examRepo.findAll(examExample, new Sort(Direction.DESC,"id"));
-    }
-
-    /**
-     * 按ID获取考试批次
-     * @param examId
-     * @return
-     */
-    public Exam getExamById(Long examId){
-        return examRepo.findOne(examId);
-    }
-
-    /**
-     * 保存考试批次
-     * @param exam
-     * @return
-     */
-    public Exam saveExam(Exam exam){
-    	Exam newExam = examRepo.save(exam);
-    	/*if(newExam.getId() != null){
-    		dataSendService.sendExam(newExam);
-    	}*/
-        return newExam;
-    }
-
-    @Transactional
-    public Exam insertExam(Exam exam, cn.com.qmth.examcloud.commons.web.security.bean.User accessUser){
-        ExamType examType = exam.getExamType();
-        if(examType == ExamType.OFFLINE){
-            Exam newExam = examRepo.save(exam);
-            insertExamOrgTime(newExam,accessUser);
-            return newExam;
-        }
-        return examRepo.save(exam);
-    }
-
-    /**
-     * 新增学习中心考试时间
-     * @param exam
-     * @param accessUser
-     */
-    private void insertExamOrgTime(Exam exam,cn.com.qmth.examcloud.commons.web.security.bean.User accessUser){
-        Long examId = exam.getId();
-        Long rootOrgId = accessUser.getRootOrgId();
-        String token = accessUser.getUserToken();
-        Date beginTime = exam.getBeginTime();
-        Date endTime = exam.getEndTime();
-        List<Org> orgList = orgService.findByParentId(token,rootOrgId);
-        List<ExamOrgTime> examOrgTimes = new ArrayList<>();
-        for(Org org:orgList){
-            Long orgId = org.getId();
-            String orgCode = org.getCode();
-            String orgName = org.getName();
-            ExamOrgTime examOrgTime = new ExamOrgTime(examId, orgId, orgCode,
-                                                      orgName, beginTime, endTime);
-            examOrgTimes.add(examOrgTime);
-        }
-        examOrgTimeRepo.save(examOrgTimes);
-    }
-
-    /**
-     * 删除考试批次
-     * @param ids
-     * @return
-     */
-    @Transactional
-    public void deleteExam(List<Long> ids)throws Exception{
-        List<Exam> exams = examRepo.findByIdIn(ids);
-        for(Exam exam:exams){
-            //删除考生
-            if(exam.getCanStuDel()){
-                //删除学习中心考试时间
-                examOrgTimeRepo.deleteByExamId(exam.getId());
-                examStudentService.deleteExamStudents(exam.getId());
-                examRepo.delete(exam);
-            }else{
-                throw new RuntimeException(exam.getName()+"批次已经开始,不能删除");
-            }
-        }
-    }
-
-
-    public boolean checkExamName(Exam exam){
-        if(exam.getId()!=null){
-            if(examRepo.countByNameAndRootOrgIdAndIdNot(exam.getName(),
-                                                        exam.getRootOrgId(),
-                                                        exam.getId()) > 0){
-                return false;
-            }else{
-                return true;
-            }
-        }else{
-            if(examRepo.countByNameAndRootOrgId(exam.getName(),exam.getRootOrgId()) > 0){
-                return false;
-            }else{
-                return true;
-            }
-        }
-    }
-    
-    public Exam findExamByNameAndRootOrgId(String examName,Long rootOrgId){
-    	Exam exam = new Exam();
-    	exam.setName(examName);
-    	exam.setRootOrgId(rootOrgId);
-    	return examRepo.findOne(Example.of(exam));
-    }
-    public List<Exam> findByNameAndRootOrgId(String name,Long rootOrgId) {
-        return examRepo.findByNameAndRootOrgId(name, rootOrgId);
+	@Autowired
+	ExamRepo examRepo;
+
+	@Autowired
+	ExamStudentService examStudentService;
+
+	@Autowired
+	ExamOrgTimeRepo examOrgTimeRepo;
+
+	@Autowired
+	OrgService orgService;
+
+	@Autowired
+	DataSendService dataSendService;
+
+	/**
+	 * 获取所有考试批次(分页)
+	 * 
+	 * @param examCriteria
+	 * @param pageable
+	 * @return
+	 */
+	public Page<Exam> getAllExam(Exam examCriteria, Pageable pageable) {
+		ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("name", contains());
+		Example<Exam> examExample = Example.of(examCriteria, exampleMatcher);
+		return examRepo.findAll(examExample, pageable);
+	}
+
+	/**
+	 * 获取所有考试批次
+	 * 
+	 * @param examCriteria
+	 * @return
+	 * @return
+	 */
+	public List<Exam> getAllExam(Exam examCriteria) {
+		ExampleMatcher exampleMatcher = ExampleMatcher.matching().withMatcher("name", contains());
+		Example<Exam> examExample = Example.of(examCriteria, exampleMatcher);
+		return examRepo.findAll(examExample, new Sort(Direction.DESC, "id"));
+	}
+
+	/**
+	 * 按ID获取考试批次
+	 * 
+	 * @param examId
+	 * @return
+	 */
+	public Exam getExamById(Long examId) {
+		return examRepo.findOne(examId);
+	}
+
+	/**
+	 * 保存考试批次
+	 * 
+	 * @param exam
+	 * @return
+	 */
+	public Exam saveExam(Exam exam) {
+		Exam newExam = examRepo.save(exam);
+		/*
+		 * if(newExam.getId() != null){ dataSendService.sendExam(newExam); }
+		 */
+		return newExam;
+	}
+
+	@Transactional
+	public Exam insertExam(Exam exam,
+			cn.com.qmth.examcloud.commons.web.security.bean.User accessUser) {
+		ExamType examType = exam.getExamType();
+		if (examType == ExamType.OFFLINE) {
+			Exam newExam = examRepo.save(exam);
+			insertExamOrgTime(newExam, accessUser);
+			return newExam;
+		}
+		return examRepo.save(exam);
+	}
+
+	/**
+	 * 新增学习中心考试时间
+	 * 
+	 * @param exam
+	 * @param accessUser
+	 */
+	private void insertExamOrgTime(Exam exam,
+			cn.com.qmth.examcloud.commons.web.security.bean.User accessUser) {
+		Long examId = exam.getId();
+		Long rootOrgId = accessUser.getRootOrgId();
+		String token = accessUser.getUserToken();
+		Date beginTime = exam.getBeginTime();
+		Date endTime = exam.getEndTime();
+		List<Org> orgList = orgService.findByParentId(token, rootOrgId);
+		List<ExamOrgTime> examOrgTimes = new ArrayList<>();
+		for (Org org : orgList) {
+			Long orgId = org.getId();
+			String orgCode = org.getCode();
+			String orgName = org.getName();
+			ExamOrgTime examOrgTime = new ExamOrgTime(examId, orgId, orgCode, orgName, beginTime,
+					endTime);
+			examOrgTimes.add(examOrgTime);
+		}
+		examOrgTimeRepo.save(examOrgTimes);
+	}
+
+	/**
+	 * 删除考试批次
+	 * 
+	 * @param ids
+	 * @return
+	 */
+	@Transactional
+	public void deleteExam(List<Long> ids) {
+		List<Exam> exams = examRepo.findByIdIn(ids);
+		for (Exam exam : exams) {
+			// 删除考生
+			if (exam.getCanStuDel()) {
+				// 删除学习中心考试时间
+				examOrgTimeRepo.deleteByExamId(exam.getId());
+				examStudentService.deleteExamStudents(exam.getId());
+				examRepo.delete(exam);
+			} else {
+				throw new StatusException("", "[ " + exam.getName() + " ] 已经开始,不能删除");
+			}
+		}
+	}
+
+	public boolean checkExamName(Exam exam) {
+		if (exam.getId() != null) {
+			if (examRepo.countByNameAndRootOrgIdAndIdNot(exam.getName(), exam.getRootOrgId(),
+					exam.getId()) > 0) {
+				return false;
+			} else {
+				return true;
+			}
+		} else {
+			if (examRepo.countByNameAndRootOrgId(exam.getName(), exam.getRootOrgId()) > 0) {
+				return false;
+			} else {
+				return true;
+			}
+		}
+	}
+
+	public Exam findExamByNameAndRootOrgId(String examName, Long rootOrgId) {
+		Exam exam = new Exam();
+		exam.setName(examName);
+		exam.setRootOrgId(rootOrgId);
+		return examRepo.findOne(Example.of(exam));
+	}
+
+	public List<Exam> findByNameAndRootOrgId(String name, Long rootOrgId) {
+		return examRepo.findByNameAndRootOrgId(name, rootOrgId);
 	}
 }