using System; using System.Collections.Generic; using System.Data; using System.Linq; using Kreta.Core; using Kreta.Core.MessageBroker.Contract.MobileNotification; using Kreta.Core.MessageBroker.Contract.MobileNotification.Enum; using Kreta.DataAccessManual; using Kreta.Enums; using Kreta.Job.Tasks.Entities; using Kreta.MessageBroker.Client.MobileNotification; using Kreta.Resources; using Newtonsoft.Json; using Serilog; using Serilog.Core.Enrichers; namespace Kreta.Job.Tasks.Helpers.Notification { internal static class ErtekelesNotificationHelper { internal static void SendErtekelesNotification(string connectionString) { var logger = Log.ForContext(new PropertyEnricher[] { new PropertyEnricher("LoggerId", Guid.NewGuid()) }); try { Dal.ServiceSystemConnection.Run(connectionString, h => { var ertekelesDal = h.TanuloErtekelesDal(); var ertekelesek = ertekelesDal.GetAllSchemaErtekelesNotification(DateTime.Now); List ertekelesList = new List(); foreach (DataRow row in ertekelesek.Tables[0].Rows) { if (!int.TryParse(row["ID"] == DBNull.Value ? string.Empty : row["ID"].ToString(), out int ertekelesId)) { continue; } ertekelesList.Add(new IdWithData(ertekelesId, row)); } if (ertekelesList.Count > 0) { ertekelesDal.SetErtekelesAsKikuldott(ertekelesList.Select(x => x.Id).Distinct()); var mobileNotificationMessages = new List(); ertekelesList.ForEach(item => ProcessErtekelesNotification(item.Data, item.Id, mobileNotificationMessages)); if (mobileNotificationMessages.Count > 0) { MobileNotificationMessageHelper.PostStudentNotification(mobileNotificationMessages.ToArray()); } } }); } catch (Exception ex) { logger.Fatal(ex, ex.GetType().FullName); } } private static void ProcessErtekelesNotification(DataRow row, int ertekelesId, List mobileNotificationMessages) { if (!int.TryParse(row["TanuloId"] == DBNull.Value ? string.Empty : row["TanuloId"].ToString(), out int tanuloId)) { return; } Guid.TryParse(row["IdpUniqueId"] == DBNull.Value ? string.Empty : row["IdpUniqueId"].ToString(), out Guid idpUniqueId); int.TryParse(row["ErtekelesOsztalyzatId"] == DBNull.Value ? string.Empty : row["ErtekelesOsztalyzatId"].ToString(), out int ertekelesOsztalyzatId); var ertekelesSzazalek = row["ErtekelesSzazalek"] == DBNull.Value ? null : row["ErtekelesSzazalek"].ToString(); var osztalyzatSzoveg = row["Osztalyzat"] == DBNull.Value ? string.Empty : row["Osztalyzat"].ToString(); var ertekelesSzoveg = row["ErtekelesSzoveg"] == DBNull.Value ? string.Empty : row["ErtekelesSzoveg"].ToString(); var instituteCode = row["IntezmenyAzonosito"] == DBNull.Value ? string.Empty : row["IntezmenyAzonosito"].ToString(); var tanuloNev = row["TanuloNev"] == DBNull.Value ? string.Empty : row["TanuloNev"].ToString(); var tantargyNev = row["TantargyNev"] == DBNull.Value ? string.Empty : row["TantargyNev"].ToString(); var ertekelesTipusId = row["ErtekelesTipusId"]; var ertekelesTipus = Enum.GetName(typeof(ErtekelesTipusEnum), ertekelesTipusId); var data = new { ErtekelesTipus = ertekelesTipus }; var message = CreateErtekelesMessage(tanuloNev, tantargyNev, ertekelesOsztalyzatId, ertekelesSzazalek, ertekelesSzoveg, osztalyzatSzoveg); if (string.IsNullOrWhiteSpace(message)) { return; } mobileNotificationMessages.Add(MobileNotificationMessageHelper.CreateMessage(instituteCode, tanuloId, MobileNotificationMessageType.Evaluation, ertekelesId, message, NotificationResource.ErtekelesTitle, JsonConvert.SerializeObject(data))); } private static string CreateErtekelesMessage(string tanuloNev, string tantargyNev, int ertekelesOsztalyzatId, string ertekelesSzazalek, string ertekelesSzoveg, string osztalyzatSzoveg) { if (ertekelesOsztalyzatId.IsEntityId()) { return string.Format(NotificationResource.ErdemjegyErtekelesNotificationMessage, tanuloNev, osztalyzatSzoveg, tantargyNev); } if (!string.IsNullOrWhiteSpace(ertekelesSzoveg)) { return string.Format(NotificationResource.SzovegesErtekelesNotificationMessage, tanuloNev, tantargyNev); } if (!string.IsNullOrWhiteSpace(ertekelesSzazalek)) { return string.Format(NotificationResource.SzazalekosErtekelesNotificationMessage, tanuloNev, ertekelesSzazalek, tantargyNev); } return string.Empty; } } }