using BaseOUDAL; using YLErp.BLL; using YLErp.BLL.EodSettlement; using YLErp.Model; using YLErp.Modules.EodModule; using YLErp.Modules.SuperviseReportModule.CFMMC.Model; using YLErp.QdpModule; using static YLErp.ConsGlobal; namespace YLErp.Modules.SuperviseReportModule.CFMMC.Service { /// /// 监管报告授信数据服务 /// public class SuperviseReportMonthlyService : ClientBaseService { private valuedate config; public SuperviseReportMonthlyService(OptUserInfo userInfo) : base(userInfo) { config = yldb.valuedate.FirstOrDefault(x => x.State == valuedate.当前使用); } public SuperviseReportMonthlyCreditModel SearchSuperviseCreditReportList(SuperviseReportReq req) { if (string.IsNullOrWhiteSpace(req.sidx)) { req.sidx = "ClientId"; } var systemValueDate = valuedateBLL.ValueDate; if (req.ValueDate > systemValueDate || (req.ValueDate.Year == systemValueDate.Year && req.ValueDate.Month == systemValueDate.Month)) { req.ValueDate = valuedateBLL.ValueDate; } else { req.ValueDate = req.ValueDate.AddDays(1 - req.ValueDate.Day).AddMonths(1).AddDays(-1); req.ValueDate = QdpModule.QdpCalendarHelper.GetNonHolidayDefore(req.ValueDate); } //var query = from credit in yldb.credit // where credit.ProcessStatus == "已审批" && credit.Type == CreditTable.ClientType // && credit.ClientId > 0 && credit.Credit != null // && (credit.CreditStartDate == null || credit.CreditStartDate <= req.ValueDate) && credit.CreditDeadLine >= req.ValueDate // select new SuperviseReportMonthlyCreditRowModel // { // ClientId = credit.ClientId.Value, // Credit = credit.Credit.Value // }; var eodTradeClientIdList = (from eTrade in yldb.eod_trade where eTrade.ValueDate == req.ValueDate && eTrade.TradeStatus == "确认成交" select eTrade.ClientId).Distinct().ToList(); var MonthStartDateTime = DateTime.Parse(req.ValueDate.ToString("yyyy-MM-01")); var tradeCashClientIdList = (from cash in yldb.trade_cash join trade in yldb.trade on cash.TradeId equals trade.id where cash.ValueDate >= MonthStartDateTime && cash.ValueDate <= req.ValueDate && cash.Action == "系统操作-期权费" && cash.ValidState != "InValid" && cash.IsDeleted == false select trade.ClientId).Distinct().ToList(); var clientIdList = eodTradeClientIdList.Concat(tradeCashClientIdList).Distinct().ToList(); var query = from cbDaily in yldb.ClientBalanceDaily where cbDaily.BalanceDate == req.ValueDate && (cbDaily.ToDayRemainFund > 0 || cbDaily.Credit > 0 || clientIdList.Contains(cbDaily.ClientId)) select new SuperviseReportMonthlyCreditRowModel { ClientId = cbDaily.ClientId, Credit = cbDaily.Credit ?? 0 }; SuperviseReportMonthlyCreditModel result = new SuperviseReportMonthlyCreditModel(query.ToSearchList(req)); var clientIds = result.rows.Select(O => O.ClientId); var balanceDict = yldb.ClientBalanceDaily.Where(n => clientIds.Contains(n.ClientId) && n.BalanceDate == req.ValueDate).ToDictionary(K => K.ClientId, V => V); foreach (var item in result.rows) { if (balanceDict.TryGetValue(item.ClientId, out var balance)) { item.ClientPositionCredit = item.IsTradeCredit ? (balance.ToDayRemainFund ?? 0) : (balance.MarginBalance + balance.PayableMargin) ?? 0; item.ClientPositionCredit = Math.Abs(item.ClientPositionCredit > 0 ? 0 : item.ClientPositionCredit); item.ClientPositionCredit = item.Credit < item.ClientPositionCredit ? item.Credit : item.ClientPositionCredit; } var client = DataCacheProvider.GetClientDataSource().GetData(item.ClientId); if (client != null) { item.Number = client.Number; item.ClientName = client.Name; item.ClientLicenseCode = client.LicenseCode; item.CustomerNature = client.CustomerNature2; item.IsTradeCredit = client.IsTradeCredit == 1; item.CreditRange = item.Credit == 0 ? "" : (client.IsTradeCredit == 1 ? CreditRange.CoverTradePrice_InitMargin_Margin : CreditRange.CoverInitMargin_Margin); item.IsSmallAndMicroEnterprisesStr = client.IsSmallAndMicroEnterprisesStr; item.IsListedStr = client.IsListedStr; } item.Remark = ""; var clientbalance = ClientBalanceUtility.GetClientBanlances(new List { item.ClientId }, DateTime.MinValue, req.ValueDate).FirstOrDefault(); item.AmountFund = clientbalance.AmountFund; item.PayableMargin = clientbalance.PayableMargin; } var userInfo = UserBLL.GetById(UserId); result.SuperviseMainCode = config.SuperviseMainCode; result.CompanyName = config.SuperviseMainName; result.ReportDate = config.ValueDate.ToString("yyyy年M月d日"); result.DataDate = req.ValueDate.ToString("yyyy年M月份"); result.ReportUser = userInfo.Name; result.Tel = userInfo.Tel; return result; } public SuperviseReportMonthlyTradeModel SearchSuperviseTradeReportList(DateTime date, bool isCommodity) { var userInfo = UserBLL.GetById(UserId); var monthlyStart = date.AddDays(-date.Day).AddDays(1); var monthlyEnd = date.AddDays(1 - date.Day).AddMonths(1).AddDays(-1); monthlyStart = QdpCalendarHelper.GetNonHoliday(monthlyStart); monthlyEnd = EodDataHelper.GetLastSettleDate(monthlyEnd); var preMonthlyEnd = QdpCalendarHelper.GetNonHolidayDefore(monthlyStart.AddDays(-1)); var yearPnlStart = QdpCalendarHelper.GetNonHoliday(new DateTime(date.Year, 1, 1)); SuperviseReportMonthlyTradeModel result = new SuperviseReportMonthlyTradeModel(); result.SuperviseMainCode = config.SuperviseMainCode; result.CompanyName = config.SuperviseMainName; result.ReportDate = config.ValueDate.ToString("yyyy年M月d日"); result.DataDate = date.ToString("yyyy年M月份"); result.ReportUser = userInfo.Name; result.Tel = userInfo.Tel; List otherClientType = new List() { "私募基金", "券商", "风险管理公司", "银行", "一般投资公司", "保险公司" }; var insidedClientIds = DataCacheProvider.GetClientDataSource().AsQueryable().Where(O => O.IsInsided == 1).Select(O => O.id).ToList(); //Dictionary clientIdDict = DbContext.client.Where(O => O.ProcessStatus == "已开户" && !insidedClientIds.Contains(O.id)).Select(O => new { ClientType = otherClientType.Contains(O.CustomerNature2) ? "其他机构" : "产业客户", O.id }).GroupBy(O => O.ClientType).ToDictionary(K => K.Key, V => V.Select(O => O.id).ToArray()); //if (!clientIdDict.ContainsKey("产业客户")) //{ clientIdDict["产业客户"] = new int[0]; } //if (!clientIdDict.ContainsKey("其他机构")) //{ clientIdDict["其他机构"] = new int[0]; } result.MonthlyTransactionSizeInfo = getMonthlyTransactionSizeInfo(monthlyStart, monthlyEnd, preMonthlyEnd, yearPnlStart, isCommodity); //result.TransactionScaleClientInfo = getTransactionScaleClientInfo(clientIdDict["产业客户"], clientIdDict["其他机构"], monthlyEnd, preMonthlyEnd); //result.TransactionScaleClientTradeInfo = getTransactionScaleClientTradeInfo(clientIdDict["产业客户"], clientIdDict["其他机构"], monthlyStart, monthlyEnd, yearPnlStart); //result.TransactionScaleStockClientInfo = getTransactionScaleStockClientInfo(clientIdDict["产业客户"], clientIdDict["其他机构"], monthlyEnd, preMonthlyEnd); //result.TransactionScaleMarginInfo = getTransactionScaleMarginInfo(monthlyEnd, preMonthlyEnd); result.TransactionScalePnlInfo = getTransactionScalePnlInfo(monthlyStart, monthlyEnd, preMonthlyEnd, yearPnlStart, isCommodity); return result; } /// /// MR008 - 场外商品衍生品月度交易规模统计表 /// /// /// /// /// private MonthlyTransactionSizeInfoGroup getMonthlyTransactionSizeInfo(DateTime monthlyStart, DateTime monthlyEnd, DateTime preMonthlyEnd, DateTime yearPnlStart, bool isCommodity) { MonthlyTransactionSizeInfoGroup result = new MonthlyTransactionSizeInfoGroup(); var commoditys = new List { InstrumentType.CommodityFutures, InstrumentType.CommoditySpot, InstrumentType.GoldFutures, InstrumentType.GoldSpot, InstrumentType.OtherFutures, InstrumentType.OtherSpot, InstrumentType.AbroadFutures, InstrumentType.AbroadSpot }; var stocks = new List { InstrumentType.Stock, InstrumentType.HKStock, InstrumentType.AbroadStock }; var indexs = new List { InstrumentType.StockIndex, InstrumentType.HKStockIndex, InstrumentType.AbroadStockIndex }; var funds = new List { InstrumentType.Fund }; var unionInstrumentTypes = new List(); unionInstrumentTypes.AddRange(stocks); unionInstrumentTypes.AddRange(indexs); unionInstrumentTypes.AddRange(funds); unionInstrumentTypes.AddRange(commoditys); if (isCommodity) { Func buildPredicateOfSwapCommodity = new Func(O => commoditys.Contains(O.UnderlyingInstrumentType) && O.TradeType == "收益互换" && O.IsGroup != 1); //结构化交易按照子交易算,黑箱交易按照主交易算 Func buildPredicateOfOptionCommodity = new Func(O => commoditys.Contains(O.UnderlyingInstrumentType) && O.TradeType != "远期" && O.TradeType != "收益互换" && (O.TradeType != "结构化交易" || O.IsGroup == 1) && O.IsGroup != 2); Func buildPredicateOfForwardCommodity = new Func(O => commoditys.Contains(O.UnderlyingInstrumentType) && O.TradeType == "远期"); using (var db = new YLContext()) { var lastMonthlyTradeList = db.eod_trade.Where(O => O.ValueDate == preMonthlyEnd && O.TradeStatus == "确认成交" && O.ClientId > 0).Select(O => new xodTradeBase() { TradeJson = O.TradeJson }).ToList().Select(O => O.trade).ToList(); SetUnderlyingInfo(lastMonthlyTradeList); //名义本金-上月末存量 result.Notional.Swap.PreMonthlyCount.Commodity = lastMonthlyTradeList.Where(buildPredicateOfSwapCommodity).Sum(O => (O.StockEqvNotional / O.OriginalStockEqvNotionalV2 * O.StockEqvNotionalReal) ?? 0); result.Notional.Option.PreMonthlyCount.Commodity = lastMonthlyTradeList.Where(buildPredicateOfOptionCommodity).Sum(O => (O.StockEqvNotional / O.OriginalStockEqvNotionalV2 * O.StockEqvNotionalReal) ?? 0); result.Notional.Forward.PreMonthlyCount.Commodity = lastMonthlyTradeList.Where(buildPredicateOfForwardCommodity).Sum(O => (O.StockEqvNotional / O.OriginalStockEqvNotionalV2 * O.StockEqvNotionalReal) ?? 0); //交易确认书-上月末存量 result.ConfirmBook.Swap.PreMonthlyCount.Commodity = lastMonthlyTradeList.Where(buildPredicateOfSwapCommodity).Count(); result.ConfirmBook.Option.PreMonthlyCount.Commodity = lastMonthlyTradeList.Where(buildPredicateOfOptionCommodity).Count(); result.ConfirmBook.Forward.PreMonthlyCount.Commodity = lastMonthlyTradeList.Where(buildPredicateOfForwardCommodity).Count(); var monthlyOpenTradeList = db.trade.Where(x => x.TradeDate >= monthlyStart && x.TradeDate <= monthlyEnd && x.ValidState != "InValid").ToList(); SetUnderlyingInfo(monthlyOpenTradeList); var clientIds = monthlyOpenTradeList.Select(x => x.ClientId).ToArray(); var clientIdsSmallAndMicro = DataCacheProvider.GetClientDataSource().AsQueryable().Where(x => clientIds.Contains(x.id) && x.IsSmallAndMicroEnterprises == 1).Select(x => x.id).ToArray(); var clientIdsListed = DataCacheProvider.GetClientDataSource().AsQueryable().Where(x => clientIds.Contains(x.id) && x.IsListed == 1).Select(x => x.id).ToArray(); var monthlyOpenTradeListSmallAndMicro = monthlyOpenTradeList.Where(x => clientIdsSmallAndMicro.Contains(x.ClientId)).ToList(); var monthlyOpenTradeListListed = monthlyOpenTradeList.Where(x => clientIdsListed.Contains(x.ClientId)).ToList(); //名义本金-本月新增开仓 result.Notional.Swap.MonthlyOpen.Commodity = monthlyOpenTradeList.Where(buildPredicateOfSwapCommodity).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.MonthlyOpen.Commodity = monthlyOpenTradeList.Where(buildPredicateOfOptionCommodity).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.MonthlyOpen.Commodity = monthlyOpenTradeList.Where(buildPredicateOfForwardCommodity).Sum(O => O.StockEqvNotionalReal); result.Notional.Swap.MonthlyOpenSmallAndMicro.Commodity = monthlyOpenTradeListSmallAndMicro.Where(buildPredicateOfSwapCommodity).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.MonthlyOpenSmallAndMicro.Commodity = monthlyOpenTradeListSmallAndMicro.Where(buildPredicateOfOptionCommodity).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.MonthlyOpenSmallAndMicro.Commodity = monthlyOpenTradeListSmallAndMicro.Where(buildPredicateOfForwardCommodity).Sum(O => O.StockEqvNotionalReal); result.Notional.Swap.MonthlyOpenListed.Commodity = monthlyOpenTradeListListed.Where(buildPredicateOfSwapCommodity).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.MonthlyOpenListed.Commodity = monthlyOpenTradeListListed.Where(buildPredicateOfOptionCommodity).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.MonthlyOpenListed.Commodity = monthlyOpenTradeListListed.Where(buildPredicateOfForwardCommodity).Sum(O => O.StockEqvNotionalReal); //交易确认书-本月新增开仓 result.ConfirmBook.Swap.MonthlyOpen.Commodity = monthlyOpenTradeList.Where(buildPredicateOfSwapCommodity).Count(); result.ConfirmBook.Option.MonthlyOpen.Commodity = monthlyOpenTradeList.Where(buildPredicateOfOptionCommodity).Count(); result.ConfirmBook.Forward.MonthlyOpen.Commodity = monthlyOpenTradeList.Where(buildPredicateOfForwardCommodity).Count(); result.ConfirmBook.Swap.MonthlyOpenSmallAndMicro.Commodity = monthlyOpenTradeListSmallAndMicro.Where(buildPredicateOfSwapCommodity).Count(); result.ConfirmBook.Option.MonthlyOpenSmallAndMicro.Commodity = monthlyOpenTradeListSmallAndMicro.Where(buildPredicateOfOptionCommodity).Count(); result.ConfirmBook.Forward.MonthlyOpenSmallAndMicro.Commodity = monthlyOpenTradeListSmallAndMicro.Where(buildPredicateOfForwardCommodity).Count(); result.ConfirmBook.Swap.MonthlyOpenListed.Commodity = monthlyOpenTradeListListed.Where(buildPredicateOfSwapCommodity).Count(); result.ConfirmBook.Option.MonthlyOpenListed.Commodity = monthlyOpenTradeListListed.Where(buildPredicateOfOptionCommodity).Count(); result.ConfirmBook.Forward.MonthlyOpenListed.Commodity = monthlyOpenTradeListListed.Where(buildPredicateOfForwardCommodity).Count(); var tradeKey = db.eod_trade.Where(O => O.ValueDate >= monthlyStart && O.ValueDate <= monthlyEnd && O.TradeStatus == "已平仓" && O.ClientId > 0).Select(O => new { O.TradeId, O.ValueDate }); var monthlyCloseTradeCashList = (from tc in db.trade_cash join t in db.trade on tc.TradeId equals t.id where (tc.HappenedDate >= monthlyStart && tc.HappenedDate <= monthlyEnd || tc.ValueDate >= monthlyStart && tc.ValueDate <= monthlyEnd) && tc.ValidState != "InValid" && !tc.IsDeleted && tc.Action == ClientCashInCashOut.系统操作_平仓费 && tc.UnwindType != "部分行权" select new { trade = t, tc.UnwindPercentRate, tc.IsLastAction }).ToList(); SetUnderlyingInfo(monthlyCloseTradeCashList.Select(O => O.trade).ToList()); //名义本金-本月平仓 result.Notional.Swap.MonthlyUnwind.Commodity = monthlyCloseTradeCashList.Where(O => commoditys.Contains(O.trade.UnderlyingInstrumentType) && O.trade.TradeType == "收益互换" && O.trade.IsGroup != 1).Sum(O => O.trade.OriginalStockEqvNotional * O.UnwindPercentRate ?? 0); result.Notional.Option.MonthlyUnwind.Commodity = monthlyCloseTradeCashList.Where(O => commoditys.Contains(O.trade.UnderlyingInstrumentType) && O.trade.TradeType != "远期" && O.trade.TradeType != "收益互换" && (O.trade.TradeType != "结构化交易" || O.trade.IsGroup == 1) && O.trade.IsGroup != 2).Sum(O => O.trade.OriginalStockEqvNotional * O.UnwindPercentRate ?? 0); result.Notional.Forward.MonthlyUnwind.Commodity = monthlyCloseTradeCashList.Where(O => commoditys.Contains(O.trade.UnderlyingInstrumentType) && O.trade.TradeType == "远期").Sum(O => O.trade.OriginalStockEqvNotional * O.UnwindPercentRate ?? 0); //交易确认书-本月平仓 result.ConfirmBook.Swap.MonthlyUnwind.Commodity = monthlyCloseTradeCashList.Where(O => commoditys.Contains(O.trade.UnderlyingInstrumentType) && O.trade.TradeType == "收益互换" && O.trade.IsGroup != 1 && O.IsLastAction).Count(); result.ConfirmBook.Option.MonthlyUnwind.Commodity = monthlyCloseTradeCashList.Where(O => commoditys.Contains(O.trade.UnderlyingInstrumentType) && O.trade.TradeType != "远期" && O.trade.TradeType != "收益互换" && (O.trade.TradeType != "结构化交易" || O.trade.IsGroup == 1) && O.trade.IsGroup != 2 && O.IsLastAction).Count(); result.ConfirmBook.Forward.MonthlyUnwind.Commodity = monthlyCloseTradeCashList.Where(O => commoditys.Contains(O.trade.UnderlyingInstrumentType) && O.trade.TradeType == "远期" && O.IsLastAction).Count(); var monthlyExecuteTradeCashList = (from tc in db.trade_cash join t in db.trade on tc.TradeId equals t.id where (tc.HappenedDate >= monthlyStart && tc.HappenedDate <= monthlyEnd || tc.ValueDate >= monthlyStart && tc.ValueDate <= monthlyEnd) && tc.ValidState != "InValid" && !tc.IsDeleted && (tc.Action == ClientCashInCashOut.系统操作_平仓费 && tc.UnwindType == "部分行权" || tc.Action == ClientCashInCashOut.系统操作_行权费 || tc.Action == ClientCashInCashOut.系统操作_票息 || tc.Action == ClientCashInCashOut.系统操作_互换) select new { trade = t, tc.UnwindPercentRate, tc.IsLastAction }).ToList(); SetUnderlyingInfo(monthlyExecuteTradeCashList.Select(O => O.trade).ToList()); //名义本金-本月执行/终止交易 result.Notional.Swap.MonthlyExecute.Commodity = monthlyExecuteTradeCashList.Where(O => commoditys.Contains(O.trade.UnderlyingInstrumentType) && O.trade.TradeType == "收益互换" && O.trade.IsGroup != 1).Sum(O => O.trade.OriginalStockEqvNotional * O.UnwindPercentRate ?? 0); result.Notional.Option.MonthlyExecute.Commodity = monthlyExecuteTradeCashList.Where(O => commoditys.Contains(O.trade.UnderlyingInstrumentType) && O.trade.TradeType != "远期" && O.trade.TradeType != "收益互换" && (O.trade.TradeType != "结构化交易" || O.trade.IsGroup == 1) && O.trade.IsGroup != 2).Sum(O => O.trade.OriginalStockEqvNotional * O.UnwindPercentRate ?? 0); result.Notional.Forward.MonthlyExecute.Commodity = monthlyExecuteTradeCashList.Where(O => commoditys.Contains(O.trade.UnderlyingInstrumentType) && O.trade.TradeType == "远期").Sum(O => O.trade.OriginalStockEqvNotional * O.UnwindPercentRate ?? 0); //交易确认书-本月执行/终止交易 result.ConfirmBook.Swap.MonthlyExecute.Commodity = monthlyExecuteTradeCashList.Where(O => commoditys.Contains(O.trade.UnderlyingInstrumentType) && O.trade.TradeType == "收益互换" && O.trade.IsGroup != 1 && O.IsLastAction).Count(); result.ConfirmBook.Option.MonthlyExecute.Commodity = monthlyExecuteTradeCashList.Where(O => commoditys.Contains(O.trade.UnderlyingInstrumentType) && O.trade.TradeType != "远期" && O.trade.TradeType != "收益互换" && (O.trade.TradeType != "结构化交易" || O.trade.IsGroup == 1) && O.trade.IsGroup != 2 && O.IsLastAction).Count(); result.ConfirmBook.Forward.MonthlyExecute.Commodity = monthlyExecuteTradeCashList.Where(O => commoditys.Contains(O.trade.UnderlyingInstrumentType) && O.trade.TradeType == "远期" && O.IsLastAction).Count(); var monthlyTradeList = db.eod_trade .Where(O => O.ValueDate == monthlyEnd && O.TradeStatus == "确认成交" && O.ClientId > 0) .Select(O => new xodTradeBase() { TradeJson = O.TradeJson }).ToList().Select(O => O.trade).ToList(); SetUnderlyingInfo(monthlyTradeList); var clientIdsMonthlyCount = monthlyTradeList.Select(x => x.ClientId).ToArray(); var clientIdsMonthlyCountSmallAndMicro = DataCacheProvider.GetClientDataSource().AsQueryable().Where(x => clientIdsMonthlyCount.Contains(x.id) && x.IsSmallAndMicroEnterprises == 1).Select(x => x.id).ToArray(); var clientIdsMonthlyCountListed = DataCacheProvider.GetClientDataSource().AsQueryable().Where(x => clientIds.Contains(x.id) && x.IsListed == 1).Select(x => x.id).ToArray(); var monthlyTradeListSmallAndMicro = monthlyTradeList.Where(x => clientIdsMonthlyCountSmallAndMicro.Contains(x.ClientId)).ToList(); var monthlyTradeListListed = monthlyTradeList.Where(x => clientIdsMonthlyCountListed.Contains(x.ClientId)).ToList(); //名义本金-本月末存量 result.Notional.Swap.MonthlyCount.Commodity = monthlyTradeList.Where(buildPredicateOfSwapCommodity).Sum(O => (O.StockEqvNotional / O.OriginalStockEqvNotionalV2 * O.StockEqvNotionalReal) ?? 0); result.Notional.Option.MonthlyCount.Commodity = monthlyTradeList.Where(buildPredicateOfOptionCommodity).Sum(O => (O.StockEqvNotional / O.OriginalStockEqvNotionalV2 * O.StockEqvNotionalReal) ?? 0); result.Notional.Forward.MonthlyCount.Commodity = monthlyTradeList.Where(buildPredicateOfForwardCommodity).Sum(O => (O.StockEqvNotional / O.OriginalStockEqvNotionalV2 * O.StockEqvNotionalReal) ?? 0); result.Notional.Swap.MonthlyCountSmallAndMicro.Commodity = monthlyTradeListSmallAndMicro.Where(buildPredicateOfSwapCommodity).Sum(O => (O.StockEqvNotional / O.OriginalStockEqvNotionalV2 * O.StockEqvNotionalReal) ?? 0); result.Notional.Option.MonthlyCountSmallAndMicro.Commodity = monthlyTradeListSmallAndMicro.Where(buildPredicateOfOptionCommodity).Sum(O => (O.StockEqvNotional / O.OriginalStockEqvNotionalV2 * O.StockEqvNotionalReal) ?? 0); result.Notional.Forward.MonthlyCountSmallAndMicro.Commodity = monthlyTradeListSmallAndMicro.Where(buildPredicateOfForwardCommodity).Sum(O => (O.StockEqvNotional / O.OriginalStockEqvNotionalV2 * O.StockEqvNotionalReal) ?? 0); result.Notional.Swap.MonthlyCountListed.Commodity = monthlyTradeListListed.Where(buildPredicateOfSwapCommodity).Sum(O => (O.StockEqvNotional / O.OriginalStockEqvNotionalV2 * O.StockEqvNotionalReal) ?? 0); result.Notional.Option.MonthlyCountListed.Commodity = monthlyTradeListListed.Where(buildPredicateOfOptionCommodity).Sum(O => (O.StockEqvNotional / O.OriginalStockEqvNotionalV2 * O.StockEqvNotionalReal) ?? 0); result.Notional.Forward.MonthlyCountListed.Commodity = monthlyTradeListListed.Where(buildPredicateOfForwardCommodity).Sum(O => (O.StockEqvNotional / O.OriginalStockEqvNotionalV2 * O.StockEqvNotionalReal) ?? 0); //交易确认书-本月末存量 result.ConfirmBook.Swap.MonthlyCount.Commodity = monthlyTradeList.Where(buildPredicateOfSwapCommodity).Count(); result.ConfirmBook.Option.MonthlyCount.Commodity = monthlyTradeList.Where(buildPredicateOfOptionCommodity).Count(); result.ConfirmBook.Forward.MonthlyCount.Commodity = monthlyTradeList.Where(buildPredicateOfForwardCommodity).Count(); result.ConfirmBook.Swap.MonthlyCountSmallAndMicro.Commodity = monthlyTradeListSmallAndMicro.Where(buildPredicateOfSwapCommodity).Count(); result.ConfirmBook.Option.MonthlyCountSmallAndMicro.Commodity = monthlyTradeListSmallAndMicro.Where(buildPredicateOfOptionCommodity).Count(); result.ConfirmBook.Forward.MonthlyCountSmallAndMicro.Commodity = monthlyTradeListSmallAndMicro.Where(buildPredicateOfForwardCommodity).Count(); result.ConfirmBook.Swap.MonthlyCountListed.Commodity = monthlyTradeListListed.Where(buildPredicateOfSwapCommodity).Count(); result.ConfirmBook.Option.MonthlyCountListed.Commodity = monthlyTradeListListed.Where(buildPredicateOfOptionCommodity).Count(); result.ConfirmBook.Forward.MonthlyCountListed.Commodity = monthlyTradeListListed.Where(buildPredicateOfForwardCommodity).Count(); var yearTradeList = (from t in db.eod_trade join tc in db.trade_cash on new { t.ValueDate, t.TradeId } equals new { tc.ValueDate, tc.TradeId } where tc.ValueDate >= yearPnlStart && tc.ValueDate <= monthlyEnd && tc.Action == "系统操作-期权费" && tc.ValidState != ConsGlobal.InValid && !tc.IsDeleted select new xodTradeBase() { TradeJson = t.TradeJson }).ToList().Select(O => O.trade).ToList(); SetUnderlyingInfo(yearTradeList); //名义本金-本年累计新增 result.Notional.Swap.YearCount.Commodity = yearTradeList.Where(buildPredicateOfSwapCommodity).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.YearCount.Commodity = yearTradeList.Where(buildPredicateOfOptionCommodity).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.YearCount.Commodity = yearTradeList.Where(buildPredicateOfForwardCommodity).Sum(O => O.StockEqvNotionalReal); //交易确认书-本年累计新增 result.ConfirmBook.Swap.YearCount.Commodity = yearTradeList.Where(buildPredicateOfSwapCommodity).Count(); result.ConfirmBook.Option.YearCount.Commodity = yearTradeList.Where(buildPredicateOfOptionCommodity).Count(); result.ConfirmBook.Forward.YearCount.Commodity = yearTradeList.Where(buildPredicateOfForwardCommodity).Count(); } } else { Func buildPredicateOfSwapStock = new Func(O => stocks.Contains(O.UnderlyingInstrumentType) && O.TradeType == "收益互换" && O.IsGroup != 1); Func buildPredicateOfSwapIndex = new Func(O => indexs.Contains(O.UnderlyingInstrumentType) && O.TradeType == "收益互换" && O.IsGroup != 1); Func buildPredicateOfSwapFund = new Func(O => funds.Contains(O.UnderlyingInstrumentType) && O.TradeType == "收益互换" && O.IsGroup != 1); Func buildPredicateOfSwapOther = new Func(O => !unionInstrumentTypes.Contains(O.UnderlyingInstrumentType) && O.TradeType == "收益互换" && O.IsGroup != 1); Func buildPredicateOfOptionStock = new Func(O => stocks.Contains(O.UnderlyingInstrumentType) && O.TradeType != "远期" && O.TradeType != "收益互换" && O.TradeType != "结构化交易"); Func buildPredicateOfOptionIndex = new Func(O => indexs.Contains(O.UnderlyingInstrumentType) && O.TradeType != "远期" && O.TradeType != "收益互换" && O.TradeType != "结构化交易"); Func buildPredicateOfOptionFund = new Func(O => funds.Contains(O.UnderlyingInstrumentType) && O.TradeType != "远期" && O.TradeType != "收益互换" && O.TradeType != "结构化交易"); Func buildPredicateOfOptionOther = new Func(O => !unionInstrumentTypes.Contains(O.UnderlyingInstrumentType) && O.TradeType != "远期" && O.TradeType != "收益互换" && O.TradeType != "结构化交易"); Func buildPredicateOfForwardStock = new Func(O => stocks.Contains(O.UnderlyingInstrumentType) && O.TradeType == "远期"); Func buildPredicateOfForwardIndex = new Func(O => indexs.Contains(O.UnderlyingInstrumentType) && O.TradeType == "远期"); Func buildPredicateOfForwardFund = new Func(O => funds.Contains(O.UnderlyingInstrumentType) && O.TradeType == "远期"); Func buildPredicateOfForwardOther = new Func(O => !unionInstrumentTypes.Contains(O.UnderlyingInstrumentType) && O.TradeType == "远期"); using (var db = new YLContext()) { var lastMonthlyTradeList = db.eod_trade.Where(O => O.ValueDate == preMonthlyEnd && O.TradeStatus == "确认成交" && O.ClientId > 0).Select(O => new xodTradeBase() { TradeJson = O.TradeJson }).ToList().Select(O => O.trade).ToList(); SetUnderlyingInfo(lastMonthlyTradeList); //名义本金-上月末存量 result.Notional.Swap.PreMonthlyCount.Stock = lastMonthlyTradeList.Where(buildPredicateOfSwapStock).Sum(O => O.StockEqvNotionalReal); result.Notional.Swap.PreMonthlyCount.Index = lastMonthlyTradeList.Where(buildPredicateOfSwapIndex).Sum(O => O.StockEqvNotionalReal); result.Notional.Swap.PreMonthlyCount.Fund = lastMonthlyTradeList.Where(buildPredicateOfSwapFund).Sum(O => O.StockEqvNotionalReal); result.Notional.Swap.PreMonthlyCount.Other = lastMonthlyTradeList.Where(buildPredicateOfSwapOther).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.PreMonthlyCount.Stock = lastMonthlyTradeList.Where(buildPredicateOfSwapStock).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.PreMonthlyCount.Index = lastMonthlyTradeList.Where(buildPredicateOfSwapIndex).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.PreMonthlyCount.Fund = lastMonthlyTradeList.Where(buildPredicateOfOptionFund).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.PreMonthlyCount.Other = lastMonthlyTradeList.Where(buildPredicateOfOptionOther).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.PreMonthlyCount.Stock = lastMonthlyTradeList.Where(buildPredicateOfForwardStock).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.PreMonthlyCount.Index = lastMonthlyTradeList.Where(buildPredicateOfForwardIndex).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.PreMonthlyCount.Fund = lastMonthlyTradeList.Where(buildPredicateOfForwardFund).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.PreMonthlyCount.Other = lastMonthlyTradeList.Where(buildPredicateOfForwardOther).Sum(O => O.StockEqvNotionalReal); //交易确认书-上月末存量 result.ConfirmBook.Swap.PreMonthlyCount.Stock = lastMonthlyTradeList.Where(buildPredicateOfSwapStock).Count(); result.ConfirmBook.Swap.PreMonthlyCount.Index = lastMonthlyTradeList.Where(buildPredicateOfSwapIndex).Count(); result.ConfirmBook.Swap.PreMonthlyCount.Fund = lastMonthlyTradeList.Where(buildPredicateOfSwapFund).Count(); result.ConfirmBook.Swap.PreMonthlyCount.Other = lastMonthlyTradeList.Where(buildPredicateOfSwapOther).Count(); result.ConfirmBook.Option.PreMonthlyCount.Stock = lastMonthlyTradeList.Where(buildPredicateOfOptionStock).Count(); result.ConfirmBook.Option.PreMonthlyCount.Index = lastMonthlyTradeList.Where(buildPredicateOfOptionIndex).Count(); result.ConfirmBook.Option.PreMonthlyCount.Fund = lastMonthlyTradeList.Where(buildPredicateOfOptionFund).Count(); result.ConfirmBook.Option.PreMonthlyCount.Other = lastMonthlyTradeList.Where(buildPredicateOfOptionOther).Count(); result.ConfirmBook.Forward.PreMonthlyCount.Stock = lastMonthlyTradeList.Where(buildPredicateOfForwardStock).Count(); result.ConfirmBook.Forward.PreMonthlyCount.Index = lastMonthlyTradeList.Where(buildPredicateOfForwardIndex).Count(); result.ConfirmBook.Forward.PreMonthlyCount.Fund = lastMonthlyTradeList.Where(buildPredicateOfForwardFund).Count(); result.ConfirmBook.Forward.PreMonthlyCount.Other = lastMonthlyTradeList.Where(buildPredicateOfForwardOther).Count(); var monthlyOpenTradeList = (from t in db.eod_trade join tc in db.trade_cash on new { t.ValueDate, t.TradeId } equals new { tc.ValueDate, tc.TradeId } where tc.ValueDate >= monthlyStart && tc.ValueDate <= monthlyEnd && tc.Action == "系统操作-期权费" && tc.ValidState != ConsGlobal.InValid && !tc.IsDeleted select new xodTradeBase() { TradeJson = t.TradeJson }).ToList().Select(O => O.trade).ToList(); SetUnderlyingInfo(monthlyOpenTradeList); var clientIds = monthlyOpenTradeList.Select(x => x.ClientId).ToArray(); var clientIdsSmallAndMicro = DataCacheProvider.GetClientDataSource().AsQueryable().Where(x => clientIds.Contains(x.id) && x.IsSmallAndMicroEnterprises == 1).Select(x => x.id).ToArray(); var clientIdsListed = DataCacheProvider.GetClientDataSource().AsQueryable().Where(x => clientIds.Contains(x.id) && x.IsListed == 1).Select(x => x.id).ToArray(); var monthlyOpenTradeListSmallAndMicro = monthlyOpenTradeList.Where(x => clientIdsSmallAndMicro.Contains(x.ClientId)).ToList(); var monthlyOpenTradeListListed = monthlyOpenTradeList.Where(x => clientIdsListed.Contains(x.ClientId)).ToList(); //名义本金-本月新增开仓 result.Notional.Swap.MonthlyOpen = getMonthlyOpenOriginalStockEqvNotionalV2(monthlyOpenTradeList, buildPredicateOfSwapStock, buildPredicateOfSwapIndex, buildPredicateOfSwapFund, buildPredicateOfSwapOther); result.Notional.Swap.MonthlyOpenSmallAndMicro = getMonthlyOpenOriginalStockEqvNotionalV2(monthlyOpenTradeListSmallAndMicro, buildPredicateOfSwapStock, buildPredicateOfSwapIndex, buildPredicateOfSwapFund, buildPredicateOfSwapOther); result.Notional.Swap.MonthlyOpenListed = getMonthlyOpenOriginalStockEqvNotionalV2(monthlyOpenTradeListListed, buildPredicateOfSwapStock, buildPredicateOfSwapIndex, buildPredicateOfSwapFund, buildPredicateOfSwapOther); result.Notional.Option.MonthlyOpen = getMonthlyOpenOriginalStockEqvNotionalV2(monthlyOpenTradeList, buildPredicateOfOptionStock, buildPredicateOfOptionIndex, buildPredicateOfOptionFund, buildPredicateOfOptionOther); result.Notional.Option.MonthlyOpenSmallAndMicro = getMonthlyOpenOriginalStockEqvNotionalV2(monthlyOpenTradeListSmallAndMicro, buildPredicateOfOptionStock, buildPredicateOfOptionIndex, buildPredicateOfOptionFund, buildPredicateOfOptionOther); result.Notional.Option.MonthlyOpenListed = getMonthlyOpenOriginalStockEqvNotionalV2(monthlyOpenTradeListListed, buildPredicateOfOptionStock, buildPredicateOfOptionIndex, buildPredicateOfOptionFund, buildPredicateOfOptionOther); result.Notional.Forward.MonthlyOpen = getMonthlyOpenOriginalStockEqvNotionalV2(monthlyOpenTradeList, buildPredicateOfForwardStock, buildPredicateOfForwardIndex, buildPredicateOfForwardFund, buildPredicateOfForwardOther); result.Notional.Forward.MonthlyOpenSmallAndMicro = getMonthlyOpenOriginalStockEqvNotionalV2(monthlyOpenTradeListSmallAndMicro, buildPredicateOfForwardStock, buildPredicateOfForwardIndex, buildPredicateOfForwardFund, buildPredicateOfForwardOther); result.Notional.Forward.MonthlyOpenListed = getMonthlyOpenOriginalStockEqvNotionalV2(monthlyOpenTradeListListed, buildPredicateOfForwardStock, buildPredicateOfForwardIndex, buildPredicateOfForwardFund, buildPredicateOfForwardOther); //交易确认书-本月新增开仓 result.ConfirmBook.Swap.MonthlyOpen = getMonthlyOpenCount(monthlyOpenTradeList, buildPredicateOfSwapStock, buildPredicateOfSwapIndex, buildPredicateOfSwapFund, buildPredicateOfSwapOther); result.ConfirmBook.Swap.MonthlyOpenSmallAndMicro = getMonthlyOpenCount(monthlyOpenTradeListSmallAndMicro, buildPredicateOfSwapStock, buildPredicateOfSwapIndex, buildPredicateOfSwapFund, buildPredicateOfSwapOther); result.ConfirmBook.Swap.MonthlyOpenListed = getMonthlyOpenCount(monthlyOpenTradeListListed, buildPredicateOfSwapStock, buildPredicateOfSwapIndex, buildPredicateOfSwapFund, buildPredicateOfSwapOther); result.ConfirmBook.Option.MonthlyOpen = getMonthlyOpenCount(monthlyOpenTradeList, buildPredicateOfOptionStock, buildPredicateOfOptionIndex, buildPredicateOfOptionFund, buildPredicateOfOptionOther); result.ConfirmBook.Option.MonthlyOpenSmallAndMicro = getMonthlyOpenCount(monthlyOpenTradeListSmallAndMicro, buildPredicateOfOptionStock, buildPredicateOfOptionIndex, buildPredicateOfOptionFund, buildPredicateOfOptionOther); result.ConfirmBook.Option.MonthlyOpenListed = getMonthlyOpenCount(monthlyOpenTradeListListed, buildPredicateOfOptionStock, buildPredicateOfOptionIndex, buildPredicateOfOptionFund, buildPredicateOfOptionOther); result.ConfirmBook.Forward.MonthlyOpen = getMonthlyOpenCount(monthlyOpenTradeList, buildPredicateOfForwardStock, buildPredicateOfForwardIndex, buildPredicateOfForwardFund, buildPredicateOfForwardOther); result.ConfirmBook.Forward.MonthlyOpenSmallAndMicro = getMonthlyOpenCount(monthlyOpenTradeListSmallAndMicro, buildPredicateOfForwardStock, buildPredicateOfForwardIndex, buildPredicateOfForwardFund, buildPredicateOfForwardOther); result.ConfirmBook.Forward.MonthlyOpenListed = getMonthlyOpenCount(monthlyOpenTradeListListed, buildPredicateOfForwardStock, buildPredicateOfForwardIndex, buildPredicateOfForwardFund, buildPredicateOfForwardOther); var tradeKey = db.eod_trade.Where(O => O.ValueDate >= monthlyStart && O.ValueDate <= monthlyEnd && O.TradeStatus == "已平仓" && O.ClientId > 0).Select(O => new { O.TradeId, O.ValueDate }); List action = new List() { "系统操作-平仓费", "系统操作-行权费", }; var monthlyCloseTradeList = (from t in db.eod_trade join k in tradeKey on new { t.ValueDate, t.TradeId } equals new { k.ValueDate, k.TradeId } join tc in db.trade_cash on t.TradeId equals tc.TradeId where action.Contains(tc.Action) && tc.ValidState != ConsGlobal.InValid && !tc.IsDeleted select new { eod = new xodTradeBase() { TradeJson = t.TradeJson }, tc.Amount }).ToList().Select(O => { O.eod.trade.TradePrice = O.Amount; return O.eod.trade; }).ToList(); SetUnderlyingInfo(monthlyCloseTradeList); var monthlyCloseList = monthlyCloseTradeList.GroupBy(O => O.id).Select(O => O.First()); //名义本金-本月平仓 result.Notional.Swap.MonthlyUnwind.Stock = monthlyCloseList.Where(buildPredicateOfSwapStock).Sum(O => O.StockEqvNotionalReal); result.Notional.Swap.MonthlyUnwind.Index = monthlyCloseList.Where(buildPredicateOfSwapIndex).Sum(O => O.StockEqvNotionalReal); result.Notional.Swap.MonthlyUnwind.Fund = monthlyCloseList.Where(buildPredicateOfSwapFund).Sum(O => O.StockEqvNotionalReal); result.Notional.Swap.MonthlyUnwind.Other = monthlyCloseList.Where(buildPredicateOfSwapOther).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.MonthlyUnwind.Stock = monthlyCloseList.Where(buildPredicateOfOptionStock).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.MonthlyUnwind.Index = monthlyCloseList.Where(buildPredicateOfOptionIndex).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.MonthlyUnwind.Fund = monthlyCloseList.Where(buildPredicateOfOptionFund).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.MonthlyUnwind.Other = monthlyCloseList.Where(buildPredicateOfOptionOther).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.MonthlyUnwind.Stock = monthlyCloseList.Where(buildPredicateOfForwardStock).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.MonthlyUnwind.Index = monthlyCloseList.Where(buildPredicateOfForwardIndex).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.MonthlyUnwind.Fund = monthlyCloseList.Where(buildPredicateOfForwardFund).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.MonthlyUnwind.Other = monthlyCloseList.Where(buildPredicateOfForwardOther).Sum(O => O.StockEqvNotionalReal); //交易确认书-本月平仓 result.ConfirmBook.Swap.MonthlyUnwind.Stock = monthlyCloseList.Where(buildPredicateOfSwapStock).Count(); result.ConfirmBook.Swap.MonthlyUnwind.Index = monthlyCloseList.Where(buildPredicateOfSwapIndex).Count(); result.ConfirmBook.Swap.MonthlyUnwind.Fund = monthlyCloseList.Where(buildPredicateOfSwapFund).Count(); result.ConfirmBook.Swap.MonthlyUnwind.Other = monthlyCloseList.Where(buildPredicateOfSwapOther).Count(); result.ConfirmBook.Option.MonthlyUnwind.Stock = monthlyCloseList.Where(buildPredicateOfOptionStock).Count(); result.ConfirmBook.Option.MonthlyUnwind.Index = monthlyCloseList.Where(buildPredicateOfOptionIndex).Count(); result.ConfirmBook.Option.MonthlyUnwind.Fund = monthlyCloseList.Where(buildPredicateOfOptionFund).Count(); result.ConfirmBook.Option.MonthlyUnwind.Other = monthlyCloseList.Where(buildPredicateOfOptionOther).Count(); result.ConfirmBook.Forward.MonthlyUnwind.Stock = monthlyCloseList.Where(buildPredicateOfForwardStock).Count(); result.ConfirmBook.Forward.MonthlyUnwind.Index = monthlyCloseList.Where(buildPredicateOfForwardIndex).Count(); result.ConfirmBook.Forward.MonthlyUnwind.Fund = monthlyCloseList.Where(buildPredicateOfForwardFund).Count(); result.ConfirmBook.Forward.MonthlyUnwind.Other = monthlyCloseList.Where(buildPredicateOfForwardOther).Count(); var monthlyExecuteTradeList = db.eod_trade .Where(O => O.ValueDate >= monthlyStart && O.ValueDate <= monthlyEnd && (O.TradeStatus == "已执行" || O.TradeStatus == "已到期") && O.ClientId > 0) .Select(O => new xodTradeBase() { TradeJson = O.TradeJson }).ToList().Select(O => O.trade).ToList(); SetUnderlyingInfo(monthlyExecuteTradeList); //名义本金-本月执行/终止交易 result.Notional.Swap.MonthlyExecute.Stock = monthlyExecuteTradeList.Where(buildPredicateOfSwapStock).Sum(O => O.StockEqvNotionalReal); result.Notional.Swap.MonthlyExecute.Index = monthlyExecuteTradeList.Where(buildPredicateOfSwapIndex).Sum(O => O.StockEqvNotionalReal); result.Notional.Swap.MonthlyExecute.Fund = monthlyExecuteTradeList.Where(buildPredicateOfSwapFund).Sum(O => O.StockEqvNotionalReal); result.Notional.Swap.MonthlyExecute.Other = monthlyExecuteTradeList.Where(buildPredicateOfSwapOther).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.MonthlyExecute.Stock = monthlyExecuteTradeList.Where(buildPredicateOfOptionStock).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.MonthlyExecute.Index = monthlyExecuteTradeList.Where(buildPredicateOfOptionIndex).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.MonthlyExecute.Fund = monthlyExecuteTradeList.Where(buildPredicateOfOptionFund).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.MonthlyExecute.Other = monthlyExecuteTradeList.Where(buildPredicateOfOptionOther).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.MonthlyExecute.Stock = monthlyExecuteTradeList.Where(buildPredicateOfForwardStock).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.MonthlyExecute.Index = monthlyExecuteTradeList.Where(buildPredicateOfForwardIndex).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.MonthlyExecute.Fund = monthlyExecuteTradeList.Where(buildPredicateOfForwardFund).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.MonthlyExecute.Other = monthlyExecuteTradeList.Where(buildPredicateOfForwardOther).Sum(O => O.StockEqvNotionalReal); //交易确认书-本月执行/终止交易 result.ConfirmBook.Swap.MonthlyExecute.Stock = monthlyExecuteTradeList.Where(buildPredicateOfSwapStock).Count(); result.ConfirmBook.Swap.MonthlyExecute.Index = monthlyExecuteTradeList.Where(buildPredicateOfSwapIndex).Count(); result.ConfirmBook.Swap.MonthlyExecute.Fund = monthlyExecuteTradeList.Where(buildPredicateOfSwapFund).Count(); result.ConfirmBook.Swap.MonthlyExecute.Other = monthlyExecuteTradeList.Where(buildPredicateOfSwapOther).Count(); result.ConfirmBook.Option.MonthlyExecute.Stock = monthlyExecuteTradeList.Where(buildPredicateOfOptionStock).Count(); result.ConfirmBook.Option.MonthlyExecute.Index = monthlyExecuteTradeList.Where(buildPredicateOfOptionIndex).Count(); result.ConfirmBook.Option.MonthlyExecute.Fund = monthlyExecuteTradeList.Where(buildPredicateOfOptionFund).Count(); result.ConfirmBook.Option.MonthlyExecute.Other = monthlyExecuteTradeList.Where(buildPredicateOfOptionOther).Count(); result.ConfirmBook.Forward.MonthlyExecute.Stock = monthlyExecuteTradeList.Where(buildPredicateOfForwardStock).Count(); result.ConfirmBook.Forward.MonthlyExecute.Index = monthlyExecuteTradeList.Where(buildPredicateOfForwardIndex).Count(); result.ConfirmBook.Forward.MonthlyExecute.Fund = monthlyExecuteTradeList.Where(buildPredicateOfForwardFund).Count(); result.ConfirmBook.Forward.MonthlyExecute.Other = monthlyExecuteTradeList.Where(buildPredicateOfForwardOther).Count(); var monthlyTradeList = db.eod_trade .Where(O => O.ValueDate == monthlyEnd && O.TradeStatus == "确认成交" && O.ClientId > 0) .Select(O => new xodTradeBase() { TradeJson = O.TradeJson }).ToList().Select(O => O.trade).ToList(); SetUnderlyingInfo(monthlyTradeList); var clientIdsMonthlyCount = monthlyTradeList.Select(x => x.ClientId).ToArray(); var clientIdsMonthlyCountSmallAndMicro = DataCacheProvider.GetClientDataSource().AsQueryable().Where(x => clientIdsMonthlyCount.Contains(x.id) && x.IsSmallAndMicroEnterprises == 1).Select(x => x.id).ToArray(); var clientIdsMonthlyCountListed = DataCacheProvider.GetClientDataSource().AsQueryable().Where(x => clientIds.Contains(x.id) && x.IsListed == 1).Select(x => x.id).ToArray(); var monthlyTradeListSmallAndMicro = monthlyTradeList.Where(x => clientIdsMonthlyCountSmallAndMicro.Contains(x.ClientId)).ToList(); var monthlyTradeListListed = monthlyTradeList.Where(x => clientIdsMonthlyCountListed.Contains(x.ClientId)).ToList(); //名义本金-本月末存量 result.Notional.Swap.MonthlyCount = getMonthlyCountOriginalStockEqvNotionalV2(monthlyTradeList, buildPredicateOfSwapStock, buildPredicateOfSwapIndex, buildPredicateOfSwapFund, buildPredicateOfSwapOther); result.Notional.Swap.MonthlyCountSmallAndMicro = getMonthlyCountOriginalStockEqvNotionalV2(monthlyTradeListSmallAndMicro, buildPredicateOfSwapStock, buildPredicateOfSwapIndex, buildPredicateOfSwapFund, buildPredicateOfSwapOther); result.Notional.Swap.MonthlyCountListed = getMonthlyCountOriginalStockEqvNotionalV2(monthlyTradeListListed, buildPredicateOfSwapStock, buildPredicateOfSwapIndex, buildPredicateOfSwapFund, buildPredicateOfSwapOther); result.Notional.Option.MonthlyCount = getMonthlyCountOriginalStockEqvNotionalV2(monthlyTradeList, buildPredicateOfOptionStock, buildPredicateOfOptionIndex, buildPredicateOfOptionFund, buildPredicateOfOptionOther); result.Notional.Option.MonthlyCountSmallAndMicro = getMonthlyCountOriginalStockEqvNotionalV2(monthlyTradeListSmallAndMicro, buildPredicateOfOptionStock, buildPredicateOfOptionIndex, buildPredicateOfOptionFund, buildPredicateOfOptionOther); result.Notional.Option.MonthlyCountListed = getMonthlyCountOriginalStockEqvNotionalV2(monthlyTradeListListed, buildPredicateOfOptionStock, buildPredicateOfOptionIndex, buildPredicateOfOptionFund, buildPredicateOfOptionOther); result.Notional.Forward.MonthlyCount = getMonthlyCountOriginalStockEqvNotionalV2(monthlyTradeList, buildPredicateOfForwardStock, buildPredicateOfForwardIndex, buildPredicateOfForwardFund, buildPredicateOfForwardOther); result.Notional.Forward.MonthlyCountSmallAndMicro = getMonthlyCountOriginalStockEqvNotionalV2(monthlyTradeListSmallAndMicro, buildPredicateOfForwardStock, buildPredicateOfForwardIndex, buildPredicateOfForwardFund, buildPredicateOfForwardOther); result.Notional.Forward.MonthlyCountListed = getMonthlyCountOriginalStockEqvNotionalV2(monthlyTradeListListed, buildPredicateOfForwardStock, buildPredicateOfForwardIndex, buildPredicateOfForwardFund, buildPredicateOfForwardOther); //交易确认书-本月末存量 result.ConfirmBook.Swap.MonthlyCount = getMonthlyCount(monthlyTradeList, buildPredicateOfSwapStock, buildPredicateOfSwapIndex, buildPredicateOfSwapFund, buildPredicateOfSwapOther); result.ConfirmBook.Swap.MonthlyCountSmallAndMicro = getMonthlyCount(monthlyTradeListSmallAndMicro, buildPredicateOfSwapStock, buildPredicateOfSwapIndex, buildPredicateOfSwapFund, buildPredicateOfSwapOther); result.ConfirmBook.Swap.MonthlyCountListed = getMonthlyCount(monthlyTradeListListed, buildPredicateOfSwapStock, buildPredicateOfSwapIndex, buildPredicateOfSwapFund, buildPredicateOfSwapOther); result.ConfirmBook.Option.MonthlyCount = getMonthlyCount(monthlyTradeList, buildPredicateOfOptionStock, buildPredicateOfOptionIndex, buildPredicateOfOptionFund, buildPredicateOfOptionOther); result.ConfirmBook.Option.MonthlyCountSmallAndMicro = getMonthlyCount(monthlyTradeListSmallAndMicro, buildPredicateOfOptionStock, buildPredicateOfOptionIndex, buildPredicateOfOptionFund, buildPredicateOfOptionOther); result.ConfirmBook.Option.MonthlyCountListed = getMonthlyCount(monthlyTradeListListed, buildPredicateOfOptionStock, buildPredicateOfOptionIndex, buildPredicateOfOptionFund, buildPredicateOfOptionOther); result.ConfirmBook.Forward.MonthlyCount = getMonthlyCount(monthlyTradeList, buildPredicateOfForwardStock, buildPredicateOfForwardIndex, buildPredicateOfForwardFund, buildPredicateOfForwardOther); result.ConfirmBook.Forward.MonthlyCountSmallAndMicro = getMonthlyCount(monthlyTradeListSmallAndMicro, buildPredicateOfForwardStock, buildPredicateOfForwardIndex, buildPredicateOfForwardFund, buildPredicateOfForwardOther); result.ConfirmBook.Forward.MonthlyCountListed = getMonthlyCount(monthlyTradeListListed, buildPredicateOfForwardStock, buildPredicateOfForwardIndex, buildPredicateOfForwardFund, buildPredicateOfForwardOther); var yearTradeList = (from t in db.eod_trade join tc in db.trade_cash on new { t.ValueDate, t.TradeId } equals new { tc.ValueDate, tc.TradeId } where tc.ValueDate >= yearPnlStart && tc.ValueDate <= monthlyEnd && tc.Action == "系统操作-期权费" && tc.ValidState != ConsGlobal.InValid && !tc.IsDeleted select new xodTradeBase() { TradeJson = t.TradeJson }).ToList().Select(O => O.trade).ToList(); SetUnderlyingInfo(yearTradeList); //名义本金-本年累计新增 result.Notional.Swap.YearCount.Stock = yearTradeList.Where(buildPredicateOfSwapStock).Sum(O => O.StockEqvNotionalReal); result.Notional.Swap.YearCount.Index = yearTradeList.Where(buildPredicateOfSwapIndex).Sum(O => O.StockEqvNotionalReal); result.Notional.Swap.YearCount.Fund = yearTradeList.Where(buildPredicateOfSwapFund).Sum(O => O.StockEqvNotionalReal); result.Notional.Swap.YearCount.Other = yearTradeList.Where(buildPredicateOfSwapOther).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.YearCount.Stock = yearTradeList.Where(buildPredicateOfOptionStock).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.YearCount.Index = yearTradeList.Where(buildPredicateOfOptionIndex).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.YearCount.Fund = yearTradeList.Where(buildPredicateOfOptionFund).Sum(O => O.StockEqvNotionalReal); result.Notional.Option.YearCount.Other = yearTradeList.Where(buildPredicateOfOptionOther).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.YearCount.Stock = yearTradeList.Where(buildPredicateOfForwardStock).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.YearCount.Index = yearTradeList.Where(buildPredicateOfForwardIndex).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.YearCount.Fund = yearTradeList.Where(buildPredicateOfForwardFund).Sum(O => O.StockEqvNotionalReal); result.Notional.Forward.YearCount.Other = yearTradeList.Where(buildPredicateOfForwardOther).Sum(O => O.StockEqvNotionalReal); //交易确认书-本年累计新增 result.ConfirmBook.Swap.YearCount.Stock = yearTradeList.Where(buildPredicateOfSwapStock).Count(); result.ConfirmBook.Swap.YearCount.Index = yearTradeList.Where(buildPredicateOfSwapIndex).Count(); result.ConfirmBook.Swap.YearCount.Fund = yearTradeList.Where(buildPredicateOfSwapFund).Count(); result.ConfirmBook.Swap.YearCount.Other = yearTradeList.Where(buildPredicateOfSwapOther).Count(); result.ConfirmBook.Option.YearCount.Stock = yearTradeList.Where(buildPredicateOfOptionStock).Count(); result.ConfirmBook.Option.YearCount.Index = yearTradeList.Where(buildPredicateOfOptionIndex).Count(); result.ConfirmBook.Option.YearCount.Fund = yearTradeList.Where(buildPredicateOfOptionFund).Count(); result.ConfirmBook.Option.YearCount.Other = yearTradeList.Where(buildPredicateOfOptionOther).Count(); result.ConfirmBook.Forward.YearCount.Stock = yearTradeList.Where(buildPredicateOfForwardStock).Count(); result.ConfirmBook.Forward.YearCount.Index = yearTradeList.Where(buildPredicateOfForwardIndex).Count(); result.ConfirmBook.Forward.YearCount.Fund = yearTradeList.Where(buildPredicateOfForwardFund).Count(); result.ConfirmBook.Forward.YearCount.Other = yearTradeList.Where(buildPredicateOfForwardOther).Count(); } } return result; } private static void SetUnderlyingInfo(List tradeList) { foreach (var item in tradeList) { item.UnderlyingInstrumentType = DataCacheProvider.GetUnderlyingDataSource().GetData(item.UnderlyingCode)?.UnderlyingInstrumentType; } } private TransactionScaleItem getMonthlyOpenOriginalStockEqvNotionalV2(List monthlyOpenTradeList, Func buildPredicateOfStock, Func buildPredicateOfIndex, Func buildPredicateOfFund, Func buildPredicateOfOther) { var result = new TransactionScaleItem(); result.Stock = monthlyOpenTradeList.Where(buildPredicateOfStock).Sum(O => O.StockEqvNotionalReal); result.Index = monthlyOpenTradeList.Where(buildPredicateOfIndex).Sum(O => O.StockEqvNotionalReal); result.Fund = monthlyOpenTradeList.Where(buildPredicateOfFund).Sum(O => O.StockEqvNotionalReal); result.Other = monthlyOpenTradeList.Where(buildPredicateOfOther).Sum(O => O.StockEqvNotionalReal); return result; } private TransactionScaleItem getMonthlyOpenCount(List monthlyOpenTradeList, Func buildPredicateOfStock, Func buildPredicateOfIndex, Func buildPredicateOfFund, Func buildPredicateOfOther) { var result = new TransactionScaleItem(); result.Stock = monthlyOpenTradeList.Where(buildPredicateOfStock).Count(); result.Index = monthlyOpenTradeList.Where(buildPredicateOfIndex).Count(); result.Fund = monthlyOpenTradeList.Where(buildPredicateOfFund).Count(); result.Other = monthlyOpenTradeList.Where(buildPredicateOfOther).Count(); return result; } private TransactionScaleItem getMonthlyCountOriginalStockEqvNotionalV2(List monthlyTradeList, Func buildPredicateOfStock, Func buildPredicateOfIndex, Func buildPredicateOfFund, Func buildPredicateOfOther) { var result = new TransactionScaleItem(); result.Stock = monthlyTradeList.Where(buildPredicateOfStock).Sum(O => O.StockEqvNotionalReal); result.Index = monthlyTradeList.Where(buildPredicateOfIndex).Sum(O => O.StockEqvNotionalReal); result.Fund = monthlyTradeList.Where(buildPredicateOfFund).Sum(O => O.StockEqvNotionalReal); result.Other = monthlyTradeList.Where(buildPredicateOfOther).Sum(O => O.StockEqvNotionalReal); return result; } private TransactionScaleItem getMonthlyCount(List monthlyTradeList, Func buildPredicateOfStock, Func buildPredicateOfIndex, Func buildPredicateOfFund, Func buildPredicateOfOther) { var result = new TransactionScaleItem(); result.Stock = monthlyTradeList.Where(buildPredicateOfStock).Count(); result.Index = monthlyTradeList.Where(buildPredicateOfIndex).Count(); result.Fund = monthlyTradeList.Where(buildPredicateOfFund).Count(); result.Other = monthlyTradeList.Where(buildPredicateOfOther).Count(); return result; } /// /// MR009 - 场外商品衍生品交易月度盈亏情况统计表 /// /// /// /// /// /// private TransactionScalePnlInfoGroup getTransactionScalePnlInfo(DateTime monthlyStart, DateTime monthlyEnd, DateTime preMonthlyEnd, DateTime yearPnlStart, bool isCommodity) { var commoditys = new List { InstrumentType.CommodityFutures, InstrumentType.CommoditySpot, InstrumentType.GoldFutures, InstrumentType.GoldSpot, InstrumentType.OtherFutures, InstrumentType.OtherSpot, InstrumentType.AbroadFutures, InstrumentType.AbroadSpot }; TransactionScalePnlInfoGroup result = new TransactionScalePnlInfoGroup(); var trades = isCommodity ? yldb.trade.Where(x => commoditys.Contains(x.UnderlyingInstrumentType)) : yldb.trade.Where(x => !commoditys.Contains(x.UnderlyingInstrumentType)); var hedgeRealizedQuery = from ep in yldb.eod_trade_position join t in trades on ep.TradeId equals t.id where ep.ValueDate <= monthlyEnd && ep.TradeId == 0 && t.ValidState != "InValid" select new { ep.ValueDate, ep.TradeId, RealizedPnL = ep.ClosedPnL }; var optionRealizedQuery = from ep in yldb.eod_trade_position join t in trades on ep.TradeId equals t.id where ep.ValueDate <= monthlyEnd && ep.TradeId > 0 && t.ValidState != "InValid" select new { ep.ValueDate, ep.TradeId, RealizedPnL = ep.ClosedPnL }; var allRealizedQuery = from ep in yldb.eod_trade_position join t in trades on ep.TradeId equals t.id where ep.ValueDate <= monthlyEnd && t.ValidState != "InValid" select new { ep.ValueDate, ep.TradeId, RealizedPnL = ep.ClosedPnL }; result.Monthly.Hedge.RealizedPnl = hedgeRealizedQuery.Where(O => O.ValueDate >= monthlyStart).Sum(O => (double?)O.RealizedPnL) ?? 0; result.Monthly.Option.RealizedPnl = optionRealizedQuery.Where(O => O.ValueDate >= monthlyStart).Sum(O => (double?)O.RealizedPnL) ?? 0;// + hedgeEndQuery.Sum(O => O) - hedgePreEndQuery.Sum(O => O)); result.Monthly.All.RealizedPnl = allRealizedQuery.Where(O => O.ValueDate >= monthlyStart).Sum(O => (double?)O.RealizedPnL) ?? 0; result.Year.Hedge.RealizedPnl = hedgeRealizedQuery.Where(O => O.ValueDate >= yearPnlStart).Sum(O => (double?)O.RealizedPnL) ?? 0; result.Year.Option.RealizedPnl = optionRealizedQuery.Where(O => O.ValueDate >= yearPnlStart).Sum(O => (double?)O.RealizedPnL) ?? 0;// + hedgeEndQuery.Sum(O => O) - hedgeYearPnlQuery.Sum(O => O)); result.Year.All.RealizedPnl = allRealizedQuery.Where(O => O.ValueDate >= monthlyStart).Sum(O => (double?)O.RealizedPnL) ?? 0; var hedgeEndQuery = (from ep in yldb.eod_trade_position join t in trades on ep.TradeId equals t.id where ep.ValueDate == monthlyEnd && ep.TradeId == 0 && t.ValidState != "InValid" select ep.PositionPnL ).Sum(O => (double?)O) ?? 0; var hedgePreQuery = (from ep in yldb.eod_trade_position join t in trades on ep.TradeId equals t.id where ep.ValueDate == preMonthlyEnd && ep.TradeId == 0 && t.ValidState != "InValid" select ep.PositionPnL ).Sum(O => (double?)O) ?? 0; var hedgeYearStartQuery = (from ep in yldb.eod_trade_position join t in trades on ep.TradeId equals t.id where ep.ValueDate == yearPnlStart && ep.TradeId == 0 && t.ValidState != "InValid" select ep.PositionPnL ).Sum(O => (double?)O) ?? 0; result.Monthly.Hedge.Pnl = hedgeEndQuery - hedgePreQuery; result.Year.Hedge.Pnl = hedgeEndQuery - hedgeYearStartQuery; var optionEndQuery = (from ep in yldb.eod_trade_position join t in trades on ep.TradeId equals t.id where ep.ValueDate == monthlyEnd && ep.TradeId > 0 && t.ValidState != "InValid" select ep.PositionPnL ).Sum(O => (double?)O) ?? 0; var optionPreQuery = (from ep in yldb.eod_trade_position join t in trades on ep.TradeId equals t.id where ep.ValueDate == preMonthlyEnd && ep.TradeId > 0 && t.ValidState != "InValid" select ep.PositionPnL ).Sum(O => (double?)O) ?? 0; var optionYearStartQuery = (from ep in yldb.eod_trade_position join t in trades on ep.TradeId equals t.id where ep.ValueDate == yearPnlStart && ep.TradeId > 0 && t.ValidState != "InValid" select ep.PositionPnL ).Sum(O => (double?)O) ?? 0; result.Monthly.Option.Pnl = optionEndQuery - optionPreQuery; result.Year.Option.Pnl = optionEndQuery - optionYearStartQuery; var allEndQuery = (from ep in yldb.eod_trade_position join t in trades on ep.TradeId equals t.id where ep.ValueDate == monthlyEnd && t.ValidState != "InValid" select ep.PositionPnL ).Sum(O => (double?)O) ?? 0; var allPreQuery = (from ep in yldb.eod_trade_position join t in trades on ep.TradeId equals t.id where ep.ValueDate == preMonthlyEnd && t.ValidState != "InValid" select ep.PositionPnL ).Sum(O => (double?)O) ?? 0; var allYearStartQuery = (from ep in yldb.eod_trade_position join t in trades on ep.TradeId equals t.id where ep.ValueDate == yearPnlStart && t.ValidState != "InValid" select ep.PositionPnL ).Sum(O => (double?)O) ?? 0; result.Monthly.All.Pnl = allEndQuery - allPreQuery; result.Year.All.Pnl = allEndQuery - allYearStartQuery; return result; } } }