114 lines
3.4 KiB
C#
114 lines
3.4 KiB
C#
using YLErp.Abstract.DataProviders;
|
|
|
|
namespace YLErp.Modules.DataProviderModule
|
|
{
|
|
/// <summary>
|
|
/// 价格提供接口实现(数据来自手动添加)
|
|
/// </summary>
|
|
public class ManualPriceProvider : IPriceProvider
|
|
{
|
|
readonly Dictionary<string, double> _priceDic;
|
|
|
|
public ManualPriceProvider(IDictionary<string, double> priceDic = null)
|
|
{
|
|
_priceDic = priceDic == null
|
|
? new Dictionary<string, double>(StringComparer.OrdinalIgnoreCase)
|
|
: new Dictionary<string, double>(priceDic, StringComparer.OrdinalIgnoreCase);
|
|
}
|
|
|
|
public ManualPriceProvider SetPrice(string instrumentId, double price)
|
|
{
|
|
if (!string.IsNullOrEmpty(instrumentId))
|
|
{
|
|
_priceDic[instrumentId] = price;
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public double GetPrice(string instrumentId)
|
|
{
|
|
return !string.IsNullOrEmpty(instrumentId) && _priceDic.TryGetValue(instrumentId, out var price) ? price : 0;
|
|
}
|
|
|
|
public bool TryGetPrice(string instrumentId, out double price)
|
|
{
|
|
price = 0;
|
|
return !string.IsNullOrEmpty(instrumentId) && _priceDic.TryGetValue(instrumentId, out price);
|
|
}
|
|
|
|
public bool Contains(string instrumentId)
|
|
{
|
|
return _priceDic.ContainsKey(instrumentId);
|
|
}
|
|
|
|
public static implicit operator ManualPriceProvider(Dictionary<string, double> priceDic)
|
|
{
|
|
return new ManualPriceProvider(priceDic);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 单个UnderlyingID对应的价格提供接口实现
|
|
/// </summary>
|
|
public class SinglePriceProvider : IPriceProvider
|
|
{
|
|
readonly double _price;
|
|
readonly string _instrumentId;
|
|
|
|
public SinglePriceProvider(string instrumentId, double price)
|
|
{
|
|
_instrumentId = instrumentId ?? throw new ArgumentNullException(nameof(instrumentId));
|
|
_price = price;
|
|
}
|
|
|
|
public double GetPrice(string instrumentId)
|
|
{
|
|
return _instrumentId.Equals(instrumentId, StringComparison.OrdinalIgnoreCase) ? _price : 0;
|
|
}
|
|
|
|
public bool TryGetPrice(string instrumentId, out double price)
|
|
{
|
|
if (_instrumentId.Equals(instrumentId, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
price = _price;
|
|
return true;
|
|
}
|
|
price = 0;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 多个价格提供接口聚合提供价格
|
|
/// </summary>
|
|
public class AggregatePriceProvider : IPriceProvider, IAggregatePriceProvider
|
|
{
|
|
readonly IEnumerable<IPriceProvider> _priceProviders;
|
|
|
|
public AggregatePriceProvider(params IPriceProvider[] priceProviders)
|
|
{
|
|
_priceProviders = priceProviders ?? Enumerable.Empty<IPriceProvider>();
|
|
}
|
|
|
|
public double GetPrice(string instrumentCode)
|
|
{
|
|
return TryGetPrice(instrumentCode, out var price) ? price : 0;
|
|
}
|
|
|
|
public bool TryGetPrice(string instrumentCode, out double price)
|
|
{
|
|
foreach (var provider in _priceProviders)
|
|
{
|
|
if (provider != null && provider.TryGetPrice(instrumentCode, out price))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
price = 0;
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|