using YLErp.DBModels.Abstract; using YLErp.DBModels.Consts; using YLErp.Modules.DataProviderModule; using YLErp.Plugins.TradeDocGenerator.Abstracts; namespace YLErp.Modules.TradeModule.DocGenerateModule { /// /// 交易结算生成上下文 /// public class MarginReportGenerateContext : ConfirmationGenerateContext, ITradeMarginReportGeneratorContext { private readonly trade_span _tradeSpan; public MarginReportGenerateContext(int tradeCashId, trade tradeObj, trade_span tradeSpan, string contractType, OptUserInfo userInfo) : base(tradeCashId, tradeObj, contractType, userInfo) { _tradeSpan = tradeSpan ?? throw new ArgumentNullException(nameof(tradeSpan)); } //交易确认书对象 trade_contract_document _contractDoc; double? _underlyingSettlePrice = null; /// /// 获取交易确认书 /// public ITradeContractDocument GetTradeContractDocument() { if (_contractDoc == null) { var db = DbContextFactory.GetYLDbContext(); var query = from contractDoc in db.trade_contract_document join tcr in db.trade_contract_r on contractDoc.Code equals tcr.ContractCode where tcr.TradeId == Trade.id && contractDoc.Type == ContractTypeEnum.Trade && tcr.Type == ContractTypeEnum.Trade && tcr.IsValid select contractDoc; _contractDoc = query.FirstOrDefault(); } if (_contractDoc == null) { throw new ServiceException($"未找到交易确认书(交易ID:{Trade.id},交易编号:{Trade.TradeNumber})"); } return _contractDoc; } public trade_span GetTradeSpan() { return _tradeSpan; } public bool OnMarginReportGenerated() { throw new NotImplementedException(); } public double GetUnderlyingSettlePrice() { if (_underlyingSettlePrice == null) { if (EodPriceQueryService.TryGetEodPrice(_tradeSpan.ValueDate, Trade.UnderlyingCode, out var eodPrice)) { _underlyingSettlePrice = eodPrice.GetPrice(Trade.SettlementType); } else { throw new ServiceException($"未找到交易{Trade.TradeNumber}对应标的{Trade.UnderlyingCode}的{Trade.GetSettlementTypeDesc()}"); } } return _underlyingSettlePrice.Value; } public override DateTime SystemValueDate => _tradeSpan.ValueDate; } }