luohairen
2024-11-08 d28ba0adc6fcec6780c4ee500077aa46dd336957
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
package com.ycl.jxkg.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycl.jxkg.base.Result;
import com.ycl.jxkg.domain.ExamPaperAnswerInfo;
import com.ycl.jxkg.domain.entity.*;
import com.ycl.jxkg.domain.other.KeyValue;
import com.ycl.jxkg.domain.vo.admin.paper.ExamPaperAnswerPageRequestVO;
import com.ycl.jxkg.domain.vo.admin.paper.UserAnswerPageRequestVO;
import com.ycl.jxkg.domain.vo.student.exam.ExamPaperSubmitItemVO;
import com.ycl.jxkg.domain.vo.student.exam.ExamPaperSubmitVO;
import com.ycl.jxkg.domain.vo.student.exampaper.ExamPaperAnswerPageResponseVO;
import com.ycl.jxkg.domain.vo.student.exampaper.ExamPaperAnswerPageVO;
import com.ycl.jxkg.domain.vo.student.exampaper.UserAnswerPageResponseVO;
import com.ycl.jxkg.enums.ExamPaperTypeEnum;
import com.ycl.jxkg.enums.QuestionTypeEnum;
import com.ycl.jxkg.mapper.ExamPaperMapper;
import com.ycl.jxkg.mapper.ExamPaperScoreMapper;
import com.ycl.jxkg.mapper.QuestionMapper;
import com.ycl.jxkg.mapper.TaskExamCustomerAnswerMapper;
import com.ycl.jxkg.service.ExamPaperScoreDetailService;
import com.ycl.jxkg.service.ExamPaperScoreService;
import com.ycl.jxkg.service.TextContentService;
import com.ycl.jxkg.utils.DateTimeUtil;
import com.ycl.jxkg.utils.ExamUtil;
import com.ycl.jxkg.utils.JsonUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
@Service
@RequiredArgsConstructor
public class ExamPaperScoreServiceImpl extends ServiceImpl<ExamPaperScoreMapper, ExamPaperScore> implements ExamPaperScoreService {
 
    private final ExamPaperScoreMapper examPaperScoreMapper;
    private final ExamPaperMapper examPaperMapper;
    private final TextContentService textContentService;
    private final QuestionMapper questionMapper;
    private final ExamPaperScoreDetailService examPaperScoreDetailService;
    private final TaskExamCustomerAnswerMapper taskExamCustomerAnswerMapper;
 
 
    @Override
    public PageInfo<ExamPaperAnswerPageResponseVO> studentPage(ExamPaperAnswerPageVO requestVM) {
        return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() ->
                examPaperScoreMapper.studentPage(requestVM));
    }
 
 
    @Override
    public ExamPaperAnswerInfo calculateExamPaperAnswer(ExamPaperSubmitVO examPaperSubmitVO, User user) {
        ExamPaperAnswerInfo examPaperAnswerInfo = new ExamPaperAnswerInfo();
        Date now = new Date();
        ExamPaper examPaper = examPaperMapper.selectById(examPaperSubmitVO.getId());
        ExamPaperTypeEnum paperTypeEnum = ExamPaperTypeEnum.fromCode(examPaper.getPaperType());
        //任务试卷只能做一次
        if (paperTypeEnum == ExamPaperTypeEnum.RandomOrder) {
            ExamPaperScore examPaperScore = examPaperScoreMapper.getByPidUid(examPaperSubmitVO.getId(), user.getId());
            if (null != examPaperScore)
                return null;
        }
        //TODO:待完成
//        String frameTextContent = textContentService.getById(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<Question> questions = questionMapper.selectByIds(questionIds);
//        //将题目结构的转化为题目答案
//        List<ExamPaperQuestionCustomerAnswer> examPaperQuestionCustomerAnswers = examPaperTitleItemObjects.stream()
//                .flatMap(t -> t.getQuestionItems().stream()
//                        .map(q -> {
//                            Question question = questions.stream().filter(tq -> tq.getId().equals(q.getId())).findFirst().get();
//                            ExamPaperSubmitItemVO customerQuestionAnswer = examPaperSubmitVO.getAnswerItems().stream()
//                                    .filter(tq -> tq.getQuestionId().equals(q.getId()))
//                                    .findFirst()
//                                    .orElse(null);
//                            return ExamPaperQuestionCustomerAnswerFromVM(question, customerQuestionAnswer, examPaper, q.getItemOrder(), user, now);
//                        })
//                ).collect(Collectors.toList());
//
//        ExamPaperAnswer examPaperAnswer = ExamPaperAnswerFromVM(examPaperSubmitVO, examPaper, examPaperQuestionCustomerAnswers, user, now);
//        examPaperAnswerInfo.setExamPaper(examPaper);
//        examPaperAnswerInfo.setExamPaperAnswer(examPaperAnswer);
//        examPaperAnswerInfo.setExamPaperQuestionCustomerAnswers(examPaperQuestionCustomerAnswers);
        return examPaperAnswerInfo;
    }
 
    @Override
    @Transactional
    public String judge(ExamPaperSubmitVO examPaperSubmitVO) {
//        ExamPaperScore examPaperScore = examPaperScoreMapper.selectById(examPaperSubmitVO.getId());
//        List<ExamPaperSubmitItemVO> judgeItems = examPaperSubmitVO.getAnswerItems().stream().filter(d -> d.getDoRight() == null).collect(Collectors.toList());
//        List<ExamPaperAnswerUpdate> examPaperAnswerUpdates = new ArrayList<>(judgeItems.size());
//        Integer customerScore = examPaperScore.getUserScore();
//        Integer questionCorrect = examPaperScore.getQuestionCorrect();
//        for (ExamPaperSubmitItemVO d : judgeItems) {
//            ExamPaperAnswerUpdate examPaperAnswerUpdate = new ExamPaperAnswerUpdate();
//            examPaperAnswerUpdate.setId(d.getId());
//            examPaperAnswerUpdate.setCustomerScore(ExamUtil.scoreFromVM(d.getScore()));
//            boolean doRight = examPaperAnswerUpdate.getCustomerScore().equals(ExamUtil.scoreFromVM(d.getQuestionScore()));
//            examPaperAnswerUpdate.setDoRight(doRight);
//            examPaperAnswerUpdates.add(examPaperAnswerUpdate);
//            customerScore += examPaperAnswerUpdate.getCustomerScore();
//            if (examPaperAnswerUpdate.getDoRight()) {
//                ++questionCorrect;
//            }
//        }
//        examPaperScore.setUserScore(customerScore);
//        examPaperScore.setQuestionCorrect(questionCorrect);
//        examPaperScore.setStatus(ExamPaperAnswerStatusEnum.Complete.getCode());
//        examPaperScoreMapper.updateById(examPaperScore);
//        examPaperScoreDetailService.updateScore(examPaperAnswerUpdates);
//
//        ExamPaperTypeEnum examPaperTypeEnum = ExamPaperTypeEnum.fromCode(examPaperScore.getPaperType());
//        switch (examPaperTypeEnum) {
//            case RandomOrder:
                //TODO:待完成
                //任务试卷批改完成后,需要更新任务的状态
//                ExamPaper examPaper = examPaperMapper.selectById(examPaperAnswer.getExamPaperId());
//                Integer taskId = examPaper.getTaskExamId();
//                Integer userId = examPaperAnswer.getCreateUser();
//                TaskExamCustomerAnswer taskExamCustomerAnswer = taskExamCustomerAnswerMapper.getByTUid(taskId, userId);
//                TextContent textContent = textContentService.getById(taskExamCustomerAnswer.getTextContentId());
//                List<TaskItemAnswerObject> taskItemAnswerObjects = JsonUtil.toJsonListObject(textContent.getContent(), TaskItemAnswerObject.class);
//                taskItemAnswerObjects.stream()
//                        .filter(d -> d.getExamPaperAnswerId().equals(examPaperAnswer.getId()))
//                        .findFirst().ifPresent(taskItemAnswerObject -> taskItemAnswerObject.setStatus(examPaperAnswer.getStatus()));
//                textContentService.jsonConvertUpdate(textContent, taskItemAnswerObjects, null);
//                textContentService.updateById(textContent);
//                break;
//            default:
//                break;
//        }
//        return ExamUtil.scoreToVM(customerScore);
        return "1";
    }
 
    @Override
    public ExamPaperSubmitVO examPaperAnswerToVM(Integer id) {
        ExamPaperSubmitVO examPaperSubmitVO = new ExamPaperSubmitVO();
//        ExamPaperScore examPaperScore = examPaperScoreMapper.selectById(id);
//        examPaperSubmitVO.setId(examPaperScore.getId());
//        examPaperSubmitVO.setDoTime(examPaperScore.getDoTime());
//        examPaperSubmitVO.setScore(ExamUtil.scoreToVM(examPaperScore.getUserScore()));
//        List<ExamPaperScoreDetail> examPaperScoreDetails = examPaperScoreDetailService.selectListByPaperAnswerId(examPaperScore.getId());
//        List<ExamPaperSubmitItemVO> examPaperSubmitItemVOS = examPaperScoreDetails.stream()
//                .map(a -> examPaperScoreDetailService.examPaperQuestionCustomerAnswerToVM(a))
//                .collect(Collectors.toList());
//        examPaperSubmitVO.setAnswerItems(examPaperSubmitItemVOS);
        return examPaperSubmitVO;
    }
 
    @Override
    public Integer selectAllCount() {
        return examPaperScoreMapper.selectAllCount();
    }
 
    @Override
    public Integer selectAllQuestionCount() {
        return examPaperScoreMapper.selectAllQuestionCount();
    }
 
 
    @Override
    public List<Integer> selectMothCount() {
        Date startTime = DateTimeUtil.getMonthStartDay();
        Date endTime = DateTimeUtil.getMonthEndDay();
        List<KeyValue> mouthCount = examPaperScoreMapper.selectCountByDate(startTime, endTime);
        List<String> mothStartToNowFormat = DateTimeUtil.MothStartToNowFormat();
        return mothStartToNowFormat.stream().map(md -> {
            KeyValue keyValue = mouthCount.stream().filter(kv -> kv.getName().equals(md)).findAny().orElse(null);
            return null == keyValue ? 0 : keyValue.getValue();
        }).collect(Collectors.toList());
    }
 
 
    /**
     * 用户提交答案的转化存储对象
     *
     * @param question               question
     * @param customerQuestionAnswer customerQuestionAnswer
     * @param examPaper              examPaper
     * @param itemOrder              itemOrder
     * @param user                   user
     * @param now                    now
     * @return ExamPaperQuestionCustomerAnswer
     */
    private ExamPaperScoreDetail ExamPaperQuestionCustomerAnswerFromVM(Question question, ExamPaperSubmitItemVO customerQuestionAnswer, ExamPaper examPaper, Integer itemOrder, User user, Date now) {
        ExamPaperScoreDetail examPaperScoreDetail = new ExamPaperScoreDetail();
        examPaperScoreDetail.setQuestionId(question.getId());
        examPaperScoreDetail.setExamPaperId(examPaper.getId());
        examPaperScoreDetail.setQuestionScore(null);
//        examPaperScoreDetail.setSubjectId(examPaper.getSubjectId());
        examPaperScoreDetail.setItemOrder(itemOrder);
        examPaperScoreDetail.setCreateTime(now);
        examPaperScoreDetail.setCreateUser(user.getId());
        examPaperScoreDetail.setQuestionType(question.getQuestionType());
        examPaperScoreDetail.setQuestionContent(question.getContent());
        if (null == customerQuestionAnswer) {
            examPaperScoreDetail.setCustomerScore(0);
        } else {
            setSpecialFromVM(examPaperScoreDetail, question, customerQuestionAnswer);
        }
        return examPaperScoreDetail;
    }
 
    /**
     * 判断提交答案是否正确,保留用户提交的答案
     *
     * @param examPaperScoreDetail examPaperQuestionCustomerAnswer
     * @param question                        question
     * @param customerQuestionAnswer          customerQuestionAnswer
     */
    private void setSpecialFromVM(ExamPaperScoreDetail examPaperScoreDetail, Question question, ExamPaperSubmitItemVO customerQuestionAnswer) {
        QuestionTypeEnum questionTypeEnum = QuestionTypeEnum.fromCode(examPaperScoreDetail.getQuestionType());
        switch (questionTypeEnum) {
            case SingleChoice:
            case TrueFalse:
                examPaperScoreDetail.setAnswer(customerQuestionAnswer.getContent());
                examPaperScoreDetail.setDoRight(question.getCorrect().equals(customerQuestionAnswer.getContent()));
                examPaperScoreDetail.setCustomerScore(examPaperScoreDetail.getDoRight() ? null : 0);
                break;
            case MultipleChoice:
                String customerAnswer = ExamUtil.contentToString(customerQuestionAnswer.getContentArray());
                examPaperScoreDetail.setAnswer(customerAnswer);
                examPaperScoreDetail.setDoRight(customerAnswer.equals(question.getCorrect()));
                examPaperScoreDetail.setCustomerScore(examPaperScoreDetail.getDoRight() ? null : 0);
                break;
            case GapFilling:
                String correctAnswer = JsonUtil.toJsonStr(customerQuestionAnswer.getContentArray());
                examPaperScoreDetail.setAnswer(correctAnswer);
                examPaperScoreDetail.setCustomerScore(0);
                break;
            default:
                examPaperScoreDetail.setAnswer(customerQuestionAnswer.getContent());
                examPaperScoreDetail.setCustomerScore(0);
                break;
        }
    }
 
    private ExamPaperScore ExamPaperAnswerFromVM(ExamPaperSubmitVO examPaperSubmitVO, ExamPaper examPaper, List<ExamPaperScoreDetail> examPaperScoreDetails, User user, Date now) {
        Integer systemScore = examPaperScoreDetails.stream().mapToInt(a -> a.getCustomerScore()).sum();
        long questionCorrect = examPaperScoreDetails.stream().filter(a -> a.getCustomerScore().equals(a.getQuestionScore())).count();
        ExamPaperScore examPaperScore = new ExamPaperScore();
        //TODO:待完成
//        examPaperAnswer.setPaperName(examPaper.getName());
//        examPaperAnswer.setDoTime(examPaperSubmitVO.getDoTime());
//        examPaperAnswer.setExamPaperId(examPaper.getId());
//        examPaperAnswer.setCreateUser(user.getId());
//        examPaperAnswer.setCreateTime(now);
//        examPaperAnswer.setSubjectId(examPaper.getSubjectId());
//        examPaperAnswer.setQuestionCount(examPaper.getQuestionCount());
//        examPaperAnswer.setPaperScore(examPaper.getScore());
//        examPaperAnswer.setPaperType(examPaper.getPaperType());
//        examPaperAnswer.setSystemScore(systemScore);
//        examPaperAnswer.setUserScore(systemScore);
//        examPaperAnswer.setTaskExamId(examPaper.getTaskExamId());
//        examPaperAnswer.setQuestionCorrect((int) questionCorrect);
//        boolean needJudge = examPaperQuestionCustomerAnswers.stream().anyMatch(d -> QuestionTypeEnum.needSaveTextContent(d.getQuestionType()));
//        if (needJudge) {
//            examPaperAnswer.setStatus(ExamPaperAnswerStatusEnum.WaitJudge.getCode());
//        } else {
//            examPaperAnswer.setStatus(ExamPaperAnswerStatusEnum.Complete.getCode());
//        }
        return examPaperScore;
    }
 
 
    @Override
    public PageInfo<ExamPaperAnswerPageResponseVO> adminPage(ExamPaperAnswerPageRequestVO requestVM) {
        return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize()).doSelectPageInfo(() ->
                examPaperScoreMapper.adminPage(requestVM));
    }
 
    @Override
    public List<ExamPaperAnswerPageResponseVO> list(ExamPaperAnswerPageRequestVO requestVM) {
        return examPaperScoreMapper.adminPage(requestVM);
    }
 
    @Override
    public PageInfo<ExamPaperAnswerPageResponseVO> pageExamPaper(ExamPaperAnswerPageRequestVO model) {
        return PageHelper.startPage(model.getPageIndex(), model.getPageSize()).doSelectPageInfo(() ->
                examPaperScoreMapper.pageExamPaper(model));
    }
 
    @Override
    public PageInfo<UserAnswerPageResponseVO> pageUser(UserAnswerPageRequestVO model) {
        return PageHelper.startPage(model.getPageIndex(), model.getPageSize()).doSelectPageInfo(() ->
                examPaperScoreMapper.pageUser(model));
    }
 
    @Override
    public Result queryMaxAndMinScore(Integer id) {
        List<ExamPaperScore> list = new ArrayList<>();
        ExamPaperScore max = examPaperScoreMapper.selectOne(new LambdaQueryWrapper<>(ExamPaperScore.class)
                .eq(ExamPaperScore::getExamId, id)
                .orderByDesc(ExamPaperScore::getTotalScore)
                .last("limit 1"));
        list.add(max);
        if (null != max) {
            ExamPaperScore min = examPaperScoreMapper.selectOne(new LambdaQueryWrapper<>(ExamPaperScore.class)
                    .eq(ExamPaperScore::getExamId, id)
                    .orderByAsc(ExamPaperScore::getTotalScore)
                    .last("limit 1"));
            list.add(min);
        }
        return Result.ok(list);
    }
}