144 lines
6.5 KiB
C#
144 lines
6.5 KiB
C#
using BaseOUDAL;
|
|
using YLErp.Model;
|
|
using YLErp.Modules.DataProviderModule;
|
|
|
|
namespace YLErp.Modules.SuperviseReportModule.ChangJiangReport.Service
|
|
{
|
|
public class SuperviseReportCreditRiskService : YLBaseService
|
|
{
|
|
public SuperviseReportCreditRiskService(OptUserInfo userInfo) : base(userInfo)
|
|
{
|
|
}
|
|
|
|
public SuperviseReportCreditRiskService(YLBaseService baseService) : base(baseService)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从数据库中查询场外业务信用风险监控信息
|
|
/// <para>长江数据采集用</para>
|
|
/// </summary>
|
|
public SearchListResult<SuperviseCreditRiskDbModel> SearchSuperviseReportList(SuperviseReportReq req)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(req.sidx)) { req.sidx = nameof(SupervisePositionDbModel.id); }
|
|
|
|
var query = from db in DbContext.SuperviseReportCreditRisk
|
|
where db.ValueDate == req.ValueDate
|
|
select db;
|
|
|
|
return query.ToSearchList(req);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存场外业务信用风险监控信息到数据库
|
|
/// <para>长江数据采集用</para>
|
|
/// </summary>
|
|
/// <param name="valueDate">数据日期</param>
|
|
public void SaveReportData(DateTime valueDate)
|
|
{
|
|
var risks = DbContext.SuperviseReportCreditRisk.Where(a => a.ValueDate == valueDate);
|
|
if (!risks.Any())
|
|
{
|
|
var priceProvider = new EodPriceProvider(valueDate, isDiviendPrice: false);
|
|
|
|
var tradeList = (from et in DbContext.eod_trade
|
|
where
|
|
et.ValueDate == valueDate &&
|
|
et.TradeType != "结构化交易" &&
|
|
ConsTrade.LiveTradeStatusList.Contains(et.TradeStatus)
|
|
select new xodTradeBase() { TradeJson = et.TradeJson })
|
|
.ToArray().Select(O => O.trade);
|
|
var creditList = (from credit in DbContext.SuperviseReportClientCash
|
|
where credit.Date == valueDate
|
|
select credit
|
|
).ToList();
|
|
|
|
SuperviseCreditRiskDbModel obj = new SuperviseCreditRiskDbModel();
|
|
obj.ValueDate = valueDate;
|
|
var func = new Func<double, double, bool, double, double>((settlePrice, strike, isLong, notional) =>
|
|
{
|
|
return Math.Max(isLong ? (settlePrice - strike) : (strike - settlePrice), 0) * notional;
|
|
});
|
|
//Dictionary<int, double> clientBuyLoss = new Dictionary<int, double>();
|
|
obj.CreditRisk = DbContext.eod_trade_risk.Where(o => o.ValueDate == valueDate).Sum(o => (double?)o.CreditExposure) ?? 0;
|
|
//obj.CreditRisk =
|
|
//tradeList
|
|
//.Where(O => O.Comments?.FirstOrDefault() == '1' && O.BuySell == "买入")
|
|
//.Sum(O =>
|
|
//{
|
|
// var price = priceProvider.GetPrice(O.UnderlyingCode, SettlementTypeEnum.SettlePrice);
|
|
// var strike = O.Strike ?? 0;
|
|
// if (O.IsMoneynessOptionData)
|
|
// {
|
|
// strike = (O.Strike * O.SpotPrice) ?? 0;
|
|
// }
|
|
// var result = func(price, strike, O.OptionType == "看涨", O.Notional);
|
|
// if (!clientBuyLoss.ContainsKey(O.ClientId))
|
|
// {
|
|
// clientBuyLoss.Add(O.ClientId, 0);
|
|
// }
|
|
// clientBuyLoss[O.ClientId] += result;
|
|
// return result;
|
|
//});
|
|
//obj.MaxBuyLoss = clientBuyLoss.Any() ? clientBuyLoss.OrderByDescending(O => O.Value).First().Value : 0;
|
|
obj.MaxBuyLoss = DbContext.eod_trade_risk.Where(o => o.ValueDate == valueDate).AsEnumerable().GroupBy(O => O.ClientId).Select(O => O.Sum(B => B.CreditExposure)).Max(o => (double?)o) ?? 0;
|
|
obj.EAD = DbContext.ClientBalanceDaily.Where(O => O.BalanceDate == valueDate).Sum(O => (double?)O.EAD) ?? 0;
|
|
obj.BuyClientCount = tradeList.Where(O => O.BuySell == "买入").Select(O => O.ClientId).Distinct().Count();
|
|
obj.TradePrice = tradeList.Sum(O => O.TradePrice * (O.BuySell == "买入" ? -1 : 1)) ?? 0;
|
|
foreach (var item in creditList)
|
|
{
|
|
//新的框架在取数据时,不会走属性set方法,导致原有set逻辑走不到,和原有框架逻辑不符,这边强行赋值一遍,走set方法
|
|
item.ToCompanyCredit = item.ToCompanyCredit;
|
|
item.ClientMarginHold = item.ClientMarginHold;
|
|
item.CompanyMarginHold = item.CompanyMarginHold;
|
|
|
|
if (item.ClientName.Contains("保险"))
|
|
{
|
|
obj.ExchangeCredit += item.ClientPositionCredit;
|
|
}
|
|
else
|
|
{
|
|
obj.ExchangeOffCredit += item.ClientPositionCredit;
|
|
}
|
|
obj.TotalCredit += item.ClientPositionCredit;
|
|
}
|
|
|
|
//DbContext.BulkDelete<SuperviseCreditRiskDbModel>($"{nameof(SuperviseCreditRiskDbModel.ValueDate)}=@Date", new
|
|
//{
|
|
// Date = valueDate
|
|
//});
|
|
DbContext.SuperviseReportCreditRisk.Add(obj);
|
|
DbContext.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public void HandleSuperviseCreditRisk(int Id, DateTime ValueDate, string Type)
|
|
{
|
|
using (var db = DbContextFactory.GetYLDbContext())
|
|
{
|
|
var clietnCashs = db.SuperviseReportCreditRisk.Where(a => a.ValueDate == ValueDate && a.id == Id);
|
|
if (clietnCashs.Any())
|
|
{
|
|
if (Type == "update")//更新确认状态
|
|
{
|
|
foreach (var item in clietnCashs)
|
|
{
|
|
item.Status = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
db.SuperviseReportCreditRisk.RemoveRange(clietnCashs);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new ServiceException("没有数据进行操作");
|
|
}
|
|
db.SaveChanges();
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|