xiangpei
2024-05-31 16d10cef208de048f8b325facd143c54b7be9963
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.student;
 
import com.ycl.jxkg.base.BaseApiController;
import com.ycl.jxkg.base.Result;
import com.ycl.jxkg.domain.*;
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.vo.admin.exam.ExamPaperEditRequestVO;
import com.ycl.jxkg.vo.student.exam.ExamPaperReadVO;
import com.ycl.jxkg.vo.student.exam.ExamPaperSubmitVO;
import com.ycl.jxkg.vo.student.exampaper.ExamPaperAnswerPageResponseVO;
import com.ycl.jxkg.vo.student.exampaper.ExamPaperAnswerPageVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
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;
 
@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;
 
    @Autowired
    public ExamPaperAnswerController(ExamPaperAnswerService examPaperAnswerService, ExamPaperService examPaperService, SubjectService subjectService, ApplicationEventPublisher eventPublisher) {
        this.examPaperAnswerService = examPaperAnswerService;
        this.examPaperService = examPaperService;
        this.subjectService = subjectService;
        this.eventPublisher = 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.selectById(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(@RequestBody @Valid ExamPaperSubmitVO examPaperSubmitVO) {
        User user = getCurrentUser();
        ExamPaperAnswerInfo examPaperAnswerInfo = examPaperAnswerService.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);
    }
 
 
    @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.selectById(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));
        return Result.ok(score);
    }
 
    @RequestMapping(value = "/read/{id}", method = RequestMethod.POST)
    public Result<ExamPaperReadVO> read(@PathVariable Integer id) {
        ExamPaperAnswer examPaperAnswer = examPaperAnswerService.selectById(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);
    }
 
 
}