Files
zszq-trs/YLErpDAL/Modules/TradeMsgOutputModule/TradePvOutputService.cs
T
2024-05-09 14:06:26 +08:00

148 lines
6.1 KiB
C#

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
{
/// <summary>
/// 交易持仓PV导出服务
/// </summary>
public class TradePvOutputService:BaseTradeAfterEodOutputService
{
public List<TradePositionPV> GetTradePositionPV<T>(DateTime valueDate) where T: EodTradePosition
{
CheckEodStatus(valueDate);
var result = new List<TradePositionPV>();
List<TradePositionPvDto> positionPVList = null;
Dictionary<int, TradeDicSimpleDto> tradeDic = null;
using (var db = DbContextFactory.GetYLDbContext())
{
positionPVList = db.Set<T>().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<int, AssetUnitDto> 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<int, AssetUnitDto>();
}
Dictionary<int, double> 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<int, double>();
}
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<int, double> GetFixedInterestRatePV(List<int> tradeIds, DateTime valueDate)
{
var result = new Dictionary<int, double>();
List<trade_swap> tradeSwapList = null;
List<trade_cash> tradeCashList = null;
List<trade_cash_swap> 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<trade_swap>();
}
if (tradeCashList == null)
{
tradeCashList = new List<trade_cash>();
}
if (tradeCashSwapList == null)
{
tradeCashSwapList = new List<trade_cash_swap>();
}
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;
}
}
}