package com.ycl.jxkg.controller.wx.student; import com.ycl.jxkg.base.Result; import com.ycl.jxkg.controller.wx.BaseWXApiController; import com.ycl.jxkg.domain.entity.ExamPaperScore; import com.ycl.jxkg.domain.vo.admin.exam.ExamPaperEditRequestVO; import com.ycl.jxkg.domain.vo.student.exam.ExamPaperReadVO; import com.ycl.jxkg.domain.vo.student.exam.ExamPaperSubmitItemVO; import com.ycl.jxkg.domain.vo.student.exam.ExamPaperSubmitVO; import com.ycl.jxkg.enums.QuestionTypeEnum; import com.ycl.jxkg.service.ExamPaperScoreService; import com.ycl.jxkg.service.ExamPaperService; import com.ycl.jxkg.service.SubjectService; import com.ycl.jxkg.utils.ExamUtil; import lombok.RequiredArgsConstructor; import org.springframework.context.ApplicationEventPublisher; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.util.*; import java.util.stream.Collectors; @RequiredArgsConstructor @Controller("WXStudentExamPaperAnswerController") @RequestMapping(value = "/api/wx/student/exampaper/answer") @ResponseBody public class ExamPaperAnswerController extends BaseWXApiController { private final ExamPaperScoreService examPaperScoreService; private final SubjectService subjectService; private final ApplicationEventPublisher eventPublisher; private final ExamPaperService examPaperService; // @RequestMapping(value = "/pageList", method = RequestMethod.POST) // public Result> pageList(@Valid ExamPaperAnswerPageVO model) { // model.setCreateUser(getCurrentUser().getId()); // PageInfo pageInfo = examPaperScoreService.studentPage(model); // PageInfo 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; // }); // return Result.ok(page); // } @RequestMapping(value = "/answerSubmit", method = RequestMethod.POST) public Result answerSubmit(HttpServletRequest request) { //TODO:answer ExamPaperSubmitVO examPaperSubmitVO = requestToExamPaperSubmitVM(request); // User user = getCurrentUser(); // ExamPaperAnswerInfo examPaperAnswerInfo = examPaperScoreService.calculateExamPaperAnswer(examPaperSubmitVO, user); // if (null == examPaperAnswerInfo) { // return Result.fail(2, "试卷不能重复做"); // } // 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(); } private ExamPaperSubmitVO requestToExamPaperSubmitVM(HttpServletRequest request) { ExamPaperSubmitVO examPaperSubmitVO = new ExamPaperSubmitVO(); examPaperSubmitVO.setId(Integer.parseInt(request.getParameter("id"))); examPaperSubmitVO.setDoTime(Integer.parseInt(request.getParameter("doTime"))); List parameterNames = Collections.list(request.getParameterNames()).stream() .filter(n -> n.contains("_")) .collect(Collectors.toList()); //题目答案按序号分组 Map> questionGroup = parameterNames.stream().collect(Collectors.groupingBy(p -> p.substring(0, p.indexOf("_")))); List answerItems = new ArrayList<>(); questionGroup.forEach((k, v) -> { ExamPaperSubmitItemVO examPaperSubmitItemVO = new ExamPaperSubmitItemVO(); String p = v.get(0); String[] keys = p.split("_"); examPaperSubmitItemVO.setQuestionId(Integer.parseInt(keys[1])); examPaperSubmitItemVO.setItemOrder(Integer.parseInt(keys[0])); QuestionTypeEnum typeEnum = QuestionTypeEnum.fromCode(Integer.parseInt(keys[2])); if (v.size() == 1) { String content = request.getParameter(p); examPaperSubmitItemVO.setContent(content); if (typeEnum == QuestionTypeEnum.MultipleChoice) { examPaperSubmitItemVO.setContentArray(Arrays.asList(content.split(","))); } } else { //多个空 填空题 List answers = v.stream().sorted(Comparator.comparingInt(ExamUtil::lastNum)).map(inputKey -> request.getParameter(inputKey)).collect(Collectors.toList()); examPaperSubmitItemVO.setContentArray(answers); } answerItems.add(examPaperSubmitItemVO); }); examPaperSubmitVO.setAnswerItems(answerItems); return examPaperSubmitVO; } @PostMapping(value = "/read/{id}") public Result read(@PathVariable Integer id) { ExamPaperReadVO vm = new ExamPaperReadVO(); ExamPaperScore examPaperScore = examPaperScoreService.getById(id); ExamPaperEditRequestVO paper = examPaperService.examPaperToVM(examPaperScore.getPaperId()); ExamPaperSubmitVO answer = examPaperScoreService.examPaperAnswerToVM(examPaperScore.getId()); vm.setPaper(paper); vm.setAnswer(answer); return Result.ok(vm); } }