2024-03-13 18:01:38 +08:00
|
|
|
|
using Volo.Abp.Domain.Services;
|
2024-03-14 00:29:01 +08:00
|
|
|
|
using Volo.Abp.EventBus.Local;
|
2024-03-13 18:01:38 +08:00
|
|
|
|
using Yi.Framework.Bbs.Domain.Entities.Bank;
|
2024-03-15 19:12:54 +08:00
|
|
|
|
using Yi.Framework.Bbs.Domain.Managers.BankValue;
|
2024-03-14 00:29:01 +08:00
|
|
|
|
using Yi.Framework.Bbs.Domain.Shared.Enums;
|
|
|
|
|
|
using Yi.Framework.Bbs.Domain.Shared.Etos;
|
2024-03-15 19:12:54 +08:00
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Dtos;
|
2024-03-14 00:29:01 +08:00
|
|
|
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
2024-03-15 19:12:54 +08:00
|
|
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
2024-03-13 18:01:38 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Yi.Framework.Bbs.Domain.Managers
|
|
|
|
|
|
{
|
2024-03-15 19:12:54 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 银行领域,进阶了哦~
|
|
|
|
|
|
/// </summary>
|
2024-03-13 18:01:38 +08:00
|
|
|
|
public class BankManager : DomainService
|
|
|
|
|
|
{
|
2024-08-15 21:38:32 +08:00
|
|
|
|
private const decimal DefalutRate = 1.2m;
|
2024-05-22 14:35:08 +08:00
|
|
|
|
private ISqlSugarRepository<BankCardAggregateRoot> _repository;
|
2024-03-14 00:29:01 +08:00
|
|
|
|
private ILocalEventBus _localEventBus;
|
2024-05-22 14:35:08 +08:00
|
|
|
|
private ISqlSugarRepository<InterestRecordsAggregateRoot> _interestRepository;
|
2024-03-15 19:12:54 +08:00
|
|
|
|
private IBankValueProvider _bankValueProvider;
|
2024-05-22 14:35:08 +08:00
|
|
|
|
public BankManager(ISqlSugarRepository<BankCardAggregateRoot> repository, ILocalEventBus localEventBus, ISqlSugarRepository<InterestRecordsAggregateRoot> interestRepository, IBankValueProvider bankValueProvider)
|
2024-03-14 00:29:01 +08:00
|
|
|
|
{
|
|
|
|
|
|
_repository = repository;
|
|
|
|
|
|
_localEventBus = localEventBus;
|
|
|
|
|
|
_interestRepository = interestRepository;
|
2024-06-26 12:46:39 +08:00
|
|
|
|
_bankValueProvider = bankValueProvider;
|
2024-03-14 00:29:01 +08:00
|
|
|
|
}
|
2024-03-13 18:01:38 +08:00
|
|
|
|
|
2024-03-15 19:12:54 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取当前银行汇率
|
|
|
|
|
|
/// </summary>
|
2024-06-27 17:06:30 +08:00
|
|
|
|
public BankInterestRecordDto CurrentRate => GetCurrentInterestRate().GetAwaiter().GetResult();
|
2024-03-15 19:12:54 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 用于存储当前汇率数据
|
|
|
|
|
|
/// </summary>
|
2024-06-13 15:56:58 +08:00
|
|
|
|
private static BankInterestRecordDto? _currentRateStore;
|
2024-03-15 19:12:54 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取当前的银行汇率,如果为空会从数据库拿最新一条
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2024-06-27 17:06:30 +08:00
|
|
|
|
public async Task<BankInterestRecordDto> GetCurrentInterestRate()
|
2024-03-14 00:29:01 +08:00
|
|
|
|
{
|
2024-03-15 19:12:54 +08:00
|
|
|
|
var output = new BankInterestRecordDto();
|
2024-03-14 00:29:01 +08:00
|
|
|
|
//先判断时间是否与当前时间差1小时,小于1小时直接返回即可,可以由一个单例类提供
|
2024-06-13 15:56:58 +08:00
|
|
|
|
if (_currentRateStore is null || _currentRateStore.IsExpire())
|
2024-03-15 19:12:54 +08:00
|
|
|
|
{
|
2024-06-27 17:06:30 +08:00
|
|
|
|
var currentInterestRecords =await CreateInterestRecordsAsync();
|
2024-03-15 19:12:54 +08:00
|
|
|
|
output.ComparisonValue = currentInterestRecords.ComparisonValue;
|
|
|
|
|
|
output.CreationTime = currentInterestRecords.CreationTime;
|
|
|
|
|
|
output.Value = currentInterestRecords.Value;
|
2024-03-16 17:57:17 +08:00
|
|
|
|
|
2024-06-26 12:46:39 +08:00
|
|
|
|
_currentRateStore = new BankInterestRecordDto()
|
|
|
|
|
|
{
|
|
|
|
|
|
ComparisonValue = currentInterestRecords.ComparisonValue,
|
|
|
|
|
|
CreationTime = currentInterestRecords.CreationTime,
|
|
|
|
|
|
Value = currentInterestRecords.Value
|
|
|
|
|
|
};
|
2024-03-16 17:57:17 +08:00
|
|
|
|
|
2024-03-15 19:12:54 +08:00
|
|
|
|
}
|
2024-08-09 00:39:39 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
output = _currentRateStore;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-15 19:12:54 +08:00
|
|
|
|
return output;
|
2024-03-14 00:29:01 +08:00
|
|
|
|
}
|
2024-03-13 18:01:38 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-03-15 19:12:54 +08:00
|
|
|
|
/// 强制创建一个记录,不管时间到没到
|
2024-03-13 18:01:38 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2024-06-27 17:06:30 +08:00
|
|
|
|
private async Task<InterestRecordsAggregateRoot> CreateInterestRecordsAsync()
|
2024-03-13 18:01:38 +08:00
|
|
|
|
{
|
2024-06-26 12:46:39 +08:00
|
|
|
|
decimal oldValue = DefalutRate;
|
2024-03-15 19:12:54 +08:00
|
|
|
|
|
2024-06-26 12:46:39 +08:00
|
|
|
|
var thirdPartyValue = await _bankValueProvider.GetValueAsync();
|
2024-03-15 19:12:54 +08:00
|
|
|
|
//获取实际值的变化率
|
2024-06-26 12:46:39 +08:00
|
|
|
|
decimal changeRate = (thirdPartyValue - _bankValueProvider.StandardValue) / (thirdPartyValue);
|
|
|
|
|
|
|
2024-03-15 19:12:54 +08:00
|
|
|
|
//判断市场是否波动
|
|
|
|
|
|
bool isFluctuate = IsMarketVolatility();
|
|
|
|
|
|
//市场波动
|
|
|
|
|
|
if (isFluctuate)
|
|
|
|
|
|
{
|
|
|
|
|
|
changeRate = 2 * changeRate;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//根据上一次的老值进行变化率比较
|
2024-06-26 12:46:39 +08:00
|
|
|
|
var currentValue = oldValue + (oldValue * changeRate);
|
2024-03-15 19:12:54 +08:00
|
|
|
|
|
2024-07-05 17:40:02 +08:00
|
|
|
|
var entity = new InterestRecordsAggregateRoot(thirdPartyValue, currentValue, isFluctuate);
|
2024-03-14 00:29:01 +08:00
|
|
|
|
var output = await _interestRepository.InsertReturnEntityAsync(entity);
|
|
|
|
|
|
|
|
|
|
|
|
return output;
|
2024-03-13 18:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-15 19:12:54 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 判断是否为波动市场,市场波动,变化率翻倍
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private static bool IsMarketVolatility()
|
|
|
|
|
|
{
|
|
|
|
|
|
double probability = 0.1;
|
|
|
|
|
|
Random random = new Random();
|
|
|
|
|
|
return random.NextDouble() < probability;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-13 18:01:38 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 给用户申请银行卡
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2024-03-14 00:29:01 +08:00
|
|
|
|
public async Task ApplyingBankCardAsync(Guid userId, int cardNumber)
|
2024-03-13 18:01:38 +08:00
|
|
|
|
{
|
2024-05-22 14:35:08 +08:00
|
|
|
|
var entities = Enumerable.Range(1, cardNumber).Select(x => new BankCardAggregateRoot(userId)).ToList();
|
2024-03-14 00:29:01 +08:00
|
|
|
|
await _repository.InsertManyAsync(entities);
|
2024-03-13 18:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-03-14 00:29:01 +08:00
|
|
|
|
/// 进行银行卡提款
|
2024-03-13 18:01:38 +08:00
|
|
|
|
/// </summary>
|
2024-03-14 00:29:01 +08:00
|
|
|
|
/// <param name="cardId"></param>
|
2024-03-13 18:01:38 +08:00
|
|
|
|
/// <returns></returns>
|
2024-03-14 00:29:01 +08:00
|
|
|
|
public async Task DrawMoneyAsync(Guid cardId)
|
2024-03-13 18:01:38 +08:00
|
|
|
|
{
|
2024-03-14 00:29:01 +08:00
|
|
|
|
var entity = await _repository.GetByIdAsync(cardId);
|
|
|
|
|
|
if (entity.BankCardState == BankCardStateEnum.Unused)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("当前银行卡状态不能提款");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//这里其实不存在这个状态,只有等待状态,不需要去主动触发,前端判断即可
|
|
|
|
|
|
if (entity.BankCardState == BankCardStateEnum.Full)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("当前银行卡状态不能存款");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//可以提款
|
|
|
|
|
|
if (entity.BankCardState == BankCardStateEnum.Wait)
|
|
|
|
|
|
{
|
|
|
|
|
|
decimal changeMoney = 0;
|
|
|
|
|
|
//判断是否存满时间
|
|
|
|
|
|
if (entity.IsStorageFull())
|
|
|
|
|
|
{
|
2024-03-15 19:12:54 +08:00
|
|
|
|
changeMoney = this.CurrentRate.Value * entity.StorageMoney;
|
2024-03-14 00:29:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
changeMoney = entity.StorageMoney;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//提款
|
|
|
|
|
|
entity.SetDrawMoney();
|
|
|
|
|
|
await _repository.UpdateAsync(entity);
|
|
|
|
|
|
|
|
|
|
|
|
//打钱,该卡状态钱更新,并提款加到用户钱钱里
|
2024-07-25 22:34:40 +08:00
|
|
|
|
await _localEventBus.PublishAsync(new MoneyChangeEventArgs(entity.UserId, changeMoney),false);
|
2024-03-14 00:29:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-03-13 18:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 给银行卡存款
|
|
|
|
|
|
/// </summary>
|
2024-03-14 00:29:01 +08:00
|
|
|
|
/// <param name="cardId"></param>
|
2024-03-13 18:01:38 +08:00
|
|
|
|
/// <param name="moneyNum"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-03-14 00:29:01 +08:00
|
|
|
|
public async Task DepositAsync(Guid cardId, decimal moneyNum)
|
2024-03-13 18:01:38 +08:00
|
|
|
|
{
|
2024-03-14 00:29:01 +08:00
|
|
|
|
var entity = await _repository.GetByIdAsync(cardId);
|
|
|
|
|
|
if (entity.BankCardState != BankCardStateEnum.Unused)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("当前银行卡状态不能存款");
|
|
|
|
|
|
}
|
|
|
|
|
|
//存款
|
|
|
|
|
|
entity.SetStorageMoney(moneyNum);
|
|
|
|
|
|
|
|
|
|
|
|
await _repository.UpdateAsync(entity);
|
2024-07-25 22:34:40 +08:00
|
|
|
|
await _localEventBus.PublishAsync(new MoneyChangeEventArgs(entity.UserId, -moneyNum), false);
|
2024-03-14 00:29:01 +08:00
|
|
|
|
|
2024-03-13 18:01:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|