| | 1 | | using Microsoft.Extensions.Logging; |
| | 2 | | using MRA.DTO.Models; |
| | 3 | | using MRA.Infrastructure.Settings; |
| | 4 | | using MRA.Services.Excel; |
| | 5 | | using MRA.Services.Excel.Interfaces; |
| | 6 | | using MRA.Services.Models.Drawings; |
| | 7 | | using MRA.Services.RemoteConfig; |
| | 8 | | using MRA.Services.Storage; |
| | 9 | | using OfficeOpenXml; |
| | 10 | |
|
| | 11 | | namespace MRA.Services.Backup.Export; |
| | 12 | |
|
| | 13 | | public class ExportService : IExportService |
| | 14 | | { |
| | 15 | | private readonly ILogger<ExportService> _logger; |
| | 16 | | private readonly IExcelService _excelService; |
| | 17 | | private readonly IStorageService _storageService; |
| | 18 | | private readonly IDrawingService _drawingService; |
| | 19 | | private readonly IAppService _appService; |
| | 20 | | private readonly AppSettings _appConfiguration; |
| | 21 | |
|
| 1 | 22 | | public ExportService( |
| 1 | 23 | | ILogger<ExportService> logger, |
| 1 | 24 | | IExcelService excelService, |
| 1 | 25 | | IStorageService storageService, |
| 1 | 26 | | IRemoteConfigService remoteConfigService, |
| 1 | 27 | | IDrawingService drawingService, |
| 1 | 28 | | IAppService appService, |
| 1 | 29 | | AppSettings appConfiguration |
| 1 | 30 | | ) |
| | 31 | | { |
| 1 | 32 | | _appConfiguration = appConfiguration; |
| 1 | 33 | | _excelService = excelService; |
| 1 | 34 | | _logger = logger; |
| 1 | 35 | | _storageService = storageService; |
| 1 | 36 | | _drawingService = drawingService; |
| 1 | 37 | | _appService = appService; |
| 1 | 38 | | } |
| | 39 | |
|
| | 40 | |
|
| | 41 | | public async Task ExportDrawings() |
| | 42 | | { |
| | 43 | | try |
| | 44 | | { |
| 0 | 45 | | _logger.LogInformation("Iniciando Aplicación de Exportación"); |
| | 46 | |
|
| | 47 | |
|
| 0 | 48 | | _logger.LogInformation("Leyendo documentos desde Firestore"); |
| | 49 | | List<DrawingModel> listDrawings; |
| 0 | 50 | | listDrawings = (await _drawingService.GetAllDrawingsAsync(onlyIfVisible: false)).ToList(); |
| | 51 | |
|
| 0 | 52 | | _logger.LogInformation("Calculando Popularidad"); |
| 0 | 53 | | listDrawings = _appService.CalculatePopularityOfListDrawings(listDrawings).ToList(); |
| | 54 | |
|
| 0 | 55 | | _logger.LogInformation("Procediendo a crear Excel"); |
| | 56 | |
|
| 0 | 57 | | using (ExcelPackage excel = new ExcelPackage()) |
| | 58 | | { |
| 0 | 59 | | _logger.LogInformation($"Creando hoja principal \"{ExcelService.EXCEL_DRAWING_SHEET_NAME}\""); |
| 0 | 60 | | var workSheet = excel.Workbook.Worksheets.Add(ExcelService.EXCEL_DRAWING_SHEET_NAME); |
| 0 | 61 | | workSheet.View.FreezePanes(2, 2); |
| | 62 | |
|
| 0 | 63 | | _logger.LogInformation("Obteniendo propiedades del DTO de Drawing"); |
| 0 | 64 | | var drawingProperties = _excelService.GetPropertiesAttributes<DrawingModel>(); |
| | 65 | |
|
| 0 | 66 | | _logger.LogInformation("Rellenando tabla principal"); |
| 0 | 67 | | _excelService.FillDrawingTable(ref workSheet, drawingProperties, listDrawings.OrderBy(x => x.Id).ToList( |
| | 68 | |
|
| 0 | 69 | | _logger.LogInformation("Preparando hojas de diccionarios"); |
| 0 | 70 | | _excelService.FillSheetsDictionary(excel, drawingProperties, workSheet); |
| | 71 | |
|
| 0 | 72 | | _logger.LogInformation("Preparando fichero para guardar en Azure Storage"); |
| 0 | 73 | | using (var memoryStream = new MemoryStream()) |
| | 74 | | { |
| 0 | 75 | | var fileName = _excelService.GetFileName(); |
| | 76 | |
|
| 0 | 77 | | await excel.SaveAsAsync(memoryStream); |
| | 78 | |
|
| 0 | 79 | | memoryStream.Position = 0; // Restablecer la posición del Stream al inicio |
| | 80 | |
|
| | 81 | | try |
| | 82 | | { |
| 0 | 83 | | var success = await _storageService.Save(memoryStream, _appConfiguration.AzureStorage.ExportLoca |
| 0 | 84 | | if (!success) |
| | 85 | | { |
| 0 | 86 | | _logger.LogError("File not saved to Storage"); |
| | 87 | | } |
| 0 | 88 | | } |
| 0 | 89 | | catch (Exception ex) |
| | 90 | | { |
| 0 | 91 | | _logger.LogError(ex, "Error al guardar el archivo '{FileName}' en Azure Storage", fileName); |
| 0 | 92 | | } |
| 0 | 93 | | } |
| 0 | 94 | | } |
| 0 | 95 | | } |
| 0 | 96 | | catch (Exception ex) |
| | 97 | | { |
| 0 | 98 | | _logger.LogError(ex, "Error durante la exportación"); |
| 0 | 99 | | } |
| 0 | 100 | | _logger.LogInformation("Fin de la Exportación en Azure Functions"); |
| 0 | 101 | | } |
| | 102 | | } |