Files
zszq-trs/Framework/YLErp.Jobs/Imp/AtMoneyOptionQuotesJob.cs
T
2024-05-09 14:06:26 +08:00

167 lines
6.2 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Qdp.Foundation.Implementations;
using Qdp.Pricing.Base.Implementations;
using YLErp.BLL;
using YLErp.BLL.Calculation;
using YLErp.DBModels;
using YLErp.Jobs.Configs;
namespace YLErp.Jobs.Imp
{
/// <summary>
/// 手机实时报价
/// </summary>
public class AtMoneyOptionQuotesJob : BaseJob
{
static readonly IYcLogger logger = LogFactory.GetLogger<AtMoneyOptionQuotesJob>();
private readonly IServiceScopeFactory _scopeFactory;
private AtMoneyOptionQuotesJobConfig _atMoneyOptionQuotesJobConfig;
public AtMoneyOptionQuotesJob(IServiceScopeFactory scopeFactory, AtMoneyOptionQuotesJobConfig atMoneyOptionQuotesJobConfig)
{
_scopeFactory = scopeFactory;
_atMoneyOptionQuotesJobConfig = atMoneyOptionQuotesJobConfig;
}
public override Task JobImp()
{
var calendar = CalendarImpl.Get("chn");
var baseUrl = _atMoneyOptionQuotesJobConfig.QuotesUrl.TrimToNull();
if (string.IsNullOrWhiteSpace(baseUrl))
{
logger.Error("未配置AtMoneyOptionQuotesUrl");
return Task.CompletedTask;
}
try
{
var today = new Date(DateTime.Now);
if (calendar.IsHoliday(today))
{
Console.WriteLine($"[{nameof(AtMoneyOptionQuotesJob)}]今天节假日");
}
else
{
var dateStr = CalculatorHelper.RealtimeQuoteDefaultMaturityDate();
var httpClient = _scopeFactory.CreateScope().ServiceProvider.GetService<IHttpClientFactory>().CreateClient();
var url = $"{baseUrl}?maturityDate={dateStr}&onlyMainContract=false";
Console.WriteLine("执行 " + url);
return httpClient.GetAsync(url).ContinueWith(async t =>
{
if (t.Exception != null)
{
logger.Error("请求API失败", t.Exception);
}
else
{
var resultStr = await t.Result.Content.ReadAsStringAsync();
SaveAtMoneyQuote(resultStr, dateStr);
}
});
}
}
catch (Exception ex)
{
logger.Error("报价计算出错", ex);
}
return Task.CompletedTask;
}
private static void SaveAtMoneyQuote(string dataJson, string dateStr)
{
List<AtMoneyOptionQuoteRecord> quotes = null;
try
{
quotes = JsonConvert.DeserializeObject<List<AtMoneyOptionQuoteRecord>>(dataJson);
}
catch (Exception ex)
{
logger.Error("[AtMoneyOptionQuotes]对返回的数据解析失败", ex);
}
if (quotes == null)
{
return;
}
var underlyingCodes = quotes.Select(x => x.underlyingCode).ToList();
using (var con = new YLContext())
{
var oldQuotesToRemove = con.flat_price_quotation.Where(x => !underlyingCodes.Contains(x.UnderlyingCode));
con.flat_price_quotation.RemoveRange(oldQuotesToRemove);
quotes.ForEach(quote =>
{
var underlyingQuote = con.flat_price_quotation.FirstOrDefault(u => u.UnderlyingCode == quote.underlyingCode);
if (underlyingQuote == null)
{
underlyingQuote = new flat_price_quotation
{
MarketCode = quote.marketCode,
MarketName = quote.marketName,
UnderlyingCode = quote.underlyingCode,
UnderlyingType = quote.underlyingType,
UnderlyingTypeShortName = quote.underlyingTypeShortName,
SpotPrice = quote.spotPrice,
BuyOptionPrice = quote.buyOptionPrice,
SellOptionPrice = quote.sellOptionPrice,
BuyOptionVol = quote.buyOptionVol,
SellOptionVol = quote.sellOptionVol,
ComparedToLastDayPrice = quote.comparedToLastDayPrice,
OptDate = DateTime.Now,
LastUpdateTime = DateTime.Now
};
con.flat_price_quotation.Add(underlyingQuote);
}
else // update
{
underlyingQuote.MarketCode = quote.marketCode;
underlyingQuote.MarketName = quote.marketName;
underlyingQuote.UnderlyingType = quote.underlyingType;
underlyingQuote.UnderlyingTypeShortName = quote.underlyingTypeShortName;
underlyingQuote.SpotPrice = quote.spotPrice;
underlyingQuote.BuyOptionPrice = quote.buyOptionPrice;
underlyingQuote.SellOptionPrice = quote.sellOptionPrice;
underlyingQuote.BuyOptionVol = quote.buyOptionVol;
underlyingQuote.SellOptionVol = quote.sellOptionVol;
underlyingQuote.ComparedToLastDayPrice = quote.comparedToLastDayPrice;
underlyingQuote.OptDate = DateTime.Now;
underlyingQuote.LastUpdateTime = DateTime.Now;
}
});
con.SaveChanges();
}
logger.Info($"[AtMoneyOptionQuotes]更新报价数据{quotes.Count}条,到期日{dateStr}");
}
#region JobStatus Manager
static JobStatus jobStatus = JobStatus.Init;
public override JobStatus GetJobStatus()
{
return jobStatus;
}
public override void SetJobStatus(JobStatus status)
{
jobStatus=status;
}
#endregion
}
}