puzhibing
2024-03-11 2b6370e31f46eb49dfea453def3be734745e936c
cloud-server-communityWorldCup/src/main/java/com/dsh/communityWorldCup/service/impl/WorldCupCompetitorServiceImpl.java
@@ -7,15 +7,21 @@
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.UserIntegralChangesClient;
import com.dsh.communityWorldCup.feignclient.account.model.AppUser;
import com.dsh.communityWorldCup.feignclient.account.model.AppUserIdsByCityName;
import com.dsh.communityWorldCup.feignclient.account.model.SaveUserIntegralChangesVo;
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.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;
@@ -23,10 +29,8 @@
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;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * @author zhibing.pu
@@ -47,6 +51,9 @@
    @Autowired
    private IWorldCupService worldCupService;
    @Resource
    private UserIntegralChangesClient userIntegralChangesClient;
@@ -65,7 +72,7 @@
            entrantRankVo.setName(participant.getName());
        }else{
            //学员
            TStudent tStudent = studentClient.queryById(entrantRank.getId());
            TStudent tStudent = studentClient.queryById(entrantRank.getId().intValue());
            entrantRankVo.setName(tStudent.getName());
        }
        //全国排名---直接数据库分组查询后排序
@@ -164,7 +171,7 @@
        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());
            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());
@@ -172,11 +179,12 @@
            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);
                    TStudent tStudent = studentClient.queryById(participantId.intValue());
                    worldCupRankVo.setAvatar(tStudent.getHeadImg());
                    String name = tStudent.getName();
                    if(name.length() > 2){
@@ -217,11 +225,12 @@
            if(i > 19 && !b){
                if(worldCupRank.getIsStudent().equals(participantType) && worldCupRank.getId().equals(participantId)){
                    WorldCupRankVo worldCupRankVo = new WorldCupRankVo();
                    worldCupRankVo.setRank(i + 1);
                    worldCupRankVo.setTotalSession(totalSession);
                    worldCupRankVo.setWinRate(winRate);
                    //学员
                    if(participantType == 1){
                        TStudent tStudent = studentClient.queryById(participantId);
                        TStudent tStudent = studentClient.queryById(participantId.intValue());
                        worldCupRankVo.setAvatar(tStudent.getHeadImg());
                        String name = tStudent.getName();
                        if(name.length() > 2){
@@ -259,45 +268,357 @@
     * 比赛结束后通知处理逻辑
     * @param custom        开始比赛接口上传的自定义参数
     * @param red_score     红方分数
     * @param blue          蓝方分数
     * @param blue_score    蓝方分数
     */
    @Override
    public void endWorldCupCallback(String custom, Integer red_score, Integer blue) {
    public void endWorldCupCallback(String custom, Integer red_score, Integer blue_score) {
        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);
        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);
                    }
                }
            }
            //红方
            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);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        this.updateBatchById(worldCupCompetitors);
    }
    /**
     * 获取比赛排行榜列表数据
     * @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);
        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);
            AppUser appUser = appUserClient.getAppUser(appUserId);
            map.put("province", appUser.getProvince() + appUser.getCity());
            if(1 == participantType){
                TStudent tStudent = studentClient.queryById(participantId.intValue());
                map.put("name", tStudent.getName());
                map.put("phone", tStudent.getPhone());
            }else{
                Participant participant = participantClient.getParticipant(participantId);
                map.put("name", participant.getName());
                map.put("phone", participant.getPhone());
            }
        }
        Integer offset = worldCupRecords.getOffset();
        Integer limit = worldCupRecords.getLimit();
        limit += offset;
        map1.put("rows", mapList.subList(offset, mapList.size() >= limit ? limit : mapList.size()));
        int count = this.baseMapper.worldCupRecordsListCount(appUserIds);
        map1.put("total", count);
        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) && 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());
            }else{
                Participant participant = participantClient.getParticipant(participantId);
                if(ToolUtil.isNotEmpty(name) && participant.getName().indexOf(name) == -1){
                    continue;
                }
                if(ToolUtil.isNotEmpty(phone) && participant.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());
            }
            list.add(map);
        }
        map1.put("total", mapList.size());
        Integer offset = worldCupGameStatisticsInfoList.getOffset();
        Integer limit = worldCupGameStatisticsInfoList.getLimit();
        limit += offset;
        map1.put("rows", mapList.subList(offset, mapList.size() >= limit ? limit : mapList.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.HH.dd HH:mm");
        for (WorldCupCompetitor cupCompetitor : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", cupCompetitor.getId());
            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().compareTo(cupCompetitor.getOpponentScore()) > 0 ?
                    cupCompetitor.getOurScore() + ":" + cupCompetitor.getOpponentScore() :
                    cupCompetitor.getOpponentScore() + ":" + cupCompetitor.getOurScore());
            map.put("matchResult", cupCompetitor.getMatchResult() == 1 ? "胜" : cupCompetitor.getMatchResult() == 0 ? "平" : "负");
            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));
        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);
        }
        this.updateBatchById(blueList);
        List<WorldCupCompetitor> redList = this.list(new QueryWrapper<WorldCupCompetitor>().eq("code", worldCupCompetitor.getCode()).eq("participant", 2));
        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);
        }
        this.updateBatchById(blueList);
    }
    /**
     * 获取用户比赛记录详情
     * @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.compareTo(opponentScore) > 0 ?
                    ourScore + ":" + opponentScore :
                    opponentScore + ":" + ourScore);
            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;
    }
}