xiangpei
2024-05-14 d3d1235bab219b382a5bebfe34edabbba52d151c
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
package com.mindskip.xzs.service.impl;
 
import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.context.WebContext;
import com.mindskip.xzs.domain.ExamPaperAnswer;
import com.mindskip.xzs.domain.Question;
import com.mindskip.xzs.domain.SelfPractice;
import com.mindskip.xzs.domain.enums.PracticeQuestionTypeEnum;
import com.mindskip.xzs.domain.enums.PracticeTypeEnum;
import com.mindskip.xzs.domain.enums.QuestionTypeEnum;
import com.mindskip.xzs.domain.vo.QuestionContentVO;
import com.mindskip.xzs.domain.vo.QuestionVO;
import com.mindskip.xzs.domain.vo.SelfPracticeVO;
import com.mindskip.xzs.domain.vo.SubjectQuestionVO;
import com.mindskip.xzs.repository.QuestionMapper;
import com.mindskip.xzs.repository.QuestionSubjectMapper;
import com.mindskip.xzs.repository.SelfPracticeMapper;
import com.mindskip.xzs.repository.SubjectMapper;
import com.mindskip.xzs.service.SelfPracticeService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * @author:xp
 * @date:2024/5/9 16:16
 */
@Service
@RequiredArgsConstructor
public class SelfPracticeServiceImpl implements SelfPracticeService {
 
    private final SelfPracticeMapper selfPracticeMapper;
    private final WebContext webContext;
    private final QuestionSubjectMapper questionSubjectMapper;
    private final SubjectMapper subjectMapper;
    private final QuestionMapper questionMapper;
 
    @Override
    public RestResponse add(SelfPracticeVO vo) {
        SelfPractice entity = new SelfPractice();
        BeanUtils.copyProperties(vo, entity);
        entity.setSubjects(JSON.toJSONString(vo.getSubjects()));
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setDeleted(0);
        entity.setDoNum(0);
        entity.setUserId(webContext.getCurrentUser().getId());
        selfPracticeMapper.add(entity);
        return RestResponse.ok("创建成功");
    }
 
    @Override
    public RestResponse page(SelfPracticeVO query) {
        if (! Objects.equals(3, webContext.getCurrentUser().getRole())) {
            query.setUserId(webContext.getCurrentUser().getId());
        }
        PageInfo<SelfPracticeVO> info = PageHelper.startPage(query.getPageNum(), query.getPageSize()).doSelectPageInfo(() ->
                selfPracticeMapper.page(query));
        info.getList().stream().forEach(item -> {
            List<Integer> subjects = JSON.parseArray(item.getSubjectString(), Integer.class);
            item.setSubjects(subjects);
            List<String> subjectNames = subjectMapper.selectSubjectName(subjects);
            if (! CollectionUtils.isEmpty(subjectNames)) {
                item.setSubjectNames(subjectNames.stream().collect(Collectors.joining("、")));
            }
            item.setTotalQuestionNum(questionSubjectMapper.countQuestionNum(subjects));
        });
        return RestResponse.ok(info);
    }
 
    @Override
    public RestResponse remove(List<Integer> ids) {
        selfPracticeMapper.remove(ids);
        return RestResponse.ok("删除成功");
    }
 
    @Override
    public RestResponse startPractice(Integer id) {
        SelfPractice en = selfPracticeMapper.selectById(id);
        if (Objects.isNull(en)) {
            throw new RuntimeException("练习不存在");
        }
        List<Integer> subjectIds = JSON.parseArray(en.getSubjects(), Integer.class);
        if (PracticeTypeEnum.ORDERED.getValue().equals(en.getPracticeType())) {
            List<SubjectQuestionVO> list = new ArrayList<>(2);
            // 顺序做题,把选择的题库的题(id)全部查出来,前端有个序号面板,点击哪道题做哪道
            for (Integer subjectId : subjectIds) {
                String subjectName = subjectMapper.selectSubjectNameById(subjectId);
                // todo根据做题记录查询做没做过
                List<Integer> questionIds = new ArrayList<>();
                if (PracticeQuestionTypeEnum.ALL.getValue().equals(en.getQuestionType())) {
                    questionIds = questionSubjectMapper.questionsBySubjectId(subjectId);
                } else {
                    questionIds = questionSubjectMapper.questionsBySubjectIdAndQuestionType(subjectId, PracticeQuestionTypeEnum.getDataBaseValueByValue(en.getQuestionType()));
                }
                SubjectQuestionVO subjectQuestionVO = new SubjectQuestionVO();
                subjectQuestionVO.setSubjectId(subjectId);
                subjectQuestionVO.setSubjectName(subjectName);
                subjectQuestionVO.setQuestionIds(questionIds);
                list.add(subjectQuestionVO);
            }
            return RestResponse.ok(list);
        } else if (PracticeTypeEnum.RANDOM.getValue().equals(en.getPracticeType())) {
            // 随机练习,是一道题一道题练习
            List<QuestionVO> one = questionSubjectMapper.getRandomQuestionId(subjectIds, PracticeQuestionTypeEnum.getDataBaseValueByValue(en.getQuestionType()), 1);
            if (one.size() < 1) {
                throw new RuntimeException("没有找到题目,可能所选课目包含题目不足");
            }
            QuestionVO questionVO = one.get(0);
            jsonQuestion(questionVO);
            return RestResponse.ok(questionVO);
        }
        return RestResponse.ok();
    }
 
    @Override
    public RestResponse subjectQuestionNum(List<Integer> subjectIds) {
        Integer num = questionSubjectMapper.countQuestionNum(subjectIds);
        return RestResponse.ok(num);
    }
 
    @Override
    public RestResponse randomOneQuestion(Integer id) {
        SelfPractice en = selfPracticeMapper.selectById(id);
        if (Objects.isNull(en)) {
            throw new RuntimeException("练习不存在");
        }
        List<Integer> subjectIds = JSON.parseArray(en.getSubjects(), Integer.class);
        List<QuestionVO> one = questionSubjectMapper.getRandomQuestionId(subjectIds, PracticeQuestionTypeEnum.getDataBaseValueByValue(en.getQuestionType()), 1);
        if (one.size() < 1) {
            throw new RuntimeException("没有找到题目,可能所选课目包含题目不足");
        }
        QuestionVO questionVO = one.get(0);
        jsonQuestion(questionVO);
 
        return RestResponse.ok(questionVO);
    }
 
    /**
     * 处理题目内容JSON
     *
     * @param questionVO
     */
    public void jsonQuestion(QuestionVO questionVO) {
        if (StringUtils.hasText(questionVO.getContentJson())) {
            QuestionContentVO questionContent = JSON.parseObject(questionVO.getContentJson(), QuestionContentVO.class);
            questionVO.setContent(questionContent);
        }
        if (QuestionTypeEnum.MultipleChoice.getCode().equals(questionVO.getQuestionType())) {
            // 多选题需要返回答案数量,学员选中对应数量才查询答案
            if (StringUtils.hasText(questionVO.getCorrect())) {
                questionVO.setAnswerNum(questionVO.getCorrect().split(",").length);
            }
        }
        questionVO.setContentJson("");
        questionVO.setCorrect("");
        questionVO.getContent().setCorrect("");
        questionVO.getContent().setAnalyze("");
    }
}