| | | 1 | | using FAU.Entidades; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace FAU.DataAccess.Repositories; |
| | | 5 | | |
| | | 6 | | public class BeneficioSocialRepository : IBeneficioSocialRepository |
| | | 7 | | { |
| | | 8 | | private readonly ApplicationDbContext _context; |
| | | 9 | | |
| | 18 | 10 | | public BeneficioSocialRepository(ApplicationDbContext context) |
| | | 11 | | { |
| | 18 | 12 | | _context = context; |
| | 18 | 13 | | } |
| | | 14 | | |
| | | 15 | | public async Task<BeneficioSocial?> ObtenerPorIdAsync(long id) |
| | | 16 | | { |
| | 3 | 17 | | return await _context.BeneficiosSociales |
| | 3 | 18 | | .Include(b => b.TipoBeneficio) |
| | 3 | 19 | | .Include(b => b.Persona) |
| | 3 | 20 | | .FirstOrDefaultAsync(b => b.Id == id); |
| | 3 | 21 | | } |
| | | 22 | | |
| | | 23 | | public async Task<(IEnumerable<BeneficioSocial> Items, int TotalCount)> ObtenerPaginadoAsync( |
| | | 24 | | int pagina, |
| | | 25 | | int tamano, |
| | | 26 | | long? personaId = null, |
| | | 27 | | string? estado = null) |
| | | 28 | | { |
| | 3 | 29 | | var query = _context.BeneficiosSociales |
| | 3 | 30 | | .Include(b => b.TipoBeneficio) |
| | 3 | 31 | | .Include(b => b.Persona) |
| | 3 | 32 | | .AsQueryable(); |
| | | 33 | | |
| | 3 | 34 | | if (personaId.HasValue) |
| | | 35 | | { |
| | 1 | 36 | | query = query.Where(b => b.PersonaId == personaId.Value); |
| | | 37 | | } |
| | | 38 | | |
| | 3 | 39 | | if (!string.IsNullOrWhiteSpace(estado)) |
| | | 40 | | { |
| | 1 | 41 | | var estadoNormalizado = estado.Trim().ToLowerInvariant(); |
| | 1 | 42 | | query = query.Where(b => b.Estado.ToLower() == estadoNormalizado); |
| | | 43 | | } |
| | | 44 | | |
| | 3 | 45 | | var total = await query.CountAsync(); |
| | | 46 | | |
| | 3 | 47 | | var items = await query |
| | 3 | 48 | | .OrderByDescending(b => b.VigenteDesde) |
| | 3 | 49 | | .ThenByDescending(b => b.Id) |
| | 3 | 50 | | .Skip((pagina - 1) * tamano) |
| | 3 | 51 | | .Take(tamano) |
| | 3 | 52 | | .ToListAsync(); |
| | | 53 | | |
| | 3 | 54 | | return (items, total); |
| | 3 | 55 | | } |
| | | 56 | | |
| | | 57 | | public async Task<BeneficioSocial> CrearAsync(BeneficioSocial beneficioSocial) |
| | | 58 | | { |
| | 3 | 59 | | _context.BeneficiosSociales.Add(beneficioSocial); |
| | 3 | 60 | | await _context.SaveChangesAsync(); |
| | 3 | 61 | | return beneficioSocial; |
| | 3 | 62 | | } |
| | | 63 | | |
| | | 64 | | public async Task<BeneficioSocial> ActualizarAsync(BeneficioSocial beneficioSocial) |
| | | 65 | | { |
| | | 66 | | // La entidad ya viene trackeada desde ObtenerPorIdAsync dentro del mismo scope. |
| | | 67 | | // Evitamos Update() para no propagar cambios al grafo de navegación. |
| | 1 | 68 | | await _context.SaveChangesAsync(); |
| | 1 | 69 | | return beneficioSocial; |
| | 1 | 70 | | } |
| | | 71 | | |
| | | 72 | | public async Task<bool> ExistePersonaActivaAsync(long personaId, DateTime fechaReferencia) |
| | | 73 | | { |
| | 2 | 74 | | var fecha = fechaReferencia.Date; |
| | | 75 | | |
| | 2 | 76 | | return await _context.RelacionesLaborales.AnyAsync(rl => |
| | 2 | 77 | | rl.PersonaId == personaId |
| | 2 | 78 | | && rl.Estado == EstadoRelacion.Activo |
| | 2 | 79 | | && rl.FechaInicio.Date <= fecha |
| | 2 | 80 | | && (!rl.FechaFin.HasValue || rl.FechaFin.Value.Date >= fecha)); |
| | 2 | 81 | | } |
| | | 82 | | |
| | | 83 | | public async Task<bool> ExisteTipoBeneficioVigenteAsync(long tipoBeneficioId) |
| | | 84 | | { |
| | 3 | 85 | | return await _context.TiposBeneficio.AnyAsync(tb => tb.Id == tipoBeneficioId && tb.Activo); |
| | 3 | 86 | | } |
| | | 87 | | |
| | | 88 | | public async Task<TipoBeneficio?> ObtenerTipoBeneficioAsync(long tipoBeneficioId) |
| | | 89 | | { |
| | 2 | 90 | | return await _context.TiposBeneficio.FirstOrDefaultAsync(tb => tb.Id == tipoBeneficioId); |
| | 2 | 91 | | } |
| | | 92 | | |
| | | 93 | | public async Task<bool> ExisteDuplicadoEventoAsync(long personaId, long tipoBeneficioId, string claveEvento, long? e |
| | | 94 | | { |
| | 4 | 95 | | var query = _context.BeneficiosSociales.Where(b => |
| | 4 | 96 | | b.PersonaId == personaId |
| | 4 | 97 | | && b.TipoBeneficioId == tipoBeneficioId |
| | 4 | 98 | | && b.ClaveEvento == claveEvento |
| | 4 | 99 | | && b.Estado != "inactivo"); |
| | | 100 | | |
| | 4 | 101 | | if (excluirId.HasValue) |
| | 1 | 102 | | query = query.Where(b => b.Id != excluirId.Value); |
| | | 103 | | |
| | 4 | 104 | | return await query.AnyAsync(); |
| | 4 | 105 | | } |
| | | 106 | | |
| | | 107 | | public async Task<bool> ExisteBeneficioActivoMismoTipoAsync(long personaId, long tipoBeneficioId, long? excluirId = |
| | | 108 | | { |
| | 0 | 109 | | var query = _context.BeneficiosSociales.Where(b => |
| | 0 | 110 | | b.PersonaId == personaId |
| | 0 | 111 | | && b.TipoBeneficioId == tipoBeneficioId |
| | 0 | 112 | | && b.Estado != "inactivo"); |
| | | 113 | | |
| | 0 | 114 | | if (excluirId.HasValue) |
| | 0 | 115 | | query = query.Where(b => b.Id != excluirId.Value); |
| | | 116 | | |
| | 0 | 117 | | return await query.AnyAsync(); |
| | 0 | 118 | | } |
| | | 119 | | } |