TFS_ANTIGUO/TPAtesa_RecHuma/REC-HUMA.INFRASTRUCTURE/Repositories/NotificacionesRepo.cs
2026-06-26 10:14:39 -06:00

177 lines
5.8 KiB
C#

using Microsoft.EntityFrameworkCore;
using REC_HUMA.CORE.Entities;
using REC_HUMA.CORE.Interfaces;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
namespace REC_HUMA.INFRASTRUCTURE.Repositories
{
public class NotificacionesRepo : INotificacionesRepo
{
private readonly RECHUMAContext _Context;
public NotificacionesRepo(RECHUMAContext Context)
{
_Context = Context;
}
public Task<bool> DeleteNotificacion(int IdNotificacion)
{
throw new NotImplementedException();
}
public async Task<Notificaciones> GetNotificacion(int IdNotificacion)
{
var notificacion = await _Context.Notificaciones.FirstOrDefaultAsync(m => m.IdNotificacion == IdNotificacion);
return notificacion;
}
public async Task<ModeloCorreo> GetNotificacionEnvioCorreo(int id)
{
try
{
var notificacion = await _Context.Notificaciones
.Where(x => x.IdNotificacion == id)
.FirstAsync();
var destinatarios = await _Context.Destinatarios
.Where(x => x.IdNotificacion == notificacion.IdNotificacion)
.Include(x => x.IdPersonaNavigation).ToListAsync();
var personas = new List<Persona>();
destinatarios.ForEach(x => { personas.Add(x.IdPersonaNavigation); });
var adjuntos = new List<Adjunto>();
adjuntos = await _Context.Adjunto.Where(x => x.IdNotificacion == notificacion.IdNotificacion).ToListAsync();
var retornoCorreo = new ModeloCorreo
{
ListaPersona = personas,
ListaAdjunto = adjuntos,
Notificacion = notificacion
};
return retornoCorreo;
}
catch (Exception)
{
throw;
}
}
//public async Task<IEnumerable<ModeloCorreo>> GetNotificaciones()
//{
// try
// {
// List<ModeloCorreo> ListaCorreo = new List<ModeloCorreo>();
// var listaNotificaciones = await _Context.Notificaciones.Where(x => x.IdEstado == 1).Include(x => x.Adjunto).ToListAsync();
// foreach (var notificacion in listaNotificaciones)
// {
// var Correo = new ModeloCorreo();
// Correo.Notificacion = notificacion;
// var destinatarios = await _Context.Destinatarios.Include(d => d.IdPersonaNavigation).
// Where(d => d.IdNotificacion == notificacion.IdNotificacion).
// ToListAsync();
// var listaPersonas = new List<Persona>();
// foreach (var d in destinatarios)
// {
// Persona persona = new Persona();
// persona = d.IdPersonaNavigation;
// listaPersonas.Add(persona);
// }
// Correo.ListaPersona = listaPersonas;
// var ListaAdjunto = notificacion.Adjunto.ToList();
// Correo.ListaAdjunto = ListaAdjunto;
// ListaCorreo.Add(Correo);
// }
// //var list = await ListaNotificaones.ToListAsync();
// return ListaCorreo;
// }
// catch (Exception)
// {
// throw;
// }
//}
public async Task<int> PostNotificacion(ModeloCorreo modeloCorreo)
{
try
{
_Context.Notificaciones.Add(modeloCorreo.Notificacion);
await _Context.SaveChangesAsync();
var notificacion = new Notificaciones();
notificacion = modeloCorreo.Notificacion;
if (modeloCorreo.ListaPersona != null)
{
List<Persona> listaPersona = new List<Persona>();
listaPersona = modeloCorreo.ListaPersona;
listaPersona.ForEach(x =>
{
var destinatario = new Destinatarios();
destinatario.IdNotificacion = notificacion.IdNotificacion;
destinatario.IdPersona = x.IdPersona;
_Context.Destinatarios.Add(destinatario);
_Context.SaveChanges();
});
}
if (modeloCorreo.ListaAdjunto != null || modeloCorreo.ListaAdjunto.Count > 0)
{
List<Adjunto> listaAdjunto = new List<Adjunto>();
listaAdjunto = modeloCorreo.ListaAdjunto;
listaAdjunto.ForEach(x =>
{
var adjunto = new Adjunto();
adjunto = x;
adjunto.IdNotificacion = notificacion.IdNotificacion;
_Context.Adjunto.Add(adjunto);
_Context.SaveChanges();
});
}
return (int)modeloCorreo.Notificacion.IdNotificacion;
}
catch (Exception)
{
throw;
}
}
public async Task<bool> PutNotificacion(Notificaciones notificacion)
{
try
{
var notificacionActual = await GetNotificacion((int)notificacion.IdNotificacion);
notificacionActual.IdHabilitado = notificacion.IdHabilitado;
int rows = await _Context.SaveChangesAsync();
return rows > 0;
}
catch (Exception)
{
throw;
}
}
}
}