using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using YLErp.DBModels;
using YLErp.DBModels.Enums;
using YLErp.Model;
using YLErp.Modules.SwapModule;
namespace YLErp.Modules.SwapModule
{
///
/// 可测试的 SwapFlowEventService 子类
/// override 所有外部依赖方法,用内存数据替代数据库和静态调用
///
public class TestableSwapFlowEventService : SwapFlowEventService
{
private readonly trade _trade;
private readonly trade_extend _tradeExtend;
private readonly List _positions;
private readonly Dictionary _underlyings;
private readonly Func _nextBusinessDay;
private readonly long _positionId;
public List PersistedEvents { get; } = new List();
public TestableSwapFlowEventService(
OptUserInfo optUser,
trade trade,
trade_extend tradeExtend,
List positions,
Dictionary underlyings,
Func nextBusinessDay,
long positionId = 999999
) : base(optUser)
{
_trade = trade;
_tradeExtend = tradeExtend;
_positions = positions ?? new List();
_underlyings = underlyings ?? new Dictionary();
_nextBusinessDay = nextBusinessDay ?? (d => d.AddDays(1));
_positionId = positionId;
}
protected override trade FindTrade(int swapTradeId) => _trade;
protected override trade_extend FindTradeExtend(int swapTradeId) => _tradeExtend;
protected override List FindPositions(int swapTradeId) => _positions;
protected override List FindAndInvalidateFutureEvents(int swapTradeId, DateTime tradeDate)
{
// 测试环境:不需要废弃历史事件
return new List();
}
protected override underlying_manager GetUnderlying(string underlyingCode)
{
return _underlyings.TryGetValue(underlyingCode, out var ul) ? ul : new underlying_manager { UnderlyingCode = underlyingCode };
}
protected override DateTime GetNextBusinessDay(DateTime date) => _nextBusinessDay(date);
protected override long ResolvePositionId(swap_flow_merge merge, DateTime maturityDate, int direction, string tradeNumber) => _positionId;
protected override void PersistEvents(List events)
{
// 不写 DB,收集到列表供验证
PersistedEvents.AddRange(events);
}
protected override IDisposable BeginTransaction() => new NoopDisposable();
protected override void CommitTransaction(IDisposable transaction) { }
protected override void RollbackTransaction(IDisposable transaction) { }
/// 公开调用 protected 的 MergePageEvent,供测试使用
public List ExecuteMergePageEvent(int swapTradeId, List flowMergeList, DateTime tradeDate, bool needTrans = false)
{
return MergePageEvent(swapTradeId, flowMergeList, tradeDate, needTrans);
}
public override void UpdateDbOption(DBModelBaseV2 dBModel)
{
// 测试环境不设 Opt 信息,避免依赖 UserInfo
}
private class NoopDisposable : IDisposable
{
public void Dispose() { }
}
}
}