diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Jobs/InterestRecordsJob.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Jobs/InterestRecordsJob.cs new file mode 100644 index 00000000..a67e1326 --- /dev/null +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Jobs/InterestRecordsJob.cs @@ -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().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(); + } + } +} diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Entities/Bank/InterestRecordsEntity.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Entities/Bank/InterestRecordsEntity.cs index 75ab9386..245d4e40 100644 --- a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Entities/Bank/InterestRecordsEntity.cs +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Entities/Bank/InterestRecordsEntity.cs @@ -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; } + /// + /// 第三方的比较值 + /// + public decimal ComparisonValue { get; set; } + /// /// 当前汇率值 /// diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/BankManager.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/BankManager.cs index 7cf75891..95ea4f1c 100644 --- a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/BankManager.cs +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/BankManager.cs @@ -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 { + /// + /// 银行领域,进阶了哦~ + /// public class BankManager : DomainService { private ISqlSugarRepository _repository; private ILocalEventBus _localEventBus; private ISqlSugarRepository _interestRepository; - public BankManager(ISqlSugarRepository repository, ILocalEventBus localEventBus, ISqlSugarRepository interestRepository) + private IBankValueProvider _bankValueProvider; + public BankManager(ISqlSugarRepository repository, ILocalEventBus localEventBus, ISqlSugarRepository interestRepository, IBankValueProvider bankValueProvider) { _repository = repository; _localEventBus = localEventBus; _interestRepository = interestRepository; + _bankValueProvider=bankValueProvider; } - public decimal CurrentInterestRate => GetCurrentInterestRate(); - private decimal GetCurrentInterestRate() + /// + /// 获取当前银行汇率 + /// + public BankInterestRecordDto CurrentRate => GetCurrentInterestRate(); + + /// + /// 用于存储当前汇率数据 + /// + private BankInterestRecordDto? _currentRateStore; + + /// + /// 获取当前的银行汇率,如果为空会从数据库拿最新一条 + /// + /// + 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; } /// @@ -33,29 +61,65 @@ namespace Yi.Framework.Bbs.Domain.Managers /// private decimal GetThirdPartyValue() { - return 0; + return _bankValueProvider.GetValueAsync().Result; } /// - /// 创建一个记录 + /// 强制创建一个记录,不管时间到没到 /// /// public async Task 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; } + /// + /// 判断是否为波动市场,市场波动,变化率翻倍 + /// + /// + private static bool IsMarketVolatility() + { + double probability = 0.1; + Random random = new Random(); + return random.NextDouble() < probability; + } + /// /// 给用户申请银行卡 /// @@ -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 { diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/BankValue/BiyingBankValueProvider.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/BankValue/BiyingBankValueProvider.cs new file mode 100644 index 00000000..f8f32154 --- /dev/null +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/BankValue/BiyingBankValueProvider.cs @@ -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 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(); + } + } + catch(Exception ex) { + throw new Exception("BiyingBank获取数据异常", ex); + + } + + } + } +} diff --git a/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/BankValue/IBankValueProvider.cs b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/BankValue/IBankValueProvider.cs new file mode 100644 index 00000000..3882601c --- /dev/null +++ b/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/Managers/BankValue/IBankValueProvider.cs @@ -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 GetValueAsync(); + } +} diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Dtos/BankInterestRecordDto.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Dtos/BankInterestRecordDto.cs new file mode 100644 index 00000000..6a008417 --- /dev/null +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Dtos/BankInterestRecordDto.cs @@ -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 +{ + /// + /// 当前银行记录 + /// + public class BankInterestRecordDto + { + public DateTime CreationTime { get; set; } + + /// + /// 第三方的比较值 + /// + public decimal ComparisonValue { get; set; } + + /// + /// 当前汇率值 + /// + public decimal Value { get; set; } + + /// + /// 判断时间是否过期 + /// + /// + public bool IsExpire() + { + return (DateTime.Now-CreationTime).TotalHours >= 1; + } + + } +}