485 lines
20 KiB
C#
485 lines
20 KiB
C#
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<OptFlagsEnum> _validOperationType = null;
|
|
public override List<OptFlagsEnum> ValidOperationType
|
|
{
|
|
get
|
|
{
|
|
if (_validOperationType == null)
|
|
{
|
|
_validOperationType = new List<OptFlagsEnum>() {
|
|
OptFlagsEnum.A,
|
|
OptFlagsEnum.U,
|
|
OptFlagsEnum.D,
|
|
};
|
|
}
|
|
return _validOperationType;
|
|
}
|
|
}
|
|
|
|
protected override SuperviseReportTypeEnum ReportType => SuperviseReportTypeEnum.SAC_ValuationInformation;
|
|
List<SACReportNotes> noteList = new List<SACReportNotes>();
|
|
|
|
protected override BodyModel GenerateBody(out bool noData, out List<string> fileList)
|
|
{
|
|
fileList = new List<string>();
|
|
BodyModel model = new BodyModel();
|
|
List<ValuationInformationModel> dataList = new List<ValuationInformationModel>();
|
|
//if (_reqInfo.DataSource.HasFlag(SAC_ReportDataSourceEnum.Template))
|
|
//{
|
|
dataList = GetValuationInformationModelFromExcel(ref fileList);
|
|
//}
|
|
ReadOnlyCollection<string> useDel = null;
|
|
if (_operationType == OptFlagsEnum.D && (useDel = ReportStatus.GetCacheInfo($"{DataFlagsEnum.A1018}_D")).Count > 0)
|
|
{
|
|
dataList.AddRange(GetValuationInformationModelFromCache(useDel));
|
|
}
|
|
var subsystemDataList = loadSubsystemDataSource<ValuationInformationModel>(fileList, AddSubsystemNote);
|
|
if (subsystemDataList != null && subsystemDataList.Count > 0)
|
|
dataList.AddRange(subsystemDataList);
|
|
|
|
noData = dataList.Count == 0;
|
|
model.ValuationInformation = dataList;
|
|
return model;
|
|
}
|
|
|
|
private List<ValuationInformationModel> GetValuationInformationModelFromCache(IEnumerable<string> useDel)
|
|
{
|
|
List<ValuationInformationModel> result = new List<ValuationInformationModel>();
|
|
foreach (var item in useDel)
|
|
{
|
|
var notes = base.GetReportNotes(formatInfoTag(item));
|
|
foreach (var note in notes)
|
|
{
|
|
var info = new ValuationInformationModel();
|
|
var reportCacheDic = JsonHelper.ToObject<Dictionary<string, string>>(note.InfoCache) ?? new Dictionary<string, string>();
|
|
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<string> 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<ValuationInformationModel> GetValuationInformationModelFromExcel(ref List<string> fileList)
|
|
{
|
|
List<ValuationInformationModel> result = new List<ValuationInformationModel>();
|
|
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<SACReportNotes> 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<SacInfo> CheckBodyValue(BodyModel model, out bool checkStatus)
|
|
{
|
|
List<SacInfo> result = new List<SacInfo>();
|
|
if (model?.ValuationInformation != null)
|
|
{
|
|
CheckHelper<ValuationInformationModel> helper = new CheckHelper<ValuationInformationModel>();
|
|
for (int i = 0; i < model.ValuationInformation.Count; i++)
|
|
{
|
|
List<SacInfo> listRoot = new List<SacInfo>();
|
|
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<SacInfo>(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
|
|
{
|
|
/// <summary>
|
|
/// 波动率变动
|
|
/// </summary>
|
|
vol,
|
|
/// <summary>
|
|
/// 无风险利率变动
|
|
/// </summary>
|
|
r,
|
|
/// <summary>
|
|
/// 分红率变动
|
|
/// </summary>
|
|
q
|
|
}
|
|
|
|
/// <summary>
|
|
/// 计算参数
|
|
/// </summary>
|
|
private class calcParam
|
|
{
|
|
/// <summary>
|
|
/// 计算指标
|
|
/// </summary>
|
|
public clacTargetEnum Target { get; set; }
|
|
/// <summary>
|
|
/// 交易列表
|
|
/// </summary>
|
|
public IEnumerable<trade> TradeList { get; set; }
|
|
/// <summary>
|
|
/// 估值日期
|
|
/// </summary>
|
|
public DateTime Date { get; set; }
|
|
/// <summary>
|
|
/// 偏移值
|
|
/// <para>每次只变动一个参数,所以只需要一个</para>
|
|
/// </summary>
|
|
public double Offset { get; set; }
|
|
/// <summary>
|
|
/// 波动率变动
|
|
/// </summary>
|
|
public Dictionary<int, double> UpVolForTrade { get; set; }
|
|
/// <summary>
|
|
/// 无风险利率或分红率变动
|
|
/// </summary>
|
|
public Action<OptionTradeParamBase> UpParamOverride { get; set; }
|
|
/// <summary>
|
|
/// 波动率变动
|
|
/// </summary>
|
|
public Dictionary<int, double> DownVolForTrade { get; set; }
|
|
/// <summary>
|
|
/// 无风险利率或分红率变动
|
|
/// </summary>
|
|
public Action<OptionTradeParamBase> DownParamOverride { get; set; }
|
|
/// <summary>
|
|
/// PV差
|
|
/// </summary>
|
|
public Dictionary<int, double> PvDiff { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="offset"></param>
|
|
/// <param name="volForTrade"></param>
|
|
/// <param name="rParamOverride"></param>
|
|
/// <param name="qParamOverride"></param>
|
|
public calcParam(
|
|
clacTargetEnum target,
|
|
IEnumerable<trade> tradeList,
|
|
DateTime date,
|
|
double offset,
|
|
Dictionary<int, double> upVolForTrade,
|
|
Dictionary<int, double> downVolForTrade,
|
|
Action<OptionTradeParamBase> upParamOverride,
|
|
Action<OptionTradeParamBase> 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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|