| | |
| | | package com.ycl.jxkg.controller.student; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.ycl.jxkg.base.BaseApiController; |
| | | import com.ycl.jxkg.base.Result; |
| | | import com.ycl.jxkg.domain.*; |
| | | import com.ycl.jxkg.domain.entity.ExamPaperAnswer; |
| | | import com.ycl.jxkg.domain.entity.Subject; |
| | | import com.ycl.jxkg.domain.ExamPaperAnswerInfo; |
| | | import com.ycl.jxkg.domain.entity.ExamPaperScore; |
| | | import com.ycl.jxkg.domain.entity.User; |
| | | import com.ycl.jxkg.domain.entity.UserEventLog; |
| | | import com.ycl.jxkg.domain.enums.ExamPaperAnswerStatusEnum; |
| | | import com.ycl.jxkg.event.CalculateExamPaperAnswerCompleteEvent; |
| | | import com.ycl.jxkg.event.UserEvent; |
| | | import com.ycl.jxkg.service.ExamPaperAnswerService; |
| | | import com.ycl.jxkg.service.ExamPaperService; |
| | | import com.ycl.jxkg.service.SubjectService; |
| | | import com.ycl.jxkg.utils.DateTimeUtil; |
| | | import com.ycl.jxkg.utils.ExamUtil; |
| | | import com.ycl.jxkg.utils.PageInfoHelper; |
| | | import com.ycl.jxkg.domain.vo.admin.exam.ExamPaperEditRequestVO; |
| | | import com.ycl.jxkg.domain.vo.student.exam.ExamPaperReadVO; |
| | | import com.ycl.jxkg.domain.vo.PaperFixQuestionVO; |
| | | import com.ycl.jxkg.domain.vo.admin.exam.ExamPaperMarkNavbarVO; |
| | | import com.ycl.jxkg.domain.vo.admin.exam.ExamPaperScoreVO; |
| | | import com.ycl.jxkg.domain.vo.student.exam.ExamPaperSubmitVO; |
| | | import com.ycl.jxkg.domain.vo.student.exampaper.ExamPaperAnswerPageResponseVO; |
| | | import com.ycl.jxkg.domain.vo.student.exampaper.ExamPaperAnswerPageVO; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.ycl.jxkg.service.ExamPaperScoreService; |
| | | import com.ycl.jxkg.service.ExamPaperService; |
| | | import com.ycl.jxkg.service.SubjectService; |
| | | import com.ycl.jxkg.service.UserService; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.context.ApplicationEventPublisher; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.validation.Valid; |
| | | import java.util.Date; |
| | | import java.util.Objects; |
| | | |
| | | @RequiredArgsConstructor |
| | | @RestController("StudentExamPaperAnswerController") |
| | | @RequestMapping(value = "/api/student/exampaper/answer") |
| | | public class ExamPaperAnswerController extends BaseApiController { |
| | | |
| | | private final ExamPaperAnswerService examPaperAnswerService; |
| | | private final ExamPaperScoreService examPaperScoreService; |
| | | private final UserService userService; |
| | | private final ExamPaperService examPaperService; |
| | | private final SubjectService subjectService; |
| | | private final ApplicationEventPublisher eventPublisher; |
| | |
| | | @RequestMapping(value = "/pageList", method = RequestMethod.POST) |
| | | public Result<PageInfo<ExamPaperAnswerPageResponseVO>> pageList(@RequestBody @Valid ExamPaperAnswerPageVO model) { |
| | | model.setCreateUser(getCurrentUser().getId()); |
| | | PageInfo<ExamPaperAnswer> pageInfo = examPaperAnswerService.studentPage(model); |
| | | PageInfo<ExamPaperAnswerPageResponseVO> page = PageInfoHelper.copyMap(pageInfo, e -> { |
| | | ExamPaperAnswerPageResponseVO vo = new ExamPaperAnswerPageResponseVO(); |
| | | BeanUtils.copyProperties(e, vo); |
| | | Subject subject = subjectService.getById(vo.getSubjectId()); |
| | | vo.setDoTime(ExamUtil.secondToVM(e.getDoTime())); |
| | | vo.setSystemScore(ExamUtil.scoreToVM(e.getSystemScore())); |
| | | vo.setUserScore(ExamUtil.scoreToVM(e.getUserScore())); |
| | | vo.setPaperScore(ExamUtil.scoreToVM(e.getPaperScore())); |
| | | vo.setSubjectName(subject.getName()); |
| | | vo.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime())); |
| | | return vo; |
| | | }); |
| | | PageInfo<ExamPaperAnswerPageResponseVO> page = examPaperScoreService.studentPage(model); |
| | | return Result.ok(page); |
| | | } |
| | | |
| | |
| | | @RequestMapping(value = "/answerSubmit", method = RequestMethod.POST) |
| | | public Result answerSubmit(@RequestBody @Valid ExamPaperSubmitVO examPaperSubmitVO) { |
| | | User user = getCurrentUser(); |
| | | ExamPaperAnswerInfo examPaperAnswerInfo = examPaperAnswerService.calculateExamPaperAnswer(examPaperSubmitVO, user); |
| | | ExamPaperAnswerInfo examPaperAnswerInfo = examPaperScoreService.calculateExamPaperAnswer(examPaperSubmitVO, user); |
| | | if (null == examPaperAnswerInfo) { |
| | | return Result.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)); |
| | | return Result.ok(scoreVm); |
| | | // ExamPaperScore examPaperScore = examPaperAnswerInfo.getExamPaperScore(); |
| | | // Integer userScore = examPaperScore.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(examPaperScore.getDoTime()); |
| | | // userEventLog.setContent(content); |
| | | // eventPublisher.publishEvent(new CalculateExamPaperAnswerCompleteEvent(examPaperAnswerInfo)); |
| | | // eventPublisher.publishEvent(new UserEvent(userEventLog)); |
| | | // return Result.ok(scoreVm); |
| | | return Result.ok(); |
| | | } |
| | | |
| | | |
| | | @RequestMapping(value = "/edit", method = RequestMethod.POST) |
| | | public Result edit(@RequestBody @Valid ExamPaperSubmitVO examPaperSubmitVO) { |
| | | boolean notJudge = examPaperSubmitVO.getAnswerItems().stream().anyMatch(i -> i.getDoRight() == null && i.getScore() == null); |
| | | if (notJudge) { |
| | | return Result.fail(2, "有未批改题目"); |
| | | } |
| | | |
| | | ExamPaperAnswer examPaperAnswer = examPaperAnswerService.getById(examPaperSubmitVO.getId()); |
| | | ExamPaperAnswerStatusEnum examPaperAnswerStatusEnum = ExamPaperAnswerStatusEnum.fromCode(examPaperAnswer.getStatus()); |
| | | if (examPaperAnswerStatusEnum == ExamPaperAnswerStatusEnum.Complete) { |
| | | return Result.fail(3, "试卷已完成"); |
| | | } |
| | | String score = examPaperAnswerService.judge(examPaperSubmitVO); |
| | | 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)); |
| | | // boolean notJudge = examPaperSubmitVO.getAnswerItems().stream().anyMatch(i -> i.getDoRight() == null && i.getScore() == null); |
| | | // if (notJudge) { |
| | | // return Result.fail(2, "有未批改题目"); |
| | | // } |
| | | // |
| | | // ExamPaperScore examPaperScore = examPaperScoreService.getById(examPaperSubmitVO.getId()); |
| | | // ExamPaperAnswerStatusEnum examPaperAnswerStatusEnum = ExamPaperAnswerStatusEnum.fromCode(examPaperScore.getStatus()); |
| | | // if (examPaperAnswerStatusEnum == ExamPaperAnswerStatusEnum.Complete) { |
| | | // return Result.fail(3, "试卷已完成"); |
| | | // } |
| | | String score = examPaperScoreService.judge(examPaperSubmitVO); |
| | | // User user = getCurrentUser(); |
| | | // UserEventLog userEventLog = new UserEventLog(user.getId(), user.getUserName(), user.getRealName(), new Date()); |
| | | // String content = user.getUserName() + " 批改试卷:" + examPaperScore.get() + " 得分:" + score; |
| | | // userEventLog.setContent(content); |
| | | // eventPublisher.publishEvent(new UserEvent(userEventLog)); |
| | | return Result.ok(score); |
| | | } |
| | | |
| | | @RequestMapping(value = "/read/{id}", method = RequestMethod.POST) |
| | | public Result<ExamPaperReadVO> read(@PathVariable Integer id) { |
| | | ExamPaperAnswer examPaperAnswer = examPaperAnswerService.getById(id); |
| | | ExamPaperReadVO vm = new ExamPaperReadVO(); |
| | | ExamPaperEditRequestVO paper = examPaperService.examPaperToVM(examPaperAnswer.getExamPaperId()); |
| | | ExamPaperSubmitVO answer = examPaperAnswerService.examPaperAnswerToVM(examPaperAnswer.getId()); |
| | | vm.setPaper(paper); |
| | | vm.setAnswer(answer); |
| | | return Result.ok(vm); |
| | | public Result<ExamPaperScoreVO> read(@PathVariable Integer id) { |
| | | ExamPaperScore examPaperScore = examPaperScoreService.getById(id); |
| | | ExamPaperScoreVO examPaperScoreVO = new ExamPaperScoreVO(); |
| | | BeanUtils.copyProperties(examPaperScore, examPaperScoreVO); |
| | | User user = userService.getById(examPaperScore.getUserId()); |
| | | examPaperScoreVO.setUserName(Objects.nonNull(user) ? user.getRealName() : "用户已注销"); |
| | | examPaperScoreVO.setNavbar(JSON.parseArray(examPaperScore.getNavbar(), ExamPaperMarkNavbarVO.class)); |
| | | examPaperScoreVO.setTitleItems(JSON.parseArray(examPaperScore.getPaperContent(), PaperFixQuestionVO.class)); |
| | | return Result.ok(examPaperScoreVO); |
| | | } |
| | | |
| | | |
| | | } |