package com.mindskip.xzs.controller.student; import com.mindskip.xzs.base.BaseApiController; import com.mindskip.xzs.base.RestResponse; import com.mindskip.xzs.domain.ExamPaper; import com.mindskip.xzs.domain.Subject; import com.mindskip.xzs.domain.User; import com.mindskip.xzs.domain.enums.ExamPaperTypeEnum; import com.mindskip.xzs.domain.enums.QuestionTypeEnum; import com.mindskip.xzs.domain.other.QuestionRandom; import com.mindskip.xzs.service.*; import com.mindskip.xzs.utility.DateTimeUtil; import com.mindskip.xzs.utility.JsonUtil; import com.mindskip.xzs.utility.PageInfoHelper; import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVM; import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperTitleItemVM; import com.mindskip.xzs.viewmodel.admin.question.QuestionEditRequestVM; import com.github.pagehelper.PageInfo; import com.mindskip.xzs.viewmodel.student.exam.ExamPaperBuildVM; import com.mindskip.xzs.viewmodel.student.exam.ExamPaperPageResponseVM; import com.mindskip.xzs.viewmodel.student.exam.ExamPaperPageVM; import org.springframework.context.ApplicationEventPublisher; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.validation.Valid; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; import java.util.stream.Collectors; /** * @version 2.2.0 * @description: 试卷 * Copyright (C), 2020-2021, 武汉思维跳跃科技有限公司 * @date 2021 /9/7 9:45 */ @RestController("StudentExamPaperController") @RequestMapping(value = "/api/student/exam/paper") public class ExamPaperController extends BaseApiController { private final ExamPaperService examPaperService; private final ExamPaperAnswerService examPaperAnswerService; private final ApplicationEventPublisher eventPublisher; private final ClassesService classesService; private final QuestionService questionService; private final SubjectService subjectService; /** * Instantiates a new Exam paper controller. * * @param examPaperService the exam paper service * @param examPaperAnswerService the exam paper answer service * @param eventPublisher the event publisher * @param classesService the classes service * @param questionService the question service * @param subjectService the subject service */ public ExamPaperController(ExamPaperService examPaperService, ExamPaperAnswerService examPaperAnswerService, ApplicationEventPublisher eventPublisher, ClassesService classesService, QuestionService questionService, SubjectService subjectService) { this.examPaperService = examPaperService; this.examPaperAnswerService = examPaperAnswerService; this.eventPublisher = eventPublisher; this.classesService = classesService; this.questionService = questionService; this.subjectService = subjectService; } /** * 试卷查看 * * @param id the id * @return the rest response */ @RequestMapping(value = "/select/{id}", method = RequestMethod.POST) public RestResponse select(@PathVariable Integer id) { ExamPaperEditRequestVM vm = examPaperService.examPaperToVM(id); return RestResponse.ok(vm); } /** * 试卷分页 * * @param model the model * @return the rest response */ @RequestMapping(value = "/pageList", method = RequestMethod.POST) public RestResponse> pageList(@RequestBody @Valid ExamPaperPageVM model) { ExamPaperTypeEnum examPaperTypeEnum = ExamPaperTypeEnum.fromCode(model.getPaperType()); model.setLevelId(getCurrentUser().getUserLevel()); model.setDateTime(new Date()); PageInfo pageInfo = null; if (examPaperTypeEnum == ExamPaperTypeEnum.Classes) { List cIds = classesService.getJoinClasses(getCurrentUser().getId()).stream() .map(d -> d.getClassesId()).collect(Collectors.toList()); model.setCIds(cIds); pageInfo = examPaperService.studentClassesPage(model); } else { model.setUserId(getCurrentUser().getId()); pageInfo = examPaperService.studentPage(model); } PageInfo page = PageInfoHelper.copyMap(pageInfo, e -> { ExamPaperPageResponseVM vm = modelMapper.map(e, ExamPaperPageResponseVM.class); vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime())); Subject subject = subjectService.selectById(vm.getSubjectId()); vm.setSubjectName(subject.getName()); return vm; }); return RestResponse.ok(page); } /** * 智能训练 * * @param examPaperBuildVM the exam paper build vm * @return the rest response */ @RequestMapping(value = "/build", method = RequestMethod.POST) public RestResponse build(@RequestBody @Valid ExamPaperBuildVM examPaperBuildVM) { User user = getCurrentUser(); List titleItems = new ArrayList<>(); QuestionRandom questionRandom = new QuestionRandom(); questionRandom.setSubjectId(examPaperBuildVM.getSubjectId()); questionRandom.setGradeLevel(user.getUserLevel()); questionRandom.setDifficult(examPaperBuildVM.getDifficult()); Integer limit = examPaperBuildVM.getSingleChoice(); if (limit != null && limit != 0) { ExamPaperTitleItemVM examPaperTitleItemVM = randomTitle(QuestionTypeEnum.SingleChoice, limit, questionRandom); if (examPaperTitleItemVM.getQuestionItems().size() != 0) { titleItems.add(randomTitle(QuestionTypeEnum.SingleChoice, limit, questionRandom)); } } limit = examPaperBuildVM.getMultipleChoice(); if (limit != null && limit != 0) { ExamPaperTitleItemVM examPaperTitleItemVM = randomTitle(QuestionTypeEnum.MultipleChoice, limit, questionRandom); if (examPaperTitleItemVM.getQuestionItems().size() != 0) { titleItems.add(randomTitle(QuestionTypeEnum.MultipleChoice, limit, questionRandom)); } } limit = examPaperBuildVM.getTrueFalse(); if (limit != null && limit != 0) { ExamPaperTitleItemVM examPaperTitleItemVM = randomTitle(QuestionTypeEnum.TrueFalse, limit, questionRandom); if (examPaperTitleItemVM.getQuestionItems().size() != 0) { titleItems.add(randomTitle(QuestionTypeEnum.TrueFalse, limit, questionRandom)); } } if (titleItems.size() == 0) { return RestResponse.fail(2, "题库题目不足"); } Integer randomCount = examPaperService.getRandomPaperCount(user.getId()); Integer questionCount = titleItems.stream() .mapToInt(t -> t.getQuestionItems().size()).sum(); ExamPaperEditRequestVM examPaperEditRequestVM = new ExamPaperEditRequestVM(); examPaperEditRequestVM.setLevel(questionRandom.getGradeLevel()); examPaperEditRequestVM.setSubjectId(questionRandom.getSubjectId()); examPaperEditRequestVM.setPaperType(ExamPaperTypeEnum.Random.getCode()); examPaperEditRequestVM.setName(String.format("智能训练试卷 - %d", (randomCount + 1))); examPaperEditRequestVM.setSuggestTime(questionCount * 2); examPaperEditRequestVM.setTitleItems(titleItems); examPaperService.savePaperFromVM(examPaperEditRequestVM, user); return RestResponse.ok(examPaperEditRequestVM.getName()); } private ExamPaperTitleItemVM randomTitle(QuestionTypeEnum questionTypeEnum, Integer limit, QuestionRandom questionRandom) { ExamPaperTitleItemVM examPaperTitleItemVM = new ExamPaperTitleItemVM(); examPaperTitleItemVM.setName(questionTypeEnum.getName()); questionRandom.setQuestionType(questionTypeEnum.getCode()); questionRandom.setLimit(limit); List questionItems = questionService.randomQuestion(questionRandom).stream() .map(id -> questionService.getQuestionEditRequestVM(id)) .collect(Collectors.toList()); examPaperTitleItemVM.setQuestionItems(questionItems); return examPaperTitleItemVM; } }