package com.mindskip.xzs.controller.student; import com.github.pagehelper.PageInfo; import com.mindskip.xzs.base.BaseApiController; import com.mindskip.xzs.base.RestResponse; import com.mindskip.xzs.domain.*; import com.mindskip.xzs.domain.enums.ExamPaperAnswerStatusEnum; import com.mindskip.xzs.event.CalculateExamPaperAnswerCompleteEvent; import com.mindskip.xzs.event.UserEvent; import com.mindskip.xzs.service.*; import com.mindskip.xzs.utility.DateTimeUtil; import com.mindskip.xzs.utility.ExamUtil; import com.mindskip.xzs.utility.PageInfoHelper; import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVO; import com.mindskip.xzs.viewmodel.student.exam.ExamPaperReadVO; import com.mindskip.xzs.viewmodel.student.exam.ExamPaperSubmitVM; import com.mindskip.xzs.viewmodel.student.exampaper.ExamPaperAnswerPageResponseVM; import com.mindskip.xzs.viewmodel.student.exampaper.ExamPaperAnswerPageVM; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.Date; import java.util.stream.Collectors; @RestController("StudentExamPaperAnswerController") @RequestMapping(value = "/api/student/exampaper/answer") public class ExamPaperAnswerController extends BaseApiController { private final ExamPaperAnswerService examPaperAnswerService; private final ExamPaperService examPaperService; private final SubjectService subjectService; private final ApplicationEventPublisher eventPublisher; private final ExamPaperSubjectService examPaperSubjectService; private final ExamTemplatesUserCountService examTemplatesUserCountService; @Autowired public ExamPaperAnswerController(ExamPaperAnswerService examPaperAnswerService, ExamPaperService examPaperService, SubjectService subjectService, ApplicationEventPublisher eventPublisher, ExamPaperSubjectService examPaperSubjectService, ExamTemplatesUserCountService examTemplatesUserCountService) { this.examPaperAnswerService = examPaperAnswerService; this.examPaperService = examPaperService; this.subjectService = subjectService; this.eventPublisher = eventPublisher; this.examPaperSubjectService = examPaperSubjectService; this.examTemplatesUserCountService = examTemplatesUserCountService; } @RequestMapping(value = "/pageList", method = RequestMethod.POST) public RestResponse> pageList(@RequestBody @Valid ExamPaperAnswerPageVM model) { model.setCreateUser(getCurrentUser().getId()); PageInfo pageInfo = examPaperAnswerService.studentPage(model); PageInfo page = PageInfoHelper.copyMap(pageInfo, e -> { ExamPaperAnswerPageResponseVM vm = modelMapper.map(e, ExamPaperAnswerPageResponseVM.class); ExamPaperAnswer examPaperAnswer = examPaperAnswerService.getById(vm.getId()); Integer[] ids = examPaperSubjectService.getByExamPaperId(examPaperAnswer.getExamPaperId()) .stream().map(ExamPaperSubject::getSubjectId).toArray(Integer[]::new); String name = ""; if(ids.length>0){ name = subjectService.selectByIds(ids) .stream().map(Subject::getName).collect(Collectors.joining(",")); } vm.setDoTime(ExamUtil.secondToVM(e.getDoTime())); vm.setSystemScore(ExamUtil.scoreToVM(e.getSystemScore())); vm.setUserScore(ExamUtil.scoreToVM(e.getUserScore())); vm.setPaperScore(ExamUtil.scoreToVM(e.getPaperScore())); vm.setSubjectName(name); vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime())); return vm; }); return RestResponse.ok(page); } @RequestMapping(value = "/answerSubmit", method = RequestMethod.POST) public RestResponse answerSubmit(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM) { User user = getCurrentUser(); ExamPaperAnswerInfo examPaperAnswerInfo = examPaperAnswerService.calculateExamPaperAnswer(examPaperSubmitVM, user); if (null == examPaperAnswerInfo) { return RestResponse.fail(2, "试卷不能重复做"); } ExamPaperAnswer examPaperAnswer = examPaperAnswerInfo.getExamPaperAnswer(); Integer userScore = examPaperAnswer.getUserScore(); String scoreVm = ExamUtil.scoreToVM(userScore); UserEventLog userEventLog = new UserEventLog(user.getId(), user.getUserName(), user.getRealName(), new Date()); String content = user.getUserName() + " 提交试卷:" + examPaperAnswerInfo.getExamPaper().getName() + " 得分:" + scoreVm + " 耗时:" + ExamUtil.secondToVM(examPaperAnswer.getDoTime()); userEventLog.setContent(content); eventPublisher.publishEvent(new CalculateExamPaperAnswerCompleteEvent(examPaperAnswerInfo)); eventPublisher.publishEvent(new UserEvent(userEventLog)); //首页随机试卷操作 if(examPaperSubmitVM.getTemplatesId() != null){ ExamTemplatesUserCount examTemplatesUserCount = new ExamTemplatesUserCount(); examTemplatesUserCount.setUserId(user.getId()); examTemplatesUserCount.setExamPaperId(examPaperSubmitVM.getId()); examTemplatesUserCount.setExamTemplatesId(examPaperSubmitVM.getTemplatesId()); examTemplatesUserCountService.add(examTemplatesUserCount); } return RestResponse.ok(scoreVm); } @RequestMapping(value = "/edit", method = RequestMethod.POST) public RestResponse edit(@RequestBody @Valid ExamPaperSubmitVM examPaperSubmitVM) { boolean notJudge = examPaperSubmitVM.getAnswerItems().stream().anyMatch(i -> i.getDoRight() == null && i.getScore() == null); if (notJudge) { return RestResponse.fail(2, "有未批改题目"); } ExamPaperAnswer examPaperAnswer = examPaperAnswerService.selectById(examPaperSubmitVM.getId()); ExamPaperAnswerStatusEnum examPaperAnswerStatusEnum = ExamPaperAnswerStatusEnum.fromCode(examPaperAnswer.getStatus()); if (examPaperAnswerStatusEnum == ExamPaperAnswerStatusEnum.Complete) { return RestResponse.fail(3, "试卷已完成"); } String score = examPaperAnswerService.judge(examPaperSubmitVM); User user = getCurrentUser(); UserEventLog userEventLog = new UserEventLog(user.getId(), user.getUserName(), user.getRealName(), new Date()); String content = user.getUserName() + " 批改试卷:" + examPaperAnswer.getPaperName() + " 得分:" + score; userEventLog.setContent(content); eventPublisher.publishEvent(new UserEvent(userEventLog)); return RestResponse.ok(score); } @RequestMapping(value = "/read/{id}", method = RequestMethod.POST) public RestResponse read(@PathVariable Integer id) { ExamPaperAnswer examPaperAnswer = examPaperAnswerService.selectById(id); ExamPaperReadVO vm = new ExamPaperReadVO(); ExamPaperEditRequestVO paper = examPaperService.examPaperToVM(examPaperAnswer.getExamPaperId()); ExamPaperSubmitVM answer = examPaperAnswerService.examPaperAnswerToVM(examPaperAnswer.getId()); vm.setPaper(paper); vm.setAnswer(answer); return RestResponse.ok(vm); } }