using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using YLErp.DBModels; using YLErp.Modules.BasicDataModule; using YLErp.Modules.CalculationModule; using YLErp.Modules.TradeMsgOutputModule.Dto; namespace YLErp.Modules.TradeMsgOutputModule { /// /// 交易持仓PV导出服务 /// public class TradePvOutputService:BaseTradeAfterEodOutputService { public List GetTradePositionPV(DateTime valueDate) where T: EodTradePosition { CheckEodStatus(valueDate); var result = new List(); List positionPVList = null; Dictionary tradeDic = null; using (var db = DbContextFactory.GetYLDbContext()) { positionPVList = db.Set().Where(p => p.ValueDate == valueDate && p.TradeId > 0).Select(p => new TradePositionPvDto { TradeId = p.TradeId, TradeType = p.TradeType, PV = p.Pv }).ToList(); if(positionPVList != null && positionPVList.Count > 0) { var tradeIds = positionPVList.Select(p => p.TradeId).Distinct().ToList(); tradeDic = GetEodTradeDicSimpleDic(db, valueDate, tradeIds); } } if (positionPVList == null || tradeDic == null || positionPVList.Count == 0 || tradeDic.Count == 0) { return result; } Dictionary assetUnitDic = null; var assertIds = tradeDic.Values.Select(p => p.AssetId).Distinct().ToList(); if(assertIds!=null&& assertIds.Count > 0) { assetUnitDic = new AssetUnitDataService(new OptUserInfo(0, "系统", OptUserFrom.System)).GetAssertByAssertIds(assertIds); } if(assetUnitDic== null) { assetUnitDic = new Dictionary(); } Dictionary swapFixedInterestRatePVDic = null; var swapTradeIds = positionPVList.Where(p => ConsGlobal.TradeType.PayoffSwap.Equals(p.TradeType)).Select(p => p.TradeId).ToList(); if (swapTradeIds != null && swapTradeIds.Count > 0) { swapFixedInterestRatePVDic = PayoffSwapCalcService.GetFixedInterestRatePV(swapTradeIds, valueDate); } if (swapFixedInterestRatePVDic == null) { swapFixedInterestRatePVDic = new Dictionary(); } positionPVList.ForEach(p => { if (tradeDic.ContainsKey(p.TradeId)) { var tradeDto = tradeDic[p.TradeId]; var model = new TradePositionPV { TradeNumber = tradeDto.TradeNumber, ValueDate = valueDate, TradeType = p.TradeType, PV = p.PV }; if (ConsGlobal.TradeType.PayoffSwap.Equals(model.TradeType)) { model.FixedInterestRatePV = swapFixedInterestRatePVDic.ContainsKey(p.TradeId) ? swapFixedInterestRatePVDic[p.TradeId] : 0; model.EquitySubjectPV = model.PV - model.FixedInterestRatePV; } if (tradeDto.AssetId > 0&&assetUnitDic.ContainsKey(tradeDto.AssetId)) { model.AssetUnitGroupName = assetUnitDic[tradeDto.AssetId].GroupName; } result.Add(model); } }); return result; } private Dictionary GetFixedInterestRatePV(List tradeIds, DateTime valueDate) { var result = new Dictionary(); List tradeSwapList = null; List tradeCashList = null; List tradeCashSwapList = null; using (var db = DbContextFactory.GetYLDbContext()) { tradeSwapList = db.trade_swap.AsNoTracking().Where(p => tradeIds.Contains(p.TradeId)).ToList(); tradeCashList = db.trade_cash.AsNoTracking().Where(y => tradeIds.Contains(y.TradeId) && y.Action == "系统操作-互换" && y.ValidState != "InValid" && !y.IsDeleted && y.ValueDate <= valueDate).ToList(); tradeCashSwapList = db.trade_cash_swap.AsNoTracking().Where(p => tradeIds.Contains(p.TradeId)).ToList(); } if (tradeSwapList == null) { tradeSwapList = new List(); } if (tradeCashList == null) { tradeCashList = new List(); } if (tradeCashSwapList == null) { tradeCashSwapList = new List(); } tradeIds.ForEach(p => { var tradeSwap = tradeSwapList.FirstOrDefault(d => d.TradeId == p); if (tradeSwap != null) { var tradeCashIds = tradeCashList.Where(d => d.TradeId == p).Select(d => d.id).ToList(); if (tradeCashIds != null && tradeCashIds.Count > 0) { double amount = 0; if (!tradeSwap.IsGetFloatingProfit) { amount = tradeCashSwapList.Where(d => tradeCashIds.Contains(d.TradeCashId)).Sum(d => (double)d.GetAmount); } if (!tradeSwap.IsPayFloatingProfit) { amount= -tradeCashSwapList.Where(d => tradeCashIds.Contains(d.TradeCashId)).Sum(d => (double)d.PayAmount); } result.Add(p, amount); } } }); return result; } } }