using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static YLErp.DBModels.Consts.ConsReport; using YLErp.BLL; using YLErp.DBModels.Consts; using YLErp.DBModels.Enums; using YLErp.DBModels; using YLErp.Modules.SuperviseReportModule.SAC.Common; using YLErp.Modules.SuperviseReportModule.SAC.Model; using System.Data; using System.IO; using static iTextSharp.text.pdf.AcroFields; using YLErp.DBModels.Helpers; using YLErp.Modules.EodModule; using System.Collections.ObjectModel; using YLErp.Helpers; using YLErp.Modules.PricingModule.Models; using YLErp.Modules.CalculationModule; using YLErp.BLL.Calculation; using YLErp.Enums; using YLErp.Modules.DataProviderModule; using YLErp.QdpModule; using Qdp.Pricing.Base.Utilities; using YLErp.Enums; using YLErp.Configuration; using Qdp.Foundation.Implementations; using YLErp.Modules.VolatilityModule; using MoreLinq; namespace YLErp.Modules.SuperviseReportModule.SAC.Service { class ReportValuationInformationService : ReportBaseService { public ReportValuationInformationService(OptUserInfo optUser) : base(optUser) { } protected override string _excelDataSourcePath => "交易相关\\"; protected override string _excelDataSourceFileName => "import_ValuationInformation_template.xlsx"; protected override DataFlagsEnum BusiDataType => DataFlagsEnum.A1018; private List _validOperationType = null; public override List ValidOperationType { get { if (_validOperationType == null) { _validOperationType = new List() { OptFlagsEnum.A, OptFlagsEnum.U, OptFlagsEnum.D, }; } return _validOperationType; } } protected override SuperviseReportTypeEnum ReportType => SuperviseReportTypeEnum.SAC_ValuationInformation; List noteList = new List(); protected override BodyModel GenerateBody(out bool noData, out List fileList) { fileList = new List(); BodyModel model = new BodyModel(); List dataList = new List(); //if (_reqInfo.DataSource.HasFlag(SAC_ReportDataSourceEnum.Template)) //{ dataList = GetValuationInformationModelFromExcel(ref fileList); //} ReadOnlyCollection useDel = null; if (_operationType == OptFlagsEnum.D && (useDel = ReportStatus.GetCacheInfo($"{DataFlagsEnum.A1018}_D")).Count > 0) { dataList.AddRange(GetValuationInformationModelFromCache(useDel)); } var subsystemDataList = loadSubsystemDataSource(fileList, AddSubsystemNote); if (subsystemDataList != null && subsystemDataList.Count > 0) dataList.AddRange(subsystemDataList); noData = dataList.Count == 0; model.ValuationInformation = dataList; return model; } private List GetValuationInformationModelFromCache(IEnumerable useDel) { List result = new List(); foreach (var item in useDel) { var notes = base.GetReportNotes(formatInfoTag(item)); foreach (var note in notes) { var info = new ValuationInformationModel(); var reportCacheDic = JsonHelper.ToObject>(note.InfoCache) ?? new Dictionary(); info.ValuationDate = reportCacheDic.ContainsKey("ValuationDate") ? reportCacheDic["ValuationDate"] : ""; if (info.ValuationDate.IsNullOrWhiteSpace()) { continue; } info.ConfirmationNo = item; var cacheValue = $"{info.ConfirmationNo}_{info.ValuationDate}"; if (ReportStatus.CheckCacheInfo(CacheKey, cacheValue)) { continue; } info.BizID = note.BizId; info.OperationType = OptFlagsEnum.D; info.Balance = "0.00"; info.MarginRatio = "0.00"; info.OptionValuation = "0.00"; info.Delta = "0.00"; info.Gamma = "0.00"; info.Vega = "0.00"; info.Theta = "0.00"; info.Rho = "0.00"; info.RhoQ = "0.00"; info.DeltaCash = "0.00"; info.GammaCash = "0.00"; info.VegaCash = "0.00"; info.ThetaCash = "0.00"; info.RhoCash = "0.00"; info.RhoQCash = "0.00"; info.Blank1 = "0.00"; info.Blank2 = "0.00"; var exceId_TY = $"{info.ConfirmationNo}_{info.ValuationDate}"; info.ExceID = base.formatExceID(); result.Add(info); ReportStatus.AddCacheInfo(CacheKey, cacheValue); note.id = 0; note.InfoCache = $"{{\"Tag\":\"{info.ConfirmationNo}\",\"ValuationDate\":\"{info.ValuationDate}\",\"ExceId_TY\":\"{exceId_TY}\"}}"; note.ExceId = info.ExceID; note.CreateTime = DateTime.Now; note.FileTag = FileTag; note.ReportType = ReportType; note.ReportDate = _reqInfo.ReportDate; note.InfoTag = formatInfoTag(info, true); note.OptTime = note.CreateTime; note.RetCode = ""; note.RetMsg = ""; note.ReportResponse = false; note.BizId = ""; note.DataId = ""; note.changeStatus = false; noteList.Add(note); } } return result; } public bool AddSubsystemNote(ValuationInformationModel model, List fileList, string tag) { SACReportNotes note = base.GetReportNotes(ReportType, formatInfoTag(model, true)).FirstOrDefault();//参数true要加,要不A和D会查到同一条记录,在D时候,就会和A相同的数据 if (note == null) { note = new SACReportNotes() { InfoCache = $"{{\"Tag\":\"{model.ConfirmationNo}\",\"Source\":\"Subsystem\",\"SubFileTag\":\"{tag}\",\"ExceID\":\"{model.ExceID}\"}}", IsValid = true, }; } else { switch (model.OperationType) { case OptFlagsEnum.A: return false;//新增数据已报送,跳过 case OptFlagsEnum.U: //多次U的时候,每次用最新的tag来赋值SubFileTag note.InfoCache = $"{{\"Tag\":\"{model.ConfirmationNo}\",\"Source\":\"Subsystem\",\"SubFileTag\":\"{tag}\",\"ExceID\":\"{model.ExceID}\"}}"; note.IsValid = true; break; case OptFlagsEnum.D: note.IsValid = false; break; case OptFlagsEnum.NONE: default: throw new ServiceException("未知操作类型"); } } model.ExceID = base.formatExceID(); note.id = 0; note.ExceId = model.ExceID; note.CreateTime = DateTime.Now; note.FileTag = FileTag; note.ReportType = ReportType; note.ReportDate = _reqInfo.ReportDate; note.InfoTag = formatInfoTag(model, true); note.OptTime = note.CreateTime; note.RetCode = ""; note.RetMsg = ""; note.ReportResponse = false; note.BizId = ""; note.changeStatus = false; noteList.Add(note); return true; } private List GetValuationInformationModelFromExcel(ref List fileList) { List result = new List(); if (_excelDataSource != null && _excelDataSource.Tables.Contains("每日估值信息")) { DataTable dt = _excelDataSource.Tables["每日估值信息"]; for (int i = 1; i < dt.Rows.Count; i++) { if (string.IsNullOrWhiteSpace(dt.Rows[i][0]?.ToString())) { //第一列空白说明数据结束了; break; } var optType = OptFlagsEnum.NONE; switch (dt.Rows[i][1]?.ToString()) { case "0": case "A": case "首次": optType = OptFlagsEnum.A; break; case "1": case "U": case "变更": optType = OptFlagsEnum.U; break; case "2": case "D": case "废止": optType = OptFlagsEnum.D; break; } if (optType != _operationType) { continue; } ValuationInformationModel model = new ValuationInformationModel(); model.ConfirmationNo = GetDataSetValue(dt, i, 0); model.OperationType = _operationType; DateTime.TryParse(GetDataSetValue(dt, i, 2), out DateTime date); model.ValuationDate = date == DateTime.MinValue ? null : date.ToString("yyyy-MM-dd"); var cacheValue = $"{model.ConfirmationNo}_{model.ValuationDate}"; if (ReportStatus.CheckCacheInfo(CacheKey, cacheValue)) { continue; } List notes = base.GetReportNotes(ReportType, formatInfoTag(model)); var note = notes.LastOrDefault(O => O.InfoCache.Contains(model.ConfirmationNo)); if (note == null && _operationType == OptFlagsEnum.A) { note = new SACReportNotes() { InfoCache = $"{{\"Tag\":\"{model.ConfirmationNo}\",\"ValuationDate\":\"{model.ValuationDate}\",\"Source\":\"Template\"}}", IsValid = true, }; } else { if (note == null) { continue; } switch (_operationType) { case OptFlagsEnum.A: continue;//新增数据已报送,跳过 case OptFlagsEnum.U: note.IsValid = true; break; case OptFlagsEnum.D: note.IsValid = false; break; case OptFlagsEnum.NONE: default: throw new ServiceException("未知操作类型"); } } if (_operationType != OptFlagsEnum.A) { model.BizID = note.BizId; } model.Balance = GetDataSetValueToDoubleOrNull(dt, i, 3)?.ToString("0.00"); model.MarginRatio = GetDataSetValue(dt, i, 4); model.OptionValuation = GetDataSetValueToDoubleOrNull(dt, i, 5)?.ToString("0.00"); model.Delta = GetDataSetValue(dt, i, 6); model.Gamma = GetDataSetValue(dt, i, 7); model.Vega = GetDataSetValue(dt, i, 8); model.Theta = GetDataSetValue(dt, i, 9); model.Rho = GetDataSetValue(dt, i, 10); model.RhoQ = GetDataSetValue(dt, i, 11); model.DeltaCash = GetDataSetValue(dt, i, 12); model.GammaCash = GetDataSetValue(dt, i, 13); model.VegaCash = GetDataSetValue(dt, i, 14); model.ThetaCash = GetDataSetValue(dt, i, 15); model.RhoCash = GetDataSetValue(dt, i, 16); model.RhoQCash = GetDataSetValue(dt, i, 17); model.Blank1 = GetDataSetValue(dt, i, 18); ; model.Blank2 = GetDataSetValue(dt, i, 19); model.ExceID = base.formatExceID(); result.Add(model); ReportStatus.AddCacheInfo(CacheKey, cacheValue); note.id = 0; note.ExceId = model.ExceID; note.CreateTime = DateTime.Now; note.FileTag = FileTag; note.ReportType = ReportType; note.ReportDate = _reqInfo.ReportDate; note.InfoTag = formatInfoTag(model, true); note.OptTime = note.CreateTime; note.RetCode = ""; note.RetMsg = ""; note.ReportResponse = false; note.BizId = ""; note.DataId = ""; note.changeStatus = false; noteList.Add(note); } } return result; } private string formatInfoTag(ValuationInformationModel model, bool suffixType = false) { string result = $"{BusiDataType}_{model.ConfirmationNo.Replace("_", "-")}_{model.ValuationDate}_"; if (suffixType) { result = $"{result}{_operationType}"; } return result; } private string formatInfoTag(string confirmationNo) { string result = $"{BusiDataType}_{confirmationNo.Replace("_", "-")}_"; return result; } protected override string _changeCodeOfInfoTag(string infoTag, string newCode, out string originalCode) { var arr = infoTag.Split('_'); if (arr.Length != 4) { throw new ServiceException($"InfoTag信息不匹配:{infoTag}"); } originalCode = arr[1]; arr[1] = newCode.Replace("_", "-"); return string.Join("_", arr); } protected override List CheckBodyValue(BodyModel model, out bool checkStatus) { List result = new List(); if (model?.ValuationInformation != null) { CheckHelper helper = new CheckHelper(); for (int i = 0; i < model.ValuationInformation.Count; i++) { List listRoot = new List(); var item = model.ValuationInformation[i]; helper.ExecuteCheck(item, (name, value, msg) => { listRoot.Add(new SacInfo(name, value, msg)); }); if (listRoot.Count > 0) { var errMsg = new SacInfo("ValuationInformation", i); errMsg.FieldValue = item.ConfirmationNo; errMsg.SubMaps = new List(listRoot); result.Add(errMsg); } } } checkStatus = result.Count > 0; return result; } public override bool BeforeOfGenerated(out string errMsg) { errMsg = ""; for (int i = 0; i < noteList.Count; i++) { base.SaveReportNotes(noteList[i]); } return true; } private enum clacTargetEnum { /// /// 波动率变动 /// vol, /// /// 无风险利率变动 /// r, /// /// 分红率变动 /// q } /// /// 计算参数 /// private class calcParam { /// /// 计算指标 /// public clacTargetEnum Target { get; set; } /// /// 交易列表 /// public IEnumerable TradeList { get; set; } /// /// 估值日期 /// public DateTime Date { get; set; } /// /// 偏移值 /// 每次只变动一个参数,所以只需要一个 /// public double Offset { get; set; } /// /// 波动率变动 /// public Dictionary UpVolForTrade { get; set; } /// /// 无风险利率或分红率变动 /// public Action UpParamOverride { get; set; } /// /// 波动率变动 /// public Dictionary DownVolForTrade { get; set; } /// /// 无风险利率或分红率变动 /// public Action DownParamOverride { get; set; } /// /// PV差 /// public Dictionary PvDiff { get; set; } /// /// /// /// /// /// /// public calcParam( clacTargetEnum target, IEnumerable tradeList, DateTime date, double offset, Dictionary upVolForTrade, Dictionary downVolForTrade, Action upParamOverride, Action downParamOverride) { this.Target = target; this.TradeList = tradeList; this.Date = date; this.Offset = offset; this.UpVolForTrade = upVolForTrade; this.DownVolForTrade = downVolForTrade; this.UpParamOverride = upParamOverride; this.DownParamOverride = downParamOverride; } } } }