| | | 1 | | using FAU.DataAccess.Repositories; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | using FAU.Logica.DTOs; |
| | | 4 | | |
| | | 5 | | namespace FAU.Logica.Services; |
| | | 6 | | |
| | | 7 | | public class EscalafonService : IEscalafonService |
| | | 8 | | { |
| | | 9 | | private readonly IEscalafonRepository _escalafonRepository; |
| | | 10 | | private readonly IAuditoriaService _auditoriaService; |
| | | 11 | | private readonly ICurrentUserContext _currentUser; |
| | | 12 | | |
| | 7 | 13 | | public EscalafonService(IEscalafonRepository escalafonRepository, IAuditoriaService auditoriaService, ICurrentUserCo |
| | | 14 | | { |
| | 7 | 15 | | _escalafonRepository = escalafonRepository; |
| | 7 | 16 | | _auditoriaService = auditoriaService; |
| | 7 | 17 | | _currentUser = currentUser; |
| | 7 | 18 | | } |
| | | 19 | | |
| | | 20 | | public async Task<IEnumerable<EscalafonDto>> ObtenerTodosAsync() |
| | | 21 | | { |
| | 0 | 22 | | var escalafones = await _escalafonRepository.ObtenerTodosAsync(); |
| | 0 | 23 | | return escalafones.Select(e => new EscalafonDto { Id = e.Id, Codigo = e.Codigo, Denominacion = e.Denominacion }) |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | public async Task<(bool Exito, EscalafonDto? Resultado, string? Error)> CrearAsync(string codigo, string denominacio |
| | | 27 | | { |
| | | 28 | | try |
| | | 29 | | { |
| | 4 | 30 | | if (string.IsNullOrWhiteSpace(codigo)) |
| | 1 | 31 | | return (false, null, "El código es requerido"); |
| | | 32 | | |
| | 3 | 33 | | if (string.IsNullOrWhiteSpace(denominacion)) |
| | 1 | 34 | | return (false, null, "La denominación es requerida"); |
| | | 35 | | |
| | 2 | 36 | | if (await _escalafonRepository.ExisteCodigoAsync(codigo.Trim())) |
| | 1 | 37 | | return (false, null, "Ya existe un escalafón con ese código"); |
| | | 38 | | |
| | 1 | 39 | | var nuevo = new Escalafon { Codigo = codigo.Trim(), Denominacion = denominacion.Trim(), Vigente = vigente }; |
| | 1 | 40 | | var creado = await _escalafonRepository.CrearAsync(nuevo); |
| | | 41 | | |
| | 1 | 42 | | await _auditoriaService.LogAuditoriaAsync( |
| | 1 | 43 | | usuarioId: _currentUser.UserId ?? 0, |
| | 1 | 44 | | AccionEnum.CrearPersonal, |
| | 1 | 45 | | ContextoEnum.Personal, |
| | 1 | 46 | | host: _currentUser.Host, |
| | 1 | 47 | | entidad: nameof(Escalafon), |
| | 1 | 48 | | entidadId: creado.Id, |
| | 1 | 49 | | detalle: new { operacion = "crear", id = creado.Id, codigo = creado.Codigo }); |
| | | 50 | | |
| | 1 | 51 | | return (true, new EscalafonDto { Id = creado.Id, Codigo = creado.Codigo, Denominacion = creado.Denominacion |
| | | 52 | | } |
| | 0 | 53 | | catch (Exception ex) |
| | | 54 | | { |
| | 0 | 55 | | return (false, null, $"Error al crear escalafón: {ex.Message}"); |
| | | 56 | | } |
| | 4 | 57 | | } |
| | | 58 | | |
| | | 59 | | public async Task<(bool Exito, EscalafonDto? Resultado, string? Error)> ActualizarAsync(long id, string? codigo, str |
| | | 60 | | { |
| | | 61 | | try |
| | | 62 | | { |
| | 3 | 63 | | var escalafon = await _escalafonRepository.ObtenerPorIdAsync(id); |
| | 3 | 64 | | if (escalafon == null) |
| | 1 | 65 | | return (false, null, "Escalafón no encontrado"); |
| | | 66 | | |
| | 2 | 67 | | var snapshotAntes = new { id = escalafon.Id, codigo = escalafon.Codigo, denominacion = escalafon.Denominacio |
| | | 68 | | |
| | 2 | 69 | | if (!string.IsNullOrWhiteSpace(codigo)) |
| | | 70 | | { |
| | 1 | 71 | | var codigoNormalizado = codigo.Trim(); |
| | 1 | 72 | | if (!string.Equals(escalafon.Codigo, codigoNormalizado, StringComparison.Ordinal) && await _escalafonRep |
| | 1 | 73 | | return (false, null, "Ya existe un escalafón con ese código"); |
| | 0 | 74 | | escalafon.Codigo = codigoNormalizado; |
| | 0 | 75 | | } |
| | | 76 | | |
| | 1 | 77 | | if (!string.IsNullOrWhiteSpace(denominacion)) |
| | 1 | 78 | | escalafon.Denominacion = denominacion.Trim(); |
| | | 79 | | |
| | 1 | 80 | | if (vigente.HasValue) |
| | 0 | 81 | | escalafon.Vigente = vigente.Value; |
| | | 82 | | |
| | 1 | 83 | | var actualizado = await _escalafonRepository.ActualizarAsync(escalafon); |
| | | 84 | | |
| | 1 | 85 | | await _auditoriaService.LogAuditoriaAsync( |
| | 1 | 86 | | usuarioId: _currentUser.UserId ?? 0, |
| | 1 | 87 | | AccionEnum.ActualizarPersonal, |
| | 1 | 88 | | ContextoEnum.Personal, |
| | 1 | 89 | | host: _currentUser.Host, |
| | 1 | 90 | | entidad: nameof(Escalafon), |
| | 1 | 91 | | entidadId: actualizado.Id, |
| | 1 | 92 | | detalle: new { operacion = "actualizar", antes = snapshotAntes, despues = new { id = actualizado.Id, cod |
| | | 93 | | |
| | 1 | 94 | | return (true, new EscalafonDto { Id = actualizado.Id, Codigo = actualizado.Codigo, Denominacion = actualizad |
| | | 95 | | } |
| | 0 | 96 | | catch (Exception ex) |
| | | 97 | | { |
| | 0 | 98 | | return (false, null, $"Error al actualizar escalafón: {ex.Message}"); |
| | | 99 | | } |
| | 3 | 100 | | } |
| | | 101 | | } |