| | | 1 | | using FAU.DataAccess.Repositories; |
| | | 2 | | using FAU.Entidades; |
| | | 3 | | |
| | | 4 | | namespace FAU.Logica.Services; |
| | | 5 | | |
| | | 6 | | public class Form3100Service : IForm3100Service |
| | | 7 | | { |
| | | 8 | | private readonly IForm3100Repository _repo; |
| | | 9 | | private readonly IPersonalRepository _personalRepo; |
| | | 10 | | private readonly IDocumentoRepository _docRepo; |
| | | 11 | | private readonly IStorageService _storage; |
| | | 12 | | private readonly IPeriodoGuard _periodoGuard; |
| | | 13 | | |
| | 11 | 14 | | public Form3100Service( |
| | 11 | 15 | | IForm3100Repository repo, |
| | 11 | 16 | | IPersonalRepository personalRepo, |
| | 11 | 17 | | IDocumentoRepository docRepo, |
| | 11 | 18 | | IStorageService storage, |
| | 11 | 19 | | IPeriodoGuard periodoGuard) |
| | | 20 | | { |
| | 11 | 21 | | _repo = repo; |
| | 11 | 22 | | _personalRepo = personalRepo; |
| | 11 | 23 | | _docRepo = docRepo; |
| | 11 | 24 | | _storage = storage; |
| | 11 | 25 | | _periodoGuard = periodoGuard; |
| | 11 | 26 | | } |
| | | 27 | | |
| | | 28 | | public async Task<Form3100> CrearAsync(string cedulaTitular, Form3100 form, long usuarioId) |
| | | 29 | | { |
| | 3 | 30 | | await _periodoGuard.VerificarPeriodoAbiertoAsync(); |
| | 3 | 31 | | var relacion = await _personalRepo.GetByCedulaAsync(cedulaTitular) |
| | 3 | 32 | | ?? throw new ArgumentException($"Funcionario con cédula {cedulaTitular} no encontrado."); |
| | | 33 | | |
| | 2 | 34 | | var personaId = relacion.PersonaId; |
| | | 35 | | |
| | 2 | 36 | | if (await _repo.ExisteVigenciaAsync(personaId, form.VigenteDesde_Anio, form.VigenteDesde_Mes)) |
| | 1 | 37 | | throw new InvalidOperationException( |
| | 1 | 38 | | $"Ya existe un formulario para {cedulaTitular} con vigencia {form.VigenteDesde_Mes:D2}/{form.VigenteDesd |
| | | 39 | | |
| | 1 | 40 | | form.PersonaId = personaId; |
| | 1 | 41 | | form.UsuarioCreacionId = usuarioId; |
| | 1 | 42 | | form.FechaCreacion = DateTime.Now; |
| | | 43 | | |
| | 1 | 44 | | return await _repo.CreateAsync(form); |
| | 1 | 45 | | } |
| | | 46 | | |
| | | 47 | | public async Task<IEnumerable<Form3100>> GetHistorialAsync(string cedulaTitular, int? anio = null, int? mes = null) |
| | | 48 | | { |
| | 2 | 49 | | var relacion = await _personalRepo.GetByCedulaAsync(cedulaTitular); |
| | 3 | 50 | | if (relacion == null) return Enumerable.Empty<Form3100>(); |
| | 1 | 51 | | return await _repo.GetHistorialAsync(relacion.PersonaId, anio, mes); |
| | 2 | 52 | | } |
| | | 53 | | |
| | | 54 | | public async Task<Form3100?> GetVigenteParaPeriodoAsync(string cedulaTitular, int anio, int mes) |
| | | 55 | | { |
| | 2 | 56 | | var relacion = await _personalRepo.GetByCedulaAsync(cedulaTitular); |
| | 3 | 57 | | if (relacion == null) return null; |
| | 1 | 58 | | return await _repo.GetVigenteParaPeriodoAsync(relacion.PersonaId, anio, mes); |
| | 2 | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | public Task<Form3100?> GetByIdAsync(long id) => _repo.GetByIdAsync(id); |
| | | 62 | | |
| | | 63 | | public async Task<Form3100PersonaACargo> AgregarPersonaACargoAsync(long form3100Id, Form3100PersonaACargo persona) |
| | | 64 | | { |
| | 2 | 65 | | await _periodoGuard.VerificarPeriodoAbiertoAsync(); |
| | 2 | 66 | | var form = await _repo.GetByIdAsync(form3100Id) |
| | 2 | 67 | | ?? throw new ArgumentException($"Form3100 {form3100Id} no encontrado."); |
| | | 68 | | |
| | 2 | 69 | | if (!new[] { 0, 50, 100 }.Contains((int)persona.PorcentajeAtribucion)) |
| | 1 | 70 | | throw new ArgumentException("PorcentajeAtribucion debe ser 0, 50 o 100."); |
| | | 71 | | |
| | 1 | 72 | | persona.Form3100Id = form3100Id; |
| | 1 | 73 | | return await _repo.AddPersonaACargoAsync(persona); |
| | 1 | 74 | | } |
| | | 75 | | |
| | | 76 | | public async Task EliminarPersonaACargoAsync(long form3100Id, long personaCargoId) |
| | | 77 | | { |
| | 2 | 78 | | await _periodoGuard.VerificarPeriodoAbiertoAsync(); |
| | 2 | 79 | | var p = await _repo.GetPersonaACargoAsync(personaCargoId) |
| | 2 | 80 | | ?? throw new ArgumentException($"Persona a cargo {personaCargoId} no encontrada."); |
| | | 81 | | |
| | 2 | 82 | | if (p.Form3100Id != form3100Id) |
| | 1 | 83 | | throw new ArgumentException("La persona a cargo no pertenece al formulario indicado."); |
| | | 84 | | |
| | 1 | 85 | | await _repo.DeletePersonaACargoAsync(personaCargoId); |
| | 1 | 86 | | } |
| | | 87 | | |
| | | 88 | | public async Task<string> SubirArchivoAsync(long form3100Id, Stream contenido, string nombreOriginal, string content |
| | | 89 | | { |
| | 0 | 90 | | await _periodoGuard.VerificarPeriodoAbiertoAsync(); |
| | 0 | 91 | | var form = await _repo.GetByIdAsync(form3100Id) |
| | 0 | 92 | | ?? throw new ArgumentException($"Form3100 {form3100Id} no encontrado."); |
| | | 93 | | |
| | 0 | 94 | | var nombreSanitizado = SanitizarNombre(nombreOriginal); |
| | 0 | 95 | | var rutaRelativa = $"form3100/{form3100Id}/{nombreSanitizado}"; |
| | | 96 | | |
| | | 97 | | // Eliminar archivo anterior si existe |
| | 0 | 98 | | if (form.DocumentoId.HasValue) |
| | | 99 | | { |
| | 0 | 100 | | var docAnterior = await _docRepo.GetByIdAsync(form.DocumentoId.Value); |
| | 0 | 101 | | if (docAnterior != null) |
| | | 102 | | { |
| | 0 | 103 | | await _storage.EliminarAsync(docAnterior.RutaRelativa); |
| | 0 | 104 | | await _docRepo.DeleteAsync(docAnterior.Id); |
| | | 105 | | } |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | // Obtener tamaño |
| | 0 | 109 | | var tamanio = contenido.CanSeek ? contenido.Length : 0; |
| | | 110 | | |
| | 0 | 111 | | await _storage.GuardarAsync(contenido, rutaRelativa, contentType); |
| | | 112 | | |
| | 0 | 113 | | var documento = await _docRepo.CreateAsync(new Documento |
| | 0 | 114 | | { |
| | 0 | 115 | | Tipo = "form3100", |
| | 0 | 116 | | EntidadId = form3100Id, |
| | 0 | 117 | | RutaRelativa = rutaRelativa, |
| | 0 | 118 | | NombreOriginal = nombreOriginal, |
| | 0 | 119 | | ContentType = contentType, |
| | 0 | 120 | | TamanioBytes = tamanio, |
| | 0 | 121 | | UsuarioCargaId = usuarioId |
| | 0 | 122 | | }); |
| | | 123 | | |
| | 0 | 124 | | form.DocumentoId = documento.Id; |
| | 0 | 125 | | await _repo.UpdateAsync(form); |
| | | 126 | | |
| | 0 | 127 | | return rutaRelativa; |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | public async Task<(Stream Contenido, string NombreOriginal, string ContentType)> ObtenerArchivoAsync(long form3100Id |
| | | 131 | | { |
| | 0 | 132 | | var form = await _repo.GetByIdAsync(form3100Id) |
| | 0 | 133 | | ?? throw new ArgumentException($"Form3100 {form3100Id} no encontrado."); |
| | | 134 | | |
| | 0 | 135 | | if (!form.DocumentoId.HasValue || form.Documento == null) |
| | 0 | 136 | | throw new KeyNotFoundException($"Form3100 {form3100Id} no tiene archivo adjunto."); |
| | | 137 | | |
| | 0 | 138 | | var stream = await _storage.ObtenerAsync(form.Documento.RutaRelativa); |
| | 0 | 139 | | return (stream, form.Documento.NombreOriginal, form.Documento.ContentType); |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | private static string SanitizarNombre(string nombre) |
| | | 143 | | { |
| | 0 | 144 | | var invalidos = Path.GetInvalidFileNameChars(); |
| | 0 | 145 | | var limpio = string.Concat(nombre.Select(c => invalidos.Contains(c) ? '_' : c)); |
| | 0 | 146 | | return limpio.Replace(' ', '_').ToLower(); |
| | | 147 | | } |
| | | 148 | | } |