using System;
using System.Collections.Generic;
using System.Text;
namespace YLErp.Modules.SwapModule
{
///
/// 计息计算过程追踪器(默认关闭,零运行时成本)。
///
/// 在 CalcDailySimpleInterest / CalcDailyCompoundInterest 等计息函数中调用
/// SwapCalcTrace.Record(...),开启后逐日记录:计息起点、日终归档 ValueDate 地板、
/// 计息基数、当日利率、当日利息、累计利息。便于定位"算出来一个数却不对"的根因,
/// 也便于把一次真实平仓的逐步过程打印出来与 Excel 对账。
///
/// 用法(单元测试或临时排障):
/// SwapCalcTrace.IsEnabled = true;
/// SwapCalcTrace.Reset();
/// ... 调用计息 ...
/// Console.WriteLine(SwapCalcTrace.Dump());
/// SwapCalcTrace.IsEnabled = false;
///
public static class SwapCalcTrace
{
public static bool IsEnabled { get; set; } = false;
[ThreadStatic]
private static List _lines;
private static List Lines => _lines ??= new List();
public static void Reset() => Lines.Clear();
public static void Header(string title)
{
if (IsEnabled) Lines.Add($"== {title} ==");
}
public static void Line(string text)
{
if (IsEnabled) Lines.Add(text);
}
/// 记录某一计息日的明细。
public static void Day(int idx, DateTime date, decimal rate, decimal basePrincipal, decimal dayInterest, decimal accumulated)
{
if (IsEnabled)
Lines.Add($" [{idx}] {date:yyyy-MM-dd} rate={rate:P6} base={basePrincipal:F4} day={dayInterest:F6} acc={accumulated:F6}");
}
public static string Dump() => string.Join(Environment.NewLine, Lines);
}
}