using bdAsegasa.db; using bdAsegasa.dbcontext; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using tsUtilidades; using tsUtilidades.Extensiones; namespace Servicio_Gestion_Asegasa.Procesos { public class ProcesosMensajes { private static bool ServicioSMSSuspendido = false; public static async Task EnviaSMSAsync() { if (!ServicioSMSSuspendido) { using var de = bdAsegasa.db.gestionasegasaEntities.NuevoContextoCN(); try { var mensajespendientes = de.mensajes .Where(m => m.FechaEnvio == null && m.FechaAnulacion == null) .OrderBy(m => m.FechaUltimoIntento) .Take(50) .ToList(); bool bIntentar; bool bAlmacenar = false; var cta = de.enumeraciones.First(x => x.Codigo == "CONF.CTAITSOFT"); try { foreach (var mensaje in mensajespendientes) { bIntentar = false; if (mensaje.FechaUltimoIntento == null) { bIntentar = true; } else { if ((DateTime.Now - mensaje.FechaUltimoIntento.Value).TotalSeconds > cta.ValorNumerico1 * 60 * 60) { bIntentar = true; } } if (bIntentar) { try { bAlmacenar = true; if ((DateTime.Now - mensaje.FechaCreacion.Value).TotalHours > 10) { throw new Exception("MENSAJE_ANTIGUO"); } mensaje.Cuenta = "ITSOFT"; string sRespuesta = await itsm.sms.Enviarsms(mensaje.Destinatario, mensaje.Mensaje, cta.ValorAlfabetico1); mensaje.RespuestaServicio = sRespuesta.Acortar(1024); mensaje.FechaEnvio = DateTime.Now; } catch (Exception ex) { mensaje.FechaUltimoIntento = DateTime.Now; mensaje.RespuestaServicio = ex.Message.Acortar(1024); if (ex.Message == "TELEFONO_INVALIDO") { mensaje.FechaAnulacion = DateTime.Now; } else if (ex.Message.StartsWith("El mensaje tiene más")) { mensaje.FechaAnulacion = DateTime.Now; await tsUtilidades.TsNotificacionesClient.RegistrarAsync($"El Mensaje para el destinatario {mensaje.Destinatario} ES DEMASIADO LARGO.", ex.Message, TsNotificacionesClient.TipoNotificacionEnum.ADVERTENCIA); } else if (ex.Message == "MENSAJE_ANTIGUO") { mensaje.FechaAnulacion = DateTime.Now; await tsUtilidades.TsNotificacionesClient.RegistrarAsync($"El Mensaje para el destinatario {mensaje.Destinatario} ES DEMASIADO ANTIGUO.", ex.Message, TsNotificacionesClient.TipoNotificacionEnum.ADVERTENCIA); } else { if ((DateTime.Now - mensaje.FechaCreacion.Value).TotalMinutes > cta.ValorNumerico1 * 60) { mensaje.FechaAnulacion = DateTime.Now; await tsUtilidades.TsNotificacionesClient.RegistrarAsync($"El Mensaje para el destinatario {mensaje.Destinatario} ha fallado y será marcado como anulado.", ex.Message, TsNotificacionesClient.TipoNotificacionEnum.ADVERTENCIA); } } } } } } finally { try { if (bAlmacenar) de.GuardarCambios(); } catch (Exception ex) { ServicioSMSSuspendido = true; await tsUtilidades.TsNotificacionesClient.RegistrarAsync("SERVICIO SMS SUSPENDIDO", ex.Message, TsNotificacionesClient.TipoNotificacionEnum.ERROR); } } Thread.Sleep(5000); } catch (Exception ex) { await tsUtilidades.TsNotificacionesClient.RegistrarAsync("Error", ex.Message, TsNotificacionesClient.TipoNotificacionEnum.ERROR); } } } internal static void CompruebaSMSNoEnviados() { using var bd = bdAsegasa.db.gestionasegasaEntities.NuevoContextoCN(); var fechahorainicio = DateTime.Now.AddMinutes(-180); var mensajesnoenviados = bd.mensajes.Where(x => x.FechaCreacion > fechahorainicio && x.FechaEnvio == null && x.FechaUltimoIntento != null).ToList(); if (mensajesnoenviados.Count > 0) { var ctacorreo = bd.cuentascorreo.First(x => x.Codigo == "SEG.GENERALES"); var idTipoFichero = bd.enumeraciones.First(x => x.Codigo == "TIPFIC.ADJCOR").idEnumeracion; var sAsunto = "Mensajes no enviados desde hace 2 horas"; var sCuerpo = "Advertencia: Los siguientes mensajes no han sido aún enviados desde su fecha de creación."; var sDestinatario = bd.enumeraciones.First(x => x.Codigo == "CONF.EMAILCONTA").ValorAlfabeticoLargo; var l = mensajesnoenviados.Select(x => new { idmensaje = x.idMensaje, Aplicacion = x.Aplicacion, idAplicacion = x.idAplicacion, Telefono = x.Destinatario, FechaCreacion = x.FechaCreacion, Mensaje = x.Mensaje, RespuestaServicio = x.RespuestaServicio }).ToList(); // Cambiar esta ruta estática de tsWPF si la librería se movió a tsUtilidades (ej. tsUtilidades.Excel.IEnumerableAExcel) byte[] b = tsUtilidades.Excel.IEnumerableAExcel(l); var f = new ficheros { idTipo = idTipoFichero, NombreFichero = "MensajesNoEnviados" + DateTime.Now.ToString("yyyyMMddHHmm") + ".xlsx", Descripcion = "Mensajes No Enviados " + DateTime.Now.ToString("yyyyMMddHHmm"), Fichero = b }; bd.ficheros.Add(f); bd.SaveChanges(); bdAsegasa.db.correos.GeneraRegistroCorreo(bd, sAsunto, sCuerpo, ctacorreo, sDestinatario, null, null, f.idFichero, Remitente:"sevilla@tecnosis.net"); foreach (var m in mensajesnoenviados) { m.FechaAvisoError = DateTime.Now; } bd.SaveChanges(); } } } }