linzhijie
2021-03-11 93af1c6ffb9ae0e894689ad3a37b548e57d54cff
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package com.ots.project.exam.service.impl;
 
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ots.common.enums.QuestionTypeEnum;
import com.ots.project.exam.domain.ExamPaperAnswer;
import com.ots.project.exam.domain.ExamPaperQuestionCustomerAnswer;
import com.ots.project.exam.domain.ExamUser;
import com.ots.project.exam.domain.TExamPaper;
import com.ots.project.exam.domain.TQuestion;
import com.ots.project.exam.dto.ExamPaperAnswerInfo;
import com.ots.project.exam.dto.ExamPaperQuestionItemObject;
import com.ots.project.exam.dto.ExamPaperTitleItemObject;
import com.ots.project.exam.dto.QuestionPart;
import com.ots.project.exam.dto.SubmitReport;
import com.ots.project.exam.mapper.ExamPaperAnswerMapper;
import com.ots.project.exam.mapper.TExamPaperMapper;
import com.ots.project.exam.mapper.TQuestionMapper;
import com.ots.project.exam.service.ExamPaperAnswerService;
import com.ots.project.exam.service.ITTextContentService;
import com.ots.project.exam.viewmodel.ExamPaperAnswerPageVM;
import com.ots.project.exam.viewmodel.ExamPaperSubmitItemVM;
import com.ots.project.exam.viewmodel.ExamPaperSubmitVM;
import com.ots.project.tool.exam.ExamUtil;
import com.ots.project.tool.exam.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
@Service
public class ExamPaperAnswerServiceImpl implements ExamPaperAnswerService {
    private static final Logger log = LoggerFactory.getLogger(ExamPaperAnswerServiceImpl.class);
 
    @Autowired
    private TExamPaperMapper examPaperMapper;
    @Autowired
    private ExamPaperAnswerMapper examPaperAnswerMapper;
    @Autowired
    private ITTextContentService textContentService;
    @Autowired
    private TQuestionMapper questionMapper;
 
    @Override
    public ExamPaperAnswerInfo calculateExamPaperAnswer(ExamPaperSubmitVM examPaperSubmitVM, ExamUser user) {
        ExamPaperAnswerInfo examPaperAnswerInfo = new ExamPaperAnswerInfo();
        Date now = new Date();
        TExamPaper examPaper = examPaperMapper.selectTExamPaperById(examPaperSubmitVM.getId().longValue());
        String frameTextContent = textContentService.selectTTextContentById(examPaper.getFrameTextContentId()).getContent();
        List<ExamPaperTitleItemObject> examPaperTitleItemObjects = JsonUtil.toJsonListObject(frameTextContent, ExamPaperTitleItemObject.class);
        List<Integer> questionIds = examPaperTitleItemObjects.stream().flatMap(t -> t.getQuestionItems().stream().map(q -> q.getId())).collect(Collectors.toList());
        List<TQuestion> questions = questionMapper.selectByIds(questionIds);
 
        // 是否跳到下一段
        setWhichPart(examPaperSubmitVM, examPaperAnswerInfo, examPaperTitleItemObjects, questions);
 
        examPaperAnswerInfo.setExamPaper(examPaper);
 
        return examPaperAnswerInfo;
    }
 
    // 判断是否跳到下一段
    private void setWhichPart(ExamPaperSubmitVM examPaperSubmitVM, ExamPaperAnswerInfo examPaperAnswerInfo, List<ExamPaperTitleItemObject> examPaperTitleItemObjects, List<TQuestion> questions) {
        // 分片不足多少分不能答题,答题本应该的分数是否到了阀值。
        if (Objects.isNull(examPaperSubmitVM.getPartOrder())) {
            return;
        }
        int answerPartOrder = examPaperSubmitVM.getPartOrder();
 
        SubmitReport submitReport = new SubmitReport();
        if (Objects.isNull(answerPartOrder)) {
            submitReport.setJump("this");
            examPaperAnswerInfo.setSubmitReport(submitReport);
            return;
        }
 
        // 计算是否得分
        List<ExamPaperSubmitItemVM> answerItems = examPaperSubmitVM.getAnswerItems();
 
        // 用户答的选项
        // (questionId, content答的项)
        Map<Integer, String> answerContentMap = getAnswerContentMap(answerItems);
 
        // 原始分片
        Map<Integer, QuestionPart> partMap = getParts(examPaperTitleItemObjects);
 
        // 那一题是那一片
        // (questionId, partOrder)
        Map<Integer, Integer> questionPartOrdeMap = getQuestionPartOrdeMap(examPaperTitleItemObjects);
        // 用户答多少分得分
        // (partOrder, score)
        Map<Integer, Integer> partAnswerScoreMap = new HashMap<>();
        // 本段本来应该得多少分
        // (partOrder, score)
        Map<Integer, Integer> partQuestionScoreMap = new HashMap<>();
        // 用户本段耗时
        Map<Integer, Integer> partAnswerTimeMap = new HashMap<>();
 
        setAnswerScoreMap(questions, answerContentMap, questionPartOrdeMap, partAnswerScoreMap, partQuestionScoreMap);
 
        // 本应得多少分到了阀值。
//        System.out.println("本段应该得多少分:" + partQuestionScoreMap.get(answerPartOrder));
//        System.out.println("本段最低分:" + partMap.get(answerPartOrder).getLowestScore());
//        System.out.println("自己得分:" + partAnswerScoreMap.get(answerPartOrder));
//        if (Objects.nonNull(answerPartOrder) && partQuestionScoreMap.get(answerPartOrder) >= partMap.get(answerPartOrder).getLowestScore()) {
//            if (partAnswerScoreMap.get(answerPartOrder) < partQuestionScoreMap.get(answerPartOrder)) {
//                submitReport.setJump("next");
//            } else {
//                submitReport.setJump("this");
//            }
//        } else {
//            submitReport.setJump("this");
//        }
        examPaperAnswerInfo.setSubmitReport(submitReport);
        // 时间的判断
 
    }
 
    private void setAnswerScoreMap(List<TQuestion> questions, Map<Integer, String> answerContentMap, Map<Integer, Integer> questionPartOrdeMap, Map<Integer, Integer> partAnswerScoreMap, Map<Integer, Integer> partQuestionScoreMap) {
        for (TQuestion tQuestion : questions) {
            // 题目的段
            int partOrder = questionPartOrdeMap.get(tQuestion.getId().intValue());
 
            // 初始值集合
            if (Objects.isNull(partQuestionScoreMap.get(partOrder))){
                partQuestionScoreMap.put(partOrder, 0);
            }
 
            if (Objects.isNull(partAnswerScoreMap.get(partOrder))) {
                partAnswerScoreMap.put(partOrder, 0);
            }
 
            // 本来应该的多少分
            // 用户能答多少分得分
            if (Objects.nonNull(answerContentMap.get(tQuestion.getId().intValue()))) {
                setAnswerScoreMap(partQuestionScoreMap, tQuestion, partOrder);
            }
 
            // 分数的判断
//            System.out.println("题目号:" + tQuestion.getId().intValue());
//            System.out.println("题目的正确答案:" + tQuestion.getCorrect());
//            System.out.println("自己选择:" + answerContentMap.get(tQuestion.getId().intValue()));
            if (Objects.equals(tQuestion.getCorrect(), answerContentMap.get(tQuestion.getId().intValue()))) {
                // 用户能答多少分得分
                setAnswerScoreMap(partAnswerScoreMap, tQuestion, partOrder);
            }
        }
    }
 
    private Map<Integer, Integer> getQuestionPartOrdeMap(List<ExamPaperTitleItemObject> examPaperTitleItemObjects) {
        Map<Integer, Integer> questionPartOrdeMap = new HashMap<>();
        for (ExamPaperTitleItemObject examPaperTitleItemObject : examPaperTitleItemObjects) {
            List<ExamPaperQuestionItemObject> questionItems = examPaperTitleItemObject.getQuestionItems();
            for (ExamPaperQuestionItemObject examPaperQuestionItemObject : questionItems) {
                questionPartOrdeMap.put(examPaperQuestionItemObject.getId(), examPaperQuestionItemObject.getPartOrder());
            }
        }
        return questionPartOrdeMap;
    }
 
    private Map<Integer, QuestionPart> getParts(List<ExamPaperTitleItemObject> examPaperTitleItemObjects) {
        List<QuestionPart> parts = examPaperTitleItemObjects.get(0).getParts();
        // (partOrder, questionPart)
        Map<Integer, QuestionPart> partMap = new HashMap<>();
        for (QuestionPart questionPart : parts) {
            partMap.put(questionPart.getPartOrder(), questionPart);
        }
        return partMap;
    }
 
    private Map<Integer, String> getAnswerContentMap(List<ExamPaperSubmitItemVM> answerItems) {
        Map<Integer, String> answerContentMap = new HashMap<>();
        answerItems.stream().forEach(answerItem ->{
            answerContentMap.put(answerItem.getQuestionId(), answerItem.getContent());
        });
        return answerContentMap;
    }
 
    private void setAnswerScoreMap(Map<Integer, Integer> partQuestionScoreMap, TQuestion tQuestion, int partOrder) {
        int questonScore = Objects.nonNull(tQuestion.getScore()) ? tQuestion.getScore().intValue() : 0;
        if (Objects.nonNull(partQuestionScoreMap.get(partOrder))) {
            int score = partQuestionScoreMap.get(partOrder).intValue() + questonScore;
            partQuestionScoreMap.put(partOrder, score);
        } else {
            partQuestionScoreMap.put(partOrder, questonScore);
        }
    }
 
    /**
     * 用户提交答案的转化存储对象
     *
     * @param question               question
     * @param customerQuestionAnswer customerQuestionAnswer
     * @param examPaper              examPaper
     * @param itemOrder              itemOrder
     * @param user                   user
     * @param now                    now
     * @return ExamPaperQuestionCustomerAnswer
     */
    private ExamPaperQuestionCustomerAnswer ExamPaperQuestionCustomerAnswerFromVM(TQuestion question, ExamPaperSubmitItemVM customerQuestionAnswer, TExamPaper examPaper, Integer itemOrder, ExamUser user, Date now) {
        ExamPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer = new ExamPaperQuestionCustomerAnswer();
        examPaperQuestionCustomerAnswer.setQuestionId(question.getId().intValue());
        examPaperQuestionCustomerAnswer.setExamPaperId(examPaper.getId().intValue());
        // examPaperQuestionCustomerAnswer.setWhichPart(question.getScore().intValue());
        // examPaperQuestionCustomerAnswer.setSubjectId(examPaper.getSubjectId().intValue());
        examPaperQuestionCustomerAnswer.setItemOrder(itemOrder);
        examPaperQuestionCustomerAnswer.setCreateTime(now);
        examPaperQuestionCustomerAnswer.setCreateUser(user.getId());
        examPaperQuestionCustomerAnswer.setQuestionType(question.getQuestionType().intValue());
        examPaperQuestionCustomerAnswer.setQuestionTextContentId(question.getInfoTextContentId().intValue());
        if (null == customerQuestionAnswer) {
            examPaperQuestionCustomerAnswer.setCustomerScore(0);
        } else {
            setSpecialFromVM(examPaperQuestionCustomerAnswer, question, customerQuestionAnswer);
        }
        return examPaperQuestionCustomerAnswer;
    }
 
    /**
     * 判断提交答案是否正确,保留用户提交的答案
     *
     * @param examPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer
     * @param question                        question
     * @param customerQuestionAnswer          customerQuestionAnswer
     */
    private void setSpecialFromVM(ExamPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer, TQuestion question, ExamPaperSubmitItemVM customerQuestionAnswer) {
        QuestionTypeEnum questionTypeEnum = QuestionTypeEnum.fromCode(examPaperQuestionCustomerAnswer.getQuestionType());
        switch (questionTypeEnum) {
            case SingleChoice:
            case TrueFalse:
                examPaperQuestionCustomerAnswer.setAnswer(customerQuestionAnswer.getContent());
                examPaperQuestionCustomerAnswer.setDoRight(question.getCorrect().equals(customerQuestionAnswer.getContent()));
                examPaperQuestionCustomerAnswer.setCustomerScore(0);
                break;
            case MultipleChoice:
                String customerAnswer = ExamUtil.contentToString(customerQuestionAnswer.getContentArray());
                examPaperQuestionCustomerAnswer.setAnswer(customerAnswer);
                examPaperQuestionCustomerAnswer.setDoRight(customerAnswer.equals(question.getCorrect()));
                examPaperQuestionCustomerAnswer.setCustomerScore(0);
                break;
            case GapFilling:
                String correctAnswer = JsonUtil.toJsonStr(customerQuestionAnswer.getContentArray());
                examPaperQuestionCustomerAnswer.setAnswer(correctAnswer);
                examPaperQuestionCustomerAnswer.setCustomerScore(0);
                break;
            default:
                examPaperQuestionCustomerAnswer.setAnswer(customerQuestionAnswer.getContent());
                examPaperQuestionCustomerAnswer.setCustomerScore(0);
                break;
        }
    }
 
 
    @Override
    public PageInfo<ExamPaperAnswer> studentPage(ExamPaperAnswerPageVM requestVM) {
        return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() ->
                examPaperAnswerMapper.studentPage(requestVM));
    }
 
 
    @Override
    public String judge(ExamPaperSubmitVM examPaperSubmitVM) {
        return null;
    }
 
    @Override
    public ExamPaperSubmitVM examPaperAnswerToVM(Integer id) {
        return null;
    }
 
    @Override
    public Integer selectAllCount() {
        return null;
    }
 
    @Override
    public List<Integer> selectMothCount() {
        return null;
    }
 
    @Override
    public int deleteById(Integer id) {
        return 0;
    }
 
    @Override
    public int insert(ExamPaperAnswer record) {
        return examPaperAnswerMapper.insertSelective(record);
    }
 
    @Override
    public int insertByFilter(ExamPaperAnswer record) {
        return examPaperAnswerMapper.insertSelective(record);
    }
 
    @Override
    public ExamPaperAnswer selectById(Integer id) {
        return null;
    }
 
    @Override
    public int updateByIdFilter(ExamPaperAnswer record) {
        return 0;
    }
 
    @Override
    public int updateById(ExamPaperAnswer record) {
        return 0;
    }
}