game-api/ff-game/src/main/java/com/ff/api/controller/ApiGameController.java

593 lines
26 KiB
Java
Raw Normal View History

2025-02-11 15:27:15 +08:00
package com.ff.api.controller;
2025-02-12 13:42:52 +08:00
import cn.hutool.core.util.NumberUtil;
2025-02-11 15:27:15 +08:00
import com.ff.annotation.CheckHeader;
import com.ff.api.request.*;
import com.ff.api.response.*;
2025-02-11 15:27:15 +08:00
import com.ff.base.constant.Constants;
import com.ff.base.core.controller.BaseController;
import com.ff.base.core.domain.AjaxResult;
import com.ff.base.core.page.TableDataInfo;
import com.ff.base.enums.*;
import com.ff.base.exception.base.ApiException;
import com.ff.base.exception.base.BaseException;
2025-02-11 15:27:15 +08:00
import com.ff.base.utils.StringUtils;
import com.ff.base.utils.bean.BeanUtils;
import com.ff.base.system.domain.TenantSecretKey;
import com.ff.common.dto.GameBalanceExchange;
2025-02-12 13:42:52 +08:00
import com.ff.common.service.ITenantGameQuotaFlowService;
import com.ff.common.service.ITenantGameQuotaService;
2025-02-11 15:27:15 +08:00
import com.ff.config.KeyConfig;
import com.ff.game.api.IGamesService;
2025-02-12 13:42:52 +08:00
import com.ff.game.api.request.*;
import com.ff.game.domain.*;
import com.ff.game.dto.GameBettingDetailsDTO;
import com.ff.game.dto.GameSecretKeyCurrencyDTO;
import com.ff.game.dto.GameSecretKeyLangDTO;
import com.ff.game.service.*;
2025-02-11 15:27:15 +08:00
import com.ff.member.domain.Member;
import com.ff.member.service.IMemberService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
2025-02-11 15:27:15 +08:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
2025-02-12 13:42:52 +08:00
import org.springframework.transaction.annotation.Transactional;
2025-02-11 15:27:15 +08:00
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
2025-02-11 15:27:15 +08:00
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
2025-02-12 13:42:52 +08:00
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
2025-02-11 15:27:15 +08:00
/**
* api
*
* @author shi
* @date 2025/02/10
*/
@RestController
@CheckHeader
@RequestMapping("/api/game")
@Slf4j
2025-02-11 15:27:15 +08:00
public class ApiGameController extends BaseController {
@Autowired
private Map<String, IGamesService> gamesService;
@Resource
private IGameService gameService;
@Resource
private KeyConfig keyConfig;
@Resource
private IGameSecretKeyService gameSecretKeyService;
@Resource
private IMemberService memberService;
@Resource
private IGamePlatformService gamePlatformService;
2025-02-12 13:42:52 +08:00
@Resource
private ITenantGameQuotaService tenantGameQuotaService;
@Resource
private ITenantGameQuotaFlowService tenantGameQuotaFlowService;
@Resource
private IGameBettingDetailsService gameBettingDetailsService;
@Resource
private IGameFreeRecordService gameFreeRecordService;
@Resource
private IGameExchangeMoneyService gameExchangeMoneyService;
2025-02-12 13:42:52 +08:00
@Resource
private IGameSecretKeyCurrencyService gameSecretKeyCurrencyService;
@Resource
private IGameSecretKeyLangService gameSecretKeyLangService;
@Autowired
@Qualifier("threadPoolTaskExecutor")
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
/**
*
*
* @return {@link AjaxResult }
*/
@PostMapping("/list")
public AjaxResult list() {
List<GameResponse> gameResponses = gameService.selectGameResponseList();
for (GameResponse gameRespons : gameResponses) {
List<GameSecretKeyCurrencyDTO> gameSecretKeyCurrencies = gameSecretKeyCurrencyService.findByGameSecretKeyCurrencyDTOList(GameSecretKeyCurrencyDTO.builder().platformCode(gameRespons.getPlatformCode()).build());
List<String> currencyCode = gameSecretKeyCurrencies.stream().map(GameSecretKeyCurrencyDTO::getSystemCurrency).collect(Collectors.toList());
gameRespons.setCurrencyCode(currencyCode);
}
return AjaxResult.success(gameResponses);
}
2025-02-11 15:27:15 +08:00
/**
*
*
* @param memberCreateApiRequest api
* @return {@link AjaxResult }
*/
@PostMapping("/login")
public AjaxResult login(@Validated @RequestBody GameLoginRequest memberCreateApiRequest) {
Game game = gameService.selectGameById(memberCreateApiRequest.getGameId());
ApiException.notNull(game, ErrorCode.GAME_NOT_EXIST.getCode());
2025-02-11 15:27:15 +08:00
GamePlatform gamePlatform = gamePlatformService.selectGamePlatformById(game.getPlatformId());
ApiException.notNull(gamePlatform, ErrorCode.PLATFORM_NOT_EXIST.getCode());
2025-02-11 15:27:15 +08:00
IGamesService iGamesService = gamesService.get(gamePlatform.getPlatformCode() + Constants.SERVICE);
TenantSecretKey tenantSecretKey = keyConfig.get();
GameSecretKeyCurrencyDTO secretKeyCurrencyDTO = gameSecretKeyCurrencyService.findByGameSecretKeyCurrencyDTO(GameSecretKeyCurrencyDTO.builder()
.platformCode(gamePlatform.getPlatformCode())
.systemCurrency(memberCreateApiRequest.getCurrencyCode()).build());
ApiException.notNull(secretKeyCurrencyDTO, ErrorCode.CURRENCY_NOT_EXIST.getCode());
GameSecretKeyLangDTO gameSecretKeyLangDTO = gameSecretKeyLangService.findGameSecretKeyLangDTO(GameSecretKeyLangDTO.builder()
.platformCode(gamePlatform.getPlatformCode())
.systemLangCode(memberCreateApiRequest.getLangCode())
.build());
ApiException.notNull(gameSecretKeyLangDTO, ErrorCode.LANG_NOT_EXIST.getCode());
2025-02-11 15:27:15 +08:00
Member member = memberService.selectMemberByAccount(memberCreateApiRequest.getAccount(), memberCreateApiRequest.getCurrencyCode(), gamePlatform.getPlatformCode());
ApiException.notNull(member, ErrorCode.ACCOUNT_NOT_EXIST.getCode());
2025-02-11 15:27:15 +08:00
GamesLogin gamesLogin = GamesLogin.builder()
.agentId(secretKeyCurrencyDTO.getCode())
.agentKey(secretKeyCurrencyDTO.getKey())
2025-02-11 15:27:15 +08:00
.account(member.getGameAccount())
.gameType(game.getGameSourceType())
.currency(secretKeyCurrencyDTO.getCurrency())
2025-02-11 15:27:15 +08:00
.gameId(game.getGameCode())
.homeUrl(memberCreateApiRequest.getHomeUrl())
.betLimit(memberCreateApiRequest.getBetLimit())
2025-02-11 15:27:15 +08:00
.platform(memberCreateApiRequest.getPlatform())
.disableFullScreen(memberCreateApiRequest.getDisableFullScreen())
.lang(gameSecretKeyLangDTO.getLang())
2025-02-11 15:27:15 +08:00
.build();
String login = iGamesService.loginWithoutRedirect(gamesLogin);
return AjaxResult.success("操作成功", login);
2025-02-11 15:27:15 +08:00
}
2025-02-12 13:42:52 +08:00
/**
*
*
* @param gameExchangeBalanceRequest
* @return {@link AjaxResult }
*/
@PostMapping("/exchange/balance")
@Transactional
public AjaxResult exchangeBalance(@Validated @RequestBody GameExchangeBalanceRequest gameExchangeBalanceRequest) {
IGamesService iGamesService = gamesService.get(gameExchangeBalanceRequest.getPlatformCode() + Constants.SERVICE);
ApiException.notNull(iGamesService, ErrorCode.PLATFORM_NOT_EXIST.getCode());
2025-02-11 15:27:15 +08:00
2025-02-12 13:42:52 +08:00
TenantSecretKey tenantSecretKey = keyConfig.get();
GameSecretKeyCurrencyDTO gameSecretKey = gameSecretKeyCurrencyService.findByGameSecretKeyCurrencyDTO(GameSecretKeyCurrencyDTO.builder()
.platformCode(gameExchangeBalanceRequest.getPlatformCode())
.systemCurrency(gameExchangeBalanceRequest.getCurrencyCode()).build());
ApiException.notNull(gameSecretKey, ErrorCode.CURRENCY_NOT_EXIST.getCode());
2025-02-12 13:42:52 +08:00
BigDecimal quota = tenantGameQuotaService.gameBalanceExchange(GameBalanceExchange.builder()
.platformCode(gameExchangeBalanceRequest.getPlatformCode())
.sourceId(gameExchangeBalanceRequest.getOrderId())
.currencyCode(gameExchangeBalanceRequest.getCurrencyCode())
.transferType(gameExchangeBalanceRequest.getTransferType())
.amount(gameExchangeBalanceRequest.getAmount())
.account(gameExchangeBalanceRequest.getAccount())
2025-02-12 13:42:52 +08:00
.tenantKey(tenantSecretKey.getTenantKey())
.build());
// 获取用户信息
Member member = memberService.selectMemberByAccount(gameExchangeBalanceRequest.getAccount(), gameExchangeBalanceRequest.getCurrencyCode(), gameExchangeBalanceRequest.getPlatformCode());
ApiException.notNull(member, ErrorCode.ACCOUNT_NOT_EXIST.getCode());
2025-02-12 13:42:52 +08:00
//操作第三方额度接口
2025-02-12 13:42:52 +08:00
ExchangeTransferMoneyRequestDTO exchangeTransferMoneyRequestDTO = ExchangeTransferMoneyRequestDTO.builder()
.agentId(gameSecretKey.getCode())
.agentKey(gameSecretKey.getKey())
.orderId(gameExchangeBalanceRequest.getOrderId())
2025-02-12 13:42:52 +08:00
.account(member.getGameAccount())
.currency(gameSecretKey.getCurrency())
.tenantKey(tenantSecretKey.getTenantKey())
.quota(quota)
2025-02-12 13:42:52 +08:00
.amount(gameExchangeBalanceRequest.getAmount())
.transferType(gameExchangeBalanceRequest.getTransferType())
.build();
Long exchangeTransferId = iGamesService.exchangeTransferByAgentId(exchangeTransferMoneyRequestDTO);
GameExchangeMoney gameExchangeMoney = gameExchangeMoneyService.selectGameExchangeMoneyById(exchangeTransferId);
GameExchangeBalanceResponse gameExchangeBalanceResponse = new GameExchangeBalanceResponse();
BeanUtils.copyProperties(gameExchangeMoney, gameExchangeBalanceResponse);
return AjaxResult.success(gameExchangeBalanceResponse);
2025-02-12 13:42:52 +08:00
}
2025-02-11 15:27:15 +08:00
/**
*
*
* @param gameExchangeStateRequest
* @return {@link AjaxResult }
*/
@PostMapping("/exchange/state")
@Transactional
public AjaxResult exchangeState(@Validated @RequestBody GameExchangeStateRequest gameExchangeStateRequest) {
TenantSecretKey tenantSecretKey = keyConfig.get();
GameExchangeMoney gameExchangeMoney = GameExchangeMoney.builder()
.tenantKey(tenantSecretKey.getTenantKey())
.orderId(gameExchangeStateRequest.getOrderId())
.build();
List<GameExchangeMoney> gameExchangeMonies = gameExchangeMoneyService.selectGameExchangeMoneyList(gameExchangeMoney);
ApiException.isTrue(!CollectionUtils.isEmpty(gameExchangeMonies), ErrorCode.ORDER_NOT_EXIST.getCode());
GameExchangeBalanceResponse gameExchangeBalanceResponse = new GameExchangeBalanceResponse();
BeanUtils.copyProperties(gameExchangeMonies.get(0), gameExchangeBalanceResponse);
return AjaxResult.success(gameExchangeBalanceResponse);
}
/**
*
*
* @param gameCreateFreeSpinRequest
* @return {@link AjaxResult }
*/
@PostMapping("/create/free/spin")
public AjaxResult createFreeSpin(@Validated @RequestBody GameCreateFreeSpinRequest gameCreateFreeSpinRequest) {
IGamesService iGamesService = gamesService.get(gameCreateFreeSpinRequest.getPlatformCode() + Constants.SERVICE);
ApiException.notNull(iGamesService, ErrorCode.PLATFORM_NOT_EXIST.getCode());
TenantSecretKey tenantSecretKey = keyConfig.get();
GameSecretKeyCurrencyDTO gameSecretKey = gameSecretKeyCurrencyService.findByGameSecretKeyCurrencyDTO(GameSecretKeyCurrencyDTO.builder()
.platformCode(gameCreateFreeSpinRequest.getPlatformCode())
.systemCurrency(gameCreateFreeSpinRequest.getCurrencyCode()).build());
ApiException.notNull(gameSecretKey, ErrorCode.CURRENCY_NOT_EXIST.getCode());
Member member = memberService.selectMemberByAccount(gameCreateFreeSpinRequest.getAccount(), gameCreateFreeSpinRequest.getCurrencyCode(), gameCreateFreeSpinRequest.getPlatformCode());
ApiException.notNull(member, ErrorCode.ACCOUNT_NOT_EXIST.getCode());
CreateFreeSpinRequestDTO createFreeSpinRequestDTO = CreateFreeSpinRequestDTO.builder()
.account(member.getGameAccount())
.currency(gameCreateFreeSpinRequest.getCurrencyCode())
.agentId(gameSecretKey.getCode())
.agentKey(gameSecretKey.getKey())
.referenceId(gameCreateFreeSpinRequest.getReferenceId())
.freeSpinValidity(gameCreateFreeSpinRequest.getFreeSpinValidity())
.numberOfRounds(gameCreateFreeSpinRequest.getNumberOfRounds())
.gameIds(gameCreateFreeSpinRequest.getGameIds())
.betValue(gameCreateFreeSpinRequest.getBetValue())
.startTime(gameCreateFreeSpinRequest.getStartTime())
.build();
return AjaxResult.success(iGamesService.createFreeSpin(createFreeSpinRequestDTO));
}
/**
*
*
* @param gameCreateFreeSpinRequest
* @return {@link AjaxResult }
*/
@PostMapping("/get/bet/record")
public TableDataInfo getBetRecord(@Validated @RequestBody GameGetBetRecordRequest gameCreateFreeSpinRequest) {
TenantSecretKey tenantSecretKey = keyConfig.get();
startPage();
PageHelper.startPage(gameCreateFreeSpinRequest.getPageNo(), gameCreateFreeSpinRequest.getPageSize(), "wagers_time desc");
GameBettingDetailsDTO gameBettingDetails = GameBettingDetailsDTO.builder()
.platformCode(gameCreateFreeSpinRequest.getPlatformCode())
.currencyCode(gameCreateFreeSpinRequest.getCurrencyCode())
.tenantKey(tenantSecretKey.getTenantKey())
.timeType(gameCreateFreeSpinRequest.getTimeType())
.build();
Map<String, Object> params = gameBettingDetails.getParams();
params.put("beginTime", gameCreateFreeSpinRequest.getBeginTime());
params.put("endTime", gameCreateFreeSpinRequest.getEndTime());
List<GameBettingDetails> bettingDetails = gameBettingDetailsService.selectGameBettingDetailsList(gameBettingDetails);
TableDataInfo dataTable = getDataTable(bettingDetails);
List<GameBettingDetailsResponse> result = new ArrayList<>();
for (GameBettingDetails row : (List<GameBettingDetails>) dataTable.getRows()) {
GameBettingDetailsResponse gameBettingDetailsResponse = new GameBettingDetailsResponse();
BeanUtils.copyProperties(row, gameBettingDetailsResponse);
Member member = memberService.selectMemberById(row.getMemberId());
gameBettingDetailsResponse.setAccount(member.getMemberAccount());
result.add(gameBettingDetailsResponse);
}
dataTable.setRows(result);
return dataTable;
}
/**
*
*
* @param gameGetDetailRequest
* @return {@link AjaxResult }
*/
@PostMapping("/get/detail")
public AjaxResult getDetail(@Validated @RequestBody GameGetDetailRequest gameGetDetailRequest) {
GameSecretKeyCurrencyDTO gameSecretKey = gameSecretKeyCurrencyService.findByGameSecretKeyCurrencyDTO(GameSecretKeyCurrencyDTO.builder()
.platformCode(gameGetDetailRequest.getPlatformCode())
.systemCurrency(gameGetDetailRequest.getCurrencyCode()).build());
ApiException.notNull(gameSecretKey, ErrorCode.CURRENCY_NOT_EXIST.getCode());
IGamesService iGamesService = gamesService.get(gameGetDetailRequest.getPlatformCode() + Constants.SERVICE);
ApiException.notNull(iGamesService, ErrorCode.PLATFORM_NOT_EXIST.getCode());
GameSecretKeyLangDTO gameSecretKeyLang = gameSecretKeyLangService.findGameSecretKeyLangDTO(GameSecretKeyLangDTO.builder()
.platformCode(gameGetDetailRequest.getPlatformCode())
.systemLangCode(gameGetDetailRequest.getLangCode())
.build());
ApiException.notNull(gameSecretKeyLang, ErrorCode.LANG_NOT_EXIST.getCode());
GetGameDetailResponseDTO gameDetail = iGamesService.getGameDetail(GetGameDetailRequestDTO.builder()
.wagersId(gameGetDetailRequest.getWagersId())
.lang(gameSecretKeyLang.getLang())
.agentId(gameSecretKey.getCode())
.agentKey(gameSecretKey.getKey())
.build());
return AjaxResult.success(gameDetail);
}
/**
*
*
* @param gameKickMemeberRequest
* @return {@link AjaxResult }
*/
@PostMapping("/kick/member")
public AjaxResult kickMember(@Validated @RequestBody GameKickMemeberRequest gameKickMemeberRequest) {
GameSecretKeyCurrencyDTO gameSecretKey = gameSecretKeyCurrencyService.findByGameSecretKeyCurrencyDTO(GameSecretKeyCurrencyDTO.builder()
.platformCode(gameKickMemeberRequest.getPlatformCode())
.systemCurrency(gameKickMemeberRequest.getCurrencyCode()).build());
ApiException.notNull(gameSecretKey, ErrorCode.CURRENCY_NOT_EXIST.getCode());
Member member = memberService.selectMemberByAccount(gameKickMemeberRequest.getAccount(), gameKickMemeberRequest.getCurrencyCode(), gameKickMemeberRequest.getPlatformCode());
ApiException.notNull(member, ErrorCode.ACCOUNT_NOT_EXIST.getCode());
IGamesService iGamesService = gamesService.get(gameKickMemeberRequest.getPlatformCode() + Constants.SERVICE);
ApiException.notNull(iGamesService, ErrorCode.PLATFORM_NOT_EXIST.getCode());
return AjaxResult.success(iGamesService.kickMember(KickMemberRequestDTO.builder()
.account(member.getGameAccount())
.agentId(gameSecretKey.getCode())
.currency(gameSecretKey.getCurrency())
.agentKey(gameSecretKey.getKey())
.build()));
}
@PostMapping("/kick/member/all")
public AjaxResult kickMemberAll(@Validated @RequestBody GameKickMemeberAllRequest gameKickMemeberAllRequest) {
GameSecretKeyCurrencyDTO gameSecretKey = gameSecretKeyCurrencyService.findByGameSecretKeyCurrencyDTO(GameSecretKeyCurrencyDTO.builder()
.platformCode(gameKickMemeberAllRequest.getPlatformCode())
.systemCurrency(gameKickMemeberAllRequest.getCurrencyCode()).build());
ApiException.notNull(gameSecretKey, ErrorCode.CURRENCY_NOT_EXIST.getCode());
IGamesService iGamesService = gamesService.get(gameKickMemeberAllRequest.getPlatformCode() + Constants.SERVICE);
ApiException.notNull(iGamesService, ErrorCode.PLATFORM_NOT_EXIST.getCode());
KickMemberAllDTO kickMemberAllDTO = KickMemberAllDTO.builder()
.agentId(gameSecretKey.getCode())
.agentKey(gameSecretKey.getKey())
.currency(gameSecretKey.getCurrency())
.build();
if (!ObjectUtils.isEmpty(gameKickMemeberAllRequest.getGameId())) {
Game game = gameService.selectGameById(gameKickMemeberAllRequest.getGameId());
ApiException.notNull(game, ErrorCode.GAME_NOT_EXIST.getCode());
kickMemberAllDTO.setGameId(game.getGameCode());
}
return AjaxResult.success(iGamesService.kickMemberAll(kickMemberAllDTO));
}
/**
*
*
* @param gameGetFreeSpinDashflowRequest dashflow
* @return {@link TableDataInfo }
*/
@PostMapping("/get/free/spin/dashflow")
public TableDataInfo getFreeSpinDashflow(@Validated @RequestBody GameGetFreeSpinDashflowRequest gameGetFreeSpinDashflowRequest) {
PageHelper.startPage(gameGetFreeSpinDashflowRequest.getPageNo(), gameGetFreeSpinDashflowRequest.getPageSize(), "free_update_time desc");
GameFreeRecord gameFreeRecord = GameFreeRecord.builder()
.gameId(gameGetFreeSpinDashflowRequest.getGameId())
.platformCode(gameGetFreeSpinDashflowRequest.getPlatformCode())
.currencyCode(gameGetFreeSpinDashflowRequest.getCurrencyCode())
.build();
Map<String, Object> params = gameFreeRecord.getParams();
params.put("beginTime", gameGetFreeSpinDashflowRequest.getBeginTime());
params.put("endTime", gameGetFreeSpinDashflowRequest.getEndTime());
List<GameFreeRecord> gameFreeRecords = gameFreeRecordService.selectGameFreeRecordList(gameFreeRecord);
TableDataInfo dataTable = getDataTable(gameFreeRecords);
List<GameFreeRecordResponse> result = new ArrayList<>();
for (GameFreeRecord row : (List<GameFreeRecord>) dataTable.getRows()) {
GameFreeRecordResponse gameFreeRecordResponse = new GameFreeRecordResponse();
BeanUtils.copyProperties(row, gameFreeRecordResponse);
Member member = memberService.selectMemberById(row.getMemberId());
gameFreeRecordResponse.setMemberAccount(member.getMemberAccount());
result.add(gameFreeRecordResponse);
}
dataTable.setRows(result);
return dataTable;
}
/**
*
*
* @param gameGetFreeSpinDashflowRequest dashflow
* @return {@link TableDataInfo }
*/
@PostMapping("/cancel/free/spin")
public AjaxResult cancelFreeSpin(@Validated @RequestBody GameCancelFreeSpinRequest gameGetFreeSpinDashflowRequest) {
GameSecretKeyCurrencyDTO gameSecretKey = gameSecretKeyCurrencyService.findByGameSecretKeyCurrencyDTO(GameSecretKeyCurrencyDTO.builder()
.platformCode(gameGetFreeSpinDashflowRequest.getPlatformCode())
.systemCurrency(gameGetFreeSpinDashflowRequest.getCurrencyCode()).build());
ApiException.notNull(gameSecretKey, ErrorCode.CURRENCY_NOT_EXIST.getCode());
IGamesService iGamesService = gamesService.get(gameGetFreeSpinDashflowRequest.getPlatformCode() + Constants.SERVICE);
ApiException.notNull(iGamesService, ErrorCode.PLATFORM_NOT_EXIST.getCode());
Boolean cancelFreeSpin = iGamesService.cancelFreeSpin(CancelFreeSpinRequestDTO.builder()
.agentId(gameSecretKey.getCode())
.agentKey(gameSecretKey.getKey())
.referenceId(gameGetFreeSpinDashflowRequest.getReferenceId())
.build());
return AjaxResult.success(cancelFreeSpin);
}
/**
*
*
* @param gameExchangeBalanceAllRequest api
* @return {@link AjaxResult }
*/
@PostMapping("/exchange/balance/all")
public AjaxResult exchangeBalanceAll(@Validated @RequestBody GameExchangeBalanceAllRequest gameExchangeBalanceAllRequest) {
TenantSecretKey tenantSecretKey = keyConfig.get();
List<GameSecretKeyCurrencyDTO> gameSecretKeys = gameSecretKeyCurrencyService.findByGameSecretKeyCurrencyDTOList(GameSecretKeyCurrencyDTO.builder()
.systemCurrency(gameExchangeBalanceAllRequest.getCurrencyCode()).build());
// 创建线程池
Map<String, BigDecimal> balanceMap = new LinkedHashMap<>();
CountDownLatch latch = new CountDownLatch(gameSecretKeys.size());
// 使用List存储Future对象用于获取异步执行的结果
List<Future<Long>> futures = new ArrayList<>();
// 提交异步任务到线程池
for (GameSecretKeyCurrencyDTO gameSecretKeyCurrencyDTO : gameSecretKeys) {
futures.add(threadPoolTaskExecutor.submit(() -> {
try {
IGamesService iGamesService = gamesService.get(gameSecretKeyCurrencyDTO.getPlatformCode() + Constants.SERVICE);
Member member = memberService.selectMemberByAccount(gameExchangeBalanceAllRequest.getAccount(), gameExchangeBalanceAllRequest.getCurrencyCode(), gameSecretKeyCurrencyDTO.getPlatformCode());
ApiException.notNull(member, ErrorCode.ACCOUNT_NOT_EXIST.getCode());
//操作第三方钱包
ExchangeTransferMoneyRequestDTO exchangeTransferMoneyRequestDTO = ExchangeTransferMoneyRequestDTO.builder()
.agentId(gameSecretKeyCurrencyDTO.getCode())
.agentKey(gameSecretKeyCurrencyDTO.getKey())
.orderId(gameExchangeBalanceAllRequest.getOrderId())
.amount(BigDecimal.ONE)
.currency(gameSecretKeyCurrencyDTO.getCurrency())
.tenantKey(tenantSecretKey.getTenantKey())
.account(member.getGameAccount())
.transferType(TransferType.ALL.getCode())
.build();
return iGamesService.exchangeTransferByAgentId(exchangeTransferMoneyRequestDTO);
} catch (Exception e) {
return 0L;
} finally {
latch.countDown(); // 任务完成后减少计数
}
}));
}
BigDecimal balanceAll = BigDecimal.ZERO;
try {
// 等待所有线程执行完毕
latch.await();
// 获取每个Future的结果
for (Future<Long> future : futures) {
// 汇总结果
Long id = future.get();
GameExchangeMoney gameExchangeMoney = gameExchangeMoneyService.selectGameExchangeMoneyById(id);
if (ObjectUtils.isEmpty(gameExchangeMoney)) {
continue;
}
Member member = memberService.selectMemberById(gameExchangeMoney.getMemberId());
balanceMap.put(gameExchangeMoney.getPlatformCode(), gameExchangeMoney.getBalance());
BigDecimal balance = gameExchangeMoney.getBalance();
balanceAll = NumberUtil.add(balanceAll, balance);
//操作租户额度
tenantGameQuotaService.gameBalanceExchange(GameBalanceExchange.builder()
.platformCode(gameExchangeMoney.getPlatformCode())
.currencyCode(gameExchangeMoney.getCurrencyCode())
.sourceId(gameExchangeBalanceAllRequest.getOrderId())
.transferType(TransferType.ALL.getCode())
.amount(gameExchangeMoney.getBalance())
.account(member.getMemberAccount())
.tenantKey(tenantSecretKey.getTenantKey())
.build());
}
} catch (Exception e) {
log.error("拉回用户余额失败", e);
throw new BaseException("拉回用户余额失败");
}
balanceMap.put("balanceAll", balanceAll);
return AjaxResult.success(balanceMap);
}
2025-02-11 15:27:15 +08:00
}