using System;
using YLErp.Derivatives.Interest;
using YLErp.Modules.DataProviderModule;
namespace YLErp.Modules.SwapModule;
///
/// IIndexFixer 的生产实现:FR007 等浮动利率取价。
/// 直接调用 EodPriceQueryService.TryGetPrice,不依赖 SwapDealService 实例,便于独立测试。
/// 未来 SwapDealService 的 6 处 inline 取价代码迁移到本类后,取价规则只在此一处维护。
///
public sealed class Fr007IndexFixer : IndexFixerBase, IIndexFixer
{
public static readonly Fr007IndexFixer Instance = new();
public bool TryGetFixing(DateTime fixingDate, string underlyingCode, out decimal rate)
{
bool ok = EodPriceQueryService.TryGetPrice(fixingDate, underlyingCode, out double r);
rate = Convert.ToDecimal(r);
return ok;
}
///
/// 取价日 → 取价 → 取不到抛异常。等价于原 SwapDealService 里散落 6 处的 inline 代码:
/// GetNonHolidayDefore(date.AddDays(rule)) + TryGetFloatRate + throw
/// 取到 0 视为"取到一个 0 值"(返回 0),由调用方决定是否沿用旧值;
/// 只有 TryGetFixing 返回 false 才抛异常——对齐既有语义。
///
public decimal GetFixingOrThrow(DateTime resetDate, int? interestRule, string underlyingCode)
{
var fixingDate = GetFixingDate(resetDate, interestRule ?? 0);
if (TryGetFixing(fixingDate, underlyingCode, out decimal rate))
return rate;
throw new Exception($"获取不到{underlyingCode}在{fixingDate:yyyy年MM月dd日}的价格");
}
}