Fix Bancas
This commit is contained in:
		| @@ -277,21 +277,24 @@ public class LowPriorityDataWorker : BackgroundService | ||||
|  | ||||
|       _logger.LogInformation("Iniciando sondeo de Bancas a nivel Provincial y para {count} Secciones Electorales...", seccionesElectorales.Count); | ||||
|  | ||||
|       // Creamos una lista para recolectar todas las proyecciones que encontremos. | ||||
|       var nuevasProyecciones = new List<ProyeccionBanca>(); | ||||
|       // --- LÓGICA CORREGIDA --- | ||||
|       bool hasReceivedAnyNewData = false; | ||||
|       var todasLasProyecciones = new List<ProyeccionBanca>(); | ||||
|  | ||||
|       // 1. Bucle para el nivel Provincial | ||||
|       // 1. Bucle Provincial | ||||
|       foreach (var categoria in categoriasDeBancas) | ||||
|       { | ||||
|         if (stoppingToken.IsCancellationRequested) break; | ||||
|         var repartoBancas = await _apiService.GetBancasAsync(authToken, provincia.DistritoId!, null, categoria.Id); | ||||
|  | ||||
|         // Si la lista de bancas no es nula (incluso si está vacía), la procesamos. | ||||
|         if (repartoBancas?.RepartoBancas != null) | ||||
|         // Comprobamos si la respuesta no es nula y si la lista de bancas TIENE ELEMENTOS. | ||||
|         if (repartoBancas?.RepartoBancas is { Count: > 0 } bancas) | ||||
|         { | ||||
|           foreach (var banca in repartoBancas.RepartoBancas) | ||||
|           // Si encontramos datos, activamos la bandera. | ||||
|           hasReceivedAnyNewData = true; | ||||
|           foreach (var banca in bancas) | ||||
|           { | ||||
|             nuevasProyecciones.Add(new ProyeccionBanca | ||||
|             todasLasProyecciones.Add(new ProyeccionBanca | ||||
|             { | ||||
|               AmbitoGeograficoId = provincia.Id, | ||||
|               AgrupacionPoliticaId = banca.IdAgrupacion, | ||||
| @@ -301,7 +304,7 @@ public class LowPriorityDataWorker : BackgroundService | ||||
|         } | ||||
|       } | ||||
|  | ||||
|       // 2. Bucle para el nivel de Sección Electoral | ||||
|       // 2. Bucle por Sección | ||||
|       foreach (var seccion in seccionesElectorales) | ||||
|       { | ||||
|         if (stoppingToken.IsCancellationRequested) break; | ||||
| @@ -310,11 +313,12 @@ public class LowPriorityDataWorker : BackgroundService | ||||
|           if (stoppingToken.IsCancellationRequested) break; | ||||
|           var repartoBancas = await _apiService.GetBancasAsync(authToken, seccion.DistritoId!, seccion.SeccionProvincialId!, categoria.Id); | ||||
|  | ||||
|           if (repartoBancas?.RepartoBancas != null) | ||||
|           if (repartoBancas?.RepartoBancas is { Count: > 0 } bancas) | ||||
|           { | ||||
|             foreach (var banca in repartoBancas.RepartoBancas) | ||||
|             hasReceivedAnyNewData = true; | ||||
|             foreach (var banca in bancas) | ||||
|             { | ||||
|               nuevasProyecciones.Add(new ProyeccionBanca | ||||
|               todasLasProyecciones.Add(new ProyeccionBanca | ||||
|               { | ||||
|                 AmbitoGeograficoId = seccion.Id, | ||||
|                 AgrupacionPoliticaId = banca.IdAgrupacion, | ||||
| @@ -326,15 +330,16 @@ public class LowPriorityDataWorker : BackgroundService | ||||
|       } | ||||
|  | ||||
|       // 3. Guardado Final | ||||
|       // Ahora la condición es simple: si nuestra lista recolectora tiene CUALQUIER COSA, actualizamos la BD. | ||||
|       if (nuevasProyecciones.Any()) | ||||
|       // La lógica de guardado ahora depende de la bandera. | ||||
|       if (hasReceivedAnyNewData) | ||||
|       { | ||||
|         _logger.LogInformation("Se recibieron {count} registros de proyección de bancas. Actualizando la tabla...", nuevasProyecciones.Count); | ||||
|         _logger.LogInformation("Se recibieron {count} registros de proyección de bancas. Actualizando la tabla...", todasLasProyecciones.Count); | ||||
|  | ||||
|         await using var transaction = await dbContext.Database.BeginTransactionAsync(stoppingToken); | ||||
|  | ||||
|         await dbContext.Database.ExecuteSqlRawAsync("DELETE FROM ProyeccionesBancas", stoppingToken); | ||||
|         await dbContext.ProyeccionesBancas.AddRangeAsync(nuevasProyecciones, stoppingToken); | ||||
|         // Guardamos la lista completa, incluso los que tienen 0 bancas. | ||||
|         await dbContext.ProyeccionesBancas.AddRangeAsync(todasLasProyecciones, stoppingToken); | ||||
|         await dbContext.SaveChangesAsync(stoppingToken); | ||||
|         await transaction.CommitAsync(stoppingToken); | ||||
|  | ||||
| @@ -342,8 +347,7 @@ public class LowPriorityDataWorker : BackgroundService | ||||
|       } | ||||
|       else | ||||
|       { | ||||
|         // Si después de todas las llamadas, la lista sigue vacía, no hacemos nada. | ||||
|         _logger.LogInformation("Sondeo de Bancas completado. No se encontraron datos de proyección, la tabla no fue modificada."); | ||||
|         _logger.LogInformation("Sondeo de Bancas completado. No se encontraron datos nuevos de proyección, la tabla no fue modificada."); | ||||
|       } | ||||
|     } | ||||
|     catch (Exception ex) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user