diff --git a/Contracts/Utils/IFileManagerRepository.cs b/Contracts/Utils/IFileManagerRepository.cs index 236cd1c..8068a2f 100644 --- a/Contracts/Utils/IFileManagerRepository.cs +++ b/Contracts/Utils/IFileManagerRepository.cs @@ -12,5 +12,6 @@ namespace CORRESPONSALBackend.Contracts.Utils public Task UpdateFileNameById(long id, string newFileName); public Task> GetFilesByProcessId(int process); public Task UpdateFileProcessById(long fileId, int process); + public Task> GetTraficoFiles(int idTrafico); } } \ No newline at end of file diff --git a/Controllers/FileManagerController.cs b/Controllers/FileManagerController.cs index de0c4e6..77713b1 100644 --- a/Controllers/FileManagerController.cs +++ b/Controllers/FileManagerController.cs @@ -430,5 +430,16 @@ namespace CORRESPONSALBackend.Controllers return BadRequest(ex.Message); } } + + [HttpGet("FileListByReferencia")] + public async Task GeFileListByReferencia([FromQuery] string referencia){ + try{ + var trafico = await _traficosRepository.GetByReferencia(referencia); + var fileList = await _Repo.GetTraficoFiles(trafico.id); + return Ok(fileList); + }catch(Exception ex){ + return BadRequest(ex.Message); + } + } } } \ No newline at end of file diff --git a/Controllers/Utils/ArchivoElectronicoController.cs b/Controllers/Utils/ArchivoElectronicoController.cs index b07d8a4..41c05e0 100644 --- a/Controllers/Utils/ArchivoElectronicoController.cs +++ b/Controllers/Utils/ArchivoElectronicoController.cs @@ -1,5 +1,9 @@ using CORRESPONSALBackend.Contracts.ArchivoElectronico; +using CORRESPONSALBackend.Contracts.Corresponsalias; +using CORRESPONSALBackend.Contracts.Utils; using CORRESPONSALBackend.DTO.ArchivoElectronico; +using CORRESPONSALBackend.Models.Utils; +using CORRESPONSALBackend.Repository.Utils; using GEMCO.Backend.Models.Reportes; using Microsoft.AspNetCore.Mvc; @@ -9,8 +13,20 @@ namespace CORRESPONSALBackend.Controllers.Utils{ public class ArchivoElectronicoController : ControllerBase { private readonly IArchivoElectronicoRepository _AERepo; - public ArchivoElectronicoController(IArchivoElectronicoRepository AERepo){ + private readonly IConfiguration _config; + private readonly IFilePaths4ProcessRepository _RepoRelativePath; + private readonly string RootPathCorresponsales; + private readonly ICorresponsaliasTraficosRepository _traficosRepository; + private readonly IFileManagerRepository _fileManagerRepo; + public ArchivoElectronicoController(IArchivoElectronicoRepository AERepo, IConfiguration config, + ICorresponsaliasTraficosRepository traficosRepository, IFilePaths4ProcessRepository RepoRelativePath, + IFileManagerRepository fileManagerRepo){ _AERepo = AERepo; + _traficosRepository = traficosRepository; + _config = config; + _RepoRelativePath = RepoRelativePath; + _fileManagerRepo = fileManagerRepo; + RootPathCorresponsales = _config.GetValue("AllFiles"); } [HttpGet] @@ -25,10 +41,50 @@ namespace CORRESPONSALBackend.Controllers.Utils{ [Route("DownloadZippedArchivosOficiales")] public async Task DownloadZippedArchivosOficiales([FromBody] DTOAEPeriodoSeleccion data){ try{ - if(data.Referencias.Count > 0){ - + //Se obtiene la ruta donde se alojará temporalmente el zip + FilePaths4Process tempFilesRelativePath = await _RepoRelativePath.getPaths4ProcessById(45); // Path: Corresponsales\Zips\Facturacion\Entregas\ + var tempFilesPath = RootPathCorresponsales + tempFilesRelativePath.Path;// Path: C:data\Corresponsales\Zips\Clientes\ArchivosElectronicos + string tempFolderName = "ReporteArchivosElectronicos";//Nombre del folder Temporal + string tempFolderPath= tempFilesPath + $"{tempFolderName}\\";//ruta del folder temporal Path: C:data\Corresponsales\Zips\Clientes\ArchivosElectronicos\ReporteArchivosElectronicos + string zipFileName = $"{tempFolderName}.zip";//Nombre del zip final + var zipFilePath = tempFilesPath + zipFileName;//Ruta del zip final Path: C:data\Corresponsales\Zips\Clientes\ArchivosElectronicos\ReporteArchivosElectronicos.zip + + if(System.IO.File.Exists(zipFilePath)){//Si ya existe un zip de una descarga anterior lo borra + System.IO.File.Delete(zipFilePath); + } + + if(System.IO.Directory.Exists(tempFolderPath)){//Si ya existe la carpeta temporal de una descarga anterior la borra + System.IO.Directory.Delete(tempFolderPath, true); + } + + System.IO.Directory.CreateDirectory(tempFolderPath);//Se crea el folder temporal donde se pondran los archivos oficiales + foreach(var referencia in data.Referencias){ + var trafico = await _traficosRepository.GetByReferencia(referencia); + string folderAE = tempFolderPath + $"{referencia}\\";//Se obtiene el nombre de la subcarpeta del AE de cada referencia + if(System.IO.Directory.Exists(folderAE)){//Si existe la carpeta se borra + System.IO.Directory.Delete(folderAE); + } + System.IO.Directory.CreateDirectory(folderAE);//Se crea la carpeta + //Obtener la lista de archivos de la referencia actual. + var filesLog = await _fileManagerRepo.GetTraficoFiles(trafico.id); + var filteredFilesLog = filesLog.Where(x => new List(){2,10,11,12,13,14,15,19,17,18,37,38,39,41}.Contains(x.Proceso)); + foreach(var fileLog in filteredFilesLog){ + FilePaths4Process fileRelativePath = await _RepoRelativePath.getPaths4ProcessById(fileLog.Proceso);//Se obtiene la ruta relativa donde debe estar alojado el archivo + var filePath = RootPathCorresponsales + fileRelativePath.Path + fileLog!.NombreArchivo;//Ahora se obtiene la ruta completa del archivo. + if(System.IO.File.Exists(filePath)){ + var bytes = await System.IO.File.ReadAllBytesAsync(filePath); + var originalFileStream = new MemoryStream(bytes); + using(var tempFileStream = System.IO.File.Create(folderAE + fileLog.NombreArchivo)){ + await originalFileStream.CopyToAsync(tempFileStream); + } + } + } } - return Ok(); + System.IO.Compression.ZipFile.CreateFromDirectory(tempFolderPath, zipFilePath);//Se comprime la carpeta con los archivos electronicos de las referencias + Directory.Delete(tempFolderPath, true);//Se borra el folder temporal + var zipBytes = await System.IO.File.ReadAllBytesAsync(zipFilePath); + var zipStream = new MemoryStream(zipBytes); + return File(zipStream, "application/zip", zipFileName); }catch(Exception ex){ return BadRequest(ex.Message); } diff --git a/Repository/Utils/FileManagerRepository.cs b/Repository/Utils/FileManagerRepository.cs index 4248a94..9a7b861 100644 --- a/Repository/Utils/FileManagerRepository.cs +++ b/Repository/Utils/FileManagerRepository.cs @@ -122,5 +122,16 @@ namespace CORRESPONSALBackend.Repository.Utils commandType: CommandType.StoredProcedure); return entrada.FirstOrDefault(); } + + public async Task> GetTraficoFiles(int idTrafico){ + string query = "[Utils.FileManager.GetTraficoFiles]"; + using var connection = _context.CreateConnection(); + var entrada = await connection.QueryAsync(query, new + { + @idtrafico = idTrafico + }, + commandType: CommandType.StoredProcedure); + return entrada.ToList(); + } } } \ No newline at end of file