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