puzhibing
2024-03-06 fba3b84ee4e8b60f1536c50d032e4f199ec172a2
cloud-server-communityWorldCup/src/main/java/com/dsh/communityWorldCup/service/impl/WorldCupCompetitorServiceImpl.java
@@ -1,7 +1,9 @@
package com.dsh.communityWorldCup.service.impl;
import com.alibaba.fastjson.JSON;
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;
@@ -11,15 +13,18 @@
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.EntrantRank;
import com.dsh.communityWorldCup.model.EntrantRankVo;
import com.dsh.communityWorldCup.model.*;
import com.dsh.communityWorldCup.service.IWorldCupCompetitorService;
import com.dsh.communityWorldCup.service.IWorldCupService;
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.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -38,6 +43,9 @@
    @Resource
    private AppUserClient appUserClient;
    @Autowired
    private IWorldCupService worldCupService;
@@ -116,4 +124,180 @@
        }
        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;
    }
    /**
     * 比赛结束后通知处理逻辑
     * @param custom        开始比赛接口上传的自定义参数
     * @param red_score     红方分数
     * @param blue          蓝方分数
     */
    @Override
    public void endWorldCupCallback(String custom, Integer red_score, Integer blue) {
        List<Long> ids = JSON.parseArray(custom, Long.class);
        List<WorldCupCompetitor> worldCupCompetitors = this.listByIds(ids);
        WorldCupCompetitor worldCupCompetitor1 = worldCupCompetitors.get(0);
        WorldCup worldCup = worldCupService.getById(worldCupCompetitor1.getWorldCupId());
        for (WorldCupCompetitor worldCupCompetitor : worldCupCompetitors) {
            //蓝方
            if(worldCupCompetitor.getParticipant() == 1){
                worldCupCompetitor.setMatchResult(blue.compareTo(red_score));
                worldCupCompetitor.setOurScore(blue);
                worldCupCompetitor.setOpponentScore(red_score);
                worldCupCompetitor.setEndTime(new Date());
                worldCupCompetitor.setWinIntegral(0);
                if(null != worldCup.getWinIntegral() && 0 < worldCup.getWinIntegral() && blue.compareTo(red_score) >= 0){
                    worldCupCompetitor.setWinIntegral(worldCup.getWinIntegral());
                    AppUser appUser = appUserClient.getAppUser(worldCupCompetitor.getAppUserId());
                    appUser.setIntegral(appUser.getIntegral() + worldCup.getWinIntegral());
                    appUserClient.updateAppUser(appUser);
                }
            }
            //红方
            if(worldCupCompetitor.getParticipant() == 2){
                worldCupCompetitor.setMatchResult(red_score.compareTo(blue));
                worldCupCompetitor.setOurScore(red_score);
                worldCupCompetitor.setOpponentScore(blue);
                worldCupCompetitor.setEndTime(new Date());
                worldCupCompetitor.setWinIntegral(0);
                if(null != worldCup.getWinIntegral() && 0 < worldCup.getWinIntegral() && red_score.compareTo(blue) >= 0){
                    worldCupCompetitor.setWinIntegral(worldCup.getWinIntegral());
                    AppUser appUser = appUserClient.getAppUser(worldCupCompetitor.getAppUserId());
                    appUser.setIntegral(appUser.getIntegral() + worldCup.getWinIntegral());
                    appUserClient.updateAppUser(appUser);
                }
            }
        }
        this.updateBatchById(worldCupCompetitors);
    }
}