feat: 完成第三方利息汇率对接

This commit is contained in:
陈淳
2024-03-15 19:12:54 +08:00
parent e989313b0d
commit 822febba2e
6 changed files with 201 additions and 17 deletions

View File

@@ -0,0 +1,31 @@
using Quartz;
using Volo.Abp.BackgroundWorkers.Quartz;
using Yi.Framework.Bbs.Domain.Managers;
namespace Yi.Framework.Bbs.Application.Jobs
{
public class InterestRecordsJob : QuartzBackgroundWorkerBase
{
private BankManager _bankManager;
public InterestRecordsJob(BankManager bankManager)
{
_bankManager = bankManager;
JobDetail = JobBuilder.Create<InterestRecordsJob>().WithIdentity(nameof(InterestRecordsJob)).Build();
//每个小时整点执行一次
Trigger = TriggerBuilder.Create().WithIdentity(nameof(InterestRecordsJob)).WithCronSchedule("0 */1 * * *").Build();
//测试
// Trigger = TriggerBuilder.Create().WithIdentity(nameof(InterestRecordsJob))
//.WithSimpleSchedule(x => x
// .WithIntervalInSeconds(5)
// .RepeatForever())
//.Build();
}
public override async Task Execute(IJobExecutionContext context)
{
//创建一个记录,莫得了
await _bankManager.CreateInterestRecordsAsync();
}
}
}

View File

@@ -17,16 +17,21 @@ namespace Yi.Framework.Bbs.Domain.Entities.Bank
{
public InterestRecordsEntity()
{ }
public InterestRecordsEntity(decimal inputValue, bool isFluctuate, decimal oldValue = 0)
public InterestRecordsEntity(decimal comparisonValue, decimal inputValue, bool isFluctuate = false)
{
//这里写好根据数据的值,以及是否要波动期,进行得出真是利息
//有了老值和新值我们可以根据这个变化程度去做一个涨幅或跌幅Todo
Value=inputValue;
ComparisonValue = comparisonValue;
Value = inputValue;
IsFluctuate = isFluctuate;
}
[SugarColumn(ColumnName = "Id", IsPrimaryKey = true)]
public override Guid Id { get; protected set; }
public DateTime CreationTime { get; set; }
/// <summary>
/// 第三方的比较值
/// </summary>
public decimal ComparisonValue { get; set; }
/// <summary>
/// 当前汇率值
/// </summary>

View File

@@ -1,30 +1,58 @@
using Volo.Abp.Domain.Services;
using Volo.Abp.EventBus.Local;
using Yi.Framework.Bbs.Domain.Entities.Bank;
using Yi.Framework.Bbs.Domain.Managers.BankValue;
using Yi.Framework.Bbs.Domain.Shared.Enums;
using Yi.Framework.Bbs.Domain.Shared.Etos;
using Yi.Framework.Rbac.Domain.Shared.Dtos;
using Yi.Framework.SqlSugarCore.Abstractions;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace Yi.Framework.Bbs.Domain.Managers
{
/// <summary>
/// 银行领域,进阶了哦~
/// </summary>
public class BankManager : DomainService
{
private ISqlSugarRepository<BankCardEntity> _repository;
private ILocalEventBus _localEventBus;
private ISqlSugarRepository<InterestRecordsEntity> _interestRepository;
public BankManager(ISqlSugarRepository<BankCardEntity> repository, ILocalEventBus localEventBus, ISqlSugarRepository<InterestRecordsEntity> interestRepository)
private IBankValueProvider _bankValueProvider;
public BankManager(ISqlSugarRepository<BankCardEntity> repository, ILocalEventBus localEventBus, ISqlSugarRepository<InterestRecordsEntity> interestRepository, IBankValueProvider bankValueProvider)
{
_repository = repository;
_localEventBus = localEventBus;
_interestRepository = interestRepository;
_bankValueProvider=bankValueProvider;
}
public decimal CurrentInterestRate => GetCurrentInterestRate();
private decimal GetCurrentInterestRate()
/// <summary>
/// 获取当前银行汇率
/// </summary>
public BankInterestRecordDto CurrentRate => GetCurrentInterestRate();
/// <summary>
/// 用于存储当前汇率数据
/// </summary>
private BankInterestRecordDto? _currentRateStore;
/// <summary>
/// 获取当前的银行汇率,如果为空会从数据库拿最新一条
/// </summary>
/// <returns></returns>
private BankInterestRecordDto GetCurrentInterestRate()
{
var output = new BankInterestRecordDto();
//先判断时间是否与当前时间差1小时小于1小时直接返回即可,可以由一个单例类提供
GetThirdPartyValue();
return 1.30m;
if (this._currentRateStore is null || this._currentRateStore.IsExpire())
{
var currentInterestRecords = CreateInterestRecordsAsync().Result;
output.ComparisonValue = currentInterestRecords.ComparisonValue;
output.CreationTime = currentInterestRecords.CreationTime;
output.Value = currentInterestRecords.Value;
}
return output;
}
/// <summary>
@@ -33,29 +61,65 @@ namespace Yi.Framework.Bbs.Domain.Managers
/// <returns></returns>
private decimal GetThirdPartyValue()
{
return 0;
return _bankValueProvider.GetValueAsync().Result;
}
/// <summary>
/// 创建一个记录
/// 强制创建一个记录,不管时间到没到
/// </summary>
/// <returns></returns>
public async Task<InterestRecordsEntity> CreateInterestRecordsAsync()
{
//获取最新的实体
var newEntity = await _interestRepository._DbQueryable.OrderByDescending(x => x.CreationTime).FirstAsync();
var lastEntity = await _interestRepository._DbQueryable.OrderByDescending(x => x.CreationTime).FirstAsync();
decimal oldValue = 1.3m;
if (newEntity is not null)
//获取第三方的值
var thirdPartyValue = GetThirdPartyValue();
//获取上一次第三方标准值
var lastThirdPartyStandardValue = thirdPartyValue;
//获取实际值的变化率
decimal changeRate = 0;
//说明不是第一次
if (lastEntity is not null)
{
oldValue = newEntity.Value;
oldValue = lastEntity.Value;
lastThirdPartyStandardValue = lastEntity.ComparisonValue;
changeRate = (thirdPartyValue - lastThirdPartyStandardValue) / (thirdPartyValue);
}
var currentValue = GetThirdPartyValue();
var entity = new InterestRecordsEntity(currentValue, false, oldValue);
//判断市场是否波动
bool isFluctuate = IsMarketVolatility();
//市场波动
if (isFluctuate)
{
changeRate = 2 * changeRate;
}
//根据上一次的老值进行变化率比较
var currentValue = oldValue * changeRate;
var entity = new InterestRecordsEntity(lastThirdPartyStandardValue, currentValue);
var output = await _interestRepository.InsertReturnEntityAsync(entity);
return output;
}
/// <summary>
/// 判断是否为波动市场,市场波动,变化率翻倍
/// </summary>
/// <returns></returns>
private static bool IsMarketVolatility()
{
double probability = 0.1;
Random random = new Random();
return random.NextDouble() < probability;
}
/// <summary>
/// 给用户申请银行卡
/// </summary>
@@ -92,7 +156,7 @@ namespace Yi.Framework.Bbs.Domain.Managers
//判断是否存满时间
if (entity.IsStorageFull())
{
changeMoney = this.CurrentInterestRate * entity.StorageMoney;
changeMoney = this.CurrentRate.Value * entity.StorageMoney;
}
else
{

View File

@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Volo.Abp.DependencyInjection;
namespace Yi.Framework.Bbs.Domain.Managers.BankValue
{
public class BiyingBankValueProvider : IBankValueProvider, ITransientDependency
{
private const string Url = "https://api.biyingapi.com/hsrl/ssjy/600519/504d30854dbd93312d";
public async Task<decimal> GetValueAsync()
{
try
{
using (HttpClient client = new HttpClient())
{
var reponse = await client.GetAsync(Url);
reponse.EnsureSuccessStatusCode();
var dataStr = await reponse.Content.ReadAsStringAsync();
JObject jsonObject = JObject.Parse(dataStr);
return jsonObject["p"].Value<decimal>();
}
}
catch(Exception ex) {
throw new Exception("BiyingBank获取数据异常", ex);
}
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Bbs.Domain.Managers.BankValue
{
public interface IBankValueProvider
{
public Task<decimal> GetValueAsync();
}
}

View File

@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Rbac.Domain.Shared.Dtos
{
/// <summary>
/// 当前银行记录
/// </summary>
public class BankInterestRecordDto
{
public DateTime CreationTime { get; set; }
/// <summary>
/// 第三方的比较值
/// </summary>
public decimal ComparisonValue { get; set; }
/// <summary>
/// 当前汇率值
/// </summary>
public decimal Value { get; set; }
/// <summary>
/// 判断时间是否过期
/// </summary>
/// <returns></returns>
public bool IsExpire()
{
return (DateTime.Now-CreationTime).TotalHours >= 1;
}
}
}