fix(swap): 修复平仓事件中事件日期=平仓日期的处理逻辑

- 将平仓条件判断从 UnwindDate 改为 EventDate
- 移除不再使用的 priorClosePositionIds 变量
- 统一使用 NormalizeEventUnwindDate 替代 NormalizeIncomeUnwindDate
- 在多个平仓方法中添加 NormalizeEventUnwindDate 调用
- 更新流程事件的 EventDate 和 UnwindDate 字段
- 修改单元测试以验证按 EventDate 分桶的逻辑
- 在前端控制器中将 unwindDate 设置为 valueDate
- 添加 maxdate 属性到平仓日期选择器
- 实现事件日期与平仓日期的双向同步功能
- 更新利息列表获取接口使用统一日期参数
This commit is contained in:
张名锐
2026-08-07 13:46:24 +08:00
parent b3ce94b67b
commit 3a435ad89b
7 changed files with 86 additions and 59 deletions
@@ -401,10 +401,7 @@ namespace YLErp.Modules.SwapModule
PositionType = 0,
EventType = (int)SwapEventTypeEnum.,
EventDate = new DateTime(2026, 7, 9),
// UnwindDate 必须显式设置:ResolveInterestLegPositionsAsOf 自 a1cdb2cd 起按 UnwindDate
// (经济生效日)分桶,而非 EventDate(簿记日)。生产平仓事件总会设 UnwindDate
// InitUnwind:327、AuotoSwapUnwind:1590)。此处同日场景:UnwindDate == EventDate。
UnwindDate = new DateTime(2026, 7, 9),
UnwindDate = new DateTime(2026, 7, 10),
InterestMode = (int)InterestModeEnum.,
InterestPrincipal = 3000m
};
@@ -414,7 +411,7 @@ namespace YLErp.Modules.SwapModule
PositionType = 1,
EventType = (int)SwapEventTypeEnum.,
EventDate = new DateTime(2026, 7, 9),
UnwindDate = new DateTime(2026, 7, 9),
UnwindDate = new DateTime(2026, 7, 10),
TradingAmount = 3000000m
};
var originalWithFloat = new List<swap_position>
@@ -437,17 +434,15 @@ namespace YLErp.Modules.SwapModule
}
/// <summary>
/// [SPC_008] EventDate ≠ UnwindDate 时,ResolveInterestLegPositionsAsOf 按 UnwindDate(经济生效日)分桶。
/// [SPC_008] EventDate ≠ UnwindDate 时,ResolveInterestLegPositionsAsOf 按 EventDate(事件日期)分桶。
/// ----------------------------------------------------------------------------
/// 锁定 a1cdb2cd 的修复价值:平仓事件的簿记日(EventDate)可能滞后于经济生效日(UnwindDate)
/// (如 T+N 结算、手动补录)。重放预付金腿 as-of 本金时,分桶必须按 UnwindDate:
/// - settleDate &lt; UnwindDate → 平仓"未发生"as-of=原始本金
/// - settleDate &gt;= UnwindDate → 平仓"已生效"as-of=实时剩余本金
/// 修复前按 EventDate 分桶:settleDate 落在 [UnwindDate, EventDate) 区间时,会被误判为"未发生"。
/// 本测试构造 EventDate=7/11、UnwindDate=7/9,验证 settleDate=7/10 时已按 UnwindDate 生效。
/// 锁定事件日期作为历史重放的生效边界:
/// - settleDate &lt; EventDate → 平仓"未发生"as-of=原始本金
/// - settleDate &gt;= EventDate → 平仓"已生效"as-of=实时剩余本金
/// 本测试构造 EventDate=7/9、UnwindDate=7/10,验证 settleDate=7/9 时已按 EventDate 生效。
/// </summary>
[TestMethod]
public void SPC_008_EventDateDiffersFromUnwindDate_BucketsByUnwindDate()
public void SPC_008_EventDateDiffersFromUnwindDate_BucketsByEventDate()
{
const long originalPositionId = 2;
var original = new swap_position
@@ -463,14 +458,14 @@ namespace YLErp.Modules.SwapModule
InterestMode = (int)InterestModeEnum.,
InterestPrincipalFix = 7000m
};
// 关键:EventDate(簿记 7/11) 滞后于 UnwindDate(经济生效 7/9) —— T+N 结算/补录常见
// 关键:EventDate 为 7/9,as-of 应按事件日期判断。
var close = new swap_flow_event
{
PositionId = originalPositionId,
PositionType = 0,
EventType = (int)SwapEventTypeEnum.,
EventDate = new DateTime(2026, 7, 11),
UnwindDate = new DateTime(2026, 7, 9),
EventDate = new DateTime(2026, 7, 9),
UnwindDate = new DateTime(2026, 7, 10),
InterestMode = (int)InterestModeEnum.,
InterestPrincipal = 3000m
};
@@ -479,8 +474,8 @@ namespace YLErp.Modules.SwapModule
PositionId = 1,
PositionType = 1,
EventType = (int)SwapEventTypeEnum.,
EventDate = new DateTime(2026, 7, 11),
UnwindDate = new DateTime(2026, 7, 9),
EventDate = new DateTime(2026, 7, 9),
UnwindDate = new DateTime(2026, 7, 10),
TradingAmount = 3000000m
};
var originalWithFloat = new List<swap_position>
@@ -490,29 +485,26 @@ namespace YLErp.Modules.SwapModule
};
var flows = new[] { close, floatClose };
// settleDate=7/8经济生效日前)→ as-of=原始 10000
// settleDate=7/8事件日期前)→ as-of=原始 10000
var beforeEffective = SwapDealService.ResolveInterestLegPositionsAsOf(
originalWithFloat, new List<swap_position> { realtime }, flows, new DateTime(2026, 7, 8))
.Single(x => x.id == originalPositionId);
Assert.AreEqual(10000m, beforeEffective.InterestPrincipalFix,
"7/8经济生效日前):平仓未发生,as-of 本金应=原始 10000");
"7/8事件日期前):平仓未发生,as-of 本金应=原始 10000");
// settleDate=7/9经济生效日当天)→ as-of=实时剩余 7000
// settleDate=7/9事件日期当天)→ as-of=实时剩余 7000
var onEffectiveDate = SwapDealService.ResolveInterestLegPositionsAsOf(
originalWithFloat, new List<swap_position> { realtime }, flows, new DateTime(2026, 7, 9))
.Single(x => x.id == originalPositionId);
Assert.AreEqual(7000m, onEffectiveDate.InterestPrincipalFix,
"7/9经济生效日):平仓已生效,as-of 本金应=实时剩余 7000");
"7/9事件日期):平仓已生效,as-of 本金应=实时剩余 7000");
// 【关键·锁定 a1cdb2cd】settleDate=7/10(生效后、簿记前)→ 应按 UnwindDate 判为已生效 =7000
// 修复前按 EventDate(7/11) 分桶:7/10 < 7/11 → 误判"未发生" → 返回 10000(错误)
// 修复后按 UnwindDate(7/9) 分桶:7/10 >= 7/9 → 已生效 → 返回 7000(正确)
// settleDate=7/10(事件日期后)→ 仍为实时剩余 7000
var afterEffectiveBeforeBook = SwapDealService.ResolveInterestLegPositionsAsOf(
originalWithFloat, new List<swap_position> { realtime }, flows, new DateTime(2026, 7, 10))
.Single(x => x.id == originalPositionId);
Assert.AreEqual(7000m, afterEffectiveBeforeBook.InterestPrincipalFix,
"7/10生效后、簿记前):必须按 UnwindDate 判已生效 → 7000。" +
"若返回 10000,说明 a1cdb2cd 修复被回滚(退回按 EventDate 分桶)。");
"7/10事件日期后):必须按 EventDate 判已生效 → 7000。");
}
}
}
@@ -217,6 +217,19 @@ namespace YLErp.Modules.SwapModule
Console.WriteLine($"UW_006: CloseReCheck={service.CloseReCheckCallCount}次, SwapRealizedPnL={service.SaveSwapDealCalls[0].data.SwapRealizedPnL} ✅");
}
[TestMethod]
public void UW_014_事件日期与平仓日期强绑定()
{
var td = SwapDealTestFactory.CreateTrade();
var service = new TestableSwapDealService(td);
var unwindData = SwapDealTestFactory.CreateUnwindData(swapRealizedPnL: 0m);
service.ApplySwapTrade(unwindData, (int)SwapEventTypeEnum.);
Assert.AreEqual(unwindData.ValueDate, service.SaveSwapDealCalls[0].data.UnwindDate);
Assert.AreEqual(unwindData.ValueDate, unwindData.UnwindDate);
}
// ================================================================
// 场景7:前端传"占期初(A)"语义,后端入口转"占剩余(B)" —— 全平判定
// 原始名义本金 100M / 剩余 60M,前端传 A=0.6(平掉原始 60M = 剩余全部)