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

508 lines
23 KiB
C#

using BaseOUDAL;
using Qdp.Foundation.Implementations;
using System.Data;
using YieldChain.Helpers;
using YLErp.BLL;
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;
using YLErp.QdpModule;
using static YLErp.DBModels.Consts.ConsReport;
namespace YLErp.Modules.SuperviseReportModule.SAC.Service
{
class ReportSwapConfirmationService : ReportBaseService
{
public ReportSwapConfirmationService(OptUserInfo optUser) : base(optUser)
{
}
protected override string _excelDataSourcePath => "交易相关\\";
protected override string _excelDataSourceFileName => "import_SwapConfirmation_template.xlsx";
protected override DataFlagsEnum BusiDataType => DataFlagsEnum.A1005;
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_SwapConfirmation;
readonly List<SACReportNotes> noteList = new();
IEnumerable<SACReportNotes> SACReportNotesCache = null;
protected override BodyModel GenerateBody(out bool noData, out List<string> fileList)
{
fileList = new List<string>();
var model = new BodyModel();
SACReportNotesCache = base.InitReportNotes(ReportType);
var dataList = new List<SwapConfirmationModel>();
//if (_reqInfo.DataSource.HasFlag(SAC_ReportDataSourceEnum.Template))
//{
dataList = GetSwapConfirmationModel(ref fileList);
//}
var subsystemDataList = loadSubsystemDataSource<SwapConfirmationModel>(fileList, AddSubsystemNote);
if (subsystemDataList != null && subsystemDataList.Count > 0)
{
dataList.AddRange(subsystemDataList);
}
fileList = fileList.Distinct().ToList();
noData = dataList.Count == 0;
model.SwapConfirmation = dataList;
return model;
}
public bool AddSubsystemNote(SwapConfirmationModel model, List<string> fileList, string tag)
{
var note = base.GetReportNotes(ReportType, formatInfoTag(model, true), dataSource: SACReportNotesCache).FirstOrDefault();//参数true要加,要不A和D会查到同一条记录,在D时候,就会和A相同的数据
if (note == null)
{
note = new SACReportNotes()
{
InfoCache = $"{{\"NotinalPrincipleAmt\":\"{model.NotinalPrincipleAmt}\",\"DueDate\":\"{model.DueDate}\",\"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 = $"{{\"NotinalPrincipleAmt\":\"{model.NotinalPrincipleAmt}\",\"DueDate\":\"{model.DueDate}\",\"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("未知操作类型");
}
}
if (!string.IsNullOrWhiteSpace(model.PerformanceGuaranteeAtt))
{
fileList.Add(model.PerformanceGuaranteeAtt);
}
if (!string.IsNullOrWhiteSpace(model.ComplianceOpinion))
{
fileList.Add(model.ComplianceOpinion);
}
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);
note.OptTime = note.CreateTime;
note.RetCode = "";
note.RetMsg = "";
note.ReportResponse = false;
note.BizId = "";
note.changeStatus = false;
noteList.Add(note);
return true;
}
private List<SwapConfirmationModel> GetSwapConfirmationModel(ref List<string> fileList)
{
var result = new List<SwapConfirmationModel>();
var path = "";
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][3]?.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 SwapConfirmationModel
{
OperationType = optType,
MasterAgrmtNo = GetDataSetValue(dt, i, 0),
SupAgrmtNo = GetDataSetValue(dt, i, 1),
ConfirmationNo = GetDataSetValue(dt, i, 2),
ConfirmationType = ConsReport.ReportTypeMap[optType == OptFlagsEnum.A ? "首次" : "变更"],
FillParty = ConsReport.FillPartyMap[GetDataSetValue(dt, i, 4)],
SwapType = ConsReport.SwapTypeMap[GetDataSetValue(dt, i, 5)],
StartDate = GetDataSetValue(dt, i, 6),
DueDate = GetDataSetValue(dt, i, 7),
SettlementDate = GetDataSetValue(dt, i, 8)
};
var currencyStr = GetDataSetValue(dt, i, 9);
currencyStr = currencyStr == "其他" ? "Other" : currencyStr;
if (Enum.TryParse(currencyStr.ToUpper(), out CurrencyEnum currency))
{
model.Currency = ((int)currency).ToString();
}
model.NotinalPrincipleAmt = GetDataSetValue(dt, i, 10);
model.ClearingAgency = ConsReport.ClearingAgencyMap[GetDataSetValue(dt, i, 11)];
model.TradingPlace = ConsReport.TradingPlaceMap[GetDataSetValue(dt, i, 12)];
model.TradingPlaceOther = GetDataSetValue(dt, i, 13);
model.UndrlygAssetType = ConsReport.UndrlygAssetTypeMap[GetDataSetValue(dt, i, 14)];
model.ConfirmationRemark = GetDataSetValue(dt, i, 15);
model.PerformanceGuaranteeType = ConsReport.PerformanceGuaranteeMap[GetDataSetValue(dt, i, 16)];
model.PerformanceCollProvider = ConsReport.PerformanceCollProviderMap[GetDataSetValue(dt, i, 17)];
model.PartyUseColl = ConsReport.BoolMap[GetDataSetValue(dt, i, 18)];
model.CollInstruction = GetDataSetValue(dt, i, 19);
model.CalculateCollInterest = ConsReport.BoolMap[GetDataSetValue(dt, i, 20)];
model.PerformanceCollInitialRatio = GetDataSetValue(dt, i, 21);
model.PerformanceCollAddtlRatio = GetDataSetValue(dt, i, 22);
model.PerformanceCollOffsetRatio = GetDataSetValue(dt, i, 23);
model.MaintainGuaranteeRatio = GetDataSetValue(dt, i, 24);
model.PerformanceGuaranteeAtt = GetDataSetValue(dt, i, 25);
model.PerformanceGuaranteeRemark = GetDataSetValue(dt, i, 26);
model.Remark = GetDataSetValue(dt, i, 27);
model.ComplianceOpinion = GetDataSetValue(dt, i, 28);
model.PtyAUndrlygAssetNo = ConsReport.UndrlygAssetNoMap[GetDataSetValue(dt, i, 29)];
model.PtyAPrOfitCalculationInfo = GetDataSetValue(dt, i, 30);
model.PtyAProfitRemark = GetDataSetValue(dt, i, 31);
model.PtyAPdctName = GetDataSetValue(dt, i, 32);
model.PytAPdctCode = GetDataSetValue(dt, i, 33);
model.PtyBPdctName = GetDataSetValue(dt, i, 34);
model.PytBPdctCode = GetDataSetValue(dt, i, 35);
model.Blank1 = GetDataSetValue(dt, i, 36);
model.Blank2 = GetDataSetValue(dt, i, 37);
model.CostPaymentTuple = GetCostPaymentTuple(model.ConfirmationNo);
model.PerformanceCollTuple = GetPerformanceGuaranteeAttModel(model.ConfirmationNo);
var notinalPrincipleAmt = GetDataSetValueToDouble(dt, i, 10);
var cacheValue = (model.ConfirmationNo ?? "").ToString();
if (cacheValue.IsNullOrWhiteSpace())
{
continue;
}
if (ReportStatus.CheckCacheInfo(CacheKey, cacheValue))
{
continue;
}
var note = base.GetReportNotes(ReportType, formatInfoTag(model), dataSource: SACReportNotesCache).FirstOrDefault();
if (note == null && _operationType == OptFlagsEnum.A)
{
note = new SACReportNotes()
{
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("未知操作类型");
}
var reportCacheDic = JsonHelper.ToObject<Dictionary<string, string>>(note.InfoCache) ?? new Dictionary<string, string>();
if (_operationType == OptFlagsEnum.U)
{
if (reportCacheDic.ContainsKey("DueDate") && !reportCacheDic["DueDate"].StartsWith(model.DueDate) && !model.DueDate.StartsWith(reportCacheDic["DueDate"]))
{
throw new ServiceException("补正时,如果该交易还有存续期记录,到期日字段不能修改");
}
if (reportCacheDic.ContainsKey("NotinalPrincipleAmt") && reportCacheDic["NotinalPrincipleAmt"] != model.NotinalPrincipleAmt && reportCacheDic["NotinalPrincipleAmt"] != notinalPrincipleAmt.ToString("0"))
{
throw new ServiceException("补正时,如果该交易还有存续期记录,名义本金额(人民币)字段不能修改");
}
}
}
if (model.OperationType != OptFlagsEnum.A)
{
model.ConfirmationID = note.BizId;//确认书编号;
}
if (!string.IsNullOrWhiteSpace(model.PerformanceGuaranteeAtt))
{
path = Path.Combine(sourcePath, model.PerformanceGuaranteeAtt);
if (!ReportStatus.CheckFileLength(path))
{
break;
}
fileList.Add(path);
}
if (!string.IsNullOrWhiteSpace(model.ComplianceOpinion))
{
path = Path.Combine(sourcePath, model.ComplianceOpinion);
if (!ReportStatus.CheckFileLength(path))
{
break;
}
fileList.Add(path);
}
model.ExceID = base.formatExceID(SACReportNotesCache);
result.Add(model);
ReportStatus.AddCacheInfo(CacheKey, cacheValue);
note.id = 0;
note.InfoCache = $"{{\"NotinalPrincipleAmt\":\"{model.NotinalPrincipleAmt}\",\"DueDate\":\"{model.DueDate}\",\"Tag\":\"{model.ConfirmationNo}\",\"Source\":\"Template\"}}";
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 result;
}
private List<PaymentMethodModel> GetCostPaymentTuple(string confirmationNo)
{
var result = new List<PaymentMethodModel>();
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 PaymentMethodModel
{
PaymentMethod = ConsReport.PaymentMethodMap[GetDataSetValue(dt, i, 1)],
Payer = ConsReport.PayerMap[GetDataSetValue(dt, i, 2)],
PaymentFreq = ConsReport.PaymentFreqMap[GetDataSetValue(dt, i, 3)],
FixedInterestRate = GetDataSetValue(dt, i, 4),
FloatInterestRate = ConsReport.RefRateTypeMap[GetDataSetValue(dt, i, 5)],
ReferenceofFloatingInterestRate = GetDataSetValue(dt, i, 6),
BasePoint = GetDataSetValue(dt, i, 7),
Blank1 = GetDataSetValue(dt, i, 8),
Blank2 = GetDataSetValue(dt, i, 9)
};
result.Add(model);
}
}
return result;
}
private List<SwapPerformanceGuaranteeAttModel> GetPerformanceGuaranteeAttModel(string confirmationNo)
{
var result = new List<SwapPerformanceGuaranteeAttModel>();
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 SwapPerformanceGuaranteeAttModel
{
PerformanceCollType = ConsReport.PerformanceCollTypeMap[GetDataSetValue(dt, i, 1)],
PerformanceCollRange = ConsReport.PerformanceCollRangeMap[GetDataSetValue(dt, i, 2)],
MultiConfirmationID = GetDataSetValue(dt, i, 3),
Remarks = GetDataSetValue(dt, i, 4)
};
result.Add(model);
}
}
return result;
}
private string formatInfoTag(SwapConfirmationModel model, bool suffixType = false)
{
var result = $"{BusiDataType.ToString()}_{model.ConfirmationNo.Replace("_", "-")}_成交_";
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 != 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)
{
var result = new List<SacInfo>();
if (model?.SwapConfirmation != null)
{
var helper = new Common.CheckHelper<SwapConfirmationModel>();
var pAttHelper = new Common.CheckHelper<SwapPerformanceGuaranteeAttModel>();
var cAttHelper = new Common.CheckHelper<PaymentMethodModel>();
for (var i = 0; i < model.SwapConfirmation.Count; i++)
{
var listRoot = new List<SacInfo>();
var item = model.SwapConfirmation[i];
if (item.IgnoreCheck)
{
continue;
}
helper.ExecuteCheck(item, (name, value, msg) =>
{
listRoot.Add(new SacInfo(name, value, msg));
});
if (item.CostPaymentTuple != null)
{
for (var j = 0; j < item.CostPaymentTuple.Count; j++)
{
var listItem = new List<SacInfo>();
var attItem = item.CostPaymentTuple[j];
cAttHelper.ExecuteCheck(attItem, (name, value, msg) =>
{
listItem.Add(new SacInfo(name, value, msg));
});
if (listItem.Count > 0)
{
var temp = new SacInfo("CostPaymentTuple", j)
{
SubMaps = new List<SacInfo>(listItem)
};
listRoot.Add(temp);
}
}
}
if (item.PerformanceCollTuple != null)
{
for (var j = 0; j < item.PerformanceCollTuple.Count; j++)
{
var listItem = new List<SacInfo>();
var attItem = item.PerformanceCollTuple[j];
pAttHelper.ExecuteCheck(attItem, (name, value, msg) =>
{
listItem.Add(new SacInfo(name, value, msg));
});
if (listItem.Count > 0)
{
var temp = new SacInfo("PerformanceCollTuple", j)
{
SubMaps = new List<SacInfo>(listItem)
};
listRoot.Add(temp);
}
}
}
if (listRoot.Count > 0)
{
var errMsg = new SacInfo("SwapConfirmation", 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;
}
}
}