Files
zszq-trs/YLErpDAL/Modules/SuperviseReportModule/SAC/Service/ReportSwapTerminationService.cs
T

477 lines
22 KiB
C#

using Microsoft.Office.Interop.Excel;
using NPOI.SS.UserModel;
using System.Data;
using System.Net;
using System.Text.RegularExpressions;
using YieldChain.Helpers;
using YLErp.BLL;
using YLErp.Commons;
using YLErp.DBModels;
using YLErp.DBModels.Consts;
using YLErp.DBModels.Enums;
using YLErp.Models;
using YLErp.Modules.EodModule;
using YLErp.Modules.SuperviseReportModule.SAC.Common;
using YLErp.Modules.SuperviseReportModule.SAC.Model;
using static YLErp.ConsGlobal;
using static YLErp.DBModels.Consts.ConsReport;
namespace YLErp.Modules.SuperviseReportModule.SAC.Service
{
class ReportSwapTerminationService : ReportBaseService
{
public ReportSwapTerminationService(OptUserInfo optUser) : base(optUser)
{
}
protected override string _excelDataSourcePath => "交易相关\\";
protected override string _excelDataSourceFileName => "import_SwapTermination_template.xlsx";
protected override DataFlagsEnum BusiDataType => DataFlagsEnum.A1006;
private List<OptFlagsEnum>? _validOperationType = null;
public override List<OptFlagsEnum> ValidOperationType
{
get
{
_validOperationType ??= new List<OptFlagsEnum>() {
OptFlagsEnum.A,
OptFlagsEnum.U,
OptFlagsEnum.D,
};
return _validOperationType;
}
}
protected override SuperviseReportTypeEnum ReportType => SuperviseReportTypeEnum.SAC_SwapDurationManagement;
readonly List<SACReportNotes> noteList = new();
IEnumerable<SACReportNotes> SACReportNotesCache = null;
IEnumerable<SACReportNotes> SACReportNotesCache_confirmation = null;
protected override BodyModel GenerateBody(out bool noData, out List<string> fileList)
{
fileList = new List<string>();
var model = new BodyModel();
SACReportNotesCache = base.InitReportNotes(ReportType);
SACReportNotesCache_confirmation = base.InitReportNotes(SuperviseReportTypeEnum.SAC_OptionConfirmation);
var dataList = new List<SwapDurationManagementModel>();
//if (_reqInfo.DataSource.HasFlag(SAC_ReportDataSourceEnum.Template))
//{
dataList = GetSwapDurationManagementModel(ref fileList);
//}
var subsystemDataList = loadSubsystemDataSource<SwapDurationManagementModel>(fileList, AddSubsystemNote);
if (subsystemDataList != null && subsystemDataList.Count > 0)
{
dataList.AddRange(subsystemDataList);
}
noData = dataList.Count == 0;
model.SwapDurationManagement = dataList;
return model;
}
public bool AddSubsystemNote(SwapDurationManagementModel model, List<string> fileList, string tag)
{
SACReportNotes note = base.GetReportNotes(ReportType, formatInfoTag(model, true), dataSource: SACReportNotesCache).FirstOrDefault();//参数true要加,要不A和D会查到同一条记录,在D时候,就会和A相同的数据
var isExtensionTime = model.DurationOperationType == ConsReport.SwapOperationTypeMap["展期"];
var date = DateTime.MinValue;
if (isExtensionTime)
{
DateTime.TryParse(model.RenewalDate, out date);
}
else
{
DateTime.TryParse(model.DurationOperationDate, out date);
}
if (note == null)
{
if (isExtensionTime)
{
var oldDateStr = "";
var oldData = base.GetReportNotes(SuperviseReportTypeEnum.SAC_OptionConfirmation, $"A1005_{model.ConfirmationNo.Replace("_", "-")}_成交_A", dataSource: SACReportNotesCache_confirmation).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")}\",\"DurationEventNO\":\"{model.DurationEventNO}\",\"Source\":\"Subsystem\",\"SubFileTag\":\"{tag}\",\"ExceID\":\"{model.ExceID}\"}}",
IsValid = true,
};
}
else
{
note = new SACReportNotes()
{
InfoCache = $"{{\"Tag\":\"{model.ConfirmationNo}\",\"ValueDate\":\"{date.ToString("yyyy-MM-dd")}\",\"DurationEventNO\":\"{model.DurationEventNO}\",\"Source\":\"Subsystem\",\"SubFileTag\":\"{tag}\",\"ExceID\":\"{model.ExceID}\"}}",
IsValid = true,
};
}
}
else
{
switch (_operationType)
{
case OptFlagsEnum.A:
return false;//新增数据已报送,跳过
case OptFlagsEnum.U:
if (isExtensionTime)
{
var oldDateStr = "";
var oldData = base.GetReportNotes(SuperviseReportTypeEnum.SAC_OptionConfirmation, $"A1005_{model.ConfirmationNo.Replace("_", "-")}_成交_A", dataSource: SACReportNotesCache_confirmation).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")}\",\"DurationEventNO\":\"{model.DurationEventNO}\",\"Source\":\"Subsystem\",\"SubFileTag\":\"{tag}\",\"ExceID\":\"{model.ExceID}\"}}";
}
else
{
note.InfoCache = $"{{\"Tag\":\"{model.ConfirmationNo}\",\"ValueDate\":\"{date.ToString("yyyy-MM-dd")}\",\"DurationEventNO\":\"{model.DurationEventNO}\",\"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("未知操作类型");
}
}
if (!string.IsNullOrWhiteSpace(model.SwapDurationManagementAtt))
{
fileList.Add(model.SwapDurationManagementAtt);
}
var extensionTimeInfo = isExtensionTime ? "-" : "";
model.ExceID = base.formatExceID(SACReportNotesCache);
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.changeStatus = false;
noteList.Add(note);
return true;
}
private List<SwapDurationManagementModel> GetSwapDurationManagementModel(ref List<string> fileList)
{
fileList = new List<string>();
var result = new List<SwapDurationManagementModel>();
if (_excelDataSource != null && _excelDataSource.Tables.Contains("互换交易存续期明细"))
{
var 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()))
{
//第一列空白说明数据结束了;
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;
}
var model = new SwapDurationManagementModel
{
ConfirmationNo = GetDataSetValue(dt, i, 0),
OperationType = optType,
SwapType = ConsReport.SwapTypeMap[GetDataSetValue(dt, i, 2)],
DurationEventNO = GetDataSetValue(dt, i, 3),
DurationOperationType = ConsReport.SwapOperationTypeMap[GetDataSetValue(dt, i, 4)],
DurationOperationDate = GetDataSetValue(dt, i, 5),
RenewalDate = GetDataSetValue(dt, i, 6),
DefaultingParty = ConsReport.FillPartyMap[GetDataSetValue(dt, i, 7)],
DefaultEvent = GetDataSetValue(dt, i, 8),
ChangeAmount = GetDataSetValue(dt, i, 9),
Balance = GetDataSetValue(dt, i, 10),
AmountPaidThisTime = GetDataSetValue(dt, i, 11),
MarginRatio = GetDataSetValue(dt, i, 12),
SwapDurationManagementAtt = GetDataSetValue(dt, i, 13),
Blank1 = GetDataSetValue(dt, i, 14),
Blank2 = GetDataSetValue(dt, i, 15)
};
var isExtensionTime = model.DurationOperationType == ConsReport.SwapOperationTypeMap["展期"];
var date = DateTime.MinValue;
if (isExtensionTime)
{
DateTime.TryParse(model.RenewalDate, out date);
}
else
{
DateTime.TryParse(model.DurationOperationDate, out date);
}
var cacheValue = $"{model.ConfirmationNo}_{model.DurationEventNO}_";
if (ReportStatus.CheckCacheInfo(CacheKey, cacheValue))
{
continue;
}
if (!string.IsNullOrWhiteSpace(model.SwapDurationManagementAtt))
{
var path = Path.Combine(sourcePath, model.SwapDurationManagementAtt);
if (!ReportStatus.CheckFileLength(path))
{
break;
}
fileList.Add(path);
}
var extensionTimeInfo = isExtensionTime ? "-" : "";
List<SACReportNotes> notes = base.GetReportNotes(ReportType, $"_{model.ConfirmationNo.Replace("_", "-")}_", true, SACReportNotesCache);
//永远查找现有成功报送了结的新增记录,而不是修改;
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)
{
var oldDateStr = "";
var oldData = base.GetReportNotes(SuperviseReportTypeEnum.SAC_OptionConfirmation, $"A1005_{model.ConfirmationNo.Replace("_", "-")}_成交_A", dataSource: SACReportNotesCache_confirmation).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")}\",\"DurationEventNO\":\"{model.DurationEventNO}\",\"Source\":\"Template\"}}",
IsValid = true,
};
}
else
{
note = new SACReportNotes()
{
InfoCache = $"{{\"Tag\":\"{model.ConfirmationNo}\",\"ValueDate\":\"{date.ToString("yyyy-MM-dd")}\",\"DurationEventNO\":\"{model.DurationEventNO}\",\"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"));
}
model.CurrentPositionDetails = GetCurrentPositionDetailsModel(model.ConfirmationNo);
if (model.OperationType != OptFlagsEnum.A)
{
model.BizID = note.BizId;
}
model.ExceID = base.formatExceID(SACReportNotesCache);
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;
}
private List<CurrentPositionDetailModel> GetCurrentPositionDetailsModel(string confirmationNo)
{
var result = new List<CurrentPositionDetailModel>();
if (_excelDataSource != null && _excelDataSource.Tables.Contains("合约持仓明细"))
{
var dt = _excelDataSource.Tables["合约持仓明细"];
for (var i = 1; i < dt.Rows.Count; i++)
{
if (string.IsNullOrWhiteSpace(dt.Rows[i][0]?.ToString()))
{
//第一列空白说明数据结束了;
break;
}
if (dt.Rows[i][0]?.ToString().Trim() != confirmationNo)
{
continue;
}
var model = new CurrentPositionDetailModel
{
UndrlygAssetCode = GetDataSetValue(dt, i, 1),
UndrlygAssetDtldType = ConsReport.SwapUndrlygAssetDtldTypeMap[GetDataSetValue(dt, i, 2)],
UndrlygAssetName = GetDataSetValue(dt, i, 3),
UndrlygAssetTradgPlc = GetDataSetValue(dt, i, 4),
UndrlygAssetPrice = GetDataSetValue(dt, i, 5),
InvestorPosition = ConsReport.SwapTypeMap[GetDataSetValue(dt, i, 6)],
UndrlygAssetAmt = GetDataSetValue(dt, i, 7),
ContractMultiplier = GetDataSetValue(dt, i, 8),
NotinalPrincipleAmt = GetDataSetValue(dt, i, 9),
Blank1 = GetDataSetValue(dt, i, 10),
Blank2 = GetDataSetValue(dt, i, 11)
};
result.Add(model);
}
}
return result;
}
private string formatInfoTag(SwapDurationManagementModel model, bool suffixType = false, string extensionTimeInfo = "")
{
var result = $"{BusiDataType}_{model.ConfirmationNo.Replace("_", "-")}_{(string.IsNullOrWhiteSpace(extensionTimeInfo) ? "了结" : $"_{extensionTimeInfo}_展期")}_{model.DurationEventNO}_";
if (suffixType)
{
result = $"{result}{_operationType}";
}
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);
}
protected override List<SacInfo> CheckBodyValue(BodyModel model, out bool checkStatus)
{
var result = new List<SacInfo>();
if (model?.SwapDurationManagement != null)
{
var helper = new CheckHelper<SwapDurationManagementModel>();
var positionhelper = new CheckHelper<CurrentPositionDetailModel>();
for (var i = 0; i < model.SwapDurationManagement.Count; i++)
{
var listRoot = new List<SacInfo>();
var item = model.SwapDurationManagement[i];
if (item.IgnoreCheck)
{
continue;
}
helper.ExecuteCheck(item, (name, value, msg) =>
{
listRoot.Add(new SacInfo(name, value, msg));
});
if (item.CurrentPositionDetails != null)
{
for (var j = 0; j < item.CurrentPositionDetails.Count; j++)
{
var attList = new List<SacInfo>();
var attItem = item.CurrentPositionDetails[j];
positionhelper.ExecuteCheck(attItem, (name, value, msg) =>
{
attList.Add(new SacInfo(name, value, msg));
});
if (attList.Count > 0)
{
var temp = new SacInfo("CurrentPositionDetails", j)
{
SubMaps = new List<SacInfo>(attList)
};
listRoot.Add(temp);
}
}
}
if (listRoot.Count > 0)
{
var errMsg = new SacInfo("SwapDurationManagement", i)
{
FieldValue = item.ConfirmationNo,
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 (var i = 0; i < noteList.Count; i++)
{
base.SaveReportNotes(noteList[i]);
}
return true;
}
}
}