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