hjl
2024-06-06 81714ac84eb9cf515c0dbf701b5b87d02bafb6bd
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
package com.ruoyi.study.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.study.domain.*;
import com.ruoyi.study.dto.StudyWeekDTO;
import com.ruoyi.study.mapper.TStudyMapper;
import com.ruoyi.study.service.ITGameRecordService;
import com.ruoyi.study.service.ITStudyService;
import com.ruoyi.study.service.ITSubjectService;
import com.ruoyi.study.service.ITUserStudyService;
import com.ruoyi.study.vo.*;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 学习类型 听 周目配置 服务实现类
 * </p>
 *
 * @author 无关风月
 * @since 2024-04-26
 */
@Service
public class TStudyServiceImpl extends ServiceImpl<TStudyMapper, TStudy> implements ITStudyService {
 
    @Resource
    private ITUserStudyService userStudyService;
    @Resource
    private ITSubjectService subjectService;
    @Resource
    private ITGameRecordService gameRecordService;
    @Resource
    private TokenService tokenService;
 
    private final static Map<String, Integer> GAME_DIFFICULTY_MAP = new HashMap<>();
 
    static {
        GAME_DIFFICULTY_MAP.put("1", 0);
        GAME_DIFFICULTY_MAP.put("2", 1);
    }
 
    @Override
    public List<StudyWeekDTO> weekList(Integer type, Integer quarter) {
        return baseMapper.weekList(type, quarter);
    }
 
    @Override
    public TUserStudy studySchedule(String userId, Integer week) {
        return userStudyService.studySchedule(userId, week);
    }
 
    @Override
    public void checkDifficulty(Integer difficulty, Integer week, TGame game) {
        // 判断用户是否完成上一个等级
        Integer level = GAME_DIFFICULTY_MAP.get(String.valueOf(difficulty));
        if (null == level) {
            throw new GlobalException("游戏等级异常,请重试!");
        }
        // 获取用户游戏进度
        Integer userId = tokenService.getLoginUserStudy().getUserid();
        List<TGameRecord> list = gameRecordService.lambdaQuery().eq(TGameRecord::getUserId, userId).eq(TGameRecord::getGameId, game.getId()).list();
        boolean contains = list.stream().map(TGameRecord::getGameDifficulty).collect(Collectors.toList()).contains(level);
        if (!contains) {
            throw new GlobalException("请先完成上一难度再挑战当前难度!");
        }
    }
 
    @Override
    public StudyListenResultVO listenSelectPicture(Integer week, Integer day, List<TStudyListen> studyListens) {
        if (studyListens.isEmpty()) {
            throw new GlobalException("当前学习周目题目数量不足!");
        }
        // 随机获取一组题
        Random rand = new Random();
        TStudyListen data;
        if (studyListens.size() == 1) {
            data = studyListens.get(0);
        } else {
            data = studyListens.get(rand.nextInt(studyListens.size()));
        }
        List<TSubject> subjectList = getSubjects(data.getSubject().split(","));
        return new StudyListenResultVO(data, subjectList);
    }
 
    @Override
    public StudyLookResultVO pictureSelectVoice(Integer week, Integer day, List<TStudyLook> lookList) {
        if (lookList.isEmpty()) {
            throw new GlobalException("当前学习周目题目数量不足!");
        }
        // 随机获取一组题
        Random rand = new Random();
        TStudyLook data;
        if (lookList.size() == 1) {
            data = lookList.get(0);
        } else {
            data = lookList.get(rand.nextInt(lookList.size()));
        }
        List<TSubject> subjectList = getSubjects(data.getSubject().split(","));
        return new StudyLookResultVO(data, subjectList);
    }
 
    @Override
    public StudyInductionResultVO induceExclude(Integer week, Integer day, List<TStudyInduction> inductionList) {
        if (inductionList.isEmpty()) {
            throw new GlobalException("当前学习周目题目数量不足!");
        }
        // 随机获取一组题
        Random rand = new Random();
        TStudyInduction data;
        if (inductionList.size() == 1) {
            data = inductionList.get(0);
        } else {
            data = inductionList.get(rand.nextInt(inductionList.size()));
        }
        String[] ids = data.getSubject().split(",");
        List<TSubject> subjectList = new ArrayList<>();
        for (String id : ids) {
            if (id.startsWith("-")) {
                id = id.replace("-", "");
            }
            subjectList.add(subjectService.getById(id));
        }
        return new StudyInductionResultVO(data, subjectList);
    }
 
    @Override
    public StudyAnswerResultVO questionsAndAnswers(Integer week, Integer day, List<TStudyAnswer> answerList) {
        if (answerList.isEmpty()) {
            throw new GlobalException("当前学习周目题目数量不足!");
        }
        // 随机获取一组题
        Random rand = new Random();
        TStudyAnswer data;
        TStudyAnswer dataTwo;
        if (answerList.size() == 1) {
            data = answerList.get(0);
            dataTwo = answerList.get(0);
        } else {
            data = answerList.get(rand.nextInt(answerList.size()));
            dataTwo = answerList.get(rand.nextInt(answerList.size()));
        }
        AnswerVO one = new AnswerVO();
        BeanUtils.copyProperties(data, one);
        answerList.remove(data);
        AnswerVO two = new AnswerVO();
        BeanUtils.copyProperties(dataTwo, two);
        // 获取问题题目 和 回答题目
        List<String> ids = new ArrayList<>();
        ids.add(String.valueOf(one.getSubject()));
        ids.add(String.valueOf(one.getAnswerSubject()));
        // 有问有答
        List<TStudyAnswer> answers = new ArrayList<>();
        answers.add(one);
        one.setSubjectList(getSubjects(ids.toArray(new String[0])));
        // 第二题信息
        List<String> twoIds = new ArrayList<>();
        answers.add(two);
        twoIds.add(String.valueOf(two.getSubject()));
        twoIds.add(String.valueOf(two.getAnswerSubject()));
        two.setSubjectList(getSubjects(twoIds.toArray(new String[0])));
        return new StudyAnswerResultVO(answers);
    }
 
    @Override
    public StudyPairResultVO pictureMateVoice(Integer week, Integer day, List<TStudyPair> pair) {
        if (pair.isEmpty()) {
            throw new GlobalException("当前学习周目题目数量不足!");
        }
        // 随机获取一组题
        Random rand = new Random();
        TStudyPair data;
        if (pair.size() == 1) {
            data = pair.get(0);
        } else {
            data = pair.get(rand.nextInt(pair.size()));
        }
        List<TSubject> subjectList = getSubjects(data.getSubject().split(","));
        return new StudyPairResultVO(data, subjectList);
    }
 
    /**
     * 根据参数id查询图片及语音
     *
     * @param ids 多个id
     * @return 图片及语音集合
     */
    private List<TSubject> getSubjects(String[] ids) {
        List<TSubject> list = new ArrayList<>();
        for (String id : ids) {
            TSubject data = subjectService.lambdaQuery().eq(TSubject::getId, id)
                    .eq(TSubject::getDisabled, 0).one();
            list.add(data);
        }
        return list;
    }
 
}