无关风月
2025-05-08 9486766c806fe1d9e082b2fd02ea1cc558f1b443
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
package com.dsh.communityWorldCup.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsh.communityWorldCup.entity.WorldCup;
import com.dsh.communityWorldCup.entity.WorldCupCompetitor;
import com.dsh.communityWorldCup.feignclient.account.AppUserClient;
import com.dsh.communityWorldCup.feignclient.account.StudentClient;
import com.dsh.communityWorldCup.feignclient.account.StudentHonorClient;
import com.dsh.communityWorldCup.feignclient.account.UserIntegralChangesClient;
import com.dsh.communityWorldCup.feignclient.account.model.*;
import com.dsh.communityWorldCup.feignclient.competition.ParticipantClient;
import com.dsh.communityWorldCup.feignclient.competition.model.Participant;
import com.dsh.communityWorldCup.feignclient.other.StoreClient;
import com.dsh.communityWorldCup.mapper.WorldCupCompetitorMapper;
import com.dsh.communityWorldCup.mapper.WorldCupPaymentParticipantMapper;
import com.dsh.communityWorldCup.model.*;
import com.dsh.communityWorldCup.service.IWorldCupCompetitorService;
import com.dsh.communityWorldCup.service.IWorldCupPaymentParticipantService;
import com.dsh.communityWorldCup.service.IWorldCupService;
import com.dsh.communityWorldCup.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * @author zhibing.pu
 * @Date 2024/2/19 15:47
 */
@Service
public class WorldCupCompetitorServiceImpl extends ServiceImpl<WorldCupCompetitorMapper, WorldCupCompetitor> implements IWorldCupCompetitorService {
 
    @Resource
    private StudentClient studentClient;
 
    @Resource
    private ParticipantClient participantClient;
 
    @Resource
    private AppUserClient appUserClient;
 
    @Autowired
    private IWorldCupService worldCupService;
 
    @Resource
    private UserIntegralChangesClient userIntegralChangesClient;
 
    @Resource
    private StudentHonorClient studentHonorClient;
 
 
 
 
 
    /**
     * 获取参赛人员名次信息
     * @param entrantRank
     * @return
     */
    @Override
    public EntrantRankVo getEntrantRank(EntrantRank entrantRank) {
        EntrantRankVo entrantRankVo = new EntrantRankVo();
        if(entrantRank.getIsStudent() == 0){
            //参赛人员
            Participant participant = participantClient.getParticipant(entrantRank.getId());
            entrantRankVo.setName(participant.getName());
        }else{
            //学员
            TStudent tStudent = studentClient.queryById(entrantRank.getId().intValue());
            entrantRankVo.setName(tStudent.getName());
        }
        //全国排名---直接数据库分组查询后排序
        entrantRankVo.setNationalRank(0);
        List<Map<String, Object>> mapList = this.baseMapper.getNumberOfGamesRanked(null);
        for (int i = 0; i < mapList.size(); i++) {
            Map<String, Object> map = mapList.get(i);
            Integer participantType = Integer.valueOf(map.get("participantType").toString());
            Long participantId = Long.valueOf(map.get("participantId").toString());
            Integer num = Integer.valueOf(map.get("num").toString());
            if(null != participantId && participantId.equals(entrantRank.getId()) && participantType.equals(entrantRank.getIsStudent())){
                entrantRankVo.setNationalRank(i + 1);
            }
        }
        /**
         * 城市排名
         * 1、先查询出当前用户对应的城市
         * 2、再根据城市查询对应的所有人员
         * 3、再根据查询出来的参赛人分组查询出参赛次数后排序
         */
        entrantRankVo.setCityRank(0);
        AppUser appUser = appUserClient.getAppUser(entrantRank.getAppUserId());
        entrantRankVo.setCityName(appUser.getCity());
        List<Integer> appUserIds = appUserClient.getAppUserIds(appUser.getCityCode());
        List<Map<String, Object>> mapList1 = this.baseMapper.getNumberOfGamesRanked(appUserIds);
        for (int i = 0; i < mapList1.size(); i++) {
            Map<String, Object> map = mapList1.get(i);
            Integer participantType = Integer.valueOf(map.get("participantType").toString());
            Long participantId = Long.valueOf(map.get("participantId").toString());
            Integer num = Integer.valueOf(map.get("num").toString());
            if(null != participantId && participantId.equals(entrantRank.getId()) && participantType.equals(entrantRank.getIsStudent())){
                entrantRankVo.setCityRank(i + 1);
            }
        }
        QueryWrapper<WorldCupCompetitor> wrapper = new QueryWrapper<>();
        if(entrantRank.getIsStudent() == 0){
            wrapper.eq("participantType", 2);
        }else{
            wrapper.eq("participantType", 1);
        }
        int win = this.count(wrapper.eq("participantId", entrantRank.getId()).in("matchResult", Arrays.asList(0, 1)));
        entrantRankVo.setWin(win);
 
        wrapper = new QueryWrapper<>();
        if(entrantRank.getIsStudent() == 0){
            wrapper.eq("participantType", 2);
        }else{
            wrapper.eq("participantType", 1);
        }
        int lose = this.count(wrapper.eq("participantId", entrantRank.getId()).eq("matchResult", -1));
        entrantRankVo.setLose(lose);
        if((win + lose) == 0){
            entrantRankVo.setWinRate(0D);
        }else{
            entrantRankVo.setWinRate(new BigDecimal(win).divide(new BigDecimal(win + lose), new MathContext(4, RoundingMode.HALF_EVEN)).multiply(new BigDecimal(100)).doubleValue());
        }
        return entrantRankVo;
    }
 
 
    /**
     * 获取比赛记录
     * @param matchRecord
     * @return
     */
    @Override
    public MatchRecordVo getMatchRecord(MatchRecord matchRecord) {
        matchRecord.setIsStudent(matchRecord.getIsStudent() == 0 ? 2 : 1);
        int pageNo = (matchRecord.getPageNo() - 1) * matchRecord.getPageSize();
        matchRecord.setPageNo(pageNo);
        MatchRecordVo matchRecordVo = new MatchRecordVo();
        int count = this.count(new QueryWrapper<WorldCupCompetitor>().eq("participantId", matchRecord.getId())
                .eq("participantType", matchRecord.getIsStudent()).isNotNull("matchResult"));
        matchRecordVo.setTotalSession(count);
        List<MatchRecordList> matchRecord1 = this.baseMapper.getMatchRecord(matchRecord);
        matchRecordVo.setList(matchRecord1);
        return matchRecordVo;
    }
 
 
    /**
     * 获取参赛排名
     * @param worldCupRank
     * @return
     */
    @Override
    public List<WorldCupRankVo> getWorldCupRank(WorldCupRank worldCupRank) {
        worldCupRank.setIsStudent(worldCupRank.getIsStudent() == 0 ? 2 : 1);
        List<Integer> appUserIds = null;
        if(worldCupRank.getRadius() == 2){
            AppUser appUser = appUserClient.getAppUser(worldCupRank.getAppUserId());
            appUserIds = appUserClient.getAppUserIds(appUser.getCityCode());
        }
        //总场次
        List<Map<String, Object>> lists = this.baseMapper.getWorldCupRank(worldCupRank, appUserIds);
        //未负的数据
        List<Map<String, Object>> lists1 = this.baseMapper.getWorldCupRank1(worldCupRank, appUserIds);
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (int i = 0; i < lists.size(); i++) {
            Map<String, Object> map = lists.get(i);
            Integer participantType = Integer.valueOf(map.get("participantType").toString());
            Long participantId = Long.valueOf(map.get("participantId").toString());
            Integer appUserId = Integer.valueOf(map.get("appUserId").toString());
            Integer num = Integer.valueOf(null == map.get("num") ? "0" : map.get("num").toString());
            //学员
            if(participantType == 1){
                TStudent tStudent = studentClient.queryById(participantId.intValue());
                if(tStudent.getState() != 1){
                    continue;
                }
            }
            //参赛人员
            if(participantType == 2){
                Participant participant = participantClient.getParticipant(participantId);
                if(participant.getState() != 1){
                    continue;
                }
            }
            map.put("totalSession", num);
            map.put("winRate", "0");
            for (Map<String, Object> objectMap : lists1) {
                Integer participantType1 = Integer.valueOf(objectMap.get("participantType").toString());
                Long participantId1 = Long.valueOf(objectMap.get("participantId").toString());
                Integer appUserId1 = Integer.valueOf(objectMap.get("appUserId").toString());
                Integer num1 = Integer.valueOf(null == objectMap.get("num") ? "0" : objectMap.get("num").toString());
                if(participantType.compareTo(participantType1) == 0 && participantId.compareTo(participantId1) == 0 && appUserId.compareTo(appUserId1) == 0){
                    map.put("winRate", new BigDecimal(num1).divide(new BigDecimal(num), new MathContext(4, RoundingMode.HALF_EVEN)).multiply(new BigDecimal(100)).doubleValue());
                    break;
                }
            }
            mapList.add(map);
        }
        if(worldCupRank.getSort() == 1){
            mapList.sort(new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    return Double.valueOf(o1.get("totalSession").toString()).compareTo(Double.valueOf(o2.get("totalSession").toString())) * -1;
                }
            });
        }
        if(worldCupRank.getSort() == 2){
            mapList.sort(new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    return Double.valueOf(o1.get("winRate").toString()).compareTo(Double.valueOf(o2.get("winRate").toString())) * -1;
                }
            });
        }
        List<WorldCupRankVo> list = new ArrayList<>();
        for (int i = 0; i < mapList.size(); i++) {
            Map<String, Object> map = mapList.get(i);
            Integer participantType = Integer.valueOf(map.get("participantType").toString());
            Long participantId = Long.valueOf(map.get("participantId").toString());
            Integer appUserId = Integer.valueOf(map.get("appUserId").toString());
            Integer totalSession = Integer.valueOf(map.get("totalSession").toString());
            Double winRate = Double.valueOf(map.get("winRate").toString());
            //自己排名在20内的标识
            boolean b = false;
            if(i <= 19){
                WorldCupRankVo worldCupRankVo = new WorldCupRankVo();
                worldCupRankVo.setRank(i + 1);
                worldCupRankVo.setTotalSession(totalSession);
                worldCupRankVo.setWinRate(winRate);
                //学员
                if(participantType == 1){
                    TStudent tStudent = studentClient.queryById(participantId.intValue());
                    worldCupRankVo.setAvatar(tStudent.getHeadImg());
                    String name = tStudent.getName();
                    if(name.length() > 2){
                        name = name.charAt(0) + "*" + name.substring(2);
                    }else{
                        name = name.charAt(0) + "*";
                    }
                    worldCupRankVo.setName(name);
                }
                //参赛人员
                if(participantType == 2){
                    Participant participant = participantClient.getParticipant(participantId);
                    worldCupRankVo.setAvatar(participant.getHeadImg());
                    String name = participant.getName();
                    if(name.length() > 2){
                        name = name.charAt(0) + "*" + name.substring(2);
                    }else{
                        name = name.charAt(0) + "*";
                    }
                    worldCupRankVo.setName(name);
 
                }
                if(worldCupRank.getIsStudent().compareTo(participantType) == 0 && worldCupRank.getId().compareTo(participantId) == 0){
                    worldCupRankVo.setOneself(1);
                    b = true;
                }else{
                    worldCupRankVo.setOneself(0);
                }
 
                list.add(worldCupRankVo);
            }
            //排名20内,且包含自己直接返回
            if(i == 19 && b){
                break;
            }
            //排名前20的数据添加完成后且包含自己,需要将自己找出来后添加到21位
            if(i > 19 && !b){
                if(worldCupRank.getIsStudent().compareTo(participantType) == 0 && worldCupRank.getId().compareTo(participantId) == 0){
                    WorldCupRankVo worldCupRankVo = new WorldCupRankVo();
                    worldCupRankVo.setRank(i + 1);
                    worldCupRankVo.setTotalSession(totalSession);
                    worldCupRankVo.setWinRate(winRate);
                    //学员
                    if(participantType == 1){
                        TStudent tStudent = studentClient.queryById(participantId.intValue());
                        worldCupRankVo.setAvatar(tStudent.getHeadImg());
                        String name = tStudent.getName();
                        if(name.length() > 2){
                            name = name.charAt(0) + "*" + name.substring(2);
                        }else{
                            name = name.charAt(0) + "*";
                        }
                        worldCupRankVo.setName(name);
                    }
                    //参赛人员
                    if(participantType == 2){
                        Participant participant = participantClient.getParticipant(participantId);
                        worldCupRankVo.setAvatar(participant.getHeadImg());
                        String name = participant.getName();
                        if(name.length() > 2){
                            name = name.charAt(0) + "*" + name.substring(2);
                        }else{
                            name = name.charAt(0) + "*";
                        }
                        worldCupRankVo.setName(name);
 
                    }
                    worldCupRankVo.setOneself(1);
                    list.add(worldCupRankVo);
                    break;
                }
            }
        }
        return list;
    }
 
 
    /**
     * 比赛结束后通知处理逻辑
     * @param custom        开始比赛接口上传的自定义参数
     * @param red_score     红方分数
     * @param blue_score    蓝方分数
     */
    @Override
    public void endWorldCupCallback(String custom, Integer red_score, Integer blue_score) {
        JSONObject jsonObject = JSON.parseObject(custom);
        Integer gameType = jsonObject.getInteger("gameType");
        //智慧球场比赛
        if(2 == gameType){
            return;
        }
        List<Long> ids = jsonObject.getJSONArray("ids").toJavaList(Long.class);
        List<WorldCupCompetitor> worldCupCompetitors = this.listByIds(ids);
        try {
            WorldCupCompetitor worldCupCompetitor1 = worldCupCompetitors.get(0);
            WorldCup worldCup = worldCupService.getById(worldCupCompetitor1.getWorldCupId());
            for (WorldCupCompetitor worldCupCompetitor : worldCupCompetitors) {
                worldCupCompetitor.setWorldCupId(null);
                //蓝方
                if(worldCupCompetitor.getParticipant() == 1){
                    worldCupCompetitor.setMatchResult(blue_score.compareTo(red_score));
                    worldCupCompetitor.setOurScore(blue_score);
                    worldCupCompetitor.setOpponentScore(red_score);
                    worldCupCompetitor.setEndTime(new Date());
                    worldCupCompetitor.setWinIntegral(0);
                    if(null != worldCup.getWinIntegral() && 0 < worldCup.getWinIntegral() && blue_score.compareTo(red_score) >= 0){
                        worldCupCompetitor.setWinIntegral(worldCup.getWinIntegral());
                        //增加积分明细
                        SaveUserIntegralChangesVo vo = new SaveUserIntegralChangesVo();
                        vo.setAppUserId(worldCupCompetitor.getAppUserId());
                        vo.setIntegral(worldCup.getWinIntegral());
                        vo.setType(7);
                        userIntegralChangesClient.saveUserIntegralChanges(vo);
                    }
                }
                //红方
                if(worldCupCompetitor.getParticipant() == 2){
                    worldCupCompetitor.setMatchResult(red_score.compareTo(blue_score));
                    worldCupCompetitor.setOurScore(red_score);
                    worldCupCompetitor.setOpponentScore(blue_score);
                    worldCupCompetitor.setEndTime(new Date());
                    worldCupCompetitor.setWinIntegral(0);
                    if(null != worldCup.getWinIntegral() && 0 < worldCup.getWinIntegral() && red_score.compareTo(blue_score) >= 0){
                        worldCupCompetitor.setWinIntegral(worldCup.getWinIntegral());
                        //增加积分明细
                        SaveUserIntegralChangesVo vo = new SaveUserIntegralChangesVo();
                        vo.setAppUserId(worldCupCompetitor.getAppUserId());
                        vo.setIntegral(worldCup.getWinIntegral());
                        vo.setType(7);
                        userIntegralChangesClient.saveUserIntegralChanges(vo);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
 
        this.updateBatchById(worldCupCompetitors);
 
        //添加勋章
        for (WorldCupCompetitor worldCupCompetitor : worldCupCompetitors) {
            Integer appUserId = worldCupCompetitor.getAppUserId();
            //常胜将军
            if(1 == worldCupCompetitor.getMatchResult()){
                int count = this.count(new QueryWrapper<WorldCupCompetitor>().eq("appUserId", appUserId).eq("matchResult", 1));
                StudentHonor studentHonor = new StudentHonor();
                studentHonor.setAppUserId(appUserId);
                studentHonor.setHonorType(5);
                studentHonor.setNumber(count);
                studentHonorClient.saveStudentHonor(studentHonor);
            }
            //越战越勇
            int count = this.count(new QueryWrapper<WorldCupCompetitor>().eq("appUserId", appUserId));
            StudentHonor studentHonor = new StudentHonor();
            studentHonor.setAppUserId(appUserId);
            studentHonor.setHonorType(6);
            studentHonor.setNumber(count);
            studentHonorClient.saveStudentHonor(studentHonor);
        }
 
    }
 
 
    /**
     * 获取比赛排行榜列表数据
     * @param worldCupRecords
     * @return
     */
    @Override
    public Map<String, Object> worldCupRecordsList(WorldCupRecords worldCupRecords) {
        Map<String, Object> map1 = new HashMap<>();
        AppUserIdsByCityName appUserIdsByCityName = new AppUserIdsByCityName();
        appUserIdsByCityName.setProvince(worldCupRecords.getProvince());
        appUserIdsByCityName.setCity(worldCupRecords.getCity());
        List<Integer> appUserIds = appUserClient.getAppUserIdsByCityName(appUserIdsByCityName);
        List<Map<String, Object>> mapList = this.baseMapper.worldCupRecordsList(worldCupRecords, appUserIds);
        List<Map<String, Object>> mapList1 = this.baseMapper.worldCupRecordsList1(worldCupRecords, appUserIds);
        List<Map<String, Object>> list = new ArrayList<>();
        int row_num = 0;
        for (int i = 0; i < mapList.size(); i++) {
            Map<String, Object> map = mapList.get(i);
            Integer participantType = Integer.valueOf(map.get("participantType").toString());
            Long participantId = Long.valueOf(map.get("participantId").toString());
            Integer appUserId = Integer.valueOf(map.get("appUserId").toString());
            Integer num = Integer.valueOf(null == map.get("num") ? "0" : map.get("num").toString());
            //学员
            if(participantType == 1){
                TStudent tStudent = studentClient.queryById(participantId.intValue());
                if(tStudent.getState() != 1){
                    continue;
                }
            }
            //参赛人员
            if(participantType == 2){
                Participant participant = participantClient.getParticipant(participantId);
                if(participant.getState() != 1){
                    continue;
                }
            }
            map.put("totalSession", num);
            map.put("winRate", "0");
            map.put("win", "0");
            for (Map<String, Object> objectMap : mapList1) {
                Integer participantType1 = Integer.valueOf(objectMap.get("participantType").toString());
                Long participantId1 = Long.valueOf(objectMap.get("participantId").toString());
                Integer appUserId1 = Integer.valueOf(objectMap.get("appUserId").toString());
                Integer num1 = Integer.valueOf(null == objectMap.get("num") ? "0" : objectMap.get("num").toString());
                if(participantType.compareTo(participantType1) == 0 && participantId.compareTo(participantId1) == 0 && appUserId.compareTo(appUserId1) == 0){
                    map.put("winRate", new BigDecimal(num1).divide(new BigDecimal(num), new MathContext(4, RoundingMode.HALF_EVEN)).multiply(new BigDecimal(100)).doubleValue());
                    map.put("win", num1);
                    break;
                }
            }
            Integer win = Integer.valueOf(map.get("win").toString());
            Integer lose = num - win;
            map.put("lose", lose);
            AppUser appUser = appUserClient.getAppUser(appUserId);
            map.put("province", appUser.getProvince() + appUser.getCity());
            if(1 == participantType){
                TStudent tStudent = studentClient.queryById(participantId.intValue());
                if(tStudent.getState() != 1){
                    continue;
                }
                map.put("name", tStudent.getName());
                map.put("phone", ToolUtil.isNotEmpty(tStudent.getPhone()) ? tStudent.getPhone() : appUser.getPhone());
            }else{
                Participant participant = participantClient.getParticipant(participantId);
                if(participant.getState() != 1){
                    continue;
                }
                map.put("name", participant.getName());
                map.put("phone", ToolUtil.isNotEmpty(participant.getPhone()) ? participant.getPhone() : appUser.getPhone());
            }
            row_num++;
            map.put("row_num", row_num);
            list.add(map);
        }
        list.sort(new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                return Double.valueOf(o1.get("totalSession").toString()).compareTo(Double.valueOf(o2.get("totalSession").toString())) * -1;
            }
        });
        
        Integer offset = worldCupRecords.getOffset();
        Integer limit = worldCupRecords.getLimit();
        limit += offset;
        map1.put("rows", list.subList(offset, list.size() >= limit ? limit : list.size()));
        map1.put("total", list.size());
        return map1;
    }
 
 
    /**
     * 获取比赛统计详情列表
     * @param worldCupGameStatisticsInfoList
     * @return
     */
    @Override
    public Map<String, Object> worldCupGameStatisticsInfoList(WorldCupGameStatisticsInfoList worldCupGameStatisticsInfoList) {
        Map<String, Object> map1 = new HashMap<>();
        Integer id = worldCupGameStatisticsInfoList.getId();
        String name = worldCupGameStatisticsInfoList.getName();
        String phone = worldCupGameStatisticsInfoList.getPhone();
        String idcard = worldCupGameStatisticsInfoList.getIdcard();
        List<Map<String, Object>> mapList = this.baseMapper.worldCupGameStatisticsInfoList(id);
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < mapList.size(); i++) {
            Map<String, Object> map = mapList.get(i);
            Integer participantType = Integer.valueOf(map.get("participantType").toString());
            Long participantId = Long.valueOf(map.get("participantId").toString());
            Integer appUserId = Integer.valueOf(map.get("appUserId").toString());
            Integer totalSession = Integer.valueOf(map.get("totalSession").toString());
            Integer win = Integer.valueOf(map.get("win").toString());
            Integer lose = totalSession - win;
            map.put("lose", lose);
            map.put("participantType", participantType);
            map.put("participantId", participantId);
            AppUser appUser = appUserClient.getAppUser(appUserId);
            map.put("province", appUser.getProvince() + appUser.getCity());
            if(1 == participantType){
                TStudent tStudent = studentClient.queryById(participantId.intValue());
                if(ToolUtil.isNotEmpty(name) && tStudent.getName().indexOf(name) == -1){
                    continue;
                }
                if(ToolUtil.isNotEmpty(phone) && (ToolUtil.isEmpty(tStudent.getPhone()) ? appUser.getPhone() : tStudent.getPhone()).indexOf(phone) == -1){
                    continue;
                }
                if(ToolUtil.isNotEmpty(idcard) && tStudent.getIdCard().indexOf(idcard) == -1){
                    continue;
                }
 
                map.put("name", tStudent.getName());
                map.put("phone", ToolUtil.isEmpty(tStudent.getPhone()) ? appUser.getPhone() : tStudent.getPhone());
                map.put("idcard", tStudent.getIdCard());
            }else{
                Participant participant = participantClient.getParticipant(participantId);
                if(ToolUtil.isNotEmpty(name) && participant.getName().indexOf(name) == -1){
                    continue;
                }
                if(ToolUtil.isNotEmpty(phone) && (ToolUtil.isNotEmpty(participant.getPhone()) ? participant.getPhone() : appUser.getPhone()).indexOf(phone) == -1){
                    continue;
                }
                if(ToolUtil.isNotEmpty(idcard) && participant.getIdcard().indexOf(idcard) == -1){
                    continue;
                }
 
                map.put("name", participant.getName());
                map.put("phone", ToolUtil.isNotEmpty(participant.getPhone()) ? participant.getPhone() : appUser.getPhone());
                map.put("idcard", participant.getIdcard());
            }
            list.add(map);
        }
        map1.put("total", list.size());
        Integer offset = worldCupGameStatisticsInfoList.getOffset();
        Integer limit = worldCupGameStatisticsInfoList.getLimit();
        limit += offset;
        map1.put("rows", list.subList(offset, list.size() >= limit ? limit : list.size()));
        return map1;
    }
 
 
    /**
     * 获取单场参赛详情列表
     * @return
     */
    @Override
    public Map<String, Object> worldCupGameStatisticsListInfo(WorldCupGameStatisticsListInfo worldCupGameStatisticsListInfo) {
        Long participantId = worldCupGameStatisticsListInfo.getParticipantId();
        Integer participantType = worldCupGameStatisticsListInfo.getParticipantType();
        String name = worldCupGameStatisticsListInfo.getName();
        List<WorldCupCompetitor> list = this.list(new QueryWrapper<WorldCupCompetitor>()
                .eq("participantType", participantType).eq("participantId", participantId)
                .isNotNull("matchResult").orderByDesc("startTime"));
        List<Map<String, Object>> mapList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm");
        for (WorldCupCompetitor cupCompetitor : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", cupCompetitor.getId().toString());
            if(1 == participantType){
                TStudent tStudent = studentClient.queryById(participantId.intValue());
                if(ToolUtil.isNotEmpty(name) && tStudent.getName().indexOf(name) == -1){
                    continue;
                }
 
                map.put("name", tStudent.getName());
            }else{
                Participant participant = participantClient.getParticipant(participantId);
                if(ToolUtil.isNotEmpty(name) && participant.getName().indexOf(name) == -1){
                    continue;
                }
                map.put("name", participant.getName());
            }
            map.put("startTime", sdf.format(cupCompetitor.getStartTime()));
            map.put("score", cupCompetitor.getOurScore() + ":" + cupCompetitor.getOpponentScore());
            map.put("matchResult", cupCompetitor.getMatchResult() == 1 ? "胜" : cupCompetitor.getMatchResult() == 0 ? "平" : "负");
            map.put("blue", cupCompetitor.getParticipant() == 1 ? cupCompetitor.getOurScore() : cupCompetitor.getOpponentScore());
            map.put("red", cupCompetitor.getParticipant() == 2 ? cupCompetitor.getOurScore() : cupCompetitor.getOpponentScore());
            mapList.add(map);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("rows", mapList);
        map.put("total", mapList.size());
        return map;
    }
 
 
    /**
     * 修改比分
     * @param changeScore
     */
    @Override
    public void changeScore(ChangeScore changeScore) {
        Long id = changeScore.getId();
        Integer blue = changeScore.getBlue();
        Integer red = changeScore.getRed();
        WorldCupCompetitor worldCupCompetitor = this.getById(id);
        List<WorldCupCompetitor> blueList = this.list(new QueryWrapper<WorldCupCompetitor>().eq("code", worldCupCompetitor.getCode())
                .eq("participant", 1).eq("worldCupId", worldCupCompetitor.getWorldCupId()));
        WorldCup worldCup = worldCupService.getById(blueList.get(0).getWorldCupId());
        for (WorldCupCompetitor cupCompetitor : blueList) {
            //如果之前输了,现在赢/平了,需要增加积分
            if(cupCompetitor.getMatchResult() == -1 && (blue.compareTo(red) > 0 || blue.compareTo(red) == 0)){
                //增加积分明细
                SaveUserIntegralChangesVo vo = new SaveUserIntegralChangesVo();
                vo.setAppUserId(cupCompetitor.getAppUserId());
                vo.setIntegral(worldCup.getWinIntegral());
                vo.setType(7);
                userIntegralChangesClient.saveUserIntegralChanges(vo);
 
                cupCompetitor.setMatchResult(1);
                cupCompetitor.setWinIntegral(worldCup.getWinIntegral());
            }
            //如果之前赢了,现在输了,需要扣减积分
            if(cupCompetitor.getMatchResult() == 1 && blue.compareTo(red) < 0){
                //增加积分明细
                SaveUserIntegralChangesVo vo = new SaveUserIntegralChangesVo();
                vo.setAppUserId(cupCompetitor.getAppUserId());
                vo.setIntegral(worldCup.getWinIntegral() * -1);
                vo.setType(8);
                userIntegralChangesClient.saveUserIntegralChanges(vo);
 
                cupCompetitor.setMatchResult(-1);
                cupCompetitor.setWinIntegral(0);
            }
            //如果之前平了,现在输了,需要扣减积分
            if(cupCompetitor.getMatchResult() == 0 && blue.compareTo(red) < 0){
                //增加积分明细
                SaveUserIntegralChangesVo vo = new SaveUserIntegralChangesVo();
                vo.setAppUserId(cupCompetitor.getAppUserId());
                vo.setIntegral(worldCup.getWinIntegral() * -1);
                vo.setType(8);
                userIntegralChangesClient.saveUserIntegralChanges(vo);
 
                cupCompetitor.setMatchResult(-1);
                cupCompetitor.setWinIntegral(0);
            }
            cupCompetitor.setOurScore(blue);
            cupCompetitor.setOpponentScore(red);
            cupCompetitor.setWorldCupId(null);
        }
        this.updateBatchById(blueList);
 
        List<WorldCupCompetitor> redList = this.list(new QueryWrapper<WorldCupCompetitor>().eq("code", worldCupCompetitor.getCode())
                .eq("participant", 2).eq("worldCupId", worldCupCompetitor.getWorldCupId()));
        for (WorldCupCompetitor cupCompetitor : redList) {
            //如果之前输了,现在赢/平了,需要增加积分
            if(cupCompetitor.getMatchResult() == -1 && (red.compareTo(blue) > 0 || red.compareTo(blue) == 0)){
                //增加积分明细
                SaveUserIntegralChangesVo vo = new SaveUserIntegralChangesVo();
                vo.setAppUserId(cupCompetitor.getAppUserId());
                vo.setIntegral(worldCup.getWinIntegral());
                vo.setType(7);
                userIntegralChangesClient.saveUserIntegralChanges(vo);
 
                cupCompetitor.setMatchResult(1);
                cupCompetitor.setWinIntegral(worldCup.getWinIntegral());
            }
            //如果之前赢了,现在输了,需要扣减积分
            if(cupCompetitor.getMatchResult() == 1 && red.compareTo(blue) < 0){
                //增加积分明细
                SaveUserIntegralChangesVo vo = new SaveUserIntegralChangesVo();
                vo.setAppUserId(cupCompetitor.getAppUserId());
                vo.setIntegral(worldCup.getWinIntegral() * -1);
                vo.setType(8);
                userIntegralChangesClient.saveUserIntegralChanges(vo);
 
                cupCompetitor.setMatchResult(-1);
                cupCompetitor.setWinIntegral(0);
            }
            //如果之前平了,现在输了,需要扣减积分
            if(cupCompetitor.getMatchResult() == 0 && red.compareTo(blue) < 0){
                //增加积分明细
                SaveUserIntegralChangesVo vo = new SaveUserIntegralChangesVo();
                vo.setAppUserId(cupCompetitor.getAppUserId());
                vo.setIntegral(worldCup.getWinIntegral() * -1);
                vo.setType(8);
                userIntegralChangesClient.saveUserIntegralChanges(vo);
 
                cupCompetitor.setMatchResult(-1);
                cupCompetitor.setWinIntegral(0);
            }
            cupCompetitor.setOurScore(red);
            cupCompetitor.setOpponentScore(blue);
            cupCompetitor.setWorldCupId(null);
        }
        this.updateBatchById(redList);
 
 
        //修改勋章
        List<WorldCupCompetitor> worldCupCompetitors = this.list(new QueryWrapper<WorldCupCompetitor>().eq("code", worldCupCompetitor.getCode()));
        for (WorldCupCompetitor wcc : worldCupCompetitors) {
            Integer appUserId = wcc.getAppUserId();
            //常胜将军
            if(1 == wcc.getMatchResult()){
                int count = this.count(new QueryWrapper<WorldCupCompetitor>().eq("appUserId", appUserId).eq("matchResult", 1));
                StudentHonor studentHonor = new StudentHonor();
                studentHonor.setAppUserId(appUserId);
                studentHonor.setHonorType(5);
                studentHonor.setNumber(count);
                studentHonorClient.saveStudentHonor(studentHonor);
            }
            //越战越勇
            int count = this.count(new QueryWrapper<WorldCupCompetitor>().eq("appUserId", appUserId));
            StudentHonor studentHonor = new StudentHonor();
            studentHonor.setAppUserId(appUserId);
            studentHonor.setHonorType(6);
            studentHonor.setNumber(count);
            studentHonorClient.saveStudentHonor(studentHonor);
        }
 
    }
 
 
    /**
     * 获取用户比赛记录详情
     * @param userGameRecordList
     * @return
     */
    @Override
    public Map<String, Object> userGameRecordList(UserGameRecordList userGameRecordList) {
        String name = userGameRecordList.getUserName();
        List<Map<String, Object>> list = this.baseMapper.userGameRecordList(userGameRecordList.getName());
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Map<String, Object> map : list) {
            Long participantType = Long.valueOf(map.get("participantType").toString());
            Long participantId = Long.valueOf(map.get("participantId").toString());
            Integer ourScore = Integer.valueOf(map.get("ourScore").toString());
            Integer opponentScore = Integer.valueOf(map.get("opponentScore").toString());
            Integer matchResult = Integer.valueOf(map.get("matchResult").toString());
            if(1 == participantType){
                TStudent tStudent = studentClient.queryById(participantId.intValue());
                if(ToolUtil.isNotEmpty(name) && tStudent.getName().indexOf(name) == -1){
                    continue;
                }
                map.put("userName", tStudent.getName());
            }else{
                Participant participant = participantClient.getParticipant(participantId);
                if(ToolUtil.isNotEmpty(name) && participant.getName().indexOf(name) == -1){
                    continue;
                }
                map.put("userName", participant.getName());
            }
            map.put("score", ourScore + ":" + opponentScore);
            map.put("matchResult", matchResult == 1 ? "胜" : matchResult == 0 ? "平" :"负");
            mapList.add(map);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total", mapList.size());
        Integer offset = userGameRecordList.getOffset();
        Integer limit = userGameRecordList.getLimit();
        limit += offset;
        map.put("rows", mapList.subList(offset, mapList.size() >= limit ? limit : mapList.size()));
        return map;
    }
 
 
    /**
     * 获取比赛场次
     * @param worldCupId
     * @return
     */
    @Override
    public int getMatchTime(Integer worldCupId) {
        return this.baseMapper.getMatchTime(worldCupId);
    }
}