sf115 / software-project

软件工程专业工作管理系统--SpringBoot
0 stars 0 forks source link

将冲突提示消息封装在Map中,传给前端 #10

Open zhanyeye opened 5 years ago

zhanyeye commented 5 years ago

解决思路来自-------- 黄冲

解决方案的实现细节


public Map setExamDetail(int eid, int uid) {
        User user = userRepository.findById(uid).get();
        Exam exam = examRepository.findById(eid).get();

        //考试需要监考人数 和 已分配人数 比较
        log.debug(eid + "");
        log.debug(examDetailRepository.coutByEid(eid) + " ");
        if (exam.getNumbersOfTeacher() <= examDetailRepository.coutByEid(eid)) {
            throw new ResponseStatusException(HttpStatus.CONFLICT, "该考试监考人员已经分配完成,无需再分配!");
        }

        List<Exam> examList = examDetailRepository.listExamByTeacher(uid); //uid 老师的所有监考考试
        int conflictCount = 0; //记录exam 和 uid 老师已分派考试的时间的冲突次数:冲突达到 2 则不能再分配了
        Exam conflictExam = null; //记录和哪门考试冲突

        //循环比较待插入Exam 和老师监考的冲突次数
        for(Exam e : examList) {
            log.debug(e.getName());
            if (timeUtils.isTimeConflict(e, exam)) {
                conflictCount++;
                conflictExam = e;
                if (conflictCount == 2) {
                    throw new ResponseStatusException(HttpStatus.CONFLICT, "同一时间同一教师只能执行 2 次不同监考!");
                }
            }
        }

        ExamDetail examDetail = new ExamDetail();
        examDetail.setTeacher(user);
        examDetail.setExam(exam);
        examDetail = examDetailRepository.save(examDetail);

        if (conflictCount == 0) {
            return Map.of("examDetail",examDetailRepository.refresh(examDetail));
        } else {
            String warning = generateWarningMessage(user, conflictExam, exam);
            return Map.of("examDetail",examDetailRepository.refresh(examDetail), "warning", warning);
        }
    }
zhanyeye commented 5 years ago

实现效果