using System; using System.Collections.Generic; using System.Data; using System.Linq; using Kreta.Core; using Kreta.Core.Exceptions; using Kreta.Core.SAP; using Kreta.DataAccessManual; using Kreta.DataAccessManual.Interfaces; using Kreta.DataAccessManual.ParameterClasses; using Kreta.Enums.ManualEnums; using Kreta.Job.Tasks.Core; using Kreta.Job.Tasks.Core.Models; using Kreta.Job.Tasks.Models; using Kreta.Resources; namespace Kreta.Job.Tasks { public class SapJob : ISapJob { private ISAPService SapServiceClient { get; } private ISAPConfiguration SapConfiguration { get; } public SapJob(ISAPService sapService, ISAPConfiguration sapConfiguration) { SapServiceClient = sapService ?? throw new ArgumentNullException(nameof(sapService)); SapConfiguration = sapConfiguration ?? throw new ArgumentNullException(nameof(sapService)); } public void SyncJobKeretEsTavollet() { var exceptions = new List(); var now = DateTime.Now; foreach (var azonosito in KretaServer.KretaServer.Instance.GetOsszesIntezmeny()) { Dal.OrganizationConnection.Run(azonosito, h => { try { var icDal = h.IntezmenyConfigDAL(); var dal = h.IntezmenyDal(); var intezmenyId = dal.GetIntezmenyIdByAzonosito(azonosito); if (!bool.Parse(icDal.GetIntezmenyConfig(intezmenyId.Value, IntezmenyConfigModulEnum.HRModul.ToString(), IntezmenyConfigTipusEnum.IsEnabled.ToString()))) return; var ds = dal.GetIntezmenyIdAktivTanevIdByAzonosito(azonosito); if (ds.Tables[0].Rows.Count > 0) { var pco = new TavolletSearchPco { TanevId = ds.Tables[0].Rows[0].Field("AktivTanevId"), IntezmenyId = ds.Tables[0].Rows[0].Field("IntezmenyId"), IntezmenyAzonosito = azonosito, NaptariEv = new int[] { now.Year }, }; SyncSzabadsagKeret(h, null, pco); SyncTavollet(h, null, pco); } } catch (BlException ex) { exceptions.Add(ex); //TODO: Üzleti logika által dobott hiba egyéb kezelése! Ha kell a job-nál ezzel valamit kezdeni, akkor... } catch (Exception ex) { exceptions.Add(ex); } }); } if (exceptions.Any()) { var blExceptions = exceptions.FindAll(e => e is BlException).Cast(); if (blExceptions.Any()) { //UzenetekHelper.cs ?? SendNotificationEmail(blExceptions); } var aggregateException = new AggregateException(exceptions); throw aggregateException.Flatten(); } } private void SendNotificationEmail(IEnumerable blExceptions) { var aggregateBlException = new AggregateException(blExceptions).ToString(); var emailModel = new EmailModel { Message = aggregateBlException, Subject = $"{EmailResource.SapSyncJob}", Bcc = SapConfiguration.NotificationEmailsBussinessLogic }; Hangfire.BackgroundJob.Enqueue(email => email.SendMailAsync(emailModel)); } private int SyncSzabadsagKeret(IDalHandler dalHandler, int? alkalmazottId, TavolletSearchPco pco) { int missingSzTSz = 0; var aDal = dalHandler.Alkalmazott(); DataSet ds = aDal.GetAlkalmazottakSapAzonositoi(alkalmazottId, pco.TanevId, pco.IntezmenyId); if (ds.Tables[0].Rows.Count > 0) { var funkcioTeruletek = new HashSet(); var keretek = new Dictionary(); var requestQ = new List(); var now = DateTime.Now; foreach (DataRow row in ds.Tables[0].Rows) { string intezmenySapAzonosito = row["IntezmenySAPKod"].ToString(); string szTSzAzonosito = row["SzTSzKod"].ToString(); if (string.IsNullOrWhiteSpace(intezmenySapAzonosito)) { throw new BlException(string.Format(HRModulResource.IntezmenySapAzonositoHiany, pco.IntezmenyAzonosito)); } if (string.IsNullOrWhiteSpace(szTSzAzonosito)) { missingSzTSz++; continue; } if (!keretek.ContainsKey(szTSzAzonosito)) { keretek.Add(szTSzAzonosito, (row.Field("AlkalmazottId"), row.Field("SzabadsagKeret"))); } if (!funkcioTeruletek.Contains(intezmenySapAzonosito)) { funkcioTeruletek.Add(intezmenySapAzonosito); for (int i = 0; i < pco.NaptariEv.Length; i++) { // Időszak: január 1 - ma DateTime kezdet = new DateTime(pco.NaptariEv[i], 1, 1); DateTime vege = pco.NaptariEv[i] == now.Year ? now : new DateTime(pco.NaptariEv[i], 1, 1); requestQ.Add(new TavolletRequestModel { IntezmenyAzonosito = intezmenySapAzonosito, SzTSzAzonosito = alkalmazottId.HasValue ? szTSzAzonosito : null, Kezdet = kezdet, Vege = vege }); } } } var response = SapServiceClient.LekerdezesKeret(requestQ.ToArray()); if (response != null && response.Any()) { var updateKeretDict = new Dictionary(); var sapErrorList = new List(); foreach (var item in response) { if (ResultCode.Success == item.ErrCode) { if (keretek.TryGetValue(item.SzTSzAzonosito, out (int alkalmazottId, int keret) value) && value.keret != item.SzabadKeret) { updateKeretDict.Add(value.alkalmazottId, item.SzabadKeret); } } else { // Ha hiba van, akkor egy listában gyűjtjük az üzeneteket. sapErrorList.Add(item.ErrText); } } if (sapErrorList != null && sapErrorList.Count > 0) { throw new BlException(HRModulResource.ASzinkronizacioNemSikerult, new BlException(string.Join(Environment.NewLine, sapErrorList))); } aDal.UpdateAlkalmazottSzabadsagKeret(updateKeretDict); } } return missingSzTSz; } private int SyncTavollet(IDalHandler dalHandler, int? alkalmazottId, TavolletSearchPco pco) { int missingSzTSz = 0; IAlkalmazottDal alkalmazaottDal = dalHandler.Alkalmazott(); DataSet ds = alkalmazaottDal.GetAlkalmazottakSapAzonositoi(alkalmazottId, pco.TanevId, pco.IntezmenyId); if (ds.Tables[0].Rows.Count > 0) { var sztszError = new HashSet(); //Adathibák!!! var sapAzonositok = ds.Tables[0].Rows.OfType(); var requestQ = new List(); foreach (DataRow row in sapAzonositok) { string intezmenySapAzonosito = row["IntezmenySAPKod"].ToString(); string szTSzAzonosito = row["SzTSzKod"].ToString(); if (string.IsNullOrWhiteSpace(intezmenySapAzonosito)) { throw new BlException(string.Format(HRModulResource.IntezmenySapAzonositoHiany, pco.IntezmenyAzonosito)); } if (string.IsNullOrWhiteSpace(szTSzAzonosito)) { missingSzTSz++; continue; } // Időszak: január 1 - december 31 for (int i = 0; i < pco.NaptariEv.Length; i++) { DateTime kezdet = new DateTime(pco.NaptariEv[i], 1, 1); DateTime vege = kezdet.AddYears(1).AddSeconds(-1); requestQ.Add(new TavolletRequestModel { IntezmenyAzonosito = intezmenySapAzonosito, SzTSzAzonosito = szTSzAzonosito, Kezdet = kezdet, Vege = vege }); } } var response = SapServiceClient.Lekerdezes(requestQ.ToArray()); if (response != null && response.Any()) { ISapTavolletDal sapTavolletDal = dalHandler.SapTavolletDal(); var tavolletTipusDict = new Dictionary(); foreach (DataRow row in sapTavolletDal.GetTavolletTipusDataSet(pco.TanevId, pco.IntezmenyId).Tables[0].Rows) { tavolletTipusDict.Add(row.Field("SapCode"), row.Field("ID")); } // 'Na' érték SAP kódja string naTavollettipus = "0000"; var tavolletListDict = new Dictionary>(); var sapErrorList = new List(); foreach (var item in response) { if (ResultCode.Success == item.ErrCode) { DataRow sapAzonosito; try { sapAzonosito = sapAzonositok.Single(d => d.Field("SzTSzKod") == item.SzTSzAzonosito); } catch (Exception) { if (!sztszError.Contains(item.SzTSzAzonosito)) { sztszError.Add(item.SzTSzAzonosito); } continue; } TavolletModel e = new TavolletModel { AlkalmazottId = sapAzonosito.Field("AlkalmazottId"), ElozoTaneviAlkalmazottId = sapAzonosito.Field("ElozoTaneviAlkalmazottId"), TanevId = pco.TanevId, IntezmenyId = pco.IntezmenyId, TavolletKezdete = item.Kezdet, TavolletVege = item.Vege, TavolletIdotartamNap = item.TavolletIdotartamNap, TavolletIdotartamOra = item.TavolletIdotartamOra, TavolletTipusId = tavolletTipusDict.TryGetValue(item.TavolletKod.WithLeadZeros(), out int value) ? value : tavolletTipusDict[naTavollettipus] }; if (!tavolletListDict.ContainsKey(item.SzTSzAzonosito)) { tavolletListDict.Add(item.SzTSzAzonosito, new List()); } tavolletListDict[item.SzTSzAzonosito].Add(e); } else { // Ha hiba van, akkor egy listában gyűjtjük az üzeneteket. sapErrorList.Add(item.ErrText); } } if (sapErrorList != null && sapErrorList.Count > 0) { throw new BlException(HRModulResource.ASzinkronizacioNemSikerult, new BlException(string.Join(Environment.NewLine, sapErrorList))); } var checklistTavolletDict = new Dictionary>(); foreach (DataRow row in sapTavolletDal.GetSAPTavolletDataSet(alkalmazottId, pco).Tables[0].Rows) { TavolletModel c = new TavolletModel { AlkalmazottId = row.Field("AlkalmazottId"), IntezmenyId = row.Field("IntezmenyId"), TavolletKezdete = row.Field("TavolletKezdete"), TavolletVege = row.Field("TavolletVege"), TavolletIdotartamNap = row.Field("TavolletIdotartamNap"), TavolletIdotartamOra = row.Field("TavolletIdotartamOra"), TavolletTipusId = row.Field("TavolletTipusId") }; if (!checklistTavolletDict.ContainsKey(row.Field("SzTSzKod"))) { checklistTavolletDict.Add(row.Field("SzTSzKod"), new List()); } checklistTavolletDict[row.Field("SzTSzKod")].Add(c); } var syncKeys = new List(); foreach (var key in tavolletListDict.Keys) { int th = tavolletListDict[key].AsReadOnly().GetSequenceHashCode(); int ch = checklistTavolletDict.TryGetValue(key, out List list) ? list.AsReadOnly().GetSequenceHashCode() : 0; if (th != ch) { syncKeys.Add(key); } } if (syncKeys.Count > 0) { sapTavolletDal.SyncSapTavollet(ConvertModelToPco(tavolletListDict.Where(d => syncKeys.Contains(d.Key))), pco); } } } return missingSzTSz; } private Dictionary> ConvertModelToPco(IEnumerable>> coList) { var result = new Dictionary>(); foreach (var co in coList) { result.Add(co.Key, new List()); foreach (var item in co.Value) { result[co.Key].Add(new SAPTavolletPco { AlkalmazottId = item.AlkalmazottId, ElozoTaneviAlkalmazottId = item.ElozoTaneviAlkalmazottId, TanevId = item.TanevId, IntezmenyId = item.IntezmenyId, TavolletKezdete = item.TavolletKezdete, TavolletVege = item.TavolletVege, TavolletIdotartamNap = item.TavolletIdotartamNap, TavolletIdotartamOra = item.TavolletIdotartamOra, TavolletTipusId = item.TavolletTipusId }); } } return result; } } }