| | | 1 | | using FAU.DataAccess.Repositories; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | using FAU.Logica.DTOs; |
| | | 4 | | |
| | | 5 | | namespace FAU.Logica.Services; |
| | | 6 | | |
| | | 7 | | public class GradoService : IGradoService |
| | | 8 | | { |
| | | 9 | | private readonly IGradoRepository _gradoRepository; |
| | | 10 | | private readonly IEscalafonRepository _escalafonRepository; |
| | | 11 | | private readonly IAuditoriaService _auditoriaService; |
| | | 12 | | private readonly ICurrentUserContext _currentUser; |
| | | 13 | | |
| | 6 | 14 | | public GradoService(IGradoRepository gradoRepository, IEscalafonRepository escalafonRepository, IAuditoriaService au |
| | | 15 | | { |
| | 6 | 16 | | _gradoRepository = gradoRepository; |
| | 6 | 17 | | _escalafonRepository = escalafonRepository; |
| | 6 | 18 | | _auditoriaService = auditoriaService; |
| | 6 | 19 | | _currentUser = currentUser; |
| | 6 | 20 | | } |
| | | 21 | | |
| | | 22 | | public async Task<IEnumerable<GradoDto>> ObtenerTodosAsync() |
| | | 23 | | { |
| | 0 | 24 | | var grados = await _gradoRepository.ObtenerTodosAsync(); |
| | 0 | 25 | | return grados.Select(g => new GradoDto { Id = g.Id, Codigo = g.Codigo, Denominacion = g.Denominacion, EscalafonI |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | public async Task<(bool Exito, GradoDto? Resultado, string? Error)> CrearAsync(string codigo, string denominacion, s |
| | | 29 | | { |
| | | 30 | | try |
| | | 31 | | { |
| | 4 | 32 | | if (string.IsNullOrWhiteSpace(codigo)) |
| | 1 | 33 | | return (false, null, "El código es requerido"); |
| | | 34 | | |
| | 3 | 35 | | if (string.IsNullOrWhiteSpace(denominacion)) |
| | 0 | 36 | | return (false, null, "La denominación es requerida"); |
| | | 37 | | |
| | 3 | 38 | | if (await _gradoRepository.ExisteCodigoAsync(codigo.Trim())) |
| | 1 | 39 | | return (false, null, "Ya existe un grado con ese código"); |
| | | 40 | | |
| | 2 | 41 | | if (escalafonId.HasValue) |
| | | 42 | | { |
| | 2 | 43 | | var escalafon = await _escalafonRepository.ObtenerPorIdAsync(escalafonId.Value); |
| | 2 | 44 | | if (escalafon == null || !escalafon.Vigente) |
| | 1 | 45 | | return (false, null, "El escalafón no existe o no está vigente"); |
| | | 46 | | } |
| | | 47 | | |
| | 1 | 48 | | var nuevo = new Grado |
| | 1 | 49 | | { |
| | 1 | 50 | | Codigo = codigo.Trim(), |
| | 1 | 51 | | Denominacion = denominacion.Trim(), |
| | 1 | 52 | | Orden = orden, |
| | 1 | 53 | | EscalafonId = escalafonId, |
| | 1 | 54 | | EsOficial = esOficial, |
| | 1 | 55 | | EsSubalterno = esSubalterno, |
| | 1 | 56 | | Vigente = vigente |
| | 1 | 57 | | }; |
| | | 58 | | |
| | 1 | 59 | | var creado = await _gradoRepository.CrearAsync(nuevo); |
| | | 60 | | |
| | 1 | 61 | | await _auditoriaService.LogAuditoriaAsync( |
| | 1 | 62 | | usuarioId: _currentUser.UserId ?? 0, |
| | 1 | 63 | | AccionEnum.CrearPersonal, |
| | 1 | 64 | | ContextoEnum.Personal, |
| | 1 | 65 | | host: _currentUser.Host, |
| | 1 | 66 | | entidad: nameof(Grado), |
| | 1 | 67 | | entidadId: creado.Id, |
| | 1 | 68 | | detalle: new { operacion = "crear", id = creado.Id, codigo = creado.Codigo }); |
| | | 69 | | |
| | 1 | 70 | | return (true, new GradoDto { Id = creado.Id, Codigo = creado.Codigo, Denominacion = creado.Denominacion, Esc |
| | | 71 | | } |
| | 0 | 72 | | catch (Exception ex) |
| | | 73 | | { |
| | 0 | 74 | | return (false, null, $"Error al crear grado: {ex.Message}"); |
| | | 75 | | } |
| | 4 | 76 | | } |
| | | 77 | | |
| | | 78 | | public async Task<(bool Exito, GradoDto? Resultado, string? Error)> ActualizarAsync(long id, string? codigo, string? |
| | | 79 | | { |
| | | 80 | | try |
| | | 81 | | { |
| | 2 | 82 | | var grado = await _gradoRepository.ObtenerPorIdAsync(id); |
| | 2 | 83 | | if (grado == null) |
| | 1 | 84 | | return (false, null, "Grado no encontrado"); |
| | | 85 | | |
| | 1 | 86 | | var snapshotAntes = new { id = grado.Id, codigo = grado.Codigo }; |
| | | 87 | | |
| | 1 | 88 | | if (!string.IsNullOrWhiteSpace(codigo)) |
| | | 89 | | { |
| | 0 | 90 | | var codigoNormalizado = codigo.Trim(); |
| | 0 | 91 | | if (!string.Equals(grado.Codigo, codigoNormalizado, StringComparison.Ordinal) && await _gradoRepository. |
| | 0 | 92 | | return (false, null, "Ya existe un grado con ese código"); |
| | 0 | 93 | | grado.Codigo = codigoNormalizado; |
| | 0 | 94 | | } |
| | | 95 | | |
| | 1 | 96 | | if (!string.IsNullOrWhiteSpace(denominacion)) |
| | 1 | 97 | | grado.Denominacion = denominacion.Trim(); |
| | | 98 | | |
| | 1 | 99 | | if (orden.HasValue) |
| | 0 | 100 | | grado.Orden = orden.Value; |
| | | 101 | | |
| | 1 | 102 | | if (escalafonId.HasValue) |
| | | 103 | | { |
| | 0 | 104 | | var escalafon = await _escalafonRepository.ObtenerPorIdAsync(escalafonId.Value); |
| | 0 | 105 | | if (escalafon == null || !escalafon.Vigente) |
| | 0 | 106 | | return (false, null, "El escalafón no existe o no está vigente"); |
| | 0 | 107 | | grado.EscalafonId = escalafonId.Value; |
| | | 108 | | } |
| | | 109 | | |
| | 1 | 110 | | if (esOficial.HasValue) |
| | 0 | 111 | | grado.EsOficial = esOficial.Value; |
| | | 112 | | |
| | 1 | 113 | | if (esSubalterno.HasValue) |
| | 0 | 114 | | grado.EsSubalterno = esSubalterno.Value; |
| | | 115 | | |
| | 1 | 116 | | if (vigente.HasValue) |
| | 0 | 117 | | grado.Vigente = vigente.Value; |
| | | 118 | | |
| | 1 | 119 | | var actualizado = await _gradoRepository.ActualizarAsync(grado); |
| | | 120 | | |
| | 1 | 121 | | await _auditoriaService.LogAuditoriaAsync( |
| | 1 | 122 | | usuarioId: _currentUser.UserId ?? 0, |
| | 1 | 123 | | AccionEnum.ActualizarPersonal, |
| | 1 | 124 | | ContextoEnum.Personal, |
| | 1 | 125 | | host: _currentUser.Host, |
| | 1 | 126 | | entidad: nameof(Grado), |
| | 1 | 127 | | entidadId: actualizado.Id, |
| | 1 | 128 | | detalle: new { operacion = "actualizar", antes = snapshotAntes, despues = new { id = actualizado.Id, cod |
| | | 129 | | |
| | 1 | 130 | | return (true, new GradoDto { Id = actualizado.Id, Codigo = actualizado.Codigo, Denominacion = actualizado.De |
| | | 131 | | } |
| | 0 | 132 | | catch (Exception ex) |
| | | 133 | | { |
| | 0 | 134 | | return (false, null, $"Error al actualizar grado: {ex.Message}"); |
| | | 135 | | } |
| | 2 | 136 | | } |
| | | 137 | | } |