无关风月
2024-07-10 c0ab5b660967876fecc88c81498820012c76b035
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
package com.ruoyi.study.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.study.domain.*;
import com.ruoyi.study.dto.StudyWeekDTO;
import com.ruoyi.study.mapper.TStudyMapper;
import com.ruoyi.study.service.*;
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 ITStudyAnswerService studyAnswerService;
    @Resource
    private ITStudyInductionService studyInductionService;
    @Resource
    private ITStudyLookService studyLookService;
    @Resource
    private ITStudyListenService studyListenService;
    @Resource
    private ITStudyPairService studyPairService;
    @Resource
    private ITGameService gameService;
    @Resource
    private ITStoryListenService storyListenService;
 
    private final static Map<Integer, 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, Integer userId) {
        List<StudyWeekDTO> result = new ArrayList<>();
        // 根据季度和type查询学习配置
        List<TStudy> study = lambdaQuery().eq(TStudy::getQuarter, quarter).eq(TStudy::getType, type)
                .eq(TStudy::getDisabled, 0).orderByAsc(TStudy::getWeek).list();
        for (TStudy item : study) {
            Integer week = item.getWeek();
            String title = item.getTitle();
            Integer id = item.getId();
            // 计算总积分
            int total = 0;
            List<Integer> dayList = new ArrayList<>();
            // 有问有答
            List<TStudyAnswer> answerList = studyAnswerService.lambdaQuery()
                    .eq(TStudyAnswer::getStudyId, id)
                    .eq(TStudyAnswer::getWeek, week)
                    .eq(TStudyAnswer::getDisabled, 0).list();
            for (TStudyAnswer data : answerList) {
                Integer day = data.getDay();
                if (!dayList.contains(day)) {
                    total += data.getIntegral();
                    dayList.add(day);
                }
            }
            dayList.clear();
            // 归纳判断
            List<TStudyInduction> inductionList = studyInductionService.lambdaQuery()
                    .eq(TStudyInduction::getStudyId, id)
                    .eq(TStudyInduction::getWeek, week)
                    .eq(TStudyInduction::getDisabled, 0).list();
            for (TStudyInduction data : inductionList) {
                Integer day = data.getDay();
                if (!dayList.contains(day)) {
                    total += data.getIntegral();
                    dayList.add(day);
                }
            }
            dayList.clear();
            // 看图选音
            List<TStudyLook> lookList = studyLookService.lambdaQuery()
                    .eq(TStudyLook::getStudyId, id)
                    .eq(TStudyLook::getWeek, week)
                    .eq(TStudyLook::getDisabled, 0).list();
            for (TStudyLook data : lookList) {
                Integer day = data.getDay();
                if (!dayList.contains(day)) {
                    total += data.getIntegral();
                    dayList.add(day);
                }
            }
            dayList.clear();
            // 听音选图
            List<TStudyListen> listenList = studyListenService.lambdaQuery()
                    .eq(TStudyListen::getStudyId, id)
                    .eq(TStudyListen::getWeek, week)
                    .eq(TStudyListen::getDisabled, 0).list();
            for (TStudyListen data : listenList) {
                Integer day = data.getDay();
                if (!dayList.contains(day)) {
                    total += data.getIntegral();
                    dayList.add(day);
                }
            }
            dayList.clear();
            // 音图相配
            List<TStudyPair> pairList = studyPairService.lambdaQuery()
                    .eq(TStudyPair::getStudyId, id)
                    .eq(TStudyPair::getWeek, week)
                    .eq(TStudyPair::getDisabled, 0).list();
            for (TStudyPair data : pairList) {
                Integer day = data.getDay();
                if (!dayList.contains(day)) {
                    total += data.getIntegral();
                    dayList.add(day);
                }
            }
            dayList.clear();
            // 自主游戏
            List<TGame> gameList = gameService.lambdaQuery().eq(TGame::getStudyId, id)
                    .eq(TGame::getDisabled, 0)
                    .eq(TGame::getWeek, week).list();
            // 自主游戏-超级听力
            List<String> gameIntegral = gameList.stream().map(TGame::getIntegral).collect(Collectors.toList());
            for (String s : gameIntegral) {
                List<String> list = Arrays.stream(s.split(",")).collect(Collectors.toList());
                total += list.stream().mapToInt(Integer::parseInt).sum();
            }
            // 自主游戏-框架记忆
            List<Integer> gameAnswerIntegral = gameList.stream().map(TGame::getAnswerIntegral).collect(Collectors.toList());
            for (Integer integral : gameAnswerIntegral) {
                if (null != integral) {
                    total += integral;
                }
            }
            // 自主故事
            List<TStoryListen> storyListenList = storyListenService.lambdaQuery()
                    .eq(TStoryListen::getStudyId, id)
                    .eq(TStoryListen::getWeek, week)
                    .eq(TStoryListen::getDisabled, 0).list();
            // 自主故事 - 框架记忆
            total += storyListenList.stream().map(TStoryListen::getIntegral).mapToInt(Integer::intValue).sum();
            // 自主故事 - 看图配音
            total += storyListenList.stream().map(TStoryListen::getLookIntegral).mapToInt(Integer::intValue).sum();
            // 判断周目是否可以进入学习
            Boolean canStudy = checkWeekCanStudy(userId, item);
            result.add(new StudyWeekDTO(week, type, quarter, title, total, canStudy));
        }
        return result;
    }
 
    @Override
    public TUserStudy studySchedule(TUserStudy studyRecord, Integer week) {
        // 剩余周目
        List<TStudy> studyList = this.lambdaQuery().eq(TStudy::getDisabled, 0)
                .eq(TStudy::getType, Constants.ONE)
                .orderByAsc(TStudy::getWeek).list();
        // 已学习到的周目
        Integer studyWeek = studyRecord.getWeek();
        // 学习到的季度
        Integer studyQuarter = 1;
        // 当前所点击进入的周目所属季度
        Integer currentQuarter = 1;
        // 根据季度分组封装
        Map<Integer, List<TStudy>> integerListMap = getIntegerListMap(studyList);
        for (Map.Entry<Integer, List<TStudy>> map : integerListMap.entrySet()) {
            List<TStudy> list = map.getValue();
            for (TStudy study : list) {
                Integer itemWeek = study.getWeek();
                if (studyWeek.equals(itemWeek)) {
                    studyQuarter = map.getKey();
                    break;
                }
            }
            for (TStudy study : list) {
                Integer itemWeek = study.getWeek();
                if (week.equals(itemWeek)) {
                    currentQuarter = map.getKey();
                    break;
                }
            }
        }
        // 已学习季度 大于当前周目所属季度,所有学习均已完成
        if (studyQuarter > currentQuarter) {
            studyRecord.setWeek(week);
            studyRecord.setDay(Constants.SIX);
            studyRecord.setListen(Constants.ONE_HUNDRED);
            studyRecord.setLook(Constants.ONE_HUNDRED);
            studyRecord.setInduction(Constants.ONE_HUNDRED);
            studyRecord.setAnswer(Constants.ONE_HUNDRED);
            studyRecord.setPair(Constants.ONE_HUNDRED);
            studyRecord.setGameDifficulty(Constants.TWO);
            studyRecord.setComputeSchedule(Constants.ONE_HUNDRED);
        } else if (studyQuarter < currentQuarter) {
            studyRecord.setWeek(week);
            studyRecord.setDay(Constants.ONE);
            studyRecord.setListen(Constants.BURDEN_ONE);
            studyRecord.setLook(Constants.BURDEN_ONE);
            studyRecord.setInduction(Constants.BURDEN_ONE);
            studyRecord.setAnswer(Constants.BURDEN_ONE);
            studyRecord.setPair(Constants.BURDEN_ONE);
            studyRecord.setGameDifficulty(null);
            studyRecord.setComputeSchedule(Constants.ZERO);
        } else {
            List<TStudy> tStudies = integerListMap.get(studyQuarter);
            List<Integer> weekList = tStudies.stream().map(TStudy::getWeek).collect(Collectors.toList());
            int i = weekList.indexOf(studyWeek);
            int i1 = weekList.indexOf(week);
            if (i > i1) {
                studyRecord.setWeek(week);
                studyRecord.setDay(Constants.SIX);
                studyRecord.setListen(Constants.ONE_HUNDRED);
                studyRecord.setLook(Constants.ONE_HUNDRED);
                studyRecord.setInduction(Constants.ONE_HUNDRED);
                studyRecord.setAnswer(Constants.ONE_HUNDRED);
                studyRecord.setPair(Constants.ONE_HUNDRED);
                studyRecord.setGameDifficulty(Constants.TWO);
                studyRecord.setComputeSchedule(Constants.ONE_HUNDRED);
            } else if (i < i1) {
                studyRecord.setWeek(week);
                studyRecord.setDay(Constants.ONE);
                studyRecord.setListen(Constants.BURDEN_ONE);
                studyRecord.setLook(Constants.BURDEN_ONE);
                studyRecord.setInduction(Constants.BURDEN_ONE);
                studyRecord.setAnswer(Constants.BURDEN_ONE);
                studyRecord.setPair(Constants.BURDEN_ONE);
                studyRecord.setGameDifficulty(null);
                studyRecord.setComputeSchedule(Constants.ZERO);
            }
        }
        return studyRecord;
    }
 
    /**
     * @param userId 用户id
     * @return 当前周目是否学习
     */
    private Boolean checkWeekCanStudy(Integer userId, TStudy study) {
        TUserStudy userStudy = userStudyService.lambdaQuery().eq(TUserStudy::getUserId, userId)
                .eq(TUserStudy::getDisabled, 0).one();
        if (null == userStudy) {
            userStudy = new TUserStudy();
            userStudy.setUserId(userId);
            // 学习周目
            TStudy tStudy = this.lambdaQuery().eq(TStudy::getQuarter, Constants.ONE)
                    .orderByAsc(TStudy::getWeek).last("limit 1").one();
            userStudy.setWeek(tStudy.getWeek());
            userStudy.setDay(Constants.ONE);
            userStudy.setTotalStudy(Constants.ZERO);
            userStudy.setTodayStudy(Constants.ZERO);
            userStudy.setWeekStudy(Constants.ZERO);
            userStudy.setMonthStudy(Constants.ZERO);
            userStudy.setListen(Constants.BURDEN_ONE);
            userStudy.setLook(Constants.BURDEN_ONE);
            userStudy.setInduction(Constants.BURDEN_ONE);
            userStudy.setAnswer(Constants.BURDEN_ONE);
            userStudy.setPair(Constants.BURDEN_ONE);
            userStudyService.save(userStudy);
        }
        // 学习记录所属周目大于当前学习周目,判断为已学习
        return userStudy.getWeek() >= study.getWeek();
    }
 
    @Override
    public void checkDifficulty(Integer difficulty, Integer week, TGame game, Integer userid) {
        // 判断用户是否完成上一个等级
        if (!Constants.ZERO.equals(difficulty)) {
            Integer level = GAME_DIFFICULTY_MAP.get(difficulty);
            if (null == level) {
                throw new GlobalException("游戏等级异常,请重试!");
            }
            // 游戏通关率
            String clearanceRate = game.getRate().split(",")[level];
            // 获取用户游戏进度
            List<TGameRecord> list = gameRecordService.lambdaQuery().eq(TGameRecord::getUserId, userid)
                    .eq(TGameRecord::getGameId, game.getId())
                    .ge(TGameRecord::getAccuracy, clearanceRate)
                    .eq(TGameRecord::getGameDifficulty, level).list();
            if (list.isEmpty()) {
                throw new GlobalException("请先完成上一难度再挑战当前难度!");
            }
        }
    }
 
    @Override
    public StudyListenResultVO listenSelectPicture(Integer week, Integer day, List<TStudyListen> studyListens) {
        if (studyListens.isEmpty()) {
            throw new GlobalException("当前学习周目题目数量不足!");
        }
        // 题组信息
        LearnStudyVO learnStudy = new LearnStudyVO();
        String ids = studyListens.stream().map(TStudyListen::getId).map(String::valueOf)
                .collect(Collectors.joining(","));
        learnStudy.setId(ids);
        int total = 0;
        for (TStudyListen data : studyListens) {
            if (null != data) {
                total += data.getIntegral();
            }
        }
        learnStudy.setIntegral(total);
        // 语音及图片
        List<List<TSubject>> subjectList = new ArrayList<>();
        for (TStudyListen studyListen : studyListens) {
            List<String> subjectIds = Arrays.stream(studyListen.getSubject().split(",")).collect(Collectors.toList());
            List<TSubject> list = new ArrayList<>();
            // 图片及语音集合
            for (String id : subjectIds) {
                TSubject data = subjectService.lambdaQuery().eq(TSubject::getId, id)
                        .eq(TSubject::getDisabled, 0).one();
                list.add(data);
            }
            subjectList.add(list);
        }
        return new StudyListenResultVO(learnStudy, subjectList);
    }
 
    @Override
    public StudyLookResultVO pictureSelectVoice(Integer week, Integer day, List<TStudyLook> lookList) {
        if (lookList.isEmpty()) {
            throw new GlobalException("当前学习周目题目数量不足!");
        }
        // 题组信息
        LearnStudyVO learnStudy = new LearnStudyVO();
        String ids = lookList.stream().map(TStudyLook::getId).map(String::valueOf)
                .collect(Collectors.joining(","));
        learnStudy.setId(ids);
        int total = 0;
        for (TStudyLook data : lookList) {
            if (null != data) {
                total += data.getIntegral();
            }
        }
        learnStudy.setIntegral(total);
        // 语音及图片
        List<List<TSubject>> subjectList = new ArrayList<>();
        for (TStudyLook studyLook : lookList) {
            List<String> sortList = Arrays.stream(studyLook.getSort().split(",")).collect(Collectors.toList());
            List<String> subjectIds = Arrays.stream(studyLook.getSubject().split(",")).collect(Collectors.toList());
            List<TSubject> list = new ArrayList<>();
            // 图片及语音集合
            for (int i = 0; i < subjectIds.size(); i++) {
                TSubject data = subjectService.lambdaQuery().eq(TSubject::getId, subjectIds.get(i))
                        .eq(TSubject::getDisabled, 0).one();
                data.setSort(Integer.parseInt(sortList.get(i)));
                list.add(data);
            }
            // 根据顺序排序
            list.sort(Comparator.comparingInt(TSubject::getSort));
            subjectList.add(list);
        }
        return new StudyLookResultVO(learnStudy, subjectList);
    }
 
    @Override
    public StudyInductionResultVO induceExclude(Integer week, Integer day, List<TStudyInduction> inductionList) {
        if (inductionList.isEmpty()) {
            throw new GlobalException("当前学习周目题目数量不足!");
        }
        // 题组信息
        LearnStudyVO learnStudy = new LearnStudyVO();
        String ids = inductionList.stream().map(TStudyInduction::getId).map(String::valueOf)
                .collect(Collectors.joining(","));
        learnStudy.setId(ids);
        int total = 0;
        for (TStudyInduction data : inductionList) {
            if (null != data) {
                total += data.getIntegral();
            }
        }
        learnStudy.setIntegral(total);
        // 语音及图片
        List<List<TSubject>> subjectList = new ArrayList<>();
        for (TStudyInduction data : inductionList) {
            List<String> subjectIds = Arrays.stream(data.getSubject().split(",")).collect(Collectors.toList());
            List<TSubject> subjectLists = new ArrayList<>();
            // 第一组题 固定下标为0,1,2的题
            for (int i = 0; i < Constants.THREE; i++) {
                String id = subjectIds.get(i);
                if (!id.startsWith("-")) {
                    subjectLists.add(subjectService.getById(id));
                }
            }
            for (int i = 0; i < Constants.THREE; i++) {
                String id = subjectIds.get(i);
                if (id.startsWith("-")) {
                    id = id.replace("-", "");
                    subjectLists.add(subjectService.getById(id));
                }
            }
            // 第二组题,固定下标为3,4的题
            for (int i = Constants.THREE; i < Constants.FIVE; i++) {
                String id = subjectIds.get(i);
                if (!id.startsWith("-")) {
                    subjectLists.add(subjectService.getById(id));
                }
            }
            for (int i = Constants.THREE; i < Constants.FIVE; i++) {
                String id = subjectIds.get(i);
                if (id.startsWith("-")) {
                    id = id.replace("-", "");
                    subjectLists.add(subjectService.getById(id));
                }
            }
            subjectLists.add(subjectService.getById(subjectIds.get(subjectIds.size() - 1).replace("-", "")));
            subjectList.add(subjectLists);
        }
        return new StudyInductionResultVO(learnStudy, subjectList);
    }
 
    @Override
    public StudyAnswerResultVO questionsAndAnswers(Integer week, Integer day, List<TStudyAnswer> answerList) {
        if (answerList.isEmpty()) {
            throw new GlobalException("当前学习周目题目数量不足!");
        }
        // 题组信息
        LearnStudyVO learnStudy = new LearnStudyVO();
        String ids = answerList.stream().map(TStudyAnswer::getId).map(String::valueOf)
                .collect(Collectors.joining(","));
        learnStudy.setId(ids);
        int total = 0;
        for (TStudyAnswer data : answerList) {
            if (null != data) {
                total += data.getIntegral();
            }
        }
        learnStudy.setIntegral(total);
        // 题目语音及图片信息
        List<List<QuestionsAnswersSubjectVO>> subjectList = new ArrayList<>();
        for (int i = 0; i < answerList.size(); i += Constants.TWO) {
            List<QuestionsAnswersSubjectVO> voList = new ArrayList<>();
            // 一组题目为四道题,
            TStudyAnswer one = answerList.get(i);
            TStudyAnswer two = answerList.get(i + 1);
            voAdd(voList, one);
            voAdd(voList, two);
            subjectList.add(voList);
        }
        return new StudyAnswerResultVO(learnStudy, subjectList);
    }
 
    private void voAdd(List<QuestionsAnswersSubjectVO> voList, TStudyAnswer one) {
        // 第一道题问题及答案
        Integer subject = one.getSubject();
        Integer answerSubject = one.getAnswerSubject();
        // 问题题目信息
        QuestionsAnswersSubjectVO oneVO = new QuestionsAnswersSubjectVO();
        TSubject one1 = subjectService.lambdaQuery().eq(TSubject::getId, subject)
                .eq(TSubject::getDisabled, 0).one();
        copyProperties(one1, oneVO);
        // 回答题目信息
        QuestionsAnswersSubjectVO twoVO = new QuestionsAnswersSubjectVO();
        TSubject two1 = subjectService.lambdaQuery().eq(TSubject::getId, answerSubject)
                .eq(TSubject::getDisabled, 0).one();
        copyProperties(two1, twoVO);
        // 判断第一组题目的问题题目及回答题目,哪个是答案
        if (Constants.ZERO.equals(one.getIsAnswer())) {
            oneVO.setIsQuestion(1);
            twoVO.setIsQuestion(0);
        } else {
            oneVO.setIsQuestion(0);
            twoVO.setIsQuestion(1);
        }
        voList.add(oneVO);
        voList.add(twoVO);
    }
 
    private void copyProperties(TSubject subject, QuestionsAnswersSubjectVO vo) {
        vo.setId(subject.getId());
        vo.setName(subject.getName());
        vo.setEnglish(subject.getEnglish());
        vo.setType(subject.getType());
        vo.setState(subject.getState());
        vo.setImg(subject.getImg());
        vo.setCorrect(subject.getCorrect());
        vo.setError(subject.getError());
        vo.setSort(subject.getSort());
    }
 
    @Override
    public StudyPairResultVO pictureMateVoice(Integer week, Integer day, List<TStudyPair> pair) {
        if (pair.isEmpty()) {
            throw new GlobalException("当前学习周目题目数量不足!");
        }
        // 题组信息
        LearnStudyVO learnStudy = new LearnStudyVO();
        String ids = pair.stream().map(TStudyPair::getId).map(String::valueOf)
                .collect(Collectors.joining(","));
        learnStudy.setId(ids);
        int total = 0;
        for (TStudyPair data : pair) {
            if (null != data) {
                total += data.getIntegral();
            }
        }
        learnStudy.setIntegral(total);
        // 语音及图片
        List<List<TSubject>> subjectList = new ArrayList<>();
        for (TStudyPair data : pair) {
            List<String> subjectIds = Arrays.stream(data.getSubject().split(",")).collect(Collectors.toList());
            List<TSubject> subjectLists = new ArrayList<>();
            for (String id : subjectIds) {
                if (id.startsWith("-")) {
                    id = id.replace("-", "");
                }
                subjectLists.add(subjectService.getById(id));
            }
            subjectList.add(subjectLists);
        }
        return new StudyPairResultVO(learnStudy, subjectList);
    }
 
    @Override
    public int computeSchedule(TUserStudy result, Integer week) {
        List<TStudy> studyList = this.lambdaQuery().eq(TStudy::getDisabled, 0)
                .eq(TStudy::getType, 1)
                .orderByAsc(TStudy::getWeek).list();
        // 基础学习进度
        Integer day = result.getDay();
        Integer studyWeek = result.getWeek();
        Map<Integer, List<TStudy>> studyMap = new HashMap<>(8);
        for (TStudy study : studyList) {
            Integer quarter = study.getQuarter();
            List<TStudy> itemList = studyMap.get(quarter);
            if (null == itemList) {
                itemList = new ArrayList<>();
            }
            itemList.add(study);
            studyMap.put(quarter, itemList);
        }
        // 学习进度所属季度
        int studyQuarter = 1;
        // 当前进入周目所属季度
        int thisQuarter = 1;
        for (Map.Entry<Integer, List<TStudy>> map : studyMap.entrySet()) {
            Integer key = map.getKey();
            List<TStudy> list = map.getValue();
            List<Integer> collect = list.stream().map(TStudy::getWeek).collect(Collectors.toList());
            if (collect.contains(studyWeek)) {
                studyQuarter = key;
            }
            if (collect.contains(week)) {
                thisQuarter = key;
            }
        }
        // 默认进度为 0
        int defaultSchedule;
        // 季度判断
        if (studyQuarter > thisQuarter) {
            defaultSchedule = 100;
        } else if (studyQuarter < thisQuarter) {
            defaultSchedule = 0;
        } else {
            List<TStudy> tStudies = studyMap.get(studyQuarter);
            List<Integer> weekList = tStudies.stream().map(TStudy::getWeek).collect(Collectors.toList());
            int studyIndex = weekList.indexOf(studyWeek);
            int weekIndex = weekList.indexOf(week);
            if (studyIndex > weekIndex) {
                defaultSchedule = 100;
            } else if (studyIndex < weekIndex) {
                defaultSchedule = 0;
            } else {
                // 根据day初始化学习进度
                if (Constants.ONE.equals(day)) {
                    defaultSchedule = 0;
                } else if (Constants.TWO.equals(day)) {
                    defaultSchedule = 20;
                } else if (Constants.THREE.equals(day)) {
                    defaultSchedule = 40;
                } else if (Constants.FOUR.equals(day)) {
                    defaultSchedule = 60;
                } else if (Constants.FIVE.equals(day)) {
                    defaultSchedule = 80;
                } else {
                    defaultSchedule = 0;
                }
                // 根据五种学习计算进度
                Integer listen = result.getListen();
                if (!Constants.BURDEN_ONE.equals(listen)) {
                    defaultSchedule += (int) (((double) listen / 100) * 4);
                }
                Integer look = result.getLook();
                if (!Constants.BURDEN_ONE.equals(look)) {
                    defaultSchedule += (int) (((double) look / 100) * 4);
                }
                Integer induction = result.getInduction();
                if (!Constants.BURDEN_ONE.equals(induction)) {
                    defaultSchedule += (int) (((double) induction / 100) * 4);
                }
                Integer answer = result.getAnswer();
                if (!Constants.BURDEN_ONE.equals(answer)) {
                    defaultSchedule += (int) (((double) answer / 100) * 4);
                }
                Integer pair = result.getPair();
                if (!Constants.BURDEN_ONE.equals(pair)) {
                    defaultSchedule += (int) (((double) pair / 100) * 4);
                }
            }
        }
        // week以超过当前week,进度为 100%
        /*if (itemBool) {
            defaultSchedule = 100;
        } else {
            // 根据day初始化学习进度
            if (Constants.ONE.equals(day)) {
                defaultSchedule = 0;
            } else if (Constants.TWO.equals(day)) {
                defaultSchedule = 20;
            } else if (Constants.THREE.equals(day)) {
                defaultSchedule = 40;
            } else if (Constants.FOUR.equals(day)) {
                defaultSchedule = 60;
            } else if (Constants.FIVE.equals(day)) {
                defaultSchedule = 80;
            } else {
                defaultSchedule = 0;
            }
            // 根据五种学习计算进度
            Integer listen = result.getListen();
            if (!Constants.BURDEN_ONE.equals(listen)) {
                defaultSchedule += (int) (((double) listen / 100) * 4);
            }
            Integer look = result.getLook();
            if (!Constants.BURDEN_ONE.equals(look)) {
                defaultSchedule += (int) (((double) look / 100) * 4);
            }
            Integer induction = result.getInduction();
            if (!Constants.BURDEN_ONE.equals(induction)) {
                defaultSchedule += (int) (((double) induction / 100) * 4);
            }
            Integer answer = result.getAnswer();
            if (!Constants.BURDEN_ONE.equals(answer)) {
                defaultSchedule += (int) (((double) answer / 100) * 4);
            }
            Integer pair = result.getPair();
            if (!Constants.BURDEN_ONE.equals(pair)) {
                defaultSchedule += (int) (((double) pair / 100) * 4);
            }
        }*/
        return defaultSchedule;
    }
 
    @Override
    public int computeTotalIntegral(List<String> studyIds, Integer type, Integer accuracy) {
        int sum = 0;
        if (Constants.ONE.equals(type)) {
            List<TStudyListen> list = studyListenService.lambdaQuery().in(TStudyListen::getId, studyIds)
                    .eq(TStudyListen::getDisabled, 0).list();
            Optional<TStudyListen> any = list.stream().findAny();
            if (any.isPresent()) {
                sum = any.get().getIntegral();
            }
        } else if (Constants.TWO.equals(type)) {
            List<TStudyLook> list = studyLookService.lambdaQuery().in(TStudyLook::getId, studyIds)
                    .eq(TStudyLook::getDisabled, 0).list();
            Optional<TStudyLook> any = list.stream().findAny();
            if (any.isPresent()) {
                sum = any.get().getIntegral();
            }
        } else if (Constants.THREE.equals(type)) {
            List<TStudyInduction> list = studyInductionService.lambdaQuery().in(TStudyInduction::getId, studyIds)
                    .eq(TStudyInduction::getDisabled, 0).list();
            Optional<TStudyInduction> any = list.stream().findAny();
            if (any.isPresent()) {
                sum = any.get().getIntegral();
            }
        } else if (Constants.FOUR.equals(type)) {
            List<TStudyAnswer> list = studyAnswerService.lambdaQuery().in(TStudyAnswer::getId, studyIds)
                    .eq(TStudyAnswer::getDisabled, 0).list();
            Optional<TStudyAnswer> any = list.stream().findAny();
            if (any.isPresent()) {
                sum = any.get().getIntegral();
            }
        } else if (Constants.FIVE.equals(type)) {
            List<TStudyPair> list = studyPairService.lambdaQuery().in(TStudyPair::getId, studyIds)
                    .eq(TStudyPair::getDisabled, 0).list();
            Optional<TStudyPair> any = list.stream().findAny();
            if (any.isPresent()) {
                sum = any.get().getIntegral();
            }
        } else {
            throw new GlobalException("题目信息异常!");
        }
        if (accuracy >= 100) {
            return sum;
        } else {
            return (int) (sum * ((double) accuracy / 100));
        }
    }
 
    @Override
    public int residueWeek(TUserStudy studyRecord, List<TStudy> studyList) {
        // 已学习周目
        int residueWeek = 0;
        // 已学习到的周目
        Integer studyWeek = studyRecord.getWeek();
        // 根据季度分组封装
        Map<Integer, List<TStudy>> itemMap = getIntegerListMap(studyList);
        // 计算已学习周目
        boolean v = false;
        for (Map.Entry<Integer, List<TStudy>> map : itemMap.entrySet()) {
            List<TStudy> list = map.getValue();
            for (int i = 0; i < list.size(); i++) {
                TStudy item = list.get(i);
                if (item.getWeek().equals(studyWeek)) {
                    Integer listen = studyRecord.getListen();
                    Integer answer = studyRecord.getAnswer();
                    Integer look = studyRecord.getLook();
                    Integer induction = studyRecord.getInduction();
                    Integer pair = studyRecord.getPair();
                    Integer gameDifficulty = studyRecord.getGameDifficulty();
                    // 听音选图、看图选音、音图相配、有问有答、归纳排除的进度是否 100%,并且超级听力的游戏难度是否为2
                    boolean isStudy = Constants.ONE_HUNDRED.equals(listen) && Constants.ONE_HUNDRED.equals(look) &&
                            Constants.ONE_HUNDRED.equals(induction) && Constants.ONE_HUNDRED.equals(pair) &&
                            Constants.ONE_HUNDRED.equals(answer) && Constants.TWO.equals(gameDifficulty);
                    // 并且超级听力难度2已通过
                    TStudy study = this.lambdaQuery().eq(TStudy::getWeek, studyRecord.getWeek())
                            .eq(TStudy::getDisabled, 0)
                            .one();
                    TGame game = gameService.lambdaQuery()
                            .eq(TGame::getStudyId, study.getId())
                            .eq(TGame::getDisabled, 0)
                            .eq(TGame::getWeek, study.getWeek())
                            .one();
                    if (null != game) {
                        String rate = game.getRate().split(",")[2];
                        List<TGameRecord> gameRecordList = gameRecordService.lambdaQuery()
                                .eq(TGameRecord::getGameDifficulty, Constants.TWO)
                                .ge(TGameRecord::getAccuracy, rate)
                                .eq(TGameRecord::getUserId, studyRecord.getUserId())
                                .eq(TGameRecord::getGameId, game.getId())
                                .list();
                        if (gameRecordList.isEmpty()) {
                            isStudy = false;
                        }
                        List<TGameRecord> recordList = gameRecordService.lambdaQuery()
                                .ge(TGameRecord::getAccuracy, game.getAnswerRate())
                                .eq(TGameRecord::getUserId, studyRecord.getUserId())
                                .eq(TGameRecord::getGameId, game.getId())
                                .last("and gameDifficulty is null")
                                .list();
                        if (recordList.isEmpty()) {
                            isStudy = false;
                        }
                    }
                    if (isStudy) {
                        residueWeek++;
                    }
                    v = true;
                    break;
                } else {
                    residueWeek++;
                }
            }
            if (v) {
                break;
            }
        }
        return studyList.size() - residueWeek;
    }
 
    private Map<Integer, List<TStudy>> getIntegerListMap(List<TStudy> studyList) {
        Map<Integer, List<TStudy>> studyMap = new HashMap<>(8);
        for (TStudy study : studyList) {
            Integer quarter = study.getQuarter();
            List<TStudy> itemList = studyMap.get(quarter);
            if (null == itemList) {
                itemList = new ArrayList<>();
            }
            itemList.add(study);
            studyMap.put(quarter, itemList);
        }
        // 顺序排序
        Map<Integer, List<TStudy>> itemMap = new HashMap<>(8);
        List<Integer> keyList = new ArrayList<>();
        for (Map.Entry<Integer, List<TStudy>> map : studyMap.entrySet()) {
            Integer key = map.getKey();
            keyList.add(key);
        }
        Collections.sort(keyList);
        for (Integer key : keyList) {
            List<TStudy> itemList = studyMap.get(key);
            itemMap.put(key, itemList);
        }
        return itemMap;
    }
 
    @Override
    public void checkClearance(TGame game, Integer userid) {
        String rate = game.getRate().split(",")[Constants.TWO];
        // 获取用户游戏进度
        List<TGameRecord> list = gameRecordService.lambdaQuery().eq(TGameRecord::getUserId, userid)
                .eq(TGameRecord::getGameId, game.getId())
                .ge(TGameRecord::getAccuracy, rate)
                .eq(TGameRecord::getGameDifficulty, Constants.TWO).list();
        if (list.isEmpty()) {
            throw new GlobalException("超级听力暂未通关!");
        }
    }
 
}