找回证券业报送的逻辑,仅支持Excel模板报送
This commit is contained in:
+124
-464
@@ -1,8 +1,14 @@
|
||||
using System.Data;
|
||||
using FluentFTP.Helpers;
|
||||
using MoreLinq;
|
||||
using NPOI.SS.Formula.Functions;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using YLErp.BLL;
|
||||
using YLErp.Commons;
|
||||
using YLErp.DBModels.Consts;
|
||||
using YLErp.DBModels.Enums;
|
||||
using YLErp.Helpers;
|
||||
using YLErp.Modules.EodModule;
|
||||
using YLErp.Modules.SuperviseReportModule.SAC.Common;
|
||||
using YLErp.Modules.SuperviseReportModule.SAC.Model;
|
||||
@@ -10,6 +16,7 @@ using static YLErp.DBModels.Consts.ConsReport;
|
||||
|
||||
namespace YLErp.Modules.SuperviseReportModule.SAC.Service
|
||||
{
|
||||
//交易存续期管理
|
||||
class ReportOptionTerminationService : ReportBaseService
|
||||
{
|
||||
public ReportOptionTerminationService(OptUserInfo optUser) : base(optUser)
|
||||
@@ -22,53 +29,39 @@ namespace YLErp.Modules.SuperviseReportModule.SAC.Service
|
||||
|
||||
protected override DataFlagsEnum BusiDataType => DataFlagsEnum.A1007;
|
||||
|
||||
private List<OptFlagsEnum> _validOperationType = null;
|
||||
private List<OptFlagsEnum>? _validOperationType = null;
|
||||
public override List<OptFlagsEnum> ValidOperationType
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_validOperationType == null)
|
||||
{
|
||||
_validOperationType = new List<OptFlagsEnum>() {
|
||||
_validOperationType ??= new List<OptFlagsEnum>() {
|
||||
OptFlagsEnum.A,
|
||||
OptFlagsEnum.U,
|
||||
OptFlagsEnum.D,
|
||||
OptFlagsEnum.U,
|
||||
};
|
||||
}
|
||||
return _validOperationType;
|
||||
}
|
||||
}
|
||||
|
||||
protected override SuperviseReportTypeEnum ReportType => SuperviseReportTypeEnum.SAC_OptionTermination;
|
||||
List<SACReportNotes> noteList = new List<SACReportNotes>();
|
||||
private List<string> _cacheCodeList = new List<string>();
|
||||
|
||||
readonly List<SACReportNotes> noteList = new();
|
||||
private readonly List<string> _cacheCodeList = new();
|
||||
|
||||
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)
|
||||
var model = new BodyModel();
|
||||
var dataList = new List<OptionTerminationModel>();
|
||||
if (_reqInfo.DataSource.HasFlag(SAC_ReportDataSourceEnum.Template))
|
||||
{
|
||||
dataList = GetOptionTerminationFromExcel();
|
||||
dataList = GetOptionTerminationModel(ref fileList);
|
||||
}
|
||||
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;
|
||||
@@ -78,7 +71,7 @@ namespace YLErp.Modules.SuperviseReportModule.SAC.Service
|
||||
public bool AddSubsystemNote(OptionTerminationModel model, List<string> fileList, string tag)
|
||||
{
|
||||
var isExtensionTime = model.DurationOperationType == ConsReport.OperationTypeMap["展期"];
|
||||
DateTime date = DateTime.MinValue;
|
||||
var date = DateTime.MinValue;
|
||||
if (isExtensionTime)
|
||||
{
|
||||
DateTime.TryParse(model.RenewalDate, out date);
|
||||
@@ -88,14 +81,14 @@ namespace YLErp.Modules.SuperviseReportModule.SAC.Service
|
||||
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 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 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))
|
||||
@@ -127,7 +120,7 @@ namespace YLErp.Modules.SuperviseReportModule.SAC.Service
|
||||
//多次U的时候,每次用最新的tag来赋值SubFileTag
|
||||
if (isExtensionTime)
|
||||
{
|
||||
string oldDateStr = "";
|
||||
var 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))
|
||||
@@ -169,374 +162,6 @@ namespace YLErp.Modules.SuperviseReportModule.SAC.Service
|
||||
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>
|
||||
@@ -546,25 +171,50 @@ namespace YLErp.Modules.SuperviseReportModule.SAC.Service
|
||||
/// <returns></returns>
|
||||
private string formatInfoTag(OptionTerminationModel model, bool suffixType = false, string extensionTimeInfo = "")
|
||||
{
|
||||
string result = $"{BusiDataType}_{model.ConfirmationNo.Replace("_", "-")}_{(string.IsNullOrWhiteSpace(extensionTimeInfo) ? "了结" : ($"_{extensionTimeInfo}_展期"))}_";
|
||||
var operationTag = "";
|
||||
if (model.DurationOperationType == ConsReport.OperationTypeMap["敲入"] || model.DurationOperationType == ConsReport.OperationTypeMap["敲出"])
|
||||
{
|
||||
operationTag = $"_{model.DurationOperationType}";
|
||||
}
|
||||
string result = $"{formatInfoTagStr(model)}{operationTag}_{model.DurationEventNO}_";
|
||||
if (suffixType)
|
||||
{
|
||||
result = $"{result}{_operationType}";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private string formatInfoTagStr(OptionTerminationModel model, string extensionTimeInfo = "")
|
||||
{
|
||||
string result = $"{BusiDataType}_{model.ConfirmationNo.Replace("_", "-")}_{(string.IsNullOrWhiteSpace(extensionTimeInfo) ? "了结" : ($"_{extensionTimeInfo}_展期"))}";
|
||||
return result;
|
||||
}
|
||||
|
||||
protected override string _changeCodeOfInfoTag(string infoTag, string newCode, out string originalCode)
|
||||
{
|
||||
var arr = infoTag.Split('_');
|
||||
if (arr.Length != 5 && arr.Length != 6)
|
||||
{
|
||||
throw new ServiceException($"InfoTag信息不匹配:{infoTag}");
|
||||
}
|
||||
originalCode = arr[1];
|
||||
arr[1] = newCode.Replace("_", "-");
|
||||
return string.Join("_", arr);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从Excel获取结算信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private List<OptionTerminationModel> GetOptionTerminationFromExcel()
|
||||
private List<OptionTerminationModel> GetOptionTerminationModel(ref List<string> fileList)
|
||||
{
|
||||
List<OptionTerminationModel> result = new List<OptionTerminationModel>();
|
||||
var result = new List<OptionTerminationModel>();
|
||||
string path = "";
|
||||
if (_excelDataSource != null && _excelDataSource.Tables.Contains("期权交易存续期明细"))
|
||||
{
|
||||
var cacheKey = $"{BusiDataType}";
|
||||
DataTable dt = _excelDataSource.Tables["期权交易存续期明细"];
|
||||
for (int i = 1; i < dt.Rows.Count; i++)
|
||||
string sourcePath = Path.Combine(_excelDataSourceRootPath, "附件");
|
||||
var dt = _excelDataSource.Tables["期权交易存续期明细"];
|
||||
for (var i = 1; i < dt.Rows.Count; i++)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(dt.Rows[i][0]?.ToString()))
|
||||
{
|
||||
@@ -572,14 +222,20 @@ namespace YLErp.Modules.SuperviseReportModule.SAC.Service
|
||||
break;
|
||||
}
|
||||
var optType = OptFlagsEnum.NONE;
|
||||
switch (dt.Rows[i][1]?.ToString())
|
||||
switch (dt.Rows[i][2]?.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;
|
||||
@@ -589,63 +245,55 @@ namespace YLErp.Modules.SuperviseReportModule.SAC.Service
|
||||
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(GetDataSetValue(dt, i, 4), out DateTime expirationDate);
|
||||
var model = new OptionTerminationModel
|
||||
{
|
||||
DateTime.TryParse(model.RenewalDate, out date);
|
||||
}
|
||||
else
|
||||
DurationEventNO = GetDataSetValue(dt, i, 0),
|
||||
ConfirmationNo = GetDataSetValue(dt, i, 1),
|
||||
OperationType = optType,
|
||||
DurationOperationType = ConsReport.OperationTypeMap[GetDataSetValue(dt, i, 3)],
|
||||
ExpirationDate = expirationDate == DateTime.MinValue ? null : expirationDate.ToString("yyyy-MM-dd"),
|
||||
RenewalDate = GetDataSetValue(dt, i, 5),
|
||||
};
|
||||
model.DefaultingParty = ConsReport.FillPartyMap[GetDataSetValue(dt, i, 6)];
|
||||
model.DefaultEvent = GetDataSetValue(dt, i, 7);
|
||||
model.TerminationAmount = GetDataSetValue(dt, i, 8);
|
||||
model.Balance = GetDataSetValueToDoubleOrNull(dt, i, 9)?.ToString("0.00");
|
||||
model.AmountPaidThisTime = GetDataSetValue(dt, i, 10);
|
||||
model.ClosedTransactionsRealRateReturn = GetDataSetValue(dt, i, 11);
|
||||
// model.TerminationAmount = GetDataSetValueToDouble(dt, i, 8).ToString("0.00");
|
||||
// model.Balance = GetDataSetValueToDouble(dt, i, 9).ToString("0.00");
|
||||
// model.AmountPaidThisTime = GetDataSetValueToDouble(dt, i, 10).ToString("0.00");
|
||||
// model.ClosedTransactionsRealRateReturn = GetDataSetValueToDouble(dt, i, 11).ToString("0.00");
|
||||
model.OptionDurationManagementAtt = GetDataSetValue(dt, i, 12);
|
||||
var eventStatus = "";
|
||||
switch (model.DurationOperationType)
|
||||
{
|
||||
DateTime.TryParse(model.ExpirationDate, out date);
|
||||
case "05":
|
||||
eventStatus = "01";
|
||||
break;
|
||||
case "06":
|
||||
eventStatus = "02";
|
||||
break;
|
||||
}
|
||||
var cacheValue = model.ConfirmationNo + date.ToString("yyyy-MM-dd");
|
||||
if (ReportStatus.CheckCacheInfo(cacheKey, cacheValue))
|
||||
model.BarrierEventStatus = eventStatus;
|
||||
model.Blank1 = GetDataSetValue(dt, i, 14);
|
||||
model.Blank2 = GetDataSetValue(dt, i, 15);
|
||||
|
||||
DateTime.TryParse(model.ExpirationDate, out DateTime date);
|
||||
var cacheValue = $"{model.ConfirmationNo}_{model.DurationEventNO}_";
|
||||
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")));
|
||||
List<SACReportNotes> notes = base.GetReportNotes(ReportType, formatInfoTag(model));
|
||||
var note = notes.LastOrDefault(O => O.InfoCache.Contains(date.ToString("yyyy-MM-dd")));
|
||||
if (note == null && _operationType == OptFlagsEnum.A)
|
||||
{
|
||||
if (isExtensionTime)
|
||||
note = new SACReportNotes()
|
||||
{
|
||||
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,
|
||||
};
|
||||
}
|
||||
IsValid = true,
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -667,22 +315,31 @@ namespace YLErp.Modules.SuperviseReportModule.SAC.Service
|
||||
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;//确认书编号;
|
||||
}
|
||||
path = Path.Combine(sourcePath, model.OptionDurationManagementAtt);
|
||||
if (!string.IsNullOrWhiteSpace(model.OptionDurationManagementAtt))
|
||||
{
|
||||
if (!ReportStatus.CheckFileLength(path))
|
||||
{
|
||||
break;
|
||||
}
|
||||
fileList.Add(path);
|
||||
}
|
||||
model.ExceID = base.formatExceID();
|
||||
result.Add(model);
|
||||
ReportStatus.AddCacheInfo(cacheKey, cacheValue);
|
||||
ReportStatus.AddCacheInfo(CacheKey, cacheValue);
|
||||
note.InfoCache = $"{{\"Tag\":\"{model.ConfirmationNo}\",\"ValueDate\":\"{date.ToString("yyyy-MM-dd")}\",\"DurationEventNO\":\"{model.DurationEventNO}\"}}";
|
||||
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.InfoTag = formatInfoTag(model, true);
|
||||
note.OptTime = note.CreateTime;
|
||||
note.RetCode = "";
|
||||
note.RetMsg = "";
|
||||
@@ -698,13 +355,13 @@ namespace YLErp.Modules.SuperviseReportModule.SAC.Service
|
||||
|
||||
protected override List<SacInfo> CheckBodyValue(BodyModel model, out bool checkStatus)
|
||||
{
|
||||
List<SacInfo> result = new List<SacInfo>();
|
||||
var result = new List<SacInfo>();
|
||||
if (model?.OptionTermination != null)
|
||||
{
|
||||
CheckHelper<OptionTerminationModel> helper = new Common.CheckHelper<OptionTerminationModel>();
|
||||
for (int i = 0; i < model.OptionTermination.Count; i++)
|
||||
var helper = new Common.CheckHelper<OptionTerminationModel>();
|
||||
for (var i = 0; i < model.OptionTermination.Count; i++)
|
||||
{
|
||||
List<SacInfo> listRoot = new List<SacInfo>();
|
||||
var listRoot = new List<SacInfo>();
|
||||
var item = model.OptionTermination[i];
|
||||
helper.ExecuteCheck(item, (name, value, msg) =>
|
||||
{
|
||||
@@ -712,8 +369,11 @@ namespace YLErp.Modules.SuperviseReportModule.SAC.Service
|
||||
});
|
||||
if (listRoot.Count > 0)
|
||||
{
|
||||
var errMsg = new SacInfo("OptionTermination", i);
|
||||
errMsg.SubMaps = new List<SacInfo>(listRoot);
|
||||
var errMsg = new SacInfo("OptionTermination", i)
|
||||
{
|
||||
FieldValue = item.ConfirmationNo,
|
||||
SubMaps = new List<SacInfo>(listRoot)
|
||||
};
|
||||
result.Add(errMsg);
|
||||
}
|
||||
}
|
||||
@@ -736,7 +396,7 @@ namespace YLErp.Modules.SuperviseReportModule.SAC.Service
|
||||
LogFactory.GetLogger<ReportMasterAgrmtProductService>().Error(e);
|
||||
};
|
||||
}
|
||||
for (int i = 0; i < noteList.Count; i++)
|
||||
for (var i = 0; i < noteList.Count; i++)
|
||||
{
|
||||
base.SaveReportNotes(noteList[i]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user