Files
zszq-trs/YLErpDAL/Modules/DataCacheModule/DataSource/UnderlyingDbDataSource.cs

186 lines
5.5 KiB
C#

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using YieldChain.Commons;
using YieldChain.Helpers;
using YLErp.Abstract;
using YLErp.Abstract.DataProviders;
using YLErp.BLL;
using YLErp.Models;
using YLErp.Modules.DataProviderModule;
using YLErp.Modules.UnderlyingModule;
using YLErp.QdpModule;
namespace YLErp.Modules.DataCacheModule
{
public static partial class DataCacheManager
{
class UnderlyingDbDataSource : IUnderlyingDataSource, IBasketPriceProvider, IDataUpdater, IDataSource, IDataSource<underlying_manager>, IDataSourceEvent, IJsonSerializable
{
private readonly YLContext _context = new YLContext();
/// <summary>
/// 查询过滤条件
/// </summary>
public Expression<Func<underlying_manager, bool>> Filter { get; set; }
public int Count
{
get
{
return _context.underlying_manager.Count();
}
}
public string TableName => "underlying_manager";
public IQueryable<underlying_manager> AsQueryable(Expression<Func<underlying_manager, bool>> predicate = null)
{
if (predicate == null)
{
return _context.underlying_manager;
}
return _context.underlying_manager.Where(predicate);
}
public int GetCountRatio(string underlyingCode)
{
return GetData(underlyingCode)?.CountRatio ?? 1;
}
public underlying_manager GetData(string underlyingCode)
{
return _context.underlying_manager.FirstOrDefault(x => x.UnderlyingCode == underlyingCode);
}
public underlying_manager GetData(int keyId)
{
return _context.underlying_manager.FirstOrDefault(x => x.id == keyId);
}
public double GetPrice(int underlyingId)
{
return InnerGetPrice(GetData(underlyingId));
}
public double GetPrice(string underlyingCode)
{
return InnerGetPrice(GetData(underlyingCode));
}
public SyntheticUnderlying GetSyntheticUnderlying(string underlyingCode)
{
if (string.IsNullOrEmpty(underlyingCode))
{
return null;
}
var un = GetData(underlyingCode);
if (un?.CommodityCode != "组合标的")
{
return null;
}
var sy = _context.synthetic_underlying.AsNoTracking().FirstOrDefault(n => n.Name == underlyingCode);
return sy?.Clone();
}
public bool InitData(List<string> underlyingCodes)
{
return true;
}
public void ResetDataSource()
{
}
public bool TryGetPrice(int underlyingId, out double price)
{
return InnerTryGetPrice(GetData(underlyingId), out price);
}
public bool TryGetPrice(string underlyingCode, out double price)
{
return InnerTryGetPrice(GetData(underlyingCode), out price);
}
public bool TryGetSubPrice(string underlyingCode, out double price, out double settlePrice)
{
//篮子标的的子标的只能是普通标的
var data = GetData(underlyingCode);
if (data != null)
{
price = settlePrice = data.Price ?? 0;
return true;
}
price = settlePrice = 0;
return false;
}
public void UpdateLauchState()
{
}
public bool UpdatePrices(bool delay)
{
return true;
}
private double InnerGetPrice(underlying_manager underlying)
{
return InnerTryGetPrice(underlying, out var price) ? price : 0;
}
private bool InnerTryGetPrice(underlying_manager un, out double price)
{
price = 0;
if (un == null)
{
return false;
}
if (un.IsBasket())
{
price = BasketUnderlyingHelper.GetBasketPrice(un, this, true).Price;
}
else if (un.IsBond())
{
var valueDate = valuedateBLL.ValueDate;
var bondPrice = EodPriceQueryService.GetBondPrice(valueDate, un.UnderlyingCode);
price = bondPrice != null ? bondPrice.ClosePrice : un.Price ?? 0;
}
else
{
price = un.Price ?? 0;
}
return true;
}
public void UpdateData(IEnumerable<string> updateKeyIds)
{
}
public string ToJson()
{
return "";
}
public static readonly UnderlyingDbDataSource Default;
public event EventHandler DataSourceUpdated;
static UnderlyingDbDataSource()
{
Default = new UnderlyingDbDataSource();
}
}
}
}