747 lines
37 KiB
C#
747 lines
37 KiB
C#
using System.Data;
|
|
using System.Text.RegularExpressions;
|
|
using YLErp.BLL;
|
|
using YLErp.DBModels.Consts;
|
|
using YLErp.DBModels.Enums;
|
|
using YLErp.Modules.EodModule;
|
|
using YLErp.Modules.SuperviseReportModule.SAC.Common;
|
|
using YLErp.Modules.SuperviseReportModule.SAC.Model;
|
|
using static YLErp.DBModels.Consts.ConsReport;
|
|
|
|
namespace YLErp.Modules.SuperviseReportModule.SAC.Service
|
|
{
|
|
class ReportOptionTerminationService : ReportBaseService
|
|
{
|
|
public ReportOptionTerminationService(OptUserInfo optUser) : base(optUser)
|
|
{
|
|
}
|
|
|
|
protected override string _excelDataSourcePath => "交易相关\\";
|
|
|
|
protected override string _excelDataSourceFileName => "import_OptionTermination_template.xlsx";
|
|
|
|
protected override DataFlagsEnum BusiDataType => DataFlagsEnum.A1007;
|
|
|
|
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_OptionTermination;
|
|
List<SACReportNotes> noteList = new List<SACReportNotes>();
|
|
private List<string> _cacheCodeList = new List<string>();
|
|
|
|
protected override BodyModel GenerateBody(out bool noData, out List<string> fileList)
|
|
{
|
|
fileList = new List<string>();
|
|
BodyModel model = new BodyModel();
|
|
List<OptionTerminationModel> dataList = new List<OptionTerminationModel>();
|
|
if ((PS.Config.ErpElement.SAC_ReportDataSource & SAC_ReportDataSourceEnum.Template) == SAC_ReportDataSourceEnum.Template)
|
|
{
|
|
dataList = GetOptionTerminationFromExcel();
|
|
}
|
|
if ((PS.Config.ErpElement.SAC_ReportDataSource & SAC_ReportDataSourceEnum.System) == SAC_ReportDataSourceEnum.System)
|
|
{
|
|
var cacheKey = $"{BusiDataType}";
|
|
var nextDate = _reqInfo.ReportDate.AddDays(1);
|
|
var actions = new List<string>() { "系统操作-平仓费", "系统操作-行权费" };
|
|
using (var db = new YLContext())
|
|
{
|
|
dataList.AddRange(GetOptionTerminationExtensionTimeFromDb(db, cacheKey, nextDate));
|
|
|
|
dataList.AddRange(GetOptionTerminationSettlementFromDb(db, cacheKey, nextDate));
|
|
}
|
|
}
|
|
|
|
var subsystemDataList = loadSubsystemDataSource<OptionTerminationModel>(fileList, AddSubsystemNote);
|
|
if (subsystemDataList != null && subsystemDataList.Count > 0)
|
|
dataList.AddRange(subsystemDataList);
|
|
|
|
|
|
noData = dataList.Count == 0;
|
|
model.OptionTermination = dataList;
|
|
return model;
|
|
}
|
|
|
|
public bool AddSubsystemNote(OptionTerminationModel model, List<string> fileList, string tag)
|
|
{
|
|
var isExtensionTime = model.DurationOperationType == ConsReport.OperationTypeMap["展期"];
|
|
DateTime date = DateTime.MinValue;
|
|
if (isExtensionTime)
|
|
{
|
|
DateTime.TryParse(model.RenewalDate, out date);
|
|
}
|
|
else
|
|
{
|
|
DateTime.TryParse(model.ExpirationDate, out date);
|
|
}
|
|
var extensionTimeInfo = model.DurationOperationType == ConsReport.OperationTypeMap["展期"] ? "-" : "";
|
|
List<SACReportNotes> notes = base.GetReportNotes(ReportType, $"_{model.ConfirmationNo.Replace("_", "-")}_{_operationType}", true);//添加了operationType,要不A和D会查到同一条记录,在D时候,就会和A相同的数据
|
|
//永远查找现有成功报送了结的新增记录,而不是修改;
|
|
var note = notes.LastOrDefault(O => O.IsValid && O.InfoTag.Contains(formatInfoTag(model, extensionTimeInfo: extensionTimeInfo)) && O.InfoCache.Contains(date.ToString("yyyy-MM-dd")));
|
|
if (note == null)
|
|
{
|
|
if (isExtensionTime)
|
|
{
|
|
string oldDateStr = "";
|
|
var oldData = base.GetReportNotes(SuperviseReportTypeEnum.SAC_OptionConfirmation, $"A1004_{model.ConfirmationNo.Replace("_", "-")}_成交_A").FirstOrDefault();
|
|
oldDateStr = Regex.Match(oldData?.InfoCache ?? "", "(?<=DueDate\":\")[^\"]+(?=\")").Value;
|
|
if (DateTime.TryParse(oldDateStr, out var oldDate))
|
|
{
|
|
oldDateStr = $",\"OldMaturityDate\":\"{oldDate.ToString("yyyy-MM-dd")}\"";
|
|
}
|
|
note = new SACReportNotes()
|
|
{
|
|
InfoCache = $"{{\"Tag\":\"{model.ConfirmationNo}\"{oldDateStr},\"NewMaturityDate\":\"{date.ToString("yyyy-MM-dd")}\",\"Source\":\"Subsystem\",\"SubFileTag\":\"{tag}\",\"ExceID\":\"{model.ExceID}\"}}",
|
|
IsValid = true,
|
|
};
|
|
}
|
|
else
|
|
{
|
|
note = new SACReportNotes()
|
|
{
|
|
InfoCache = $"{{\"Tag\":\"{model.ConfirmationNo}\",\"ValueDate\":\"{date.ToString("yyyy-MM-dd")}\",\"Source\":\"Subsystem\",\"SubFileTag\":\"{tag}\",\"ExceID\":\"{model.ExceID}\"}}",
|
|
IsValid = true,
|
|
};
|
|
}
|
|
}
|
|
else//多次U的时候,每次用最新的tag来赋值SubFileTag
|
|
{
|
|
switch (model.OperationType)
|
|
{
|
|
case OptFlagsEnum.A:
|
|
return false;//新增数据已报送,跳过
|
|
case OptFlagsEnum.U:
|
|
//多次U的时候,每次用最新的tag来赋值SubFileTag
|
|
if (isExtensionTime)
|
|
{
|
|
string oldDateStr = "";
|
|
var oldData = base.GetReportNotes(SuperviseReportTypeEnum.SAC_OptionConfirmation, $"A1004_{model.ConfirmationNo.Replace("_", "-")}_成交_A").FirstOrDefault();
|
|
oldDateStr = Regex.Match(oldData?.InfoCache ?? "", "(?<=DueDate\":\")[^\"]+(?=\")").Value;
|
|
if (DateTime.TryParse(oldDateStr, out var oldDate))
|
|
{
|
|
oldDateStr = $",\"OldMaturityDate\":\"{oldDate.ToString("yyyy-MM-dd")}\"";
|
|
}
|
|
note.InfoCache = $"{{\"Tag\":\"{model.ConfirmationNo}\"{oldDateStr},\"NewMaturityDate\":\"{date.ToString("yyyy-MM-dd")}\",\"Source\":\"Subsystem\",\"SubFileTag\":\"{tag}\",\"ExceID\":\"{model.ExceID}\"}}";
|
|
}
|
|
else
|
|
{
|
|
note.InfoCache = $"{{\"Tag\":\"{model.ConfirmationNo}\",\"ValueDate\":\"{date.ToString("yyyy-MM-dd")}\",\"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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从数据库获取展期记录
|
|
/// </summary>
|
|
/// <param name="db"></param>
|
|
/// <param name="cacheKey"></param>
|
|
/// <param name="nextDate"></param>
|
|
/// <returns></returns>
|
|
private List<OptionTerminationModel> GetOptionTerminationExtensionTimeFromDb(YLContext db, string cacheKey, DateTime nextDate)
|
|
{
|
|
List<OptionTerminationModel> dataList = new List<OptionTerminationModel>();
|
|
var extensionTimeList = (from et in db.ExtensionTime
|
|
join t in db.trade
|
|
on et.TradeId equals t.id
|
|
join tr in db.trade_contract_r.Where(O => O.Type == "交易确认书")
|
|
on t.id equals tr.TradeId
|
|
where
|
|
et.OptDate >= _reqInfo.ReportDate &&
|
|
et.OptDate < nextDate &&
|
|
t.TradeType != "收益互换" &&
|
|
tr.IsValid
|
|
select new
|
|
{
|
|
et.id,
|
|
et.TradeId,
|
|
et.OldMaturityDate,
|
|
et.NewMaturityDate,
|
|
et.IsValid,
|
|
et.OptDate,
|
|
tr.ContractCode,
|
|
t.StockEqvNotional
|
|
}).ToArray();
|
|
|
|
List<int> ids = extensionTimeList.Select(O => O.TradeId).ToList();
|
|
Dictionary<int, Dictionary<string, string>> metaDic = DbContext.TradeMeta.Where(O => ids.Contains(O.TradeId)).AsEnumerable().GroupBy(O => O.TradeId).ToDictionary(K => K.Key, V => V.ToDictionary(K1 => K1.MetaKey, V1 => V1.MetaValue));
|
|
foreach (var item in extensionTimeList)
|
|
{
|
|
var cacheValue = item.id.ToString();
|
|
if (ReportStatus.CheckCacheInfo(cacheKey, cacheValue))
|
|
{
|
|
continue;
|
|
}
|
|
if (metaDic.ContainsKey(item.TradeId))
|
|
{
|
|
var metaInfo = metaDic[item.TradeId];
|
|
if (metaInfo.ContainsKey(ConsTradeMetaKey.TradingPlace) && TradingPlaceMap[metaInfo[ConsTradeMetaKey.TradingPlace]] == "1")//跳过交易场所为报价系统的交易;
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
var info = new OptionTerminationModel();
|
|
|
|
info.TradeId = item.TradeId.ToString();
|
|
info.ConfirmationNo = item.ContractCode;
|
|
List<SACReportNotes> notes = base.GetReportNotes(ReportType, $"_{info.ConfirmationNo.Replace("_", "-")}_", true);
|
|
var note = notes.FirstOrDefault(O => O.IsValid && O.InfoTag.Contains(formatInfoTag(info, extensionTimeInfo: item.id.ToString())));
|
|
if (!item.IsValid)
|
|
{//当前信息是无效的,报送过就是废止,否则就不需要报
|
|
if (note != null)
|
|
{
|
|
info.OperationType = OptFlagsEnum.D;
|
|
}
|
|
else
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
else
|
|
{//当前信息是有效的,报送过,就是修改,否则就是新增
|
|
if (note == null)
|
|
{
|
|
info.OperationType = OptFlagsEnum.A;
|
|
}
|
|
else
|
|
{
|
|
if (!note.changeStatus)
|
|
{
|
|
continue;
|
|
}
|
|
info.OperationType = OptFlagsEnum.U;
|
|
}
|
|
}
|
|
if (info.OperationType != _operationType)
|
|
{
|
|
continue;
|
|
}
|
|
switch (info.OperationType)
|
|
{
|
|
case OptFlagsEnum.A:
|
|
note = new SACReportNotes()
|
|
{
|
|
IsValid = true,
|
|
};
|
|
break;
|
|
case OptFlagsEnum.U:
|
|
note.IsValid = true;
|
|
break;
|
|
case OptFlagsEnum.D:
|
|
note.IsValid = false;
|
|
break;
|
|
}
|
|
if (info.OperationType != OptFlagsEnum.A)
|
|
{
|
|
info.BizID = note.BizId;
|
|
}
|
|
var infoCache = JsonHelper.Deserialize<Dictionary<string, string>>(note?.InfoCache) ?? new Dictionary<string, string>();
|
|
var oldMaturityDate = infoCache.TryGetValue("NewMaturityDate", out var date) ? DateTime.Parse(date) : item.OldMaturityDate;
|
|
var newMaturityDate = item.NewMaturityDate;
|
|
if (info.OperationType == OptFlagsEnum.D)
|
|
{
|
|
oldMaturityDate = infoCache.TryGetValue("OldMaturityDate", out date) ? DateTime.Parse(date) : item.OldMaturityDate;
|
|
newMaturityDate = infoCache.TryGetValue("NewMaturityDate", out date) ? DateTime.Parse(date) : item.NewMaturityDate;
|
|
}
|
|
else
|
|
{
|
|
if (newMaturityDate <= oldMaturityDate)
|
|
{
|
|
throw new ServiceException($"确认书编号:{item.ContractCode}\r\n新展期日:{newMaturityDate.ToString("yyyy-MM-dd")}\r\n原到期日:{oldMaturityDate.ToString("yyyy-MM-dd")}\r\n新展期日应大于原到期日");
|
|
}
|
|
}
|
|
//info.ExpirationDate = item.OldMaturityDate.ToString("yyyy-MM-dd");
|
|
info.DurationOperationType = OperationTypeMap["展期"];
|
|
info.RenewalDate = newMaturityDate.ToString("yyyy-MM-dd");
|
|
info.Balance = Math.Max(item.StockEqvNotional, 0).ToString("0.00");
|
|
info.ExceID = base.formatExceID();
|
|
|
|
ReportStatus.AddCacheInfo(cacheKey, cacheValue);
|
|
dataList.Add(info);
|
|
note.id = 0;
|
|
note.ExceId = info.ExceID;
|
|
note.InfoCache = $"{{\"Tag\":\"{info.ConfirmationNo}\",\"OldMaturityDate\":\"{oldMaturityDate.ToString("yyyy-MM-dd")}\",\"NewMaturityDate\":\"{newMaturityDate.ToString("yyyy-MM-dd")}\",\"Source\":\"System\"}}";
|
|
note.CreateTime = DateTime.Now;
|
|
note.FileTag = FileTag;
|
|
note.ReportType = ReportType;
|
|
note.ReportDate = _reqInfo.ReportDate;
|
|
note.InfoTag = formatInfoTag(info, true, item.id.ToString());
|
|
note.OptTime = note.CreateTime;
|
|
note.RetCode = "";
|
|
note.RetMsg = "";
|
|
note.ReportResponse = false;
|
|
note.BizId = "";
|
|
note.DataId = "";
|
|
note.changeStatus = false;
|
|
noteList.Add(note);
|
|
}
|
|
return dataList;
|
|
}
|
|
/// <summary>
|
|
/// 从数据库获取结算信息
|
|
/// </summary>
|
|
/// <param name="db"></param>
|
|
/// <param name="cacheKey"></param>
|
|
/// <param name="nextDate"></param>
|
|
/// <returns></returns>
|
|
private IEnumerable<OptionTerminationModel> GetOptionTerminationSettlementFromDb(YLContext db, string cacheKey, DateTime nextDate)
|
|
{
|
|
List<OptionTerminationModel> dataList = new List<OptionTerminationModel>();
|
|
var actions = new List<string>() { "系统操作-平仓费", "系统操作-行权费" };
|
|
//当天有回退记录的资金记录
|
|
var query = (from t in db.trade
|
|
join log in db.TradeAuditLog on t.id equals log.TradeId
|
|
join tc in db.trade_cash on t.id equals tc.TradeId
|
|
join tr in db.trade_contract_r.Where(O => O.Type == "交易确认书")
|
|
on t.id equals tr.TradeId
|
|
join td in db.trade_contract_document
|
|
on tr.ContractCode equals td.Code
|
|
where log.OptDate >= _reqInfo.ReportDate && log.OptDate < nextDate && log.OptType == "交易回退" && t.ParentTradeId == 0 &&
|
|
actions.Contains(tc.Action) && /*t.UnderlyingInstrumentType == "Stock" &&*/ t.TradeType != "收益互换" && tr.IsValid
|
|
select new
|
|
{
|
|
tcId = tc.id,
|
|
tc.OptDate,
|
|
tradeValid = t.ValidState != "InValid",
|
|
tradecashValid = tc.ValidState != "InValid" && !tc.IsDeleted,
|
|
tradeContractRValid = tr.IsValid,
|
|
t.id,
|
|
t.ClientId,
|
|
t.TradeDate,
|
|
t.ExerciseDate,
|
|
t.SettlementDate,
|
|
t.ExerciseMode,
|
|
t.BuySell,
|
|
TradeType = t.StructureType ?? t.TradeType,
|
|
t.OptionType,
|
|
t.OriginalStockEqvNotional,
|
|
t.StockEqvNotionalReal,
|
|
t.IsMoneynessOption,
|
|
t.SpotPrice,
|
|
t.Strike,
|
|
t.MarginTemplateName,
|
|
t.MarginType,
|
|
t.InitialMargin,
|
|
t.UnderlyingCode,
|
|
t.AnnualizeFactor,
|
|
tc.ValueDate,
|
|
tc.Notional,
|
|
tc.UnwindNotional,
|
|
tc.Amount,
|
|
t.OriginalNotional,
|
|
tr.ContractCode,
|
|
t.QuoteCurrency,
|
|
t.SettlementCurrency
|
|
}).Union//当天有了结的资金记录
|
|
(from t in db.trade
|
|
join tc in db.trade_cash
|
|
on t.id equals tc.TradeId
|
|
join tr in db.trade_contract_r.Where(O => O.Type == "交易确认书")
|
|
on t.id equals tr.TradeId
|
|
join td in db.trade_contract_document
|
|
on tr.ContractCode equals td.Code
|
|
where tc.ValueDate == _reqInfo.DataDate &&
|
|
t.ValidState != "InValid" && tc.ValidState != "InValid" && !tc.IsDeleted && tr.IsValid &&
|
|
/*t.UnderlyingInstrumentType == "Stock" &&*/ t.TradeType != "收益互换" &&
|
|
actions.Contains(tc.Action)
|
|
select new
|
|
{
|
|
tcId = tc.id,
|
|
tc.OptDate,
|
|
tradeValid = t.ValidState != "InValid",
|
|
tradecashValid = tc.ValidState != "InValid" && !tc.IsDeleted,
|
|
tradeContractRValid = tr.IsValid,
|
|
t.id,
|
|
t.ClientId,
|
|
t.TradeDate,
|
|
t.ExerciseDate,
|
|
t.SettlementDate,
|
|
t.ExerciseMode,
|
|
t.BuySell,
|
|
TradeType = t.StructureType ?? t.TradeType,
|
|
t.OptionType,
|
|
t.OriginalStockEqvNotional,
|
|
t.StockEqvNotionalReal,
|
|
t.IsMoneynessOption,
|
|
t.SpotPrice,
|
|
t.Strike,
|
|
t.MarginTemplateName,
|
|
t.MarginType,
|
|
t.InitialMargin,
|
|
t.UnderlyingCode,
|
|
t.AnnualizeFactor,
|
|
tc.ValueDate,
|
|
tc.Notional,
|
|
tc.UnwindNotional,
|
|
tc.Amount,
|
|
t.OriginalNotional,
|
|
tr.ContractCode,
|
|
t.QuoteCurrency,
|
|
t.SettlementCurrency
|
|
}).ToArray().GroupBy(O => new { O.id, O.ValueDate }).ToDictionary(K => K.Key, V => V.ToList());
|
|
List<int> ids = query.Keys.Select(O => O.id).ToList();
|
|
Dictionary<int, Dictionary<string, string>> metaDic = DbContext.TradeMeta.Where(O => ids.Contains(O.TradeId)).AsEnumerable().GroupBy(O => O.TradeId).ToDictionary(K => K.Key, V => V.ToDictionary(K1 => K1.MetaKey, V1 => V1.MetaValue));
|
|
foreach (var item in query)
|
|
{
|
|
string cacheValue = item.Key.id.ToString() + item.Key.ValueDate.ToString("yyyy-MM-dd");
|
|
if (ReportStatus.CheckCacheInfo(cacheKey, cacheValue))
|
|
{
|
|
continue;
|
|
}
|
|
if (metaDic.ContainsKey(item.Key.id))
|
|
{
|
|
var metaInfo = metaDic[item.Key.id];
|
|
if (metaInfo.ContainsKey(ConsTradeMetaKey.TradingPlace) && TradingPlaceMap[metaInfo[ConsTradeMetaKey.TradingPlace]] == "1")//跳过交易场所为报价系统的交易;
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
var info = new OptionTerminationModel();
|
|
|
|
info.TradeId = item.Key.id.ToString();
|
|
var validItem = item.Value.FirstOrDefault(O => O.tradecashValid);
|
|
var invalidItem = item.Value.FirstOrDefault(O => !O.tradecashValid);
|
|
var obj = invalidItem ?? validItem;
|
|
info.ExpirationDate = obj.ValueDate.ToString("yyyy-MM-dd");
|
|
info.ConfirmationNo = obj.ContractCode;
|
|
List<SACReportNotes> notes = base.GetReportNotes(ReportType, $"_{info.ConfirmationNo.Replace("_", "-")}_", true);
|
|
var note = notes.FirstOrDefault(O => O.IsValid && O.InfoTag.Contains(formatInfoTag(info)) && O.InfoCache.Contains(info.ExpirationDate));
|
|
if (validItem == null && invalidItem != null)
|
|
{//有效资金记录不存在,无效资金记录存在时,当天报送过就是废止,否则就不需要报
|
|
obj = invalidItem;
|
|
if (note != null)
|
|
{
|
|
info.OperationType = OptFlagsEnum.D;
|
|
}
|
|
else
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
else
|
|
{//有效资金记录存在时,当天报送过,就是修改,否则就是新增
|
|
obj = validItem;
|
|
if (note == null)
|
|
{
|
|
info.OperationType = OptFlagsEnum.A;
|
|
}
|
|
else
|
|
{
|
|
if (!note.changeStatus)
|
|
{
|
|
continue;
|
|
}
|
|
info.OperationType = OptFlagsEnum.U;
|
|
}
|
|
}
|
|
|
|
if (info.OperationType != _operationType)
|
|
{
|
|
continue;
|
|
}
|
|
switch (info.OperationType)
|
|
{
|
|
case OptFlagsEnum.A:
|
|
note = new SACReportNotes()
|
|
{
|
|
IsValid = true,
|
|
};
|
|
break;
|
|
case OptFlagsEnum.U:
|
|
note.IsValid = true;
|
|
break;
|
|
case OptFlagsEnum.D:
|
|
note.IsValid = false;
|
|
break;
|
|
}
|
|
if (info.OperationType != OptFlagsEnum.A)
|
|
{
|
|
info.BizID = note.BizId;
|
|
}
|
|
info.ExpirationDate = obj.ValueDate.ToString("yyyy-MM-dd");
|
|
info.DurationOperationType = OperationTypeMap["终止"];
|
|
var quoteRate = new EodCurrencyRateService(UserInfo).GetCurrencyRate(obj.QuoteCurrency, "CNY", obj.ValueDate);
|
|
var settlementRate = new EodCurrencyRateService(UserInfo).GetCurrencyRate(obj.SettlementCurrency, "CNY", obj.ValueDate);
|
|
var currentStockEqvNotional = Math.Round(Math.Min((obj.UnwindNotional / obj.OriginalNotional * obj.OriginalStockEqvNotional) ?? 0, obj.OriginalStockEqvNotional ?? 0) * quoteRate, 2);
|
|
var latestStockEqvNotional = Math.Round(Math.Min((obj.Notional / obj.OriginalNotional * obj.OriginalStockEqvNotional) ?? 0, obj.OriginalStockEqvNotional ?? 0) * quoteRate, 2);
|
|
//是否全部了结
|
|
var isComplete = Math.Abs(obj.Notional - (obj.UnwindNotional ?? 0)) < 1e-4;
|
|
if (isComplete)
|
|
{
|
|
var tcArr = DbContext.trade_cash.Where(O => O.TradeId == obj.id && O.ValueDate < obj.ValueDate && actions.Contains(O.Action)).ToArray();
|
|
var originalStockEqvNotional = obj.OriginalStockEqvNotional ?? 0;
|
|
currentStockEqvNotional = originalStockEqvNotional - tcArr.Sum(O => Math.Round(Math.Min((O.UnwindNotional / obj.OriginalNotional * originalStockEqvNotional) ?? 0, originalStockEqvNotional) * quoteRate, 2));
|
|
latestStockEqvNotional = currentStockEqvNotional;
|
|
}
|
|
info.TerminationAmount = currentStockEqvNotional.ToString("0.00");
|
|
info.Balance = Math.Max((latestStockEqvNotional - currentStockEqvNotional), 0).ToString("0.00");
|
|
info.AmountPaidThisTime = (obj.Amount * settlementRate).ToString();
|
|
info.ExceID = base.formatExceID();
|
|
|
|
ReportStatus.AddCacheInfo(cacheKey, cacheValue);
|
|
dataList.Add(info);
|
|
note.id = 0;
|
|
note.InfoCache = $"{{\"Tag\":\"{info.ConfirmationNo}\",\"ValueDate\":\"{info.ExpirationDate}\",\"Source\":\"System\"}}";
|
|
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 dataList;
|
|
}
|
|
/// <summary>
|
|
/// 格式化报送标签,用于关联报表和业务信息
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <param name="suffixType"></param>
|
|
/// <param name="extensionTimeInfo"></param>
|
|
/// <returns></returns>
|
|
private string formatInfoTag(OptionTerminationModel model, bool suffixType = false, string extensionTimeInfo = "")
|
|
{
|
|
string result = $"{BusiDataType}_{model.ConfirmationNo.Replace("_", "-")}_{(string.IsNullOrWhiteSpace(extensionTimeInfo) ? "了结" : ($"_{extensionTimeInfo}_展期"))}_";
|
|
if (suffixType)
|
|
{
|
|
result = $"{result}{_operationType}";
|
|
}
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// 从Excel获取结算信息
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private List<OptionTerminationModel> GetOptionTerminationFromExcel()
|
|
{
|
|
List<OptionTerminationModel> result = new List<OptionTerminationModel>();
|
|
if (_excelDataSource != null && _excelDataSource.Tables.Contains("期权交易存续期明细"))
|
|
{
|
|
var cacheKey = $"{BusiDataType}";
|
|
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 "首次":
|
|
optType = OptFlagsEnum.A;
|
|
break;
|
|
case "变更":
|
|
optType = OptFlagsEnum.U;
|
|
break;
|
|
case "废止":
|
|
optType = OptFlagsEnum.D;
|
|
break;
|
|
}
|
|
if (optType != _operationType)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
OptionTerminationModel model = new OptionTerminationModel();
|
|
|
|
model.ConfirmationNo = GetDataSetValue(dt, i, 0);
|
|
model.OperationType = optType;
|
|
model.ExpirationDate = GetDataSetValue(dt, i, 2);
|
|
model.DefaultingParty = ConsReport.OptionTraderMap[GetDataSetValue(dt, i, 3)];
|
|
model.DefaultEvent = GetDataSetValue(dt, i, 4);
|
|
model.RenewalDate = GetDataSetValue(dt, i, 5);
|
|
model.DurationOperationType = ConsReport.OperationTypeMap[GetDataSetValue(dt, i, 6)];
|
|
model.TerminationAmount = GetDataSetValue(dt, i, 7);
|
|
model.Balance = GetDataSetValue(dt, i, 8);
|
|
model.AmountPaidThisTime = GetDataSetValue(dt, i, 9);
|
|
|
|
var isExtensionTime = model.DurationOperationType == ConsReport.OperationTypeMap["展期"];
|
|
DateTime date = DateTime.MinValue;
|
|
if (isExtensionTime)
|
|
{
|
|
DateTime.TryParse(model.RenewalDate, out date);
|
|
}
|
|
else
|
|
{
|
|
DateTime.TryParse(model.ExpirationDate, out date);
|
|
}
|
|
var cacheValue = model.ConfirmationNo + date.ToString("yyyy-MM-dd");
|
|
if (ReportStatus.CheckCacheInfo(cacheKey, cacheValue))
|
|
{
|
|
continue;
|
|
}
|
|
var extensionTimeInfo = model.DurationOperationType == ConsReport.OperationTypeMap["展期"] ? "-" : "";
|
|
List<SACReportNotes> notes = base.GetReportNotes(ReportType, $"_{model.ConfirmationNo.Replace("_", "-")}_", true);
|
|
//永远查找现有成功报送了结的新增记录,而不是修改;
|
|
var note = notes.LastOrDefault(O => O.IsValid && O.InfoTag.Contains(formatInfoTag(model, extensionTimeInfo: extensionTimeInfo)) && O.InfoCache.Contains(date.ToString("yyyy-MM-dd")));
|
|
if (note == null && _operationType == OptFlagsEnum.A)
|
|
{
|
|
if (isExtensionTime)
|
|
{
|
|
string oldDateStr = "";
|
|
var oldData = base.GetReportNotes(SuperviseReportTypeEnum.SAC_OptionConfirmation, $"A1004_{model.ConfirmationNo.Replace("_", "-")}_成交_A").FirstOrDefault();
|
|
oldDateStr = Regex.Match(oldData?.InfoCache ?? "", "(?<=DueDate\":\")[^\"]+(?=\")").Value;
|
|
if (DateTime.TryParse(oldDateStr, out var oldDate))
|
|
{
|
|
oldDateStr = $",\"OldMaturityDate\":\"{oldDate.ToString("yyyy-MM-dd")}\"";
|
|
}
|
|
note = new SACReportNotes()
|
|
{
|
|
InfoCache = $"{{\"Tag\":\"{model.ConfirmationNo}\"{oldDateStr},\"NewMaturityDate\":\"{date.ToString("yyyy-MM-dd")}\",\"Source\":\"Template\"}}",
|
|
IsValid = true,
|
|
};
|
|
}
|
|
else
|
|
{
|
|
note = new SACReportNotes()
|
|
{
|
|
InfoCache = $"{{\"Tag\":\"{model.ConfirmationNo}\",\"ValueDate\":\"{date.ToString("yyyy-MM-dd")}\",\"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("未知操作类型");
|
|
}
|
|
note.InfoCache = Regex.Replace(note.InfoCache, "(?<=NewMaturityDate\":\")[^\"]+ (?= \")", date.ToString("yyyy-MM-dd"));
|
|
}
|
|
if (model.OperationType != OptFlagsEnum.A)
|
|
{
|
|
model.BizID = note.BizId;//确认书编号;
|
|
}
|
|
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, extensionTimeInfo);
|
|
note.OptTime = note.CreateTime;
|
|
note.RetCode = "";
|
|
note.RetMsg = "";
|
|
note.ReportResponse = false;
|
|
note.BizId = "";
|
|
note.DataId = "";
|
|
note.changeStatus = false;
|
|
noteList.Add(note);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
protected override List<SacInfo> CheckBodyValue(BodyModel model, out bool checkStatus)
|
|
{
|
|
List<SacInfo> result = new List<SacInfo>();
|
|
if (model?.OptionTermination != null)
|
|
{
|
|
CheckHelper<OptionTerminationModel> helper = new Common.CheckHelper<OptionTerminationModel>();
|
|
for (int i = 0; i < model.OptionTermination.Count; i++)
|
|
{
|
|
List<SacInfo> listRoot = new List<SacInfo>();
|
|
var item = model.OptionTermination[i];
|
|
helper.ExecuteCheck(item, (name, value, msg) =>
|
|
{
|
|
listRoot.Add(new SacInfo(name, value, msg));
|
|
});
|
|
if (listRoot.Count > 0)
|
|
{
|
|
var errMsg = new SacInfo("OptionTermination", i);
|
|
errMsg.SubMaps = new List<SacInfo>(listRoot);
|
|
result.Add(errMsg);
|
|
}
|
|
}
|
|
}
|
|
checkStatus = result.Count > 0;
|
|
return result;
|
|
}
|
|
|
|
public override bool BeforeOfGenerated(out string errMsg)
|
|
{
|
|
errMsg = "";
|
|
foreach (var file in xmlAttachmentList)
|
|
{
|
|
try
|
|
{
|
|
File.Delete(file);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LogFactory.GetLogger<ReportMasterAgrmtProductService>().Error(e);
|
|
};
|
|
}
|
|
for (int i = 0; i < noteList.Count; i++)
|
|
{
|
|
base.SaveReportNotes(noteList[i]);
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|