xiangpei
2024-03-28 0f431b52e0936456bd165d9553761bfd8a5a0517
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.mindskip.xzs.controller.student;
 
import com.mindskip.xzs.base.BaseApiController;
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.domain.ExamPaper;
import com.mindskip.xzs.domain.Subject;
import com.mindskip.xzs.domain.User;
import com.mindskip.xzs.domain.enums.ExamPaperTypeEnum;
import com.mindskip.xzs.domain.enums.QuestionTypeEnum;
import com.mindskip.xzs.domain.other.QuestionRandom;
import com.mindskip.xzs.service.*;
import com.mindskip.xzs.utility.DateTimeUtil;
import com.mindskip.xzs.utility.JsonUtil;
import com.mindskip.xzs.utility.PageInfoHelper;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVM;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperTitleItemVM;
import com.mindskip.xzs.viewmodel.admin.question.QuestionEditRequestVM;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.viewmodel.student.exam.ExamPaperBuildVM;
import com.mindskip.xzs.viewmodel.student.exam.ExamPaperPageResponseVM;
import com.mindskip.xzs.viewmodel.student.exam.ExamPaperPageVM;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
 
/**
 * @version 2.2.0
 * @description: 试卷
 * Copyright (C), 2020-2021, 武汉思维跳跃科技有限公司
 * @date 2021 /9/7 9:45
 */
@RestController("StudentExamPaperController")
@RequestMapping(value = "/api/student/exam/paper")
public class ExamPaperController extends BaseApiController {
 
    private final ExamPaperService examPaperService;
    private final ExamPaperAnswerService examPaperAnswerService;
    private final ApplicationEventPublisher eventPublisher;
    private final ClassesService classesService;
    private final QuestionService questionService;
    private final SubjectService subjectService;
 
 
    /**
     * Instantiates a new Exam paper controller.
     *
     * @param examPaperService       the exam paper service
     * @param examPaperAnswerService the exam paper answer service
     * @param eventPublisher         the event publisher
     * @param classesService         the classes service
     * @param questionService        the question service
     * @param subjectService         the subject service
     */
    public ExamPaperController(ExamPaperService examPaperService, ExamPaperAnswerService examPaperAnswerService, ApplicationEventPublisher eventPublisher, ClassesService classesService, QuestionService questionService, SubjectService subjectService) {
        this.examPaperService = examPaperService;
        this.examPaperAnswerService = examPaperAnswerService;
        this.eventPublisher = eventPublisher;
        this.classesService = classesService;
        this.questionService = questionService;
        this.subjectService = subjectService;
    }
 
 
    /**
     * 试卷查看
     *
     * @param id the id
     * @return the rest response
     */
    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public RestResponse<ExamPaperEditRequestVM> select(@PathVariable Integer id) {
        ExamPaperEditRequestVM vm = examPaperService.examPaperToVM(id);
        return RestResponse.ok(vm);
    }
 
 
    /**
     * 试卷分页
     *
     * @param model the model
     * @return the rest response
     */
    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    public RestResponse<PageInfo<ExamPaperPageResponseVM>> pageList(@RequestBody @Valid ExamPaperPageVM model) {
        ExamPaperTypeEnum examPaperTypeEnum = ExamPaperTypeEnum.fromCode(model.getPaperType());
        model.setLevelId(getCurrentUser().getUserLevel());
        model.setDateTime(new Date());
        PageInfo<ExamPaper> pageInfo = null;
        if (examPaperTypeEnum == ExamPaperTypeEnum.Classes) {
            List<Integer> cIds = classesService.getJoinClasses(getCurrentUser().getId()).stream()
                    .map(d -> d.getClassesId()).collect(Collectors.toList());
            model.setCIds(cIds);
            pageInfo = examPaperService.studentClassesPage(model);
        } else {
            model.setUserId(getCurrentUser().getId());
            pageInfo = examPaperService.studentPage(model);
        }
        PageInfo<ExamPaperPageResponseVM> page = PageInfoHelper.copyMap(pageInfo, e -> {
            ExamPaperPageResponseVM vm = modelMapper.map(e, ExamPaperPageResponseVM.class);
            vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
            Subject subject = subjectService.selectById(vm.getSubjectId());
            vm.setSubjectName(subject.getName());
            return vm;
        });
        return RestResponse.ok(page);
    }
 
 
    /**
     * 智能训练
     *
     * @param examPaperBuildVM the exam paper build vm
     * @return the rest response
     */
    @RequestMapping(value = "/build", method = RequestMethod.POST)
    public RestResponse<String> build(@RequestBody @Valid ExamPaperBuildVM examPaperBuildVM) {
        User user = getCurrentUser();
 
        List<ExamPaperTitleItemVM> titleItems = new ArrayList<>();
        QuestionRandom questionRandom = new QuestionRandom();
        questionRandom.setSubjectId(examPaperBuildVM.getSubjectId());
        questionRandom.setGradeLevel(user.getUserLevel());
        questionRandom.setDifficult(examPaperBuildVM.getDifficult());
 
        Integer limit = examPaperBuildVM.getSingleChoice();
        if (limit != null && limit != 0) {
            ExamPaperTitleItemVM examPaperTitleItemVM = randomTitle(QuestionTypeEnum.SingleChoice, limit, questionRandom);
            if (examPaperTitleItemVM.getQuestionItems().size() != 0) {
                titleItems.add(randomTitle(QuestionTypeEnum.SingleChoice, limit, questionRandom));
            }
        }
 
        limit = examPaperBuildVM.getMultipleChoice();
        if (limit != null && limit != 0) {
            ExamPaperTitleItemVM examPaperTitleItemVM = randomTitle(QuestionTypeEnum.MultipleChoice, limit, questionRandom);
            if (examPaperTitleItemVM.getQuestionItems().size() != 0) {
                titleItems.add(randomTitle(QuestionTypeEnum.MultipleChoice, limit, questionRandom));
            }
        }
 
        limit = examPaperBuildVM.getTrueFalse();
        if (limit != null && limit != 0) {
            ExamPaperTitleItemVM examPaperTitleItemVM = randomTitle(QuestionTypeEnum.TrueFalse, limit, questionRandom);
            if (examPaperTitleItemVM.getQuestionItems().size() != 0) {
                titleItems.add(randomTitle(QuestionTypeEnum.TrueFalse, limit, questionRandom));
            }
        }
 
        if (titleItems.size() == 0) {
            return RestResponse.fail(2, "题库题目不足");
        }
 
        Integer randomCount = examPaperService.getRandomPaperCount(user.getId());
        Integer questionCount = titleItems.stream()
                .mapToInt(t -> t.getQuestionItems().size()).sum();
        ExamPaperEditRequestVM examPaperEditRequestVM = new ExamPaperEditRequestVM();
        examPaperEditRequestVM.setLevel(questionRandom.getGradeLevel());
        examPaperEditRequestVM.setSubjectId(questionRandom.getSubjectId());
        examPaperEditRequestVM.setPaperType(ExamPaperTypeEnum.Random.getCode());
        examPaperEditRequestVM.setName(String.format("智能训练试卷 - %d", (randomCount + 1)));
        examPaperEditRequestVM.setSuggestTime(questionCount * 2);
        examPaperEditRequestVM.setTitleItems(titleItems);
 
        examPaperService.savePaperFromVM(examPaperEditRequestVM, user);
        return RestResponse.ok(examPaperEditRequestVM.getName());
    }
 
    private ExamPaperTitleItemVM randomTitle(QuestionTypeEnum questionTypeEnum, Integer limit, QuestionRandom questionRandom) {
        ExamPaperTitleItemVM examPaperTitleItemVM = new ExamPaperTitleItemVM();
        examPaperTitleItemVM.setName(questionTypeEnum.getName());
        questionRandom.setQuestionType(questionTypeEnum.getCode());
        questionRandom.setLimit(limit);
        List<QuestionEditRequestVM> questionItems = questionService.randomQuestion(questionRandom).stream()
                .map(id -> questionService.getQuestionEditRequestVM(id))
                .collect(Collectors.toList());
        examPaperTitleItemVM.setQuestionItems(questionItems);
        return examPaperTitleItemVM;
    }
 
}