feat(game): 添加平台成本计算功能
- 在 Constants 中添加 HUNDRED 常量,用于成本计算 - 在 ITenantPlatformService 中添加 findTenantPlatform 方法,用于获取租户平台信息 - 修改 TenantGameQuotaFlowMapper,增加 platform_code 字段查询 - 更新 TenantGameQuotaServiceImpl 中的 balanceChanges 方法,加入平台成本计算逻辑 - 在 TenantPlatformMapper 中添加 findTenantPlatform 方法的 SQL 查询- 更新 TenantPlatformServiceImpl,实现 findTenantPlatform 方法 - 修改 TenantQuotaTask 中的 updateBalance 方法,考虑平台成本因素main-p
parent
dc6e3966ff
commit
b29e21816c
|
@ -2,6 +2,7 @@ package com.ff.base.constant;
|
|||
|
||||
import io.jsonwebtoken.Claims;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
|
@ -261,4 +262,8 @@ public class Constants {
|
|||
* 租户角色
|
||||
*/
|
||||
public static final String TENANT_ROLE = "tenant";
|
||||
/**
|
||||
* 一百
|
||||
*/
|
||||
public static final BigDecimal HUNDRED =new BigDecimal("100") ;
|
||||
}
|
||||
|
|
|
@ -20,6 +20,15 @@ public interface TenantPlatformMapper
|
|||
TenantPlatform selectTenantPlatformById(Long id);
|
||||
|
||||
|
||||
/**
|
||||
* 查找租户平台
|
||||
*
|
||||
* @param tenantPlatform 租户平台
|
||||
* @return {@link TenantPlatform }
|
||||
*/
|
||||
TenantPlatform findTenantPlatform(TenantPlatform tenantPlatform);
|
||||
|
||||
|
||||
/**
|
||||
* 按租户id选择货币代码
|
||||
*
|
||||
|
|
|
@ -19,6 +19,15 @@ public interface ITenantPlatformService
|
|||
*/
|
||||
TenantPlatform selectTenantPlatformById(Long id);
|
||||
|
||||
/**
|
||||
* 查找租户平台
|
||||
*
|
||||
* @param tenantPlatform 租户平台
|
||||
* @return {@link TenantPlatform }
|
||||
*/
|
||||
TenantPlatform findTenantPlatform(TenantPlatform tenantPlatform);
|
||||
|
||||
|
||||
/**
|
||||
* 查询租户成本管理列表
|
||||
*
|
||||
|
|
|
@ -35,6 +35,17 @@ public class TenantPlatformServiceImpl implements ITenantPlatformService
|
|||
return tenantPlatformMapper.selectTenantPlatformById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找租户平台
|
||||
*
|
||||
* @param tenantPlatform 租户平台
|
||||
* @return {@link TenantPlatform }
|
||||
*/
|
||||
@Override
|
||||
public TenantPlatform findTenantPlatform(TenantPlatform tenantPlatform) {
|
||||
return tenantPlatformMapper.findTenantPlatform(tenantPlatform);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询租户成本管理列表
|
||||
*
|
||||
|
|
|
@ -37,6 +37,19 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
where id = #{id}
|
||||
</select>
|
||||
|
||||
|
||||
<select id="findTenantPlatform" parameterType="TenantPlatform" resultMap="TenantPlatformResult">
|
||||
<include refid="selectTenantPlatformVo"/>
|
||||
<where>
|
||||
<if test="tenantId != null "> and tenant_id = #{tenantId}</if>
|
||||
<if test="platformCode != null and platformCode != ''"> and platform_code = #{platformCode}</if>
|
||||
<if test="currencyCode != null and currencyCode != ''"> and currency_code = #{currencyCode}</if>
|
||||
<if test="cost != null "> and cost = #{cost}</if>
|
||||
<if test="useCost != null "> and use_cost = #{useCost}</if>
|
||||
</where>
|
||||
limit 1
|
||||
</select>
|
||||
|
||||
<select id="selectCurrencyCodeByTenantId" >
|
||||
select currency_code from ff_tenant_platform
|
||||
|
||||
|
|
|
@ -10,6 +10,8 @@ import cn.hutool.core.util.NumberUtil;
|
|||
import com.ff.base.constant.Constants;
|
||||
import com.ff.base.enums.*;
|
||||
import com.ff.base.exception.base.ApiException;
|
||||
import com.ff.base.system.domain.TenantPlatform;
|
||||
import com.ff.base.system.service.ITenantPlatformService;
|
||||
import com.ff.base.utils.DateUtils;
|
||||
import com.ff.base.utils.QuotaUtils;
|
||||
import com.ff.base.utils.StringUtils;
|
||||
|
@ -73,6 +75,8 @@ public class TenantGameQuotaServiceImpl implements ITenantGameQuotaService {
|
|||
@Resource
|
||||
private ITenantQuotaExchangeService tenantQuotaExchangeService;
|
||||
|
||||
@Resource
|
||||
private ITenantPlatformService tenantPlatformService;
|
||||
|
||||
/**
|
||||
* 查询租户游戏配额
|
||||
|
@ -285,9 +289,20 @@ public class TenantGameQuotaServiceImpl implements ITenantGameQuotaService {
|
|||
TenantQuotaExchange tenantQuotaExchange = tenantQuotaExchangeService.getTenantQuotaExchange(Constants.USDT, gameBalanceExchange.getCurrencyCode());
|
||||
ApiException.notNull(tenantQuotaExchange, ErrorCode.CURRENCY_EXCHANGE.getCode());
|
||||
|
||||
|
||||
// 获取租户信息
|
||||
TenantSecretKey tenantSecretKey = tenantSecretKeyService.selectTenantSecretKeyByTenantKey(gameBalanceExchange.getTenantKey());
|
||||
|
||||
|
||||
// 获取平台币种格外成本
|
||||
TenantPlatform tenantPlatform = tenantPlatformService.findTenantPlatform(TenantPlatform.builder()
|
||||
.tenantId(tenantSecretKey.getId())
|
||||
.currencyCode(gameBalanceExchange.getCurrencyCode())
|
||||
.platformCode(gameBalanceExchange.getPlatformCode())
|
||||
.build());
|
||||
// 检查获取平台币种格外成本是否存在,否则抛出异常
|
||||
ApiException.notNull(tenantPlatform, ErrorCode.PLATFORM_NOT_EXIST.getCode());
|
||||
|
||||
|
||||
// 获取用户信息
|
||||
Member member = memberService.selectMemberByGameAccount(StringUtils.addSuffix(gameBalanceExchange.getAccount(), gameBalanceExchange.getCurrencyCode() + tenantSecretKey.getTenantSn()));
|
||||
// 检查用户是否存在,否则抛出异常
|
||||
|
@ -317,6 +332,7 @@ public class TenantGameQuotaServiceImpl implements ITenantGameQuotaService {
|
|||
.agentKey(gameSecretKey.getKey())
|
||||
.build();
|
||||
balanceRequestAmount = iGamesService.getMemberInfo(gamesBaseRequestDTO).getBalance();
|
||||
balanceRequestAmount = NumberUtil.add(balanceRequestAmount, NumberUtil.mul(balanceRequestAmount, NumberUtil.div(tenantPlatform.getUseCost(), Constants.HUNDRED)));
|
||||
|
||||
// 计算累计转入和转出金额
|
||||
BigDecimal balanceInto = tenantGameQuotaFlowService.getExchangeMoneyByMemberId(TenantGameQuotaFlow.builder()
|
||||
|
@ -359,7 +375,7 @@ public class TenantGameQuotaServiceImpl implements ITenantGameQuotaService {
|
|||
|
||||
// 进行租户余额调整
|
||||
this.balanceChanges(BalanceChangesDTO.builder()
|
||||
.isOut(isOut)
|
||||
.isOut(Boolean.TRUE)
|
||||
.sourceId(gameBalanceExchange.getSourceId())
|
||||
.platformCode(gameBalanceExchange.getPlatformCode())
|
||||
.currencyCode(gameBalanceExchange.getCurrencyCode())
|
||||
|
@ -373,13 +389,18 @@ public class TenantGameQuotaServiceImpl implements ITenantGameQuotaService {
|
|||
.build());
|
||||
|
||||
} else {
|
||||
|
||||
// 如果是扣账操作,首先处理假额度
|
||||
String falseTenantQuotaType = QuotaUtils.getFalseTenantQuota(gameBalanceExchange.getPlatformCode(), gameBalanceExchange.getCurrencyCode());
|
||||
TenantGameQuota falseTenantQuota = selectTenantGameQuotaByTenantKey(tenantSecretKey.getTenantKey(), falseTenantQuotaType);
|
||||
balanceRequestAmount = NumberUtil.add(gameBalanceExchange.getAmount(), NumberUtil.mul(gameBalanceExchange.getAmount(), NumberUtil.div(tenantPlatform.getUseCost(), Constants.HUNDRED)));
|
||||
if (falseTenantQuota.getBalance().compareTo(BigDecimal.ZERO) > 0) {
|
||||
BigDecimal amountToDeduct = gameBalanceExchange.getAmount();
|
||||
|
||||
|
||||
|
||||
|
||||
BigDecimal falseQuotaBalance = falseTenantQuota.getBalance();
|
||||
if (falseQuotaBalance.compareTo(amountToDeduct) >= 0) {
|
||||
if (falseQuotaBalance.compareTo(balanceRequestAmount) >= 0) {
|
||||
// 假额度足够扣除本次所需金额
|
||||
this.balanceChanges(BalanceChangesDTO.builder()
|
||||
.isOut(Boolean.FALSE)
|
||||
|
@ -387,7 +408,7 @@ public class TenantGameQuotaServiceImpl implements ITenantGameQuotaService {
|
|||
.platformCode(gameBalanceExchange.getPlatformCode())
|
||||
.currencyCode(gameBalanceExchange.getCurrencyCode())
|
||||
.tenantKey(tenantSecretKey.getTenantKey())
|
||||
.balance(amountToDeduct)
|
||||
.balance(balanceRequestAmount)
|
||||
.memberId(member.getId())
|
||||
.operationType(OperationType.API_BALANCE.getCode())
|
||||
.quotaType(falseTenantQuotaType)
|
||||
|
@ -396,7 +417,7 @@ public class TenantGameQuotaServiceImpl implements ITenantGameQuotaService {
|
|||
balanceRequestAmount = BigDecimal.ZERO;
|
||||
} else {
|
||||
// 假额度不足以扣除本次所需金额,扣除全部假额度
|
||||
balanceRequestAmount = amountToDeduct.subtract(falseQuotaBalance);
|
||||
balanceRequestAmount = balanceRequestAmount.subtract(falseQuotaBalance);
|
||||
this.balanceChanges(BalanceChangesDTO.builder()
|
||||
.isOut(Boolean.FALSE)
|
||||
.sourceId(gameBalanceExchange.getSourceId())
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
package com.ff.quartz.task;
|
||||
|
||||
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
import com.ff.base.constant.Constants;
|
||||
import com.ff.base.enums.OperationType;
|
||||
import com.ff.base.enums.QuotaType;
|
||||
import com.ff.base.system.domain.TenantPlatform;
|
||||
import com.ff.base.system.domain.TenantSecretKey;
|
||||
import com.ff.base.system.service.ITenantPlatformService;
|
||||
import com.ff.base.utils.DateUtils;
|
||||
import com.ff.common.domain.TenantGameQuotaFlow;
|
||||
import com.ff.common.domain.TenantQuotaExchange;
|
||||
|
@ -42,6 +46,9 @@ public class TenantQuotaTask {
|
|||
@Resource
|
||||
private ITenantQuotaExchangeService tenantQuotaExchangeService;
|
||||
|
||||
@Resource
|
||||
private ITenantPlatformService tenantPlatformService;
|
||||
|
||||
/**
|
||||
* 更新租户实际配额
|
||||
*/
|
||||
|
@ -64,6 +71,7 @@ public class TenantQuotaTask {
|
|||
balance = balance.abs();
|
||||
isOut = Boolean.FALSE;
|
||||
}
|
||||
|
||||
TenantQuotaExchange tenantQuotaExchange = tenantQuotaExchangeService.getTenantQuotaExchange(Constants.USDT, tenantGameQuotaFlow.getCurrencyCode());
|
||||
//更新额度
|
||||
BalanceRealChangesDTO realChangesDTO = BalanceRealChangesDTO.builder()
|
||||
|
|
|
@ -326,11 +326,12 @@
|
|||
<select id="getBalanceByTenantKey" resultMap="TenantGameQuotaFlowResult">
|
||||
select tenant_key,
|
||||
sum(if(is_out, exchange_money, 0)) - sum(if(!is_out, exchange_money, 0)) as exchange_money,
|
||||
currency_code
|
||||
currency_code,
|
||||
platform_code
|
||||
from ff_tenant_game_quota_flow
|
||||
where create_time between #{params.beginTime} and #{params.endTime}
|
||||
and quota_type = #{quotaType}
|
||||
and operation_type = #{operationType}
|
||||
group by tenant_key, currency_code
|
||||
and operation_type = #{operationType} and currency_code is not null and platform_code is not null
|
||||
group by tenant_key,platform_code, currency_code
|
||||
</select>
|
||||
</mapper>
|
Loading…
Reference in New Issue