test(swap): 补ApproveSwapTrade/ApplySwapTrade测试+3个seam(借鉴testable分支)

借鉴 origin/glms/feature/refactor-swap-event-testable 的 SwapUnwindScenarioTest 场景4/5,
补全当前分支缺失的平仓审批流程覆盖。

SwapDealService 新增3个 protected virtual seam:
- FindSwapEvent(查待审核事件)
- FindFlowEventsByEventId(查事件关联流水)
- CloseReCheckSetTrade(前置校验包装)
ApproveSwapTrade/ApplySwapTrade 重构为调seam,生产行为不变。

SwapDealSettlementTest 新增2个测试(SD_005/006,共6个):
- SD_005 ApproveSwapTrade 全平仓审核: 反序列化swap_event.EventData→资金流水→TradeStatus
- SD_006 ApplySwapTrade 提交审核: CloseReCheck前置校验+SwapRealizedPnL=SwapCloseAmount

融合策略: 保留当前分支SwapIncome资金流水断言独特价值(SD_001/002),
借鉴testable分支审批流程覆盖(SD_005/006),不二选一。
SwapModule 157测试全绿,无回归。
This commit is contained in:
hjhan
2026-07-03 08:30:36 +08:00
parent cf0d965ff8
commit 129e4bed0d
2 changed files with 138 additions and 22 deletions
+29 -21
View File
@@ -95,6 +95,26 @@ namespace YLErp.Modules.SwapModule
});
}
/// <summary>查找待审核的互换/平仓事件(生产: DbContext.swap_event 查询;测试: 返回内存对象)</summary>
protected virtual swap_event FindSwapEvent(int tradeId, int eventType)
{
return DbContext.swap_event
.Where(x => x.SwapTradeId == tradeId && !x.Invalid && x.EventType == eventType)
.OrderByDescending(o => o.id).FirstOrDefault();
}
/// <summary>查找事件关联的流水事件(生产: DbContext.swap_flow_event 查询;测试: 返回内存列表)</summary>
protected virtual List<swap_flow_event> FindFlowEventsByEventId(long eventId)
{
return DbContext.swap_flow_event.Where(x => x.EventId == eventId).ToList();
}
/// <summary>平仓/互换审核的前置校验与状态设置(生产: new TradeUnwindService;测试: 空操作或计数)</summary>
protected virtual void CloseReCheckSetTrade(int swapTradeId, bool isSwap, bool needCheck)
{
new TradeUnwindService(this).CloseReCheck_SetTrade(swapTradeId, isSwap, needCheck);
}
#endregion
public SwapDealService(OptUserInfo optUser) : base(optUser)
@@ -1578,14 +1598,13 @@ namespace YLErp.Modules.SwapModule
/// <exception cref="Exception"></exception>
public void ApproveSwapTrade(trade td, int eventType)
{
Expression<Func<swap_event, bool>> eventExpression = x => x.SwapTradeId == td.id && !x.Invalid && x.EventType == eventType;
var swapEvent = DbContext.swap_event.Where(eventExpression).OrderByDescending(o => o.id).FirstOrDefault();
var swapEvent = FindSwapEvent(td.id, eventType);
if (swapEvent == null)
{
throw new Exception("该笔交易状态为平仓待复核,未找到相关记录,请检查该笔交易是否有效");
}
swapEvent.unwindData = JsonConvert.DeserializeObject<UnwindData>(swapEvent.EventData);
var flowList = DbContext.swap_flow_event.Where(x => x.EventId == swapEvent.id).ToList();
var flowList = FindFlowEventsByEventId(swapEvent.id);
string action = eventType == (int)SwapEventTypeEnum. ? ClientCashInCashOut._互换 : ClientCashInCashOut._平仓费;
int clientCashId = AddClientCash(td, Convert.ToDouble(-swapEvent.unwindData.SwapRealizedPnL), action, swapEvent.unwindData.ValueDate);
if (swapEvent.unwindData.SwapMarginAmount != 0)
@@ -1605,14 +1624,14 @@ namespace YLErp.Modules.SwapModule
}
td.UnWindDate = swapEvent.unwindData.UnwindDate;
if (eventType != (int)SwapEventTypeEnum.)
{
{
td.StockEqvNotional -= Convert.ToDouble(swapEvent.unwindData.CloseNotionalValue);
td.TradeAmount -= Convert.ToDouble(swapEvent.unwindData.CloseQty);
}
td.Notional = td.TradeAmount;
UpdateInitalPosition(flowList, swapEvent.unwindData, eventType);
DbContext.SaveChanges();
SaveAllChanges();
}
/// <summary>
@@ -1623,30 +1642,19 @@ namespace YLErp.Modules.SwapModule
/// <exception cref="ServiceException"></exception>
public void ApplySwapTrade(UnwindData unwindData, int eventType)
{
var td = DbContext.trade.Find(unwindData.SwapTradeId);
var td = FindTrade(unwindData.SwapTradeId);
if (td == null)
{
throw new ServiceException("未找到交易信息");
}
unwindData.SwapRealizedPnL = unwindData.SwapCloseAmount;
var trans = DbContext.Database.BeginTransaction();
string action = eventType == (int)SwapEventTypeEnum. ? ClientCashInCashOut._互换 : ClientCashInCashOut._平仓费;
try
ExecuteInTransaction(() =>
{
new TradeUnwindService(this).CloseReCheck_SetTrade(unwindData.SwapTradeId, eventType == (int)SwapEventTypeEnum., true);
CloseReCheckSetTrade(unwindData.SwapTradeId, eventType == (int)SwapEventTypeEnum., true);
SaveSwapDeal(unwindData, eventType, 0, action, true);
DbContext.SaveChanges();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw;
}
finally
{
trans.Dispose();
}
SaveAllChanges();
});
}
/// <summary>
/// 保存平仓/互换事件