龚焕茏
2024-07-01 c227e3938798eb7d529575ba1f3ea1c56ae63cfe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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<PageInfo<ExamPaperAnswerPageResponseVO>> pageList(@Valid ExamPaperAnswerPageVO model) {
//         model.setCreateUser(getCurrentUser().getId());
//         PageInfo<ExamPaperScore> pageInfo = examPaperScoreService.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;
//         });
//         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<String> parameterNames = Collections.list(request.getParameterNames()).stream()
                .filter(n -> n.contains("_"))
                .collect(Collectors.toList());
        //题目答案按序号分组
        Map<String, List<String>> questionGroup = parameterNames.stream().collect(Collectors.groupingBy(p -> p.substring(0, p.indexOf("_"))));
        List<ExamPaperSubmitItemVO> 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<String> 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<ExamPaperReadVO> 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);
    }
}