using BaseOUDAL; using System.Data; using YLErp.Commons; 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 { /// /// SAC主协议 /// class ReportMasterAgrmtService : ReportBaseService { public ReportMasterAgrmtService(OptUserInfo optUser) : base(optUser) { } protected override string _excelDataSourcePath => "协议相关\\"; protected override string _excelDataSourceFileName => "import_MasterAgrmt_template.xlsx"; protected override DataFlagsEnum BusiDataType => DataFlagsEnum.A1001; private List? _validOperationType = null; public override List ValidOperationType { get { _validOperationType ??= new List() { OptFlagsEnum.A, OptFlagsEnum.U, OptFlagsEnum.D, }; return _validOperationType; } } protected override SuperviseReportTypeEnum ReportType => SuperviseReportTypeEnum.SAC_MasterAgrmt; readonly List noteList = new(); protected override BodyModel GenerateBody(out bool noData, out List fileList) { fileList = new List(); var model = new BodyModel(); var dataList = new List(); //if (_reqInfo.DataSource.HasFlag(SAC_ReportDataSourceEnum.Template)) //{ dataList = GetMasterAgrmtModelFromExcel(ref fileList); //} var subsystemDataList = loadSubsystemDataSource(fileList, AddSubsystemNote); if (subsystemDataList != null && subsystemDataList.Count > 0) { dataList.AddRange(subsystemDataList); } noData = dataList.Count == 0; model.MasterAgrmt = dataList; return model; } public bool AddSubsystemNote(MasterAgrmtModel model, List fileList, string tag) { var note = base.GetReportNotes(ReportType, formatInfoTag(model, true)).FirstOrDefault();//参数true要加,要不A和D会查到同一条记录,在D时候,就会和A相同的数据 if (note == null) { note = new SACReportNotes() { InfoCache = $"{{\"Tag\":\"{model.MasterAgrmtNo}\",\"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 note.InfoCache = $"{{\"Tag\":\"{model.MasterAgrmtNo}\",\"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.MasterAgrmtAtt)) { fileList.Add(model.MasterAgrmtAtt); } 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; } private List GetMasterAgrmtModelFromExcel(ref List fileList) { var result = new List(); 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 = generiterInfoFromExcel(dt, i, optType); model.CounterpartyInformationTuple = GetCounterpartyInfomationModelFromExcel(model.MasterAgrmtNo); var cacheValue = model.MasterAgrmtNo; if (ReportStatus.CheckCacheInfo(CacheKey, cacheValue)) { continue; } var note = base.GetReportNotes(ReportType, formatInfoTag(model)).FirstOrDefault(); if (note == null && _operationType == OptFlagsEnum.A) { note = new SACReportNotes() { InfoCache = $"{{\"Tag\":\"{model.MasterAgrmtNo}\",\"Source\":\"Template\"}}", IsValid = true, }; } else { if (note == null) { continue; } switch (optType) { 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 (model.OperationType != OptFlagsEnum.A) { model.MasterAgrmtID = note.BizId; } if (!string.IsNullOrWhiteSpace(model.MasterAgrmtAtt)) { model.ExceID = base.formatExceID(); var path = Path.Combine(sourcePath, model.MasterAgrmtAtt); if (!ReportStatus.CheckFileLength(path)) { break; } else { ReportStatus.AddCacheInfo(CacheKey, cacheValue); } result.Add(model); fileList.Add(path); 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 result; } private MasterAgrmtModel generiterInfoFromExcel(DataTable dt, int i, OptFlagsEnum optType) { var model = new MasterAgrmtModel { MasterAgrmtNo = GetDataSetValue(dt, i, 0), SigningDate = GetDataSetValue(dt, i, 1), MasterAgrmtVer = ConsReport.MasterAgrmtVerMap[GetDataSetValue(dt, i, 2)], OperationType = optType == OptFlagsEnum.A ? OptFlagsEnum.A : OptFlagsEnum.U, FillParty = ConsReport.FillPartyMap[GetDataSetValue(dt, i, 4)], NameOfCounterparty = GetDataSetValue(dt, i, 5), CODS = GetDataSetValue(dt, i, 6), CounterpartyCode = GetDataSetValue(dt, i, 7), LEI = GetDataSetValue(dt, i, 8), ProCounterparty = ConsReport.ProCounterpartyMap[GetDataSetValue(dt, i, 9)], CounterpartyType = ConsReport.CounterpartyTypeMap[GetDataSetValue(dt, i, 10)], NFICode = GetDataSetValue(dt, i, 11), CounterpartyRegdCptl = GetDataSetValue(dt, i, 12), MasterAgrmtRemark = GetDataSetValue(dt, i, 13), MasterAgrmtAtt = GetDataSetValue(dt, i, 14), CounterpartyIdentity = ConsReport.CounterpartyIdentityMap[GetDataSetValue(dt, i, 15)] }; return model; } private string formatInfoTag(MasterAgrmtModel model, bool suffixType = false) { var result = $"{BusiDataType}_{model.MasterAgrmtNo.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 != 3) { throw new ServiceException($"InfoTag信息不匹配:{infoTag}"); } originalCode = arr[1]; arr[1] = newCode.Replace("_", "-"); return string.Join("_", arr); } private List GetCounterpartyInfomationModelFromExcel(string masterAgrmtNo) { var result = new List(); 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() != masterAgrmtNo) { continue; } var model = new CounterpartyInfomationModel { Name = GetDataSetValue(dt, i, 1), Title = GetDataSetValue(dt, i, 2), Telephone = GetDataSetValue(dt, i, 3), Mobile = GetDataSetValue(dt, i, 4), Email = GetDataSetValue(dt, i, 5) }; result.Add(model); } } return result; } protected override List CheckBodyValue(BodyModel model, out bool checkStatus) { var result = new List(); if (model?.MasterAgrmt != null) { var helper = new Common.CheckHelper(); var infoHelper = new Common.CheckHelper(); for (var i = 0; i < model.MasterAgrmt.Count; i++) { var listRoot = new List(); var item = model.MasterAgrmt[i]; if (item.IgnoreCheck) { continue; } helper.ExecuteCheck(item, (name, value, msg) => { listRoot.Add(new SacInfo(name, value, msg)); }); if (item.CounterpartyInformationTuple != null) { for (var j = 0; j < item.CounterpartyInformationTuple.Count; j++) { var listItem = new List(); var tuple = item.CounterpartyInformationTuple[j]; infoHelper.ExecuteCheck(tuple, (name, value, msg) => { listItem.Add(new SacInfo(name, value, msg)); }); if (listItem.Count > 0) { var temp = new SacInfo("CounterpartyInformationTuple", j) { SubMaps = new List(listItem) }; listRoot.Add(temp); } } } if (listRoot.Count > 0) { var errMsg = new SacInfo("MasterAgrmt", i) { FieldValue = item.MasterAgrmtNo, SubMaps = new List(listRoot) }; result.Add(errMsg); } } } checkStatus = result.Count > 0; return result; } public override bool BeforeOfGenerated(out string errMsg) { try { errMsg = ""; foreach (var file in xmlAttachmentList) { try { File.Delete(file); } catch (Exception e) { LogFactory.GetLogger().Error(e); }; } for (var i = 0; i < noteList.Count; i++) { base.SaveReportNotes(noteList[i]); } return true; } catch (Exception ex) { LogFactory.GetLogger().Error(ex); errMsg = "修改主协议报送状态失败!"; return false; } } } }