308 lines
18 KiB
C#
308 lines
18 KiB
C#
using BaseOUDAL;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
using YLErp.Helpers;
|
|
using YLErp.Model;
|
|
using YLErp.Model.Enum;
|
|
using YLErp.Modules.SuperviseReportModule.CFMMC.Model;
|
|
|
|
namespace YLErp.Modules.SuperviseReportModule.CFMMC.Service
|
|
{
|
|
/// <summary>
|
|
/// 监管报告-客户资金服务
|
|
/// </summary>
|
|
public class SuperviseReportClientCashService : YLBaseService
|
|
{
|
|
public SuperviseReportClientCashService(OptUserInfo userInfo) : base(userInfo)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public SearchListResult<SuperviseClientCashReportModel> SearchSuperviseReportList_old(SuperviseReportReq req)
|
|
{
|
|
if (req.sidx.IsNullOrWhiteSpace()) { req.sidx = "ClientId"; }
|
|
var insidedClientIds = DataCacheProvider.GetClientDataSource().AsQueryable().Where(O => O.IsInsided == 1).Select(O => O.id).ToList();
|
|
|
|
var query = from cb in DbContext.ClientBalanceDaily.Where(n => !insidedClientIds.Contains(n.ClientId) && n.BalanceDate == req.ValueDate)
|
|
join cr in DbContext.credit.Where(n => n.ProcessStatus == "已审批" && (n.CreditStartDate == null || n.CreditStartDate <= req.ValueDate) && n.CreditDeadLine >= req.ValueDate)
|
|
on cb.ClientId equals cr.ClientId into t_cr
|
|
from cr in t_cr.DefaultIfEmpty()
|
|
where !((cr.Credit == null || (cr.Credit < 0.0001 && cr.Credit > -0.0001)) &&
|
|
(cb.MarginBalance == null || (cb.MarginBalance < 0.0001 && cb.MarginBalance > -0.0001)) &&
|
|
(cb.PayableMargin == null || (cb.PayableMargin < 0.0001 && cb.PayableMargin > -0.0001)) &&
|
|
(cb.FrozenMarginMoney == null || cb.FrozenMarginMoney < 0.0001) &&
|
|
(cb.PositionPremiumNetCash == null || (cb.PositionPremiumNetCash < 0.0001 && cb.PositionPremiumNetCash > -0.0001)))
|
|
select new SuperviseClientCashReportModel
|
|
{
|
|
ClientId = cb.ClientId,
|
|
Date = req.ValueDate,
|
|
//ClientName = c.Name,
|
|
//ClientType = c.CustomerNature2,
|
|
//ClientLicenseCode = c.LicenseCode,
|
|
ClientTotalCredit = cr.Credit ?? 0,
|
|
//ClientPositionCredit = c.IsTradeCredit == 1 ? (cb.ToDayRemainFund ?? 0) : (cb.MarginBalance + cb.PayableMargin) ?? 0,//先查出
|
|
CompanyPositionCredit = 0,//2020-04-02 茂川临时解决;
|
|
CurrMargin = -(cb.PayableMargin ?? 0),
|
|
CurrFund = cb.PositionPremiumNetCash ?? 0,
|
|
Available = (cb.MarginBalance ?? 0.0) + (cb.PayableMargin ?? 0.0) - (cb.FrozenMarginMoney ?? 0.0),//t.FrozenMarginMoney是通道业务显示所用到的
|
|
//AddFunds = -(cBalance.Margin ?? 0),//追保金额在这里不应为负数;
|
|
PositionPnl = cb.PositionPnl,
|
|
|
|
ToDayRemainFund = cb.ToDayRemainFund,
|
|
MarginBalance = cb.MarginBalance,
|
|
PayableMargin = cb.PayableMargin
|
|
};
|
|
|
|
var result = query.ToSearchList(req);
|
|
var list = result.rows.ToArray();
|
|
int startIndex = req.rows * result.page - req.rows;
|
|
var i = 1;
|
|
var clientIdArr = result.rows.Select(O => O.ClientId).ToHashSet();
|
|
var clientMetaQuery = DbContextFactory.GetClientDbContext(UserInfo).ClientMeta.Where(O => clientIdArr.Contains(O.ClientId));
|
|
var clientReportInfo = clientMetaQuery.Where(O => O.MetaKey == "ReportName").ToDictionary(K => K.ClientId, V => V.MetaValue);
|
|
foreach (var item in list)
|
|
{
|
|
var client = DataCacheProvider.GetClientDataSource().GetData(item.ClientId);
|
|
if (client != null)
|
|
{
|
|
item.ClientName = clientReportInfo.TryGetValue(client.id, out var reportName) && !string.IsNullOrWhiteSpace(reportName) ? reportName : client.Name;
|
|
item.ClientType = client.CustomerNature2;
|
|
item.ClientLicenseCode = client.LicenseCode;
|
|
item.ClientPositionCredit = client.IsTradeCredit == 1 ? item.ToDayRemainFund ?? 0 : (item.MarginBalance + item.PayableMargin) ?? 0;
|
|
}
|
|
|
|
item.CompanyName = PS.Config.CompanyFullName;
|
|
item.Index = startIndex + i++;
|
|
item.ClientPositionCredit = Math.Abs(item.ClientPositionCredit > 0 ? 0 : item.ClientPositionCredit);
|
|
item.ClientPositionCredit = item.ClientTotalCredit < item.ClientPositionCredit ? item.ClientTotalCredit : item.ClientPositionCredit;
|
|
item.Available += item.ClientTotalCredit;
|
|
item.AddFunds = Math.Abs(Math.Min(item.Available, 0));
|
|
}
|
|
result.rows = list;
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 计算出客户资金信息
|
|
/// </summary>
|
|
public SearchListResult<SuperviseReportClientCashModel> SearchSuperviseReportList(SuperviseReportReq req, IEnumerable<int> clienIds = null)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(req.sidx)) { req.sidx = "ClientId"; }
|
|
var config = DbContext.valuedate.FirstOrDefault(x => x.State == valuedate.当前使用);
|
|
var insidedClientIds = DataCacheProvider.GetClientDataSource().AsQueryable().Where(O => O.IsInsided == 1).Select(O => O.id).ToList();
|
|
var tempQuery = DbContext.ClientBalanceDaily.Where(O => !insidedClientIds.Contains(O.ClientId) && O.BalanceDate == req.ValueDate);
|
|
|
|
#region 新增客户筛选 tw
|
|
if (clienIds != null)
|
|
{
|
|
tempQuery = tempQuery.Where(l => clienIds.Contains(l.ClientId));
|
|
}
|
|
#endregion
|
|
|
|
if (req.DataType != null && !req.DataType.Contains("全量客户"))
|
|
{
|
|
List<int> clientIds = new List<int>();
|
|
foreach (var dataType in req.DataType)
|
|
{
|
|
switch (dataType)
|
|
{
|
|
case "有持仓":
|
|
clientIds.AddRange(DbContext.eod_trade.Where(O => O.ValueDate == req.ValueDate).Select(O => O.ClientId));
|
|
break;
|
|
case "有资金":
|
|
clientIds.AddRange(from cb in DbContext.ClientBalanceDaily.Where(n => n.BalanceDate == req.ValueDate)
|
|
join cr in DbContext.credit.Where(n => n.ProcessStatus == "已审批" && (n.CreditStartDate == null || n.CreditStartDate <= req.ValueDate) && n.CreditDeadLine >= req.ValueDate)
|
|
on cb.ClientId equals cr.ClientId into t_cr
|
|
from cr in t_cr.DefaultIfEmpty()
|
|
where !((cb.ToDayRemainFund == null || (cb.ToDayRemainFund < 0.0001 && cb.ToDayRemainFund > -0.0001)) &&
|
|
//(cb.TodayRemianFundProduct == null || (cb.TodayRemianFundProduct < 0.0001 && cb.TodayRemianFundProduct > -0.0001)) &&
|
|
(cb.MarginBalance == null || (cb.MarginBalance < 0.0001 && cb.MarginBalance > -0.0001)) &&
|
|
(cb.PayableMargin == null || (cb.PayableMargin < 0.0001 && cb.PayableMargin > -0.0001)) &&
|
|
//(cb.FrozenMarginMoney == null || cb.FrozenMarginMoney < 0.0001) &&
|
|
(cb.PositionPremiumNetCash == null || (cb.PositionPremiumNetCash < 0.0001 && cb.PositionPremiumNetCash > -0.0001)) &&
|
|
(cr == null || cr.Credit == null || (cr.Credit < 0.0001 && cr.Credit > -0.0001)))
|
|
select cb.ClientId);
|
|
break;
|
|
}
|
|
}
|
|
if (clientIds.Count > 0)
|
|
{ tempQuery = tempQuery.Where(O => clientIds.Contains(O.ClientId)); }
|
|
}
|
|
else
|
|
{
|
|
if (PS.Config.Is浙期)
|
|
{
|
|
using (var clientDb = DbContextFactory.GetClientDbContext(OptUser))
|
|
{
|
|
var clientIds = (from cb in clientDb.client_black
|
|
join c in clientDb.client
|
|
on cb.Name equals c.Name
|
|
select c.id).ToList();
|
|
|
|
if (clientIds.Count > 0)
|
|
{ tempQuery = tempQuery.Where(O => !clientIds.Contains(O.ClientId)); }
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
var query = from cb in tempQuery
|
|
join t_credit in DbContext.CreditFromClient.Where(O => O.CreditStartDate <= req.ValueDate && O.CreditDeadLine >= req.ValueDate)
|
|
on cb.ClientId equals t_credit.ClientId into t_credit
|
|
from credit in t_credit.DefaultIfEmpty()
|
|
select new SuperviseReportClientCashModelEx
|
|
{
|
|
ClientId = cb.ClientId,
|
|
CustomerNature2 = "",
|
|
PositionPremiumNetCash = cb.PositionPremiumNetCash ?? 0,
|
|
ToDayRemainFund = cb.ToDayRemainFund ?? 0,
|
|
|
|
CompanyName = config.SuperviseMainName,
|
|
CompanyLicenseCode = config.SuperviseMainCode,
|
|
Date = req.ValueDate,
|
|
ToClientCredit = cb.Credit ?? 0,
|
|
TodayRemianFundProduct = cb.TodayRemianFundProduct ?? 0.0,
|
|
ToCompanyCredit = credit.Credit,
|
|
OpenBalance = cb.LastDayRemainFund ?? 0,//不包含抵押品
|
|
TodayCashInCashOut = cb.NetFund ?? 0,
|
|
TodaySettleEarnings = (cb.SettlementBalance ?? 0) + (cb.Coupon ?? 0) + (cb.SwapBalance ?? 0),
|
|
TodayOpenTradePrice = (cb.OptionPremium ?? 0) + (cb.OptionPremiumSwap ?? 0),
|
|
ClientMarginHold = -cb.PayableMargin,
|
|
AddFunds = -(cb.Margin < 0 ? 0 : cb.Margin ?? 0),//追保金额在这里不应为负数;
|
|
PositionValue = (PS.Config.IsPVRounded ? cb.RoundedPv : cb.Pv) ?? 0,
|
|
Remark = "",
|
|
PositionPnl = cb.PositionPnl
|
|
};
|
|
|
|
var clientcontext = new ClientDBContext();
|
|
var result = query.ToSearchList(req);
|
|
//var marginTypeDict = new Dictionary<string, string>();
|
|
var clientIdArr = result.rows.Select(O => O.ClientId).ToHashSet();
|
|
var clientMetaQuery = DbContextFactory.GetClientDbContext(UserInfo).ClientMeta.Where(O => clientIdArr.Contains(O.ClientId));
|
|
var clientReportInfo = clientMetaQuery.Where(O => O.MetaKey == "ReportName").ToDictionary(K => K.ClientId, V => V.MetaValue);
|
|
string marginPayTime = "";
|
|
using (ErpBaseContext basedb = new ErpBaseContext())
|
|
{
|
|
marginPayTime = (from di in basedb.DictionaryItems
|
|
join d in basedb.Dictionaries
|
|
on di.DictId equals d.Id
|
|
where d.Name == "资金报送追保到账时间"
|
|
select di.ShortName).FirstOrDefault();
|
|
}
|
|
int startIndex = req.rows * result.page - req.rows, i = 1;
|
|
var tradeList =
|
|
(from t in DbContext.eod_trade
|
|
where t.ValueDate == req.ValueDate && t.TradeStatus == "确认成交"
|
|
select new xodTradeBase() { TradeJson = t.TradeJson })
|
|
.ToArray().Select(O => O.trade);
|
|
|
|
foreach (var item in result.rows)
|
|
{
|
|
var marginHold = item.ClientMarginHold;
|
|
var client = DataCacheProvider.GetClientDataSource().GetData(item.ClientId);
|
|
if (client != null)
|
|
{
|
|
item.CustomerNature2 = client.CustomerNature2 ?? "";
|
|
item.ClientName = clientReportInfo.TryGetValue(client.id, out var reportName) && !string.IsNullOrWhiteSpace(reportName) ? reportName : client.Name;
|
|
item.ClientType = client.CustomerNature2;
|
|
item.ClientLicenseCode = client.LicenseCode;
|
|
}
|
|
else
|
|
{
|
|
continue;
|
|
}
|
|
item.MarginType = ((MarginOptionEnum)client.MarginOptionType).ToString();
|
|
item.MarginPayTime = marginPayTime;
|
|
|
|
//var buyTrades = tradeList.Where(O => O.ClientId == item.ClientId && O.BuySell == "买入").Select(O => O);
|
|
|
|
item.id = item.Index = startIndex + i++;
|
|
item.PositionTradePriceOut = 0;
|
|
//if (PS.Config.Company != Configuration.CompanyEnum.弘业)
|
|
//{
|
|
// item.PositionTradePriceIn = Math.Min(buyTrades.Sum(O => O.TradePrice ?? 0), item.ToCompanyCreditCalc);
|
|
//}
|
|
item.PositionTradePriceIn = 0;
|
|
|
|
switch (item.MarginType)
|
|
{
|
|
case "单向追保":
|
|
item.MarginType = "子公司向对手方单向追保";
|
|
item.CompanyMarginHold = 0;
|
|
item.ClientMarginHold = Math.Abs(marginHold ?? 0);
|
|
break;
|
|
case "对手方单向追保":
|
|
item.MarginType = "对手方向子公司单向追保";
|
|
item.CompanyMarginHold = Math.Abs(marginHold ?? 0);
|
|
item.ClientMarginHold = 0;
|
|
break;
|
|
case "双向追保":
|
|
item.MarginType = "子公司和对手方双向追保";
|
|
item.ClientMarginHold = Math.Max(0, (marginHold ?? 0));
|
|
if (PS.Config.Company != Configuration.CompanyEnum.弘业)
|
|
{
|
|
item.CompanyMarginHold = Math.Abs(Math.Min(0, (marginHold ?? 0)));//这里赋值是必要的,否则计算时会当做0去计算;
|
|
}
|
|
item.CompanyMarginHold = null;
|
|
if (PS.Config.Company == Configuration.CompanyEnum.国投)
|
|
{
|
|
item.CompanyMarginHold = 0;
|
|
}
|
|
break;
|
|
case "其他":
|
|
default:
|
|
break;
|
|
}
|
|
if (item.ClientType == "风险管理子公司")
|
|
{
|
|
item.ToCompanyCredit = null;
|
|
}
|
|
item.AddFunds = Math.Max(-item.AvailableCash, 0);
|
|
if (PS.Config.Is浙期)//浙期要求所有字段小数点保留两位;
|
|
{
|
|
item.ToClientCredit = item.ToClientCredit.OtcFormatValue(2);
|
|
item.ClientTradePriceCredit = item.ClientTradePriceCredit.OtcFormatValue(2);
|
|
item.ClientPositionCredit = item.ClientPositionCredit.OtcFormatValue(2);
|
|
item.PositionTradePriceOut = item.PositionTradePriceOut.OtcFormatValue(2);
|
|
item.ToCompanyCredit = item.ToCompanyCredit?.OtcFormatValue(2);
|
|
item.CompanyTradePriceCredit = item.CompanyTradePriceCredit.OtcFormatValue(2);
|
|
item.CompanyPositionCredit = item.CompanyPositionCredit.OtcFormatValue(2);
|
|
item.PositionTradePriceIn = item.PositionTradePriceIn.OtcFormatValue(2);
|
|
item.OpenBalance = item.OpenBalance.OtcFormatValue(2);
|
|
item.TodayCashInCashOut = item.TodayCashInCashOut.OtcFormatValue(2);
|
|
item.TodaySettleEarnings = item.TodaySettleEarnings.OtcFormatValue(2);
|
|
item.TodayOpenTradePrice = item.TodayOpenTradePrice.OtcFormatValue(2);
|
|
item.EndBalance = item.EndBalance.OtcFormatValue(2);
|
|
item.ClientMarginHold = item.ClientMarginHold?.OtcFormatValue(2);
|
|
item.CompanyMarginHold = item.CompanyMarginHold?.OtcFormatValue(2);
|
|
item.AvailableCash = item.AvailableCash.OtcFormatValue(2);
|
|
item.DesirableFund = item.DesirableFund.OtcFormatValue(2);
|
|
item.AddFunds = item.AddFunds.OtcFormatValue(2);
|
|
item.PositionValue = item.PositionValue.OtcFormatValue(2);
|
|
item.MarketValueEquity = item.MarketValueEquity.OtcFormatValue(2);
|
|
|
|
item.PositionPnl = item.PositionPnl?.OtcFormatValue(2);
|
|
item.PositionPremiumNetCash = item.PositionPremiumNetCash.OtcFormatValue(2);
|
|
item.ToDayRemainFund = item.ToDayRemainFund.OtcFormatValue(2);
|
|
item.TodayRemianFundProduct = item.TodayRemianFundProduct.OtcFormatValue(2);
|
|
item.ClientNumber = client.Number;
|
|
if (client.ParentId != 0)
|
|
{
|
|
var parentClient = DataCacheProvider.GetClientDataSource().GetData(client.ParentId);
|
|
item.ParentClientNumber = parentClient?.Number;
|
|
}
|
|
}
|
|
}
|
|
|
|
return new SearchListResult<SuperviseReportClientCashModel>(result, result.rows);
|
|
}
|
|
|
|
[NotMapped]
|
|
class SuperviseReportClientCashModelEx : SuperviseReportClientCashModel
|
|
{
|
|
public double TodayRemianFundProduct { get; set; }
|
|
}
|
|
}
|
|
}
|