puzhibing
2024-03-04 7e7f901b2172281dc294dfbc67e6ad00625f09f4
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
package com.dsh.communityWorldCup.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.model.AppUser;
import com.dsh.communityWorldCup.feignclient.account.model.TStudent;
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.model.*;
import com.dsh.communityWorldCup.service.IWorldCupCompetitorService;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * @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;
 
 
 
 
 
    /**
     * 获取参赛人员名次信息
     * @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());
            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());
            Integer participantId = Integer.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());
            Integer participantId = Integer.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()).eq("matchResult", 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()));
        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<WorldCupRankVo> list = 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());
            Integer participantId = Integer.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.setTotalSession(totalSession);
                worldCupRankVo.setWinRate(winRate);
                //学员
                if(participantType == 1){
                    TStudent tStudent = studentClient.queryById(participantId);
                    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){
                    AppUser appUser1 = appUserClient.getAppUser(appUserId);
                    Participant participant = participantClient.getParticipant(participantId);
                    worldCupRankVo.setAvatar(appUser1.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().equals(participantType) && worldCupRank.getId().equals(participantId)){
                    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().equals(participantType) && worldCupRank.getId().equals(participantId)){
                    WorldCupRankVo worldCupRankVo = new WorldCupRankVo();
                    worldCupRankVo.setTotalSession(totalSession);
                    worldCupRankVo.setWinRate(winRate);
                    //学员
                    if(participantType == 1){
                        TStudent tStudent = studentClient.queryById(participantId);
                        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){
                        AppUser appUser1 = appUserClient.getAppUser(appUserId);
                        Participant participant = participantClient.getParticipant(participantId);
                        worldCupRankVo.setAvatar(appUser1.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;
    }
}