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

489 lines
22 KiB
C#

using System.Data;
using YieldChain.Helpers;
using YLErp.BLL;
using YLErp.DBModels.Consts;
using YLErp.DBModels.Enums;
using YLErp.Helpers;
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 ReportSwapConfirmationAttService : ReportBaseService
{
public ReportSwapConfirmationAttService(OptUserInfo optUser) : base(optUser)
{
}
protected override string _excelDataSourcePath => "交易相关\\";
protected override string _excelDataSourceFileName => "import_SwapConfirmationAtt_template.xlsx";
protected override DataFlagsEnum BusiDataType => DataFlagsEnum.A1017;
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_ConfirmationAtt;
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();
var cacheKey = $"{BusiDataType}";
SACReportNotesCache = base.InitReportNotes(ReportType);
SACReportNotesCache_confirmation = base.InitReportNotes(SuperviseReportTypeEnum.SAC_SwapConfirmation);
var dataList = new List<ConfirmationAttModel>();
if ((PS.Config.ErpElement.SAC_ReportDataSource & SAC_ReportDataSourceEnum.System) == SAC_ReportDataSourceEnum.System)
{
var nextDate = _reqInfo.ReportDate.AddDays(1);
var nextDataDate = _reqInfo.DataDate.AddDays(1);
using (var db = new YLContext())
{
var codeList = ReportStatus.GetCacheInfo(cacheKey);
var query = (from t in db.trade
join tr in db.trade_contract_r
on t.id equals tr.TradeId
join td in db.trade_contract_document
on t.ContractCode equals td.Code
where
(_operationType == OptFlagsEnum.A && t.TradeDate >= _reqInfo.DataDate &&
t.TradeDate < nextDataDate) //新增时使用交易日期判断)//补正和废止时使用操作日期判断
&& (_operationType == OptFlagsEnum.D || (t.ValidState != "InValid")
&& t.TradeType == "收益互换")
select new
{
t.id,
tradeContractRValid = tr.IsValid,
tradeValid = t.ValidState != "InValid",
td.Paths,
td.ValueDate,
t.ClientId,
t.TradeDate,
TradeType = t.StructureType ?? t.TradeType,
t.OptionType,
tr.ContractCode,
}).OrderBy(O => O.id).ToArray().Where(O => !codeList.Contains(O.id + "")).ToArray();
List<int> ids = query.Select(O => O.id).ToList();
List<int> delIds = new List<int>();
if (_operationType == OptFlagsEnum.D)
{
delIds = DbContext.TradeAuditLog.Where(O => ids.Contains(O.TradeId) && O.OptDate >= _reqInfo.ReportDate && O.OptDate < nextDate && O.OptType == "删除交易").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));
Dictionary<string, string> reportDescDic = DbContext.AppConfig.Where(O => O.PGroup == ConsAppConfig.ReportDescSwap).ToDictionary(K => K.PName, V => V.PValue);
foreach (var item in query)
{
var cacheValue = item.id.ToString();
if (ReportStatus.CheckCacheInfo(cacheKey, cacheValue))
{
continue;
}
//如果存在重复数据,则始终用较新的数据
ConfirmationAttModel info = new ConfirmationAttModel();
info.ConfirmationNo = item.ContractCode;
var confirmationNote = base.GetReportNotes(SuperviseReportTypeEnum.SAC_SwapConfirmation, $"_{info.ConfirmationNo.Replace("_", "-")}_").FirstOrDefault();
var currentCodes = ReportStatus.GetCacheInfo(DataFlagsEnum.A1005.ToString());//取出这一次报送交易要素的交易编号
if (!currentCodes.Contains(item.id.ToString()) && (confirmationNote == null || !confirmationNote.ReportResponse))
{
continue;//跳过交易要素还没报送的确认书附件;
}
SACReportNotes note = base.GetReportNotes(formatInfoTag(info)).FirstOrDefault();
if (note == null && _operationType == OptFlagsEnum.A)
{
info.OperationType = OptFlagsEnum.A;
note = new SACReportNotes()
{
IsValid = true,
};
}
else
{
if (note == null)
{
continue;
}
switch (_operationType)
{
case OptFlagsEnum.A:
continue;//新增数据已报送,跳过
case OptFlagsEnum.U:
if (!note.changeStatus)
{
continue;//交易没有被修改过,跳过
}
info.OperationType = OptFlagsEnum.U;
note.IsValid = true;
break;
case OptFlagsEnum.D:
if (!delIds.Contains(item.id))
{
continue;//交易没有被删除,跳过
}
info.OperationType = OptFlagsEnum.D;
note.IsValid = false;
break;
case OptFlagsEnum.NONE:
default:
throw new ServiceException("未知操作类型");
}
}
if (info.OperationType != _operationType)
{
continue;
}
if (info.OperationType != OptFlagsEnum.D && (!item.tradeValid || !item.tradeContractRValid))
{//非废止报送中,应跳过所有存在无效数据的数据
continue;
}
if (metaDic.ContainsKey(item.id))
{
var metaInfo = metaDic[item.id];
var tradingPlace = metaInfo.ContainsKey(ConsTradeMetaKey.TradingPlace) ? TradingPlaceMap[metaInfo[ConsTradeMetaKey.TradingPlace]] : "";
if (tradingPlace == "1")//跳过交易场所为报价系统的交易;
{
continue;
}
}
if (info.OperationType != OptFlagsEnum.A)
{
info.BizID = note.BizId;//确认书编号;
}
string path = item.Paths;
if (!path.StartsWith("\\") || path.StartsWith("/"))
{
path = "\\" + path;
}
var absolutePath = OtcAppContext.MapPath(path);
absolutePath = Path.ChangeExtension(absolutePath, "pdf");
info.ConfirmationFilesTuple = new List<ConfirmationFilesTupleModel>() { new ConfirmationFilesTupleModel() { ConfirmationFiles = Path.GetFileName(absolutePath) } };
info.ExceID = base.formatExceID();
if (!ReportStatus.CheckFileLength(absolutePath))
{
break;
}
fileList.Add(absolutePath);
ReportStatus.AddCacheInfo(cacheKey, cacheValue);
dataList.Add(info);
note.id = 0;
note.InfoCache = $"{{\"DueDate\":\"{item.ValueDate?.ToString("yyyy-MM-dd")}\",\"Tag\":\"{info.ConfirmationNo}\",\"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.changeStatus = false;
noteList.Add(note);
}
}
}
var subsystemDataList = loadSubsystemDataSource<ConfirmationAttModel>(fileList, AddSubsystemNote);
if (subsystemDataList != null && subsystemDataList.Count > 0)
{
dataList.AddRange(subsystemDataList);
}
noData = dataList.Count == 0;
model.ConfirmationAtt = dataList;
return model;
}
public bool AddSubsystemNote(ConfirmationAttModel model, List<string> fileList, string tag)
{
SACReportNotes note = base.GetReportNotes(ReportType, formatInfoTag(model, true), dataSource: SACReportNotesCache).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("未知操作类型");
}
}
if (model.ConfirmationFilesTuple != null && model.ConfirmationFilesTuple.Count > 0)
{
fileList.AddRange(model.ConfirmationFilesTuple.Select(x => x.ConfirmationFiles));
}
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<ConfirmationAttModel> GetSwapConfirmationAttModel(ref List<string> fileList)
{
var result = new List<ConfirmationAttModel>();
if (_excelDataSource != null && _excelDataSource.Tables.Contains("收益互换确认书附件明细"))
{
var dt = _excelDataSource.Tables["收益互换确认书附件明细"];
var sourcePath = Path.Combine(_excelDataSourceRootPath, "附件");
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 ConfirmationAttModel
{
ConfirmationNo = GetDataSetValue(dt, i, 0),
OperationType = optType
};
var cacheValue = (model.ConfirmationNo ?? "").ToString();
if (cacheValue.IsNullOrWhiteSpace())
{
continue;
}
if (ReportStatus.CheckCacheInfo(CacheKey, cacheValue))
{
continue;
}
model.ConfirmationFilesTuple = new List<ConfirmationFilesTupleModel>();
var files = GetDataSetValue(dt, i, 2).Split(',');
foreach (var f in files)
{
if (!string.IsNullOrWhiteSpace(f))
{
var path = Path.Combine(sourcePath, f);
if (!ReportStatus.CheckFileLength(path))
{
break;
}
fileList.Add(path);
model.ConfirmationFilesTuple.Add(new ConfirmationFilesTupleModel() { IsSwap = "true", ConfirmationFiles = f });
}
}
List<SACReportNotes> notes = base.GetReportNotes(ReportType, formatInfoTag(model), dataSource: SACReportNotesCache);
var note = notes.LastOrDefault(O => O.InfoCache.Contains(cacheValue));
if (note == null && _operationType == OptFlagsEnum.A)
{
note = new SACReportNotes()
{
InfoCache = $"{{\"Tag\":\"{model.ConfirmationNo}\",\"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.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);
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(ConfirmationAttModel model, bool suffixType = false)
{
var result = $"{BusiDataType}_{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 != 5)
{
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?.ConfirmationAtt != null)
{
var helper = new CheckHelper<ConfirmationAttModel>();
var attHelper = new CheckHelper<ConfirmationFilesTupleModel>();
for (var i = 0; i < model.ConfirmationAtt.Count; i++)
{
var listRoot = new List<SacInfo>();
var item = model.ConfirmationAtt[i];
if (item.IgnoreCheck)
{
continue;
}
helper.ExecuteCheck(item, (name, value, msg) =>
{
listRoot.Add(new SacInfo(name, value, msg));
});
if (item.ConfirmationFilesTuple != null)
{
for (int j = 0; j < item.ConfirmationFilesTuple.Count; j++)
{
var att = item.ConfirmationFilesTuple[j];
attHelper.ExecuteCheck(att, (name, value, msg) =>
{
listRoot.Add(new SacInfo(name, value, msg));
});
}
}
if (listRoot.Count > 0)
{
var errMsg = new SacInfo("SwapConfirmationAtt", 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;
}
}
}