71 lines
3.1 KiB
C#
71 lines
3.1 KiB
C#
using MetaAssets;
|
|
using ServerBase;
|
|
namespace BrokerApiCore;
|
|
public class DailyAmountLimitChecker
|
|
{
|
|
private readonly PlanetItemExchangeOrderAmountTotalLimitRepo m_planet_item_exchange_order_amount_total_limit;
|
|
private readonly PlanetItemExchangeOrderAmountUserLimitRepo m_planet_item_exchange_order_amount_user_limit;
|
|
|
|
public DailyAmountLimitChecker(
|
|
PlanetItemExchangeOrderAmountTotalLimitRepo planetItemExchangeOrderAmountTotalLimit,
|
|
PlanetItemExchangeOrderAmountUserLimitRepo planetItemExchangeOrderAmountUserLimit)
|
|
{
|
|
m_planet_item_exchange_order_amount_total_limit = planetItemExchangeOrderAmountTotalLimit;
|
|
m_planet_item_exchange_order_amount_user_limit = planetItemExchangeOrderAmountUserLimit;
|
|
}
|
|
|
|
public async Task<(Result result,
|
|
PlanetItemExchangeOrderAmountTotalLimit? totalLimit,
|
|
PlanetItemExchangeOrderAmountUserLimit? userLimit)>
|
|
dailyLimitCheck(PlanetItemExchangeOrder order,
|
|
PlanetItemExchangePolicyMetaData exchangePolicy, CancellationToken cancellationToken = default)
|
|
{
|
|
var result = new Result();
|
|
PlanetItemExchangeOrderAmountTotalLimit? total_limit = null;
|
|
PlanetItemExchangeOrderAmountUserLimit? user_limit = null;
|
|
try
|
|
{
|
|
var exchange_date = DateOnly.FromDateTime(order.CreatedAt.ToUniversalTime());
|
|
|
|
var total_daily_limit = exchangePolicy.DailyTotalAmountLimit;
|
|
(result, total_limit) =
|
|
await m_planet_item_exchange_order_amount_total_limit.findByExchangeMetaAndDateOrCreate(
|
|
order.ExchangeMetaId,
|
|
exchange_date,
|
|
order.SeasonId,
|
|
cancellationToken);
|
|
Guard.Against.resultFail(result);
|
|
Guard.Against.isNull(total_limit, ServerErrorCode.RdbError,
|
|
()=>"PlanetItemExchangeOrderAmountTotalLimit not found");
|
|
Guard.Against.isTrue(total_limit.DailyAmount + order.ExchangeMetaAmount > total_daily_limit,
|
|
ServerErrorCode.ExchangeTotalOrderDailyLimitExceeded,
|
|
()=>$"Daily total amount limit exceeded [{exchangePolicy.ID}] => current_total_daily_limit:{total_limit.DailyAmount} + order.ExchangeMetaAmount:{order.ExchangeMetaAmount} > total_daily_limit:{total_daily_limit}"
|
|
);
|
|
|
|
var user_daily_limit = exchangePolicy.DailyUserAmountLimit;
|
|
(result, user_limit) =
|
|
await m_planet_item_exchange_order_amount_user_limit.findByExchangeMetaAndDateOrCreate(order.ExchangeMetaId,
|
|
order.UserGuid, exchange_date, order.SeasonId, cancellationToken);
|
|
Guard.Against.resultFail(result);
|
|
Guard.Against.isNull(user_limit, ServerErrorCode.RdbError,
|
|
()=>"PlanetItemExchangeOrderAmountUserLimit not found");
|
|
Guard.Against.isTrue(user_limit.DailyAmount + order.ExchangeMetaAmount > user_daily_limit,
|
|
ServerErrorCode.ExchangeUserOrderDailyLimitExceeded,
|
|
()=>$"Daily user amount limit exceeded [{exchangePolicy.ID}] => current_user_daily_limit:{user_limit.DailyAmount} + order.ExchangeMetaAmount:{order.ExchangeMetaAmount} > user_daily_limit:{user_daily_limit}"
|
|
);
|
|
}
|
|
catch (ApiException ex)
|
|
{
|
|
result.setFail((ServerErrorCode)ex.ErrorCode, ex.Message);
|
|
return (result, total_limit, user_limit);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.setFail(ServerErrorCode.InternalServerError, ex.Message);
|
|
return (result, total_limit, user_limit);
|
|
}
|
|
|
|
return (result, total_limit, user_limit);
|
|
}
|
|
}
|