using System.Data; using YLErp.DBModels.Consts; using YLErp.DBModels.Enums; 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 ReportSupAgrmtService : ReportBaseService { public ReportSupAgrmtService(OptUserInfo optUser) : base(optUser) { } protected override string _excelDataSourcePath => "协议相关\\"; protected override string _excelDataSourceFileName => "import_MasterAgrmt_template.xlsx"; protected override DataFlagsEnum BusiDataType => DataFlagsEnum.A1003; private List _validOperationType = null; public override List ValidOperationType { get { if (_validOperationType == null) { _validOperationType = new List() { OptFlagsEnum.A, OptFlagsEnum.U, OptFlagsEnum.D, }; } return _validOperationType; } } protected override SuperviseReportTypeEnum ReportType => SuperviseReportTypeEnum.SAC_SupAgrmt; List noteList = new List(); private List _cacheCodeList = new List(); protected override BodyModel GenerateBody(out bool noData, out List fileList) { fileList = new List(); BodyModel model = new BodyModel(); List dataList = new List(); if ((PS.Config.ErpElement.SAC_ReportDataSource & SAC_ReportDataSourceEnum.Template) == SAC_ReportDataSourceEnum.Template) { dataList = GetSupAgrmtModel(ref fileList); } if ((PS.Config.ErpElement.SAC_ReportDataSource & SAC_ReportDataSourceEnum.System) == SAC_ReportDataSourceEnum.System) { LogFactory.GetLogger("GenerateBody").Info($"System:1"); try { using (var baseDb = DbContextFactory.GetClientDbContext(null)) { var nextDate = _reqInfo.ReportDate.AddDays(1); var fileObjList = (from c in baseDb.client join cf in baseDb.client_file on c.id equals cf.ClientId where c.ProcessStatus == "已开户" && cf.HasSent == false && cf.IsValid && cf.FileTypeName == ConsGlobal.ClientFileType.EnhanceProtocol && cf.OptState == _operationType && ((c.ProcessOptDate >= _reqInfo.ReportDate && c.ProcessOptDate < nextDate && cf.OptDate < nextDate) ||//如果这一天开户,则把这一天之前(含这一天)的所有文件都报送了 (cf.OptDate >= _reqInfo.ReportDate && cf.OptDate < nextDate && c.ProcessOptDate < _reqInfo.ReportDate))//如果这一天不是开户日期,则只在报送日期大于开户日期时,报送当日修改的文件 && cf.ApprovalOrder < 1 select new { cf.id, cf.OptState, cf.ProtocolNumber, cf.MainProtocolNumber, cf.SignDate, cf.FilePath, cf.FileName, cf.FileDesc, }).ToArray(); var cacheKey = $"{BusiDataType}"; foreach (var item in fileObjList) { var info = new SupAgrmtModel(); info.MasterAgrmtNo = item.MainProtocolNumber; info.OperationType = item.OptState; info.SupAgrmtNo = item.ProtocolNumber; info.SigningDate = item.SignDate?.ToString("yyyy-MM-dd"); info.SupAgrmtType = ReportTypeMap[item.OptState == OptFlagsEnum.A ? "首次" : "变更"]; info.SupAgrmtRemark = item.FileDesc; info.SupAgrmtAttTuple = new List(); var tuple = new SupAgrmtAttModel(); tuple.SupAgrmtAtt = item.FileName; var cacheValue = info.MasterAgrmtNo + "_" + info.SupAgrmtNo; if (ReportStatus.CheckCacheInfo(cacheKey, cacheValue)) { continue; } info.SupAgrmtAttTuple.Add(tuple); SACReportNotes note = base.GetReportNotes(ReportType, formatInfoTag(info)).FirstOrDefault(); if (note == null && _operationType == OptFlagsEnum.A) { note = new SACReportNotes() { InfoCache = $"{{\"Tag\":\"{info.SupAgrmtNo}\",\"Source\":\"System\"}}", 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 (info.OperationType != OptFlagsEnum.A) { info.SupAgrmtID = note.BizId; } info.ExceID = base.formatExceID(); if (!ReportStatus.CheckFileLength(item.FilePath)) { break; } else { ReportStatus.AddCacheInfo(cacheKey, cacheValue); } dataList.Add(info); fileList.Add(item.FilePath); note.id = 0; note.DataId = item.id.ToString(); 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); } } } catch(Exception ex) { LogFactory.GetLogger("GenerateBody").Info($"System:发生异常:" + ex.ToString()); ; } } LogFactory.GetLogger("GenerateBody").Info($"System:2"); var subsystemDataList = loadSubsystemDataSource(fileList, AddSubsystemNote); if (subsystemDataList != null && subsystemDataList.Count > 0) dataList.AddRange(subsystemDataList); noData = dataList.Count == 0; model.SupAgrmt = dataList; return model; } public bool AddSubsystemNote(SupAgrmtModel model, List fileList, string tag) { SACReportNotes note = base.GetReportNotes(ReportType, formatInfoTag(model, true)).FirstOrDefault();//参数true要加,要不A和D会查到同一条记录,在D时候,就会和A相同的数据 if (note == null) { note = new SACReportNotes() { InfoCache = $"{{\"Tag\":\"{model.SupAgrmtNo}\",\"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.SupAgrmtNo}\",\"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.SupAgrmtAttTuple != null && model.SupAgrmtAttTuple.Count > 0) { fileList.AddRange(model.SupAgrmtAttTuple.Select(x => x.SupAgrmtAtt)); } 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 GetSupAgrmtModel(ref List fileList) { List result = new List(); if (_excelDataSource != null && _excelDataSource.Tables.Contains("补充协议")) { var cacheKey = $"{BusiDataType}"; string sourcePath = Path.Combine(_excelDataSourceRootPath, "附件"); DataTable dt = _excelDataSource.Tables["补充协议"]; for (int i = 1; i < dt.Rows.Count; i++) { if (string.IsNullOrWhiteSpace(dt.Rows[i][0]?.ToString())) { //第一列空白说明数据结束了; break; } var optType = OptFlagsEnum.NONE; switch (dt.Rows[i][2]?.ToString()) { case "首次": optType = OptFlagsEnum.A; break; case "变更": optType = OptFlagsEnum.U; break; case "废止": optType = OptFlagsEnum.D; break; } if (optType != _operationType) { continue; } SupAgrmtModel model = new SupAgrmtModel(); model.OperationType = optType == OptFlagsEnum.A ? OptFlagsEnum.A : OptFlagsEnum.U; model.MasterAgrmtNo = GetDataSetValue(dt, i, 0); model.SupAgrmtNo = GetDataSetValue(dt, i, 1); var cacheValue = model.MasterAgrmtNo + "_" + model.SupAgrmtNo; if (ReportStatus.CheckCacheInfo(cacheKey, cacheValue)) { continue; } model.SupAgrmtType = ConsReport.ReportTypeMap[optType == OptFlagsEnum.A ? "首次" : "变更"]; model.SigningDate = GetDataSetValue(dt, i, 3); model.SupAgrmtRemark = GetDataSetValue(dt, i, 4); string[] atts = ((GetDataSetValue(dt, i, 5)) ?? "").Split(','); foreach (var item in atts) { if (string.IsNullOrWhiteSpace(item)) { continue; } string path = Path.Combine(sourcePath, item); if (!ReportStatus.CheckFileLength(path)) { break; } if (model.SupAgrmtAttTuple == null) { model.SupAgrmtAttTuple = new List(); } model.SupAgrmtAttTuple.Add(new SupAgrmtAttModel() { SupAgrmtAtt = item }); fileList.Add(path); } SACReportNotes note = base.GetReportNotes(ReportType, formatInfoTag(model)).FirstOrDefault(); if (note == null && _operationType == OptFlagsEnum.A) { note = new SACReportNotes() { InfoCache = $"{{\"Tag\":\"{model.SupAgrmtNo}\",\"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.SupAgrmtID = note.BizId; } if (model.SupAgrmtAttTuple?.Count > 0) { ReportStatus.AddCacheInfo(cacheKey, cacheValue); model.ExceID = base.formatExceID(); result.Add(model); 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 string formatInfoTag(SupAgrmtModel model, bool suffixType = false) { string result = $"{BusiDataType}_{model.MasterAgrmtNo.Replace("_", "-")}_{model.SupAgrmtNo.Replace("_", "-")}_"; if (suffixType) { result = $"{result}{_operationType}"; } return result; } protected override List CheckBodyValue(BodyModel model, out bool checkStatus) { List result = new List(); if (model?.SupAgrmt != null) { CheckHelper helper = new Common.CheckHelper(); CheckHelper attHelper = new CheckHelper(); for (int i = 0; i < model.SupAgrmt.Count; i++) { List listRoot = new List(); var item = model.SupAgrmt[i]; if (item.IgnoreCheck) continue; helper.ExecuteCheck(item, (name, value, msg) => { listRoot.Add(new SacInfo(name, value, msg)); }); if (item.SupAgrmtAttTuple != null) { for (int j = 0; j < item.SupAgrmtAttTuple.Count; j++) { var listItem = new List(); var tuple = item.SupAgrmtAttTuple[j]; attHelper.ExecuteCheck(tuple, (name, value, msg) => { listItem.Add(new SacInfo(name, value, msg)); }); if (listItem.Count > 0) { var temp = new SacInfo("SupAgrmtAttTuple", j); temp.SubMaps = new List(listItem); listRoot.Add(temp); } } } if (listRoot.Count > 0) { var errMsg = new SacInfo("SupAgrmt", i); errMsg.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 (int i = 0; i < noteList.Count; i++) { base.SaveReportNotes(noteList[i]); } return true; } catch (Exception ex) { LogFactory.GetLogger().Error(ex); errMsg = "修改附加协议报送状态失败"; return false; } } } }