319 lines
12 KiB
Java
319 lines
12 KiB
Java
package com.ff.api.controller;
|
||
|
||
|
||
import com.ff.annotation.CheckHeader;
|
||
import com.ff.api.request.MemberCreateApiRequest;
|
||
import com.ff.api.request.MemberInfoAllApiRequest;
|
||
import com.ff.api.request.MemberInfoApiRequest;
|
||
import com.ff.api.response.MemberInfoAllResponse;
|
||
import com.ff.api.response.MemberInfoResponse;
|
||
import com.ff.base.constant.Constants;
|
||
import com.ff.base.core.controller.BaseController;
|
||
import com.ff.base.core.domain.AjaxResult;
|
||
import com.ff.base.enums.ErrorCode;
|
||
import com.ff.base.enums.GamePlatforms;
|
||
import com.ff.base.exception.base.ApiException;
|
||
import com.ff.base.exception.base.BaseException;
|
||
import com.ff.base.system.domain.TenantSecretKey;
|
||
import com.ff.base.utils.StringUtils;
|
||
import com.ff.config.KeyConfig;
|
||
import com.ff.game.api.IGamesService;
|
||
import com.ff.game.api.request.CreateMemberRequestDTO;
|
||
import com.ff.game.api.request.MemberInfoRequestDTO;
|
||
import com.ff.game.api.request.MemberInfoResponseDTO;
|
||
import com.ff.game.domain.KeyInfo;
|
||
import com.ff.game.domain.Platform;
|
||
import com.ff.game.service.IPlatformService;
|
||
import com.ff.member.domain.Member;
|
||
import com.ff.member.service.IMemberService;
|
||
import lombok.Data;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.beans.BeanUtils;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.beans.factory.annotation.Qualifier;
|
||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
import org.springframework.util.Assert;
|
||
import org.springframework.util.ObjectUtils;
|
||
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;
|
||
import java.math.BigDecimal;
|
||
import java.util.ArrayList;
|
||
import java.util.LinkedHashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.concurrent.CountDownLatch;
|
||
import java.util.concurrent.Future;
|
||
|
||
/**
|
||
* api控制器
|
||
*
|
||
* @author shi
|
||
* @date 2025/02/10
|
||
*/
|
||
@RestController
|
||
@CheckHeader
|
||
@RequestMapping("/api/member")
|
||
@Slf4j
|
||
public class ApiMemberController extends BaseController {
|
||
|
||
@Autowired
|
||
private Map<String, IGamesService> gamesService;
|
||
|
||
@Resource
|
||
private KeyConfig keyConfig;
|
||
|
||
@Resource
|
||
private IMemberService memberService;
|
||
|
||
@Autowired
|
||
@Qualifier("threadPoolTaskExecutor")
|
||
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
|
||
|
||
@Resource
|
||
private IPlatformService platformService;
|
||
|
||
/**
|
||
* 创建成员
|
||
*
|
||
* @param memberCreateApiRequest 创建成员api请求
|
||
* @return {@link AjaxResult }
|
||
*/
|
||
@PostMapping("/create")
|
||
@Transactional
|
||
public synchronized AjaxResult createMember(@Validated @RequestBody MemberCreateApiRequest memberCreateApiRequest) {
|
||
|
||
IGamesService iGamesService = gamesService.get(memberCreateApiRequest.getPlatformCode() + Constants.SERVICE);
|
||
ApiException.notNull(iGamesService, ErrorCode.PLATFORM_NOT_EXIST.getCode());
|
||
|
||
TenantSecretKey tenantSecretKey = keyConfig.get();
|
||
|
||
Platform platform = platformService.get(memberCreateApiRequest.getPlatformCode());
|
||
ApiException.notNull(platform, ErrorCode.CURRENCY_NOT_EXIST.getCode());
|
||
String targetCurrency = platform.getCurrencyInfo().get(memberCreateApiRequest.getCurrencyCode());
|
||
ApiException.notNull(targetCurrency, ErrorCode.CURRENCY_NOT_EXIST.getCode());
|
||
|
||
KeyInfo keyInfo = null;
|
||
for (KeyInfo keyData : platform.getKeyInfo()) {
|
||
if (StringUtils.isNotEmpty(memberCreateApiRequest.getCurrencyCode())) {
|
||
if (keyData.getCurrency().equalsIgnoreCase(memberCreateApiRequest.getCurrencyCode())) {
|
||
keyInfo = keyData;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
ApiException.notNull(keyInfo, ErrorCode.CURRENCY_NOT_EXIST.getCode());
|
||
|
||
String gameAccount =memberService.getMemberGameAccount(memberCreateApiRequest.getPlatformCode(), tenantSecretKey.getTenantSn());
|
||
|
||
|
||
// 获取用户信息
|
||
Member gameMember = memberService.selectMemberByAccount(memberCreateApiRequest.getAccount(), memberCreateApiRequest.getCurrencyCode(), memberCreateApiRequest.getPlatformCode());
|
||
if (!ObjectUtils.isEmpty(gameMember)) {
|
||
throw new ApiException(ErrorCode.GAME_ACCOUNT_CREATION_FAILED.getCode());
|
||
}
|
||
|
||
|
||
//注册本地账号
|
||
Member member = Member.builder()
|
||
.tenantKey(tenantSecretKey.getTenantKey())
|
||
.memberAccount(memberCreateApiRequest.getAccount())
|
||
.gameAccount(gameAccount)
|
||
.platformCode(memberCreateApiRequest.getPlatformCode())
|
||
.currencyCode(memberCreateApiRequest.getCurrencyCode())
|
||
.build();
|
||
int insertMember = memberService.insertMember(member);
|
||
Assert.isTrue(insertMember > 0, "建立游戏账号失败");
|
||
|
||
//向第三方注册账号
|
||
CreateMemberRequestDTO gamesBaseRequestDTO = CreateMemberRequestDTO.builder()
|
||
.account(gameAccount)
|
||
.agentId(keyInfo.getCode())
|
||
.agentKey(keyInfo.getKey())
|
||
.betLimit(memberCreateApiRequest.getBetLimit())
|
||
.platformType(memberCreateApiRequest.getPlatformType())
|
||
.currency(targetCurrency)
|
||
.vendor(platform)
|
||
.keyInfo(keyInfo)
|
||
.systemCurrency(memberCreateApiRequest.getCurrencyCode())
|
||
.build();
|
||
Boolean result = iGamesService.createMember(gamesBaseRequestDTO);
|
||
Assert.isTrue(result, "建立游戏账号失败");
|
||
|
||
|
||
|
||
|
||
return toAjax(Boolean.TRUE);
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* 获取会员信息
|
||
*
|
||
* @param request 成员信息api请求
|
||
* @return {@link AjaxResult }
|
||
*/
|
||
@PostMapping("/info")
|
||
public AjaxResult getMemberInfo(@Validated @RequestBody MemberInfoApiRequest request) {
|
||
IGamesService iGamesService = gamesService.get(request.getPlatformCode() + Constants.SERVICE);
|
||
ApiException.notNull(iGamesService, ErrorCode.PLATFORM_NOT_EXIST.getCode());
|
||
|
||
|
||
Platform platform = platformService.get(request.getPlatformCode());
|
||
ApiException.notNull(platform, ErrorCode.PLATFORM_NOT_EXIST.getCode());
|
||
|
||
String targetCurrency = platform.getCurrencyInfo().get(request.getCurrencyCode());
|
||
ApiException.notNull(targetCurrency, ErrorCode.CURRENCY_NOT_EXIST.getCode());
|
||
|
||
KeyInfo keyInfo = null;
|
||
for (KeyInfo keyData : platform.getKeyInfo()) {
|
||
if (StringUtils.isNotEmpty(request.getCurrencyCode())) {
|
||
if (keyData.getCurrency().equalsIgnoreCase(request.getCurrencyCode())) {
|
||
keyInfo = keyData;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
ApiException.notNull(keyInfo, ErrorCode.CURRENCY_NOT_EXIST.getCode());
|
||
|
||
// 获取用户信息
|
||
Member member = memberService.selectMemberByAccount(request.getAccount(), request.getCurrencyCode(), request.getPlatformCode());
|
||
ApiException.notNull(member, ErrorCode.ACCOUNT_NOT_EXIST.getCode());
|
||
|
||
|
||
//向第三方查询账号
|
||
MemberInfoRequestDTO gamesBaseRequestDTO = MemberInfoRequestDTO.builder()
|
||
.accounts(member.getGameAccount())
|
||
.agentId(keyInfo.getCode())
|
||
.currency(targetCurrency)
|
||
.agentKey(keyInfo.getKey())
|
||
.vendor(platform)
|
||
.keyInfo(keyInfo)
|
||
.systemCurrency(request.getCurrencyCode())
|
||
.build();
|
||
MemberInfoResponseDTO memberInfo = iGamesService.getMemberInfo(gamesBaseRequestDTO);
|
||
MemberInfoResponse memberInfoResponse = new MemberInfoResponse();
|
||
BeanUtils.copyProperties(memberInfo, memberInfoResponse);
|
||
return AjaxResult.success(memberInfoResponse);
|
||
}
|
||
|
||
|
||
/**
|
||
* 信息全部
|
||
*
|
||
* @param memberInfoAllApiRequest 成员信息所有api请求
|
||
* @return {@link AjaxResult }
|
||
*/
|
||
@PostMapping("/info/all")
|
||
public AjaxResult infoAll(@Validated @RequestBody MemberInfoAllApiRequest memberInfoAllApiRequest) {
|
||
|
||
|
||
// List<GameSecretKeyCurrencyDTO> gameSecretKeys = gameSecretKeyCurrencyService.findByGameSecretKeyCurrencyDTOList(
|
||
// GameSecretKeyCurrencyDTO.builder()
|
||
// .systemCurrency(memberInfoAllApiRequest.getCurrencyCode()).build());
|
||
List<Key> keys = new ArrayList<>();
|
||
for (GamePlatforms platformEnum : GamePlatforms.values()) {
|
||
|
||
Platform platform = platformService.get(platformEnum.getCode());
|
||
String targetCurrency = platform.getCurrencyInfo().get(memberInfoAllApiRequest.getCurrencyCode());
|
||
if (StringUtils.isEmpty(targetCurrency)) {
|
||
continue;
|
||
}
|
||
KeyInfo keyInfo = null;
|
||
for (KeyInfo keyData : platform.getKeyInfo()) {
|
||
if (StringUtils.isNotEmpty(memberInfoAllApiRequest.getCurrencyCode())) {
|
||
if (keyData.getCurrency().equalsIgnoreCase(memberInfoAllApiRequest.getCurrencyCode())) {
|
||
keyInfo = keyData;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
if (null == keyInfo) {
|
||
continue;
|
||
}
|
||
|
||
Key key = new Key();
|
||
key.setPlatformCode(platform.getPlatformCode());
|
||
key.setCode(keyInfo.getCode());
|
||
key.setKey(keyInfo.getKey());
|
||
key.setCurrency(targetCurrency);
|
||
key.setSystemCurrency(memberInfoAllApiRequest.getCurrencyCode());
|
||
keys.add(key);
|
||
}
|
||
// 创建线程池
|
||
Map<String, BigDecimal> balanceMap = new LinkedHashMap<>();
|
||
CountDownLatch latch = new CountDownLatch(keys.size());
|
||
|
||
// 使用List存储Future对象,用于获取异步执行的结果
|
||
List<Future<MemberInfoAllResponse>> futures = new ArrayList<>();
|
||
|
||
// 提交异步任务到线程池
|
||
for (Key gameSecretKey : keys) {
|
||
|
||
futures.add(threadPoolTaskExecutor.submit(() -> {
|
||
|
||
try {
|
||
IGamesService iGamesService = gamesService.get(gameSecretKey.getPlatformCode() + Constants.SERVICE);
|
||
|
||
// 获取用户信息
|
||
Member member = memberService.selectMemberByAccount(memberInfoAllApiRequest.getAccount(), memberInfoAllApiRequest.getCurrencyCode(), gameSecretKey.getPlatformCode());
|
||
ApiException.notNull(member, ErrorCode.ACCOUNT_NOT_EXIST.getCode());
|
||
|
||
|
||
MemberInfoRequestDTO gamesBaseRequestDTO = MemberInfoRequestDTO.builder()
|
||
.accounts(member.getGameAccount())
|
||
.agentId(gameSecretKey.getCode())
|
||
.currency(gameSecretKey.getCurrency())
|
||
.systemCurrency(gameSecretKey.getSystemCurrency())
|
||
.agentKey(gameSecretKey.getKey())
|
||
.build();
|
||
//查询余额
|
||
MemberInfoResponseDTO memberInfo = iGamesService.getMemberInfo(gamesBaseRequestDTO);
|
||
return MemberInfoAllResponse.builder()
|
||
.account(member.getGameAccount())
|
||
.balance(memberInfo.getBalance())
|
||
.status(memberInfo.getStatus())
|
||
.platformCode(gameSecretKey.getPlatformCode())
|
||
.build();
|
||
} finally {
|
||
latch.countDown(); // 任务完成后减少计数
|
||
}
|
||
}));
|
||
}
|
||
|
||
|
||
// 等待所有线程执行完毕
|
||
try {
|
||
latch.await();
|
||
// 获取每个Future的结果
|
||
for (Future<MemberInfoAllResponse> future : futures) {
|
||
// 汇总结果
|
||
MemberInfoAllResponse memberInfoAllResponse = future.get();
|
||
balanceMap.put(memberInfoAllResponse.getPlatformCode(), memberInfoAllResponse.getBalance());
|
||
}
|
||
} catch (Exception e) {
|
||
log.error("获取会员信息失败", e);
|
||
throw new BaseException("获取会员信息失败");
|
||
}
|
||
|
||
|
||
return AjaxResult.success(balanceMap);
|
||
}
|
||
|
||
@Data
|
||
class Key {
|
||
private String platformCode;
|
||
private String code;
|
||
private String currency;
|
||
private String key;
|
||
private String systemCurrency;
|
||
}
|
||
|
||
}
|