diff --git a/YLErpDAL/Modules/SwapModule/SwapEodPositionService.cs b/YLErpDAL/Modules/SwapModule/SwapEodPositionService.cs index 58fc8e85..7fb7ff54 100644 --- a/YLErpDAL/Modules/SwapModule/SwapEodPositionService.cs +++ b/YLErpDAL/Modules/SwapModule/SwapEodPositionService.cs @@ -390,12 +390,35 @@ namespace YLErp.Modules.SwapModule } unwindData.NotionalValue = Convert.ToDecimal(td.OriginalStockEqvNotional ?? 0); unwindData.PosiNotionalValue = StockEqvNotional; - autoInterests.ForEach(x => + + // 预付金腿类型列表:初始预付金、追加预付金 + var premiumModes = new List() {(int)InterestModeEnum.初始预付金, (int)InterestModeEnum.追加预付金 }; + + // 分别计算预付金腿和利息腿的金额 + var premiumInterests = autoInterests.Where(x => premiumModes.Contains(x.InterestMode)).ToList(); + var interestLegs = autoInterests.Where(x => !premiumModes.Contains(x.InterestMode)).ToList(); + + // 预付金腿金额 + decimal premiumTotal = 0; + premiumInterests.ForEach(x => { var ratio = x.InterestDirection == (int)SwapDirectionEnum.收取 ? 1 : -1; - unwindData.SwapCloseAmount = unwindData.SwapCloseAmount + x.InterestClosePnL; - unwindData.SwapRealizedPnL = unwindData.SwapCloseAmount; + premiumTotal += x.InterestClosePnL * ratio; }); + unwindData.SwapMarginRebatePnl = premiumTotal; // 预付金腿金额 + + // 利息腿金额(总金额减去预付金腿金额) + decimal interestTotal = 0; + interestLegs.ForEach(x => + { + var ratio = x.InterestDirection == (int)SwapDirectionEnum.收取 ? 1 : -1; + interestTotal += x.InterestClosePnL * ratio; + }); + unwindData.SwapCloseAmount = interestTotal; // 利息腿金额 + + // 总实现盈亏 + unwindData.SwapRealizedPnL = unwindData.SwapCloseAmount + unwindData.SwapMarginRebatePnl; + SaveAutoSwapDeal(td, autoInterests, unwindData, interval); } /// @@ -409,7 +432,20 @@ namespace YLErp.Modules.SwapModule //td.UnWindDate = unwindData.ValueDate; //优先使用 interval.SettlementDate 作为资金记录发生日期,如果没有则使用 ValueDate var cashHappenDate = interval?.SettlementDate ?? unwindData.ValueDate; - int clientCashId = AddClientCashInCashOut(td, Convert.ToDouble(-unwindData.SwapCloseAmount), ClientCashInCashOut.系统操作_互换, cashHappenDate); + + int clientCashId = 0; + // 利息腿:插入资金记录(使用系统操作_互换) + if (unwindData.SwapCloseAmount != 0) + { + clientCashId = AddClientCashInCashOut(td, Convert.ToDouble(-unwindData.SwapCloseAmount), ClientCashInCashOut.系统操作_互换, cashHappenDate); + } + + // 预付金腿:单独插入一条资金记录(系统操作_预付金返息) + if (unwindData.SwapMarginRebatePnl != 0) + { + AddClientCashInCashOut(td, Convert.ToDouble(-unwindData.SwapMarginRebatePnl), ClientCashInCashOut.系统操作_预付金返息, unwindData.ValueDate); + } + string data = JsonConvert.SerializeObject(unwindData); var swapEvent = new SwapEventService(this).AddSwapEventDate(unwindData.ValueDate, unwindData.SwapTradeId, (int)SwapEventTypeEnum.自动互换, data, clientCashId, true, "系统操作-自动互换");//将互换总额存入事件 flowEvents.ForEach(x => diff --git a/YLErpDAL/Modules/SwapModule/SwapTradeBaseService.cs b/YLErpDAL/Modules/SwapModule/SwapTradeBaseService.cs index 939503eb..219ee1c1 100644 --- a/YLErpDAL/Modules/SwapModule/SwapTradeBaseService.cs +++ b/YLErpDAL/Modules/SwapModule/SwapTradeBaseService.cs @@ -387,13 +387,25 @@ namespace YLErp.Modules.SwapModule var swapEventIds = swapEvents.Select(s => s.id).ToList(); if (swapEventIds.Any()) { - // 通过 swap_event 的 ClientCashId 删除对应的资金记录 + // 通过 swap_event 的 ClientCashId 删除对应的资金记录(利息腿) var clientCashIds = swapEvents.Where(s => s.ClientCashId > 0).Select(s => s.ClientCashId).ToList(); if (clientCashIds.Any()) { var clientCashRecords = DbContext.ClientCashInCashOut.Where(x => clientCashIds.Contains(x.id)).ToList(); DbContext.ClientCashInCashOut.RemoveRange(clientCashRecords); } + + // 删除预付金腿的资金记录(通过交易ID、日期和Action类型查找,一次性查询避免MySQL连接重用问题) + var swapTradeIds = swapEvents.Select(s => s.SwapTradeId).Distinct().ToList(); + var premiumCashRecords = DbContext.ClientCashInCashOut + .Where(x => swapTradeIds.Contains(x.TradeId ?? 0) + && x.HappenDate>=valueDate + && x.Action == ClientCashInCashOut.系统操作_预付金返息) + .ToList(); + if (premiumCashRecords.Any()) + { + DbContext.ClientCashInCashOut.RemoveRange(premiumCashRecords); + } } } DbContext.swap_event.RemoveRange(swapEvents);