diff --git a/UnitTestProject/Modules/SwapModule/InterestLegs/InterestLegStrategyTest.cs b/UnitTestProject/Modules/SwapModule/InterestLegs/InterestLegStrategyTest.cs index 9ec52504..72f14a20 100644 --- a/UnitTestProject/Modules/SwapModule/InterestLegs/InterestLegStrategyTest.cs +++ b/UnitTestProject/Modules/SwapModule/InterestLegs/InterestLegStrategyTest.cs @@ -99,14 +99,78 @@ namespace UnitTestProject.Modules.SwapModule.InterestLegs #endregion - #region 守卫:三个策略对应不同mode + #region 预付金(mode 5/6):按Fix缩放,posiPrincipal=Fix全额 [TestMethod] - public void 三个策略对应不同枚举值() + public void 预付金_初始_部分平仓_本金按Fix缩放() + { + var leg = new PrepayLeg(InterestModeEnum.初始预付金); + var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 0.5m); + + Assert.AreEqual(1_000_000m, r.ClosePrincipal, "平仓本金=Fix×50%"); + Assert.AreEqual(Fix, r.PosiPrincipal, "持仓本金=Fix全额"); + Assert.AreEqual(0.5m, r.ClosePercent); + } + + [TestMethod] + public void 预付金_追加_全平_本金等于Fix() + { + var leg = new PrepayLeg(InterestModeEnum.追加预付金); + var r = leg.CalcNotional(Fix, Notional, LongNotional, ShortNotional, 1m); + + Assert.AreEqual(Fix, r.ClosePrincipal, "全平本金=Fix"); + } + + [TestMethod] + public void 预付金_构造非法mode应抛异常() + { + Assert.ThrowsException(() => + new PrepayLeg(InterestModeEnum.固定值)); + } + + #endregion + + #region 守卫:策略对应各自mode + + [TestMethod] + public void 各策略对应正确枚举值() { Assert.AreEqual(InterestModeEnum.固定值, new FixedNotionalLeg().Mode); Assert.AreEqual(InterestModeEnum.合约名义本金规模, new ContractNotionalLeg().Mode); Assert.AreEqual(InterestModeEnum.标的期初全价, new UnderlyingFullPriceLeg().Mode); + Assert.AreEqual(InterestModeEnum.初始预付金, new PrepayLeg(InterestModeEnum.初始预付金).Mode); + Assert.AreEqual(InterestModeEnum.追加预付金, new PrepayLeg(InterestModeEnum.追加预付金).Mode); + } + + #endregion + + #region 工厂:按mode分发 + + [TestMethod] + public void 工厂_返回各活跃mode的策略() + { + Assert.IsInstanceOfType(InterestLegStrategyFactory.Get(InterestModeEnum.固定值), typeof(FixedNotionalLeg)); + Assert.IsInstanceOfType(InterestLegStrategyFactory.Get(InterestModeEnum.合约名义本金规模), typeof(ContractNotionalLeg)); + Assert.IsInstanceOfType(InterestLegStrategyFactory.Get(InterestModeEnum.标的期初全价), typeof(UnderlyingFullPriceLeg)); + Assert.IsInstanceOfType(InterestLegStrategyFactory.Get(InterestModeEnum.初始预付金), typeof(PrepayLeg)); + Assert.IsInstanceOfType(InterestLegStrategyFactory.Get(InterestModeEnum.追加预付金), typeof(PrepayLeg)); + } + + [TestMethod] + public void 工厂_未注册mode抛异常() + { + Assert.ThrowsException(() => + InterestLegStrategyFactory.Get(InterestModeEnum.持仓名义本金)); + Assert.ThrowsException(() => + InterestLegStrategyFactory.Get(InterestModeEnum.持仓市值)); + } + + [TestMethod] + public void 工厂_int重载和枚举重载等价() + { + var byEnum = InterestLegStrategyFactory.Get(InterestModeEnum.固定值); + var byInt = InterestLegStrategyFactory.Get((int)InterestModeEnum.固定值); + Assert.AreEqual(byEnum.Mode, byInt.Mode); } #endregion diff --git a/YLErpDAL/Modules/SwapModule/InterestLegs/InterestLegStrategyFactory.cs b/YLErpDAL/Modules/SwapModule/InterestLegs/InterestLegStrategyFactory.cs new file mode 100644 index 00000000..a74bfb46 --- /dev/null +++ b/YLErpDAL/Modules/SwapModule/InterestLegs/InterestLegStrategyFactory.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using YLErp.DBModels; + +namespace YLErp.Modules.SwapModule.InterestLegs; + +/// +/// 利息腿策略工厂。按 InterestMode 返回对应策略实例。 +/// 替换原 SwapDealService.CalcNotionalByMode 的 switch,收敛 mode 分发逻辑到一处。 +/// +/// 当前界面活跃 mode:1 固定值 / 2 合约名义本金规模 / 9 标的期初全价 / 5/6 预付金。 +/// 死代码 mode 3/4 不注册;半死 mode 7/8 不注册(界面已注释)。 +/// 传入未注册的 mode 会抛异常,防止静默走默认分支。 +/// +public static class InterestLegStrategyFactory +{ + private static readonly Dictionary _strategies = new() + { + [InterestModeEnum.固定值] = new FixedNotionalLeg(), + [InterestModeEnum.合约名义本金规模] = new ContractNotionalLeg(), + [InterestModeEnum.标的期初全价] = new UnderlyingFullPriceLeg(), + [InterestModeEnum.初始预付金] = new PrepayLeg(InterestModeEnum.初始预付金), + [InterestModeEnum.追加预付金] = new PrepayLeg(InterestModeEnum.追加预付金), + }; + + /// 按 mode 返回对应策略。未注册的 mode 抛 ArgumentException。 + public static IInterestLegStrategy Get(InterestModeEnum mode) + { + if (_strategies.TryGetValue(mode, out var strategy)) + return strategy; + throw new ArgumentException($"未注册的计息模式: {mode}(mode 3/4/7/8 当前未启用)", nameof(mode)); + } + + /// 按 mode 值返回对应策略,便于调用方直接传 int。 + public static IInterestLegStrategy Get(int mode) => Get((InterestModeEnum)mode); +} diff --git a/YLErpDAL/Modules/SwapModule/InterestLegs/PrepayLeg.cs b/YLErpDAL/Modules/SwapModule/InterestLegs/PrepayLeg.cs new file mode 100644 index 00000000..8cb9ca08 --- /dev/null +++ b/YLErpDAL/Modules/SwapModule/InterestLegs/PrepayLeg.cs @@ -0,0 +1,33 @@ +using YLErp.DBModels; + +namespace YLErp.Modules.SwapModule.InterestLegs; + +/// +/// 预付金(保证金)利息腿。mode 5 初始预付金 / mode 6 追加预付金 共用。 +/// +/// 业务本质:预付金是客户"存"在券商的钱,券商对其付息(方向与普通利息腿相反)。 +/// 计息基数 = InterestPrincipalFix(预付金余额),随平仓递减。 +/// +/// 本类只封装 CalcNotionalByMode 的计息基数计算部分: +/// closePrincipal = Fix × closePercent +/// posiPrincipal = Fix +/// +/// 预付金腿的其它独有逻辑(方向翻转、orginPv 重映射、衡泰路径返还置0)不在此处, +/// 后续分别抽成独立方法,保持每个类/方法最小。 +/// +public sealed class PrepayLeg : IInterestLegStrategy +{ + // 预付金两个 mode(初始/追加)共用同一套计息基数公式,构造时指定 + private readonly InterestModeEnum _mode; + public InterestModeEnum Mode => _mode; + + public PrepayLeg(InterestModeEnum mode) + { + if (mode != InterestModeEnum.初始预付金 && mode != InterestModeEnum.追加预付金) + throw new ArgumentException($"PrepayLeg 仅支持 初始预付金/追加预付金,收到 {mode}", nameof(mode)); + _mode = mode; + } + + public NotionalResult CalcNotional(decimal fix, decimal posiNotional, decimal posiLong, decimal posiShort, decimal closePercent) + => new(fix * closePercent, fix, closePercent); +}