1620 lines
78 KiB
C#
1620 lines
78 KiB
C#
// src/Elecciones.Api/Controllers/ResultadosController.cs
|
|
using Elecciones.Core.DTOs;
|
|
using Elecciones.Core.DTOs.ApiResponses;
|
|
using Elecciones.Database;
|
|
using Elecciones.Database.Entities;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Elecciones.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/elecciones/{eleccionId}")]
|
|
public class ResultadosController : ControllerBase
|
|
{
|
|
private readonly EleccionesDbContext _dbContext;
|
|
private readonly ILogger<ResultadosController> _logger;
|
|
private readonly IConfiguration _configuration;
|
|
|
|
public ResultadosController(EleccionesDbContext dbContext, ILogger<ResultadosController> logger, IConfiguration configuration)
|
|
{
|
|
_dbContext = dbContext;
|
|
_logger = logger;
|
|
_configuration = configuration;
|
|
}
|
|
|
|
private string? GetLogoUrl(
|
|
string agrupacionId,
|
|
int categoriaId,
|
|
int? ambitoId,
|
|
List<LogoAgrupacionCategoria> todosLosLogos)
|
|
{
|
|
// Prioridad 1: Buscar un logo específico para este partido, categoría Y ámbito.
|
|
var logoEspecifico = todosLosLogos.FirstOrDefault(l =>
|
|
l.AgrupacionPoliticaId == agrupacionId &&
|
|
l.CategoriaId == categoriaId &&
|
|
l.AmbitoGeograficoId == ambitoId);
|
|
|
|
if (logoEspecifico != null) return logoEspecifico.LogoUrl;
|
|
|
|
// Prioridad 2: Si no hay uno específico, buscar un logo general (sin ámbito) para este partido y categoría.
|
|
var logoGeneral = todosLosLogos.FirstOrDefault(l =>
|
|
l.AgrupacionPoliticaId == agrupacionId &&
|
|
l.CategoriaId == categoriaId &&
|
|
l.AmbitoGeograficoId == null);
|
|
|
|
return logoGeneral?.LogoUrl;
|
|
}
|
|
|
|
[HttpGet("partido/{municipioId}")]
|
|
public async Task<IActionResult> GetResultadosPorPartido([FromRoute] int eleccionId, string municipioId, [FromQuery] int categoriaId)
|
|
{
|
|
var ambito = await _dbContext.AmbitosGeograficos.AsNoTracking()
|
|
.FirstOrDefaultAsync(a => a.SeccionId == municipioId && a.NivelId == 30);
|
|
|
|
if (ambito == null)
|
|
{
|
|
return NotFound(new { message = $"No se encontró el partido con ID {municipioId}" });
|
|
}
|
|
|
|
var estadoRecuento = await _dbContext.EstadosRecuentos.AsNoTracking()
|
|
.FirstOrDefaultAsync(e => e.EleccionId == eleccionId && e.AmbitoGeograficoId == ambito.Id && e.CategoriaId == categoriaId);
|
|
|
|
var agrupacionIds = await _dbContext.ResultadosVotos
|
|
.Where(rv => rv.AmbitoGeograficoId == ambito.Id && rv.CategoriaId == categoriaId)
|
|
.Select(rv => rv.AgrupacionPoliticaId).Distinct().ToListAsync();
|
|
|
|
var logosRelevantes = await _dbContext.LogosAgrupacionesCategorias.AsNoTracking()
|
|
.Where(l => l.CategoriaId == categoriaId && // <-- Usamos la categoría del parámetro
|
|
agrupacionIds.Contains(l.AgrupacionPoliticaId) &&
|
|
(l.AmbitoGeograficoId == null || l.AmbitoGeograficoId == ambito.Id))
|
|
.ToListAsync();
|
|
|
|
var resultadosVotos = await _dbContext.ResultadosVotos.AsNoTracking()
|
|
.Include(rv => rv.AgrupacionPolitica)
|
|
.Where(rv => rv.EleccionId == eleccionId && rv.AmbitoGeograficoId == ambito.Id && rv.CategoriaId == categoriaId)
|
|
.ToListAsync();
|
|
|
|
var candidatosRelevantes = await _dbContext.CandidatosOverrides.AsNoTracking()
|
|
.Where(c => c.CategoriaId == categoriaId && agrupacionIds.Contains(c.AgrupacionPoliticaId) &&
|
|
(c.AmbitoGeograficoId == null || c.AmbitoGeograficoId == ambito.Id))
|
|
.ToListAsync();
|
|
|
|
long totalVotosPositivos = resultadosVotos.Sum(r => r.CantidadVotos);
|
|
|
|
var respuestaDto = new MunicipioResultadosDto
|
|
{
|
|
MunicipioNombre = ambito.Nombre,
|
|
UltimaActualizacion = estadoRecuento?.FechaTotalizacion ?? DateTime.UtcNow,
|
|
PorcentajeEscrutado = estadoRecuento?.MesasTotalizadasPorcentaje ?? 0,
|
|
PorcentajeParticipacion = estadoRecuento?.ParticipacionPorcentaje ?? 0,
|
|
Resultados = resultadosVotos.Select(rv =>
|
|
{
|
|
var logoUrl = logosRelevantes.FirstOrDefault(l => l.AgrupacionPoliticaId == rv.AgrupacionPoliticaId && l.AmbitoGeograficoId == ambito.Id)?.LogoUrl
|
|
?? logosRelevantes.FirstOrDefault(l => l.AgrupacionPoliticaId == rv.AgrupacionPoliticaId && l.AmbitoGeograficoId == null)?.LogoUrl;
|
|
|
|
var nombreCandidato = candidatosRelevantes.FirstOrDefault(c => c.AgrupacionPoliticaId == rv.AgrupacionPoliticaId && c.AmbitoGeograficoId == ambito.Id)?.NombreCandidato
|
|
?? candidatosRelevantes.FirstOrDefault(c => c.AgrupacionPoliticaId == rv.AgrupacionPoliticaId && c.AmbitoGeograficoId == null)?.NombreCandidato;
|
|
|
|
return new AgrupacionResultadoDto
|
|
{
|
|
Id = rv.AgrupacionPolitica.Id,
|
|
Nombre = rv.AgrupacionPolitica.Nombre,
|
|
NombreCorto = rv.AgrupacionPolitica.NombreCorto,
|
|
Color = rv.AgrupacionPolitica.Color,
|
|
LogoUrl = logoUrl,
|
|
Votos = rv.CantidadVotos,
|
|
NombreCandidato = nombreCandidato,
|
|
Porcentaje = totalVotosPositivos > 0 ? (rv.CantidadVotos * 100.0m / totalVotosPositivos) : 0
|
|
};
|
|
}).OrderByDescending(r => r.Votos).ToList(),
|
|
VotosAdicionales = new VotosAdicionalesDto
|
|
{
|
|
EnBlanco = estadoRecuento?.VotosEnBlanco ?? 0,
|
|
Nulos = estadoRecuento?.VotosNulos ?? 0,
|
|
Recurridos = estadoRecuento?.VotosRecurridos ?? 0
|
|
}
|
|
};
|
|
|
|
return Ok(respuestaDto);
|
|
}
|
|
|
|
[HttpGet("provincia/{distritoId}")]
|
|
public async Task<IActionResult> GetResultadosProvinciales([FromRoute] int eleccionId, string distritoId)
|
|
{
|
|
var provincia = await _dbContext.AmbitosGeograficos.AsNoTracking()
|
|
.FirstOrDefaultAsync(a => a.DistritoId == distritoId && a.NivelId == 10);
|
|
|
|
if (provincia == null)
|
|
{
|
|
return NotFound($"No se encontró la provincia con distritoId {distritoId}");
|
|
}
|
|
|
|
var estadosPorCategoria = await _dbContext.EstadosRecuentosGenerales.AsNoTracking()
|
|
.Include(e => e.CategoriaElectoral)
|
|
.Where(e => e.EleccionId == eleccionId && e.AmbitoGeograficoId == provincia.Id)
|
|
.ToDictionaryAsync(e => e.CategoriaId);
|
|
|
|
var resultadosPorMunicipio = await _dbContext.ResultadosVotos
|
|
.AsNoTracking()
|
|
.Include(r => r.AgrupacionPolitica)
|
|
.Where(r => r.EleccionId == eleccionId && r.AmbitoGeografico.NivelId == 30) // Nivel 30 = Municipio
|
|
.ToListAsync();
|
|
|
|
// Obtenemos TODOS los logos relevantes en una sola consulta
|
|
var todosLosLogos = await _dbContext.LogosAgrupacionesCategorias.AsNoTracking()
|
|
.Where(l => l.EleccionId == eleccionId || l.EleccionId == 0) // Trae los de la elección actual y los de fallback
|
|
.ToListAsync();
|
|
|
|
// --- LÓGICA DE AGRUPACIÓN Y CÁLCULO ---
|
|
var resultadosAgrupados = resultadosPorMunicipio
|
|
.GroupBy(r => r.CategoriaId)
|
|
.Select(g => new
|
|
{
|
|
CategoriaId = g.Key,
|
|
CategoriaNombre = estadosPorCategoria.ContainsKey(g.Key) ? estadosPorCategoria[g.Key].CategoriaElectoral.Nombre : "Desconocido",
|
|
EstadoRecuento = estadosPorCategoria.GetValueOrDefault(g.Key),
|
|
TotalVotosCategoria = g.Sum(r => r.CantidadVotos),
|
|
// Agrupamos por el ID de la agrupación, no por el objeto, para evitar duplicados
|
|
ResultadosAgrupados = g.GroupBy(r => r.AgrupacionPoliticaId)
|
|
.Select(partidoGroup => new
|
|
{
|
|
Agrupacion = partidoGroup.First().AgrupacionPolitica,
|
|
Votos = partidoGroup.Sum(r => r.CantidadVotos)
|
|
})
|
|
.ToList()
|
|
})
|
|
.Select(g => new
|
|
{
|
|
g.CategoriaId,
|
|
g.CategoriaNombre,
|
|
g.EstadoRecuento,
|
|
Resultados = g.ResultadosAgrupados
|
|
.Select(r =>
|
|
{
|
|
// --- USAMOS EL NUEVO MÉTODO HELPER ---
|
|
// Para el resumen provincial, el ámbito es siempre el de la provincia.
|
|
var logoUrl = GetLogoUrl(r.Agrupacion.Id, g.CategoriaId, provincia.Id, todosLosLogos);
|
|
|
|
return new
|
|
{
|
|
Id = r.Agrupacion.Id,
|
|
r.Agrupacion.Nombre,
|
|
r.Agrupacion.NombreCorto,
|
|
r.Agrupacion.Color,
|
|
LogoUrl = logoUrl,
|
|
r.Votos,
|
|
Porcentaje = g.TotalVotosCategoria > 0 ? ((decimal)r.Votos * 100 / g.TotalVotosCategoria) : 0
|
|
};
|
|
})
|
|
.OrderByDescending(r => r.Votos)
|
|
.ToList()
|
|
})
|
|
.OrderBy(c => c.CategoriaId)
|
|
.ToList();
|
|
|
|
return Ok(resultadosAgrupados);
|
|
}
|
|
|
|
|
|
[HttpGet("bancas-por-seccion/{seccionId}/{camara}")]
|
|
public async Task<IActionResult> GetBancasPorSeccion([FromRoute] int eleccionId, string seccionId, string camara)
|
|
{
|
|
// Convertimos el string de la cámara a un enum o un valor numérico para la base de datos
|
|
// 0 = Diputados, 1 = Senadores. Esto debe coincidir con cómo lo guardas en la DB.
|
|
// O puedes usar un enum si lo tienes definido.
|
|
int CategoriaId;
|
|
if (camara.Equals("diputados", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
CategoriaId = 6; // Asume que 5 es el CategoriaId para Diputados Provinciales
|
|
}
|
|
else if (camara.Equals("senadores", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
CategoriaId = 5; // Asume que 6 es el CategoriaId para Senadores Provinciales
|
|
}
|
|
else
|
|
{
|
|
return BadRequest(new { message = "El tipo de cámara especificado no es válido. Use 'diputados' o 'senadores'." });
|
|
}
|
|
|
|
var seccion = await _dbContext.AmbitosGeograficos
|
|
.AsNoTracking()
|
|
.FirstOrDefaultAsync(a => a.SeccionProvincialId == seccionId && a.NivelId == 20);
|
|
|
|
if (seccion == null)
|
|
{
|
|
_logger.LogWarning("No se encontró la sección electoral con SeccionProvincialId: {SeccionId}", seccionId);
|
|
return NotFound(new { message = $"No se encontró la sección electoral con ID {seccionId}" });
|
|
}
|
|
|
|
// --- CAMBIO 3: Filtrar también por el cargo (cámara) ---
|
|
var proyecciones = await _dbContext.ProyeccionesBancas
|
|
.AsNoTracking()
|
|
.Include(p => p.AgrupacionPolitica)
|
|
.Where(p => p.EleccionId == eleccionId && p.AmbitoGeograficoId == seccion.Id && p.CategoriaId == CategoriaId)
|
|
.Select(p => new
|
|
{
|
|
AgrupacionId = p.AgrupacionPolitica.Id, // Añadir para el 'key' en React
|
|
AgrupacionNombre = p.AgrupacionPolitica.Nombre,
|
|
NombreCorto = p.AgrupacionPolitica.NombreCorto, // Añadir para el frontend
|
|
Color = p.AgrupacionPolitica.Color, // Añadir para el frontend
|
|
Bancas = p.NroBancas
|
|
})
|
|
.OrderByDescending(p => p.Bancas)
|
|
.ToListAsync();
|
|
|
|
if (!proyecciones.Any())
|
|
{
|
|
_logger.LogWarning("No se encontraron proyecciones de bancas para la sección: {SeccionNombre} y cámara: {Camara}", seccion.Nombre, camara);
|
|
return NotFound(new { message = $"No se han encontrado proyecciones de bancas para la sección {seccion.Nombre} ({camara})" });
|
|
}
|
|
|
|
return Ok(new
|
|
{
|
|
SeccionNombre = seccion.Nombre,
|
|
Proyeccion = proyecciones
|
|
});
|
|
}
|
|
|
|
[HttpGet("mapa")]
|
|
public async Task<IActionResult> GetResultadosParaMapa()
|
|
{
|
|
var maxVotosPorAmbito = _dbContext.ResultadosVotos
|
|
.GroupBy(rv => rv.AmbitoGeograficoId)
|
|
.Select(g => new
|
|
{
|
|
AmbitoId = g.Key,
|
|
MaxVotos = g.Max(v => v.CantidadVotos)
|
|
});
|
|
|
|
var resultadosGanadores = await _dbContext.ResultadosVotos
|
|
.Join(
|
|
maxVotosPorAmbito,
|
|
voto => new { AmbitoId = voto.AmbitoGeograficoId, Votos = voto.CantidadVotos },
|
|
max => new { AmbitoId = max.AmbitoId, Votos = max.MaxVotos },
|
|
(voto, max) => voto
|
|
)
|
|
.Include(rv => rv.AmbitoGeografico)
|
|
.Include(rv => rv.AgrupacionPolitica)
|
|
.Where(rv => rv.AmbitoGeografico.NivelId == 30)
|
|
.Select(rv => new
|
|
{
|
|
AmbitoId = rv.AmbitoGeografico.Id,
|
|
DepartamentoNombre = rv.AmbitoGeografico.Nombre,
|
|
AgrupacionGanadoraId = rv.AgrupacionPoliticaId,
|
|
ColorGanador = rv.AgrupacionPolitica.Color
|
|
})
|
|
.ToListAsync();
|
|
|
|
return Ok(resultadosGanadores);
|
|
}
|
|
|
|
[HttpGet("municipio/{ambitoId}")]
|
|
public async Task<IActionResult> GetResultadosPorMunicipio(int ambitoId, [FromQuery] int categoriaId)
|
|
{
|
|
_logger.LogInformation("Buscando resultados para AmbitoGeograficoId: {AmbitoId}, CategoriaId: {CategoriaId}", ambitoId, categoriaId);
|
|
|
|
// Validamos que el ámbito exista
|
|
var ambito = await _dbContext.AmbitosGeograficos.AsNoTracking()
|
|
.FirstOrDefaultAsync(a => a.Id == ambitoId && a.NivelId == 30);
|
|
if (ambito == null)
|
|
{
|
|
_logger.LogWarning("No se encontró el municipio con ID: {AmbitoId}", ambitoId);
|
|
return NotFound($"No se encontró el municipio con ID {ambitoId}");
|
|
}
|
|
|
|
// Obtenemos el estado del recuento para el ámbito
|
|
var estadoRecuento = await _dbContext.EstadosRecuentos
|
|
.AsNoTracking()
|
|
.FirstOrDefaultAsync(e => e.AmbitoGeograficoId == ambito.Id);
|
|
|
|
// Obtenemos los votos para ESE municipio y ESA categoría
|
|
var resultadosVotos = await _dbContext.ResultadosVotos
|
|
.AsNoTracking()
|
|
.Include(rv => rv.AgrupacionPolitica)
|
|
.Where(rv => rv.AmbitoGeograficoId == ambitoId && rv.CategoriaId == categoriaId)
|
|
.ToListAsync();
|
|
|
|
// Calculamos el total de votos solo para esta selección
|
|
var totalVotosPositivos = (decimal)resultadosVotos.Sum(r => r.CantidadVotos);
|
|
|
|
// Mapeamos los resultados de los partidos
|
|
var resultadosPartidosDto = resultadosVotos
|
|
.OrderByDescending(r => r.CantidadVotos)
|
|
.Select(rv => new AgrupacionResultadoDto
|
|
{
|
|
Id = rv.AgrupacionPolitica.Id,
|
|
Nombre = rv.AgrupacionPolitica.NombreCorto ?? rv.AgrupacionPolitica.Nombre,
|
|
Color = rv.AgrupacionPolitica.Color,
|
|
Votos = rv.CantidadVotos,
|
|
Porcentaje = totalVotosPositivos > 0 ? (rv.CantidadVotos / totalVotosPositivos) * 100 : 0
|
|
}).ToList();
|
|
|
|
// Construimos la respuesta completa del DTO
|
|
var respuestaDto = new MunicipioResultadosDto
|
|
{
|
|
MunicipioNombre = ambito.Nombre,
|
|
UltimaActualizacion = estadoRecuento?.FechaTotalizacion ?? DateTime.UtcNow, // Use null-conditional operator
|
|
PorcentajeEscrutado = estadoRecuento?.MesasTotalizadasPorcentaje ?? 0,
|
|
PorcentajeParticipacion = estadoRecuento?.ParticipacionPorcentaje ?? 0,
|
|
Resultados = resultadosPartidosDto,
|
|
VotosAdicionales = new VotosAdicionalesDto // Assuming default constructor is fine
|
|
{
|
|
EnBlanco = estadoRecuento?.VotosEnBlanco ?? 0,
|
|
Nulos = estadoRecuento?.VotosNulos ?? 0,
|
|
Recurridos = estadoRecuento?.VotosRecurridos ?? 0
|
|
}
|
|
};
|
|
|
|
return Ok(respuestaDto);
|
|
}
|
|
|
|
[HttpGet("composicion-congreso")]
|
|
public async Task<IActionResult> GetComposicionCongreso([FromRoute] int eleccionId)
|
|
{
|
|
var config = await _dbContext.Configuraciones
|
|
.AsNoTracking()
|
|
.ToDictionaryAsync(c => c.Clave, c => c.Valor);
|
|
|
|
// Aquí está el interruptor
|
|
config.TryGetValue("UsarDatosDeBancadasOficiales", out var usarDatosOficialesValue);
|
|
bool usarDatosOficiales = usarDatosOficialesValue == "true";
|
|
|
|
if (usarDatosOficiales)
|
|
{
|
|
// Si el interruptor está en 'true', llama a este método
|
|
return await GetComposicionDesdeBancadasOficiales(config, eleccionId);
|
|
}
|
|
else
|
|
{
|
|
// Si está en 'false' o no existe, llama a este otro
|
|
return await GetComposicionDesdeProyecciones(config, eleccionId);
|
|
}
|
|
}
|
|
|
|
// En ResultadosController.cs
|
|
private async Task<IActionResult> GetComposicionDesdeBancadasOficiales(Dictionary<string, string> config, int eleccionId)
|
|
{
|
|
config.TryGetValue("MostrarOcupantes", out var mostrarOcupantesValue);
|
|
bool mostrarOcupantes = mostrarOcupantesValue == "true";
|
|
|
|
IQueryable<Bancada> bancadasQuery = _dbContext.Bancadas.AsNoTracking()
|
|
.Where(b => b.EleccionId == eleccionId)
|
|
.Include(b => b.AgrupacionPolitica);
|
|
if (mostrarOcupantes)
|
|
{
|
|
bancadasQuery = bancadasQuery.Include(b => b.Ocupante);
|
|
}
|
|
var bancadas = await bancadasQuery.ToListAsync();
|
|
|
|
var bancasPorAgrupacion = bancadas
|
|
.Where(b => b.AgrupacionPolitica != null)
|
|
// Agrupamos por el ID del partido, que es un valor único y estable
|
|
.GroupBy(b => b.AgrupacionPolitica!.Id)
|
|
.Select(g =>
|
|
{
|
|
// Tomamos la información de la agrupación del primer elemento (todas son iguales)
|
|
var primeraBancaDelGrupo = g.First();
|
|
var agrupacion = primeraBancaDelGrupo.AgrupacionPolitica!;
|
|
|
|
// Filtramos los ocupantes solo para este grupo
|
|
var ocupantesDelPartido = mostrarOcupantes
|
|
? g.Select(b => b.Ocupante).Where(o => o != null).ToList()
|
|
: new List<OcupanteBanca?>();
|
|
|
|
return new
|
|
{
|
|
Agrupacion = agrupacion,
|
|
Camara = primeraBancaDelGrupo.Camara,
|
|
BancasTotales = g.Count(),
|
|
Ocupantes = ocupantesDelPartido
|
|
};
|
|
})
|
|
.ToList();
|
|
|
|
var presidenteDiputados = bancasPorAgrupacion
|
|
.Where(b => b.Camara == Core.Enums.TipoCamara.Diputados)
|
|
.OrderByDescending(b => b.BancasTotales)
|
|
.FirstOrDefault()?.Agrupacion;
|
|
|
|
config.TryGetValue("PresidenciaSenadores", out var idPartidoPresidenteSenadores);
|
|
var presidenteSenadores = await _dbContext.AgrupacionesPoliticas.FindAsync(idPartidoPresidenteSenadores);
|
|
|
|
object MapearPartidos(Core.Enums.TipoCamara camara)
|
|
{
|
|
var partidosDeCamara = bancasPorAgrupacion.Where(b => b.Camara == camara);
|
|
|
|
if (camara == Core.Enums.TipoCamara.Diputados)
|
|
partidosDeCamara = partidosDeCamara.OrderBy(p => p.Agrupacion.OrdenDiputados ?? 999);
|
|
else
|
|
partidosDeCamara = partidosDeCamara.OrderBy(p => p.Agrupacion.OrdenSenadores ?? 999);
|
|
|
|
return partidosDeCamara
|
|
.OrderByDescending(p => p.BancasTotales)
|
|
.Select(p => new
|
|
{
|
|
p.Agrupacion.Id,
|
|
p.Agrupacion.Nombre,
|
|
p.Agrupacion.NombreCorto,
|
|
p.Agrupacion.Color,
|
|
p.BancasTotales,
|
|
p.Ocupantes // Pasamos la lista de ocupantes ya filtrada
|
|
}).ToList();
|
|
}
|
|
|
|
var diputados = new
|
|
{
|
|
CamaraNombre = "Cámara de Diputados",
|
|
TotalBancas = 92,
|
|
BancasEnJuego = 0,
|
|
Partidos = MapearPartidos(Core.Enums.TipoCamara.Diputados),
|
|
PresidenteBancada = presidenteDiputados != null ? new { presidenteDiputados.Color } : null
|
|
};
|
|
|
|
var senadores = new
|
|
{
|
|
CamaraNombre = "Cámara de Senadores",
|
|
TotalBancas = 46,
|
|
BancasEnJuego = 0,
|
|
Partidos = MapearPartidos(Core.Enums.TipoCamara.Senadores),
|
|
PresidenteBancada = presidenteSenadores != null ? new { presidenteSenadores.Color } : null
|
|
};
|
|
|
|
return Ok(new { Diputados = diputados, Senadores = senadores });
|
|
}
|
|
|
|
private async Task<IActionResult> GetComposicionDesdeProyecciones(Dictionary<string, string> config, int eleccionId)
|
|
{
|
|
// 1. Obtenemos el ID del ámbito provincial para usarlo en el filtro.
|
|
var provincia = await _dbContext.AmbitosGeograficos.AsNoTracking()
|
|
.FirstOrDefaultAsync(a => a.NivelId == 10);
|
|
|
|
if (provincia == null)
|
|
{
|
|
// Si no se encuentra la provincia, no podemos continuar.
|
|
// Devolvemos un objeto vacío para no romper el frontend.
|
|
return Ok(new
|
|
{
|
|
Diputados = new { Partidos = new List<object>() },
|
|
Senadores = new { Partidos = new List<object>() }
|
|
});
|
|
}
|
|
|
|
var bancasPorAgrupacion = await _dbContext.ProyeccionesBancas
|
|
.AsNoTracking()
|
|
.Where(p => p.EleccionId == eleccionId && p.AmbitoGeograficoId == provincia.Id)
|
|
.GroupBy(p => new { p.AgrupacionPoliticaId, p.CategoriaId })
|
|
.Select(g => new
|
|
{
|
|
AgrupacionId = g.Key.AgrupacionPoliticaId,
|
|
CategoriaId = g.Key.CategoriaId,
|
|
BancasTotales = g.Sum(p => p.NroBancas)
|
|
})
|
|
.ToListAsync();
|
|
|
|
var todasAgrupaciones = await _dbContext.AgrupacionesPoliticas.AsNoTracking().ToDictionaryAsync(a => a.Id);
|
|
|
|
config.TryGetValue("PresidenciaSenadores", out var idPartidoPresidenteSenadores);
|
|
todasAgrupaciones.TryGetValue(idPartidoPresidenteSenadores ?? "", out var presidenteSenadores);
|
|
|
|
string? idPartidoPresidenteDiputados = bancasPorAgrupacion
|
|
.Where(b => b.CategoriaId == 6)
|
|
.OrderByDescending(b => b.BancasTotales)
|
|
.FirstOrDefault()?.AgrupacionId;
|
|
todasAgrupaciones.TryGetValue(idPartidoPresidenteDiputados ?? "", out var presidenteDiputados);
|
|
|
|
object MapearPartidos(int categoriaId)
|
|
{
|
|
var partidosDeCamara = bancasPorAgrupacion
|
|
.Where(b => b.CategoriaId == categoriaId && b.BancasTotales > 0)
|
|
.Select(b => new { Bancas = b, Agrupacion = todasAgrupaciones[b.AgrupacionId] });
|
|
|
|
if (categoriaId == 6) // Diputados
|
|
partidosDeCamara = partidosDeCamara.OrderBy(b => b.Agrupacion.OrdenDiputados ?? 999)
|
|
.ThenByDescending(b => b.Bancas.BancasTotales);
|
|
else // Senadores
|
|
partidosDeCamara = partidosDeCamara.OrderBy(b => b.Agrupacion.OrdenSenadores ?? 999)
|
|
.ThenByDescending(b => b.Bancas.BancasTotales);
|
|
|
|
return partidosDeCamara
|
|
.Select(b => new
|
|
{
|
|
b.Agrupacion.Id,
|
|
b.Agrupacion.Nombre,
|
|
b.Agrupacion.NombreCorto,
|
|
b.Agrupacion.Color,
|
|
b.Bancas.BancasTotales,
|
|
Ocupantes = new List<object>() // <-- Siempre vacío en modo proyección
|
|
})
|
|
.ToList();
|
|
}
|
|
|
|
var diputados = new
|
|
{
|
|
CamaraNombre = "Cámara de Diputados",
|
|
TotalBancas = 92,
|
|
BancasEnJuego = 46,
|
|
Partidos = MapearPartidos(6),
|
|
PresidenteBancada = presidenteDiputados != null ? new { presidenteDiputados.Color } : null
|
|
};
|
|
|
|
var senadores = new
|
|
{
|
|
CamaraNombre = "Cámara de Senadores",
|
|
TotalBancas = 46,
|
|
BancasEnJuego = 23,
|
|
Partidos = MapearPartidos(5),
|
|
PresidenteBancada = presidenteSenadores != null ? new { presidenteSenadores.Color } : null
|
|
};
|
|
|
|
return Ok(new { Diputados = diputados, Senadores = senadores });
|
|
}
|
|
|
|
[HttpGet("bancadas-detalle")]
|
|
public async Task<IActionResult> GetBancadasConOcupantes([FromRoute] int eleccionId)
|
|
{
|
|
var config = await _dbContext.Configuraciones.AsNoTracking().ToDictionaryAsync(c => c.Clave, c => c.Valor);
|
|
|
|
config.TryGetValue("UsarDatosDeBancadasOficiales", out var usarDatosOficialesValue);
|
|
if (usarDatosOficialesValue != "true")
|
|
{
|
|
// Si el modo oficial no está activo, SIEMPRE devolvemos un array vacío.
|
|
return Ok(new List<object>());
|
|
}
|
|
|
|
// Si el modo oficial SÍ está activo, devolvemos los detalles.
|
|
var bancadasConOcupantes = await _dbContext.Bancadas
|
|
.AsNoTracking()
|
|
.Where(b => b.EleccionId == eleccionId)
|
|
.Include(b => b.Ocupante)
|
|
.Select(b => new
|
|
{
|
|
b.Id,
|
|
b.Camara,
|
|
b.NumeroBanca,
|
|
b.AgrupacionPoliticaId,
|
|
Ocupante = b.Ocupante
|
|
})
|
|
.OrderBy(b => b.Id)
|
|
.ToListAsync();
|
|
|
|
return Ok(bancadasConOcupantes);
|
|
}
|
|
[HttpGet("configuracion-publica")]
|
|
public async Task<IActionResult> GetConfiguracionPublica()
|
|
{
|
|
// Definimos una lista de las claves de configuración que son seguras para el público.
|
|
// De esta manera, si en el futuro añadimos claves sensibles (como contraseñas de API, etc.),
|
|
// nunca se expondrán accidentalmente.
|
|
var clavesPublicas = new List<string>
|
|
{
|
|
"TickerResultadosCantidad",
|
|
"ConcejalesResultadosCantidad"
|
|
// "OtraClavePublica"
|
|
};
|
|
|
|
var configuracionPublica = await _dbContext.Configuraciones
|
|
.AsNoTracking()
|
|
.Where(c => clavesPublicas.Contains(c.Clave))
|
|
.ToDictionaryAsync(c => c.Clave, c => c.Valor);
|
|
|
|
return Ok(configuracionPublica);
|
|
}
|
|
|
|
[HttpGet("seccion-resultados/{seccionId}")]
|
|
public async Task<IActionResult> GetResultadosAgregadosPorSeccion(string seccionId, [FromQuery] int categoriaId)
|
|
{
|
|
var municipiosDeLaSeccion = await _dbContext.AmbitosGeograficos
|
|
.AsNoTracking()
|
|
.Where(a => a.NivelId == 30 && a.SeccionProvincialId == seccionId)
|
|
.Select(a => a.Id)
|
|
.ToListAsync();
|
|
|
|
if (!municipiosDeLaSeccion.Any())
|
|
{
|
|
return Ok(new { UltimaActualizacion = DateTime.UtcNow, Resultados = new List<object>() });
|
|
}
|
|
|
|
// 1. Buscamos logos que sean para esta categoría Y que sean generales (ámbito null).
|
|
var logosGenerales = await _dbContext.LogosAgrupacionesCategorias
|
|
.AsNoTracking()
|
|
.Where(l => l.CategoriaId == categoriaId && l.AmbitoGeograficoId == null)
|
|
.ToDictionaryAsync(l => l.AgrupacionPoliticaId);
|
|
|
|
var resultadosMunicipales = await _dbContext.ResultadosVotos
|
|
.AsNoTracking()
|
|
.Include(r => r.AgrupacionPolitica)
|
|
.Where(r => r.CategoriaId == categoriaId && municipiosDeLaSeccion.Contains(r.AmbitoGeograficoId))
|
|
.ToListAsync();
|
|
|
|
var totalVotosSeccion = (decimal)resultadosMunicipales.Sum(r => r.CantidadVotos);
|
|
|
|
var resultadosFinales = resultadosMunicipales
|
|
.GroupBy(r => r.AgrupacionPoliticaId)
|
|
.Select(g => new
|
|
{
|
|
Agrupacion = g.First().AgrupacionPolitica,
|
|
Votos = g.Sum(r => r.CantidadVotos)
|
|
})
|
|
.OrderByDescending(r => r.Votos)
|
|
.Select(r => new
|
|
{
|
|
Id = r.Agrupacion.Id,
|
|
r.Agrupacion.Nombre,
|
|
r.Agrupacion.NombreCorto,
|
|
r.Agrupacion.Color,
|
|
// 2. Usamos el diccionario de logos generales para buscar la URL.
|
|
LogoUrl = logosGenerales.GetValueOrDefault(r.Agrupacion.Id)?.LogoUrl,
|
|
Votos = r.Votos,
|
|
Porcentaje = totalVotosSeccion > 0 ? ((decimal)r.Votos * 100 / totalVotosSeccion) : 0
|
|
})
|
|
.ToList();
|
|
|
|
var seccionAmbito = await _dbContext.AmbitosGeograficos.AsNoTracking()
|
|
.FirstOrDefaultAsync(a => a.SeccionProvincialId == seccionId && a.NivelId == 20);
|
|
var estadoRecuento = seccionAmbito != null
|
|
? await _dbContext.EstadosRecuentos.AsNoTracking()
|
|
.FirstOrDefaultAsync(e => e.AmbitoGeograficoId == seccionAmbito.Id && e.CategoriaId == categoriaId)
|
|
: null;
|
|
|
|
return Ok(new
|
|
{
|
|
UltimaActualizacion = estadoRecuento?.FechaTotalizacion ?? DateTime.UtcNow,
|
|
Resultados = resultadosFinales
|
|
});
|
|
}
|
|
|
|
[HttpGet("mapa-por-seccion")]
|
|
public async Task<IActionResult> GetResultadosMapaPorSeccion([FromQuery] int categoriaId)
|
|
{
|
|
// 1. Obtenemos todos los resultados a nivel de MUNICIPIO para la categoría dada.
|
|
var resultadosMunicipales = await _dbContext.ResultadosVotos
|
|
.AsNoTracking()
|
|
.Include(r => r.AmbitoGeografico)
|
|
.Include(r => r.AgrupacionPolitica)
|
|
.Where(r => r.CategoriaId == categoriaId && r.AmbitoGeografico.NivelId == 30)
|
|
.ToListAsync();
|
|
|
|
// 2. Agrupamos en memoria por Sección Electoral y sumamos los votos.
|
|
var ganadoresPorSeccion = resultadosMunicipales
|
|
.GroupBy(r => r.AmbitoGeografico.SeccionProvincialId)
|
|
.Select(g =>
|
|
{
|
|
// Para cada sección, encontramos al partido con más votos.
|
|
var ganador = g
|
|
// CAMBIO CLAVE: Agrupamos por el ID de la agrupación, no por el objeto.
|
|
.GroupBy(r => r.AgrupacionPolitica.Id)
|
|
.Select(pg => new
|
|
{
|
|
// Obtenemos la entidad completa del primer elemento del grupo
|
|
Agrupacion = pg.First().AgrupacionPolitica,
|
|
TotalVotos = pg.Sum(r => r.CantidadVotos)
|
|
})
|
|
.OrderByDescending(x => x.TotalVotos)
|
|
.FirstOrDefault();
|
|
|
|
// Buscamos el nombre de la sección
|
|
var seccionInfo = _dbContext.AmbitosGeograficos
|
|
.FirstOrDefault(a => a.SeccionProvincialId == g.Key && a.NivelId == 20);
|
|
|
|
return new
|
|
{
|
|
SeccionId = g.Key,
|
|
SeccionNombre = seccionInfo?.Nombre,
|
|
AgrupacionGanadoraId = ganador?.Agrupacion.Id,
|
|
ColorGanador = ganador?.Agrupacion.Color
|
|
};
|
|
})
|
|
.Where(r => r.SeccionId != null)
|
|
.ToList();
|
|
|
|
return Ok(ganadoresPorSeccion);
|
|
}
|
|
|
|
[HttpGet("seccion/{seccionId}")]
|
|
public async Task<IActionResult> GetResultadosDetallePorSeccion(string seccionId, [FromQuery] int categoriaId)
|
|
{
|
|
var municipiosDeLaSeccion = await _dbContext.AmbitosGeograficos
|
|
.AsNoTracking()
|
|
.Where(a => a.NivelId == 30 && a.SeccionProvincialId == seccionId)
|
|
.Select(a => a.Id)
|
|
.ToListAsync();
|
|
|
|
if (!municipiosDeLaSeccion.Any()) return Ok(new List<object>());
|
|
|
|
var resultadosMunicipales = await _dbContext.ResultadosVotos
|
|
.AsNoTracking()
|
|
.Include(r => r.AgrupacionPolitica)
|
|
.Where(r => r.CategoriaId == categoriaId && municipiosDeLaSeccion.Contains(r.AmbitoGeograficoId))
|
|
.ToListAsync();
|
|
|
|
var totalVotosSeccion = (decimal)resultadosMunicipales.Sum(r => r.CantidadVotos);
|
|
|
|
var resultadosFinales = resultadosMunicipales
|
|
// 1. Agrupamos por el ID del partido para evitar duplicados.
|
|
.GroupBy(r => r.AgrupacionPoliticaId)
|
|
.Select(g => new
|
|
{
|
|
// 2. Tomamos la entidad completa del primer elemento del grupo.
|
|
Agrupacion = g.First().AgrupacionPolitica,
|
|
Votos = g.Sum(r => r.CantidadVotos)
|
|
})
|
|
.OrderByDescending(r => r.Votos)
|
|
.Select(r => new
|
|
{
|
|
id = r.Agrupacion.Id,
|
|
nombre = r.Agrupacion.NombreCorto ?? r.Agrupacion.Nombre,
|
|
votos = r.Votos,
|
|
porcentaje = totalVotosSeccion > 0 ? ((decimal)r.Votos * 100 / totalVotosSeccion) : 0,
|
|
// 3. Añadimos el color a la respuesta.
|
|
color = r.Agrupacion.Color
|
|
})
|
|
.ToList();
|
|
|
|
return Ok(resultadosFinales);
|
|
}
|
|
|
|
[HttpGet("mapa-por-municipio")]
|
|
public async Task<IActionResult> GetResultadosMapaPorMunicipio([FromQuery] int categoriaId)
|
|
{
|
|
// Obtenemos los votos primero
|
|
var votosPorMunicipio = await _dbContext.ResultadosVotos
|
|
.AsNoTracking()
|
|
.Where(r => r.CategoriaId == categoriaId && r.AmbitoGeografico.NivelId == 30)
|
|
.ToListAsync();
|
|
|
|
// Luego, los agrupamos en memoria
|
|
var ganadores = votosPorMunicipio
|
|
.GroupBy(r => r.AmbitoGeograficoId)
|
|
.Select(g => g.OrderByDescending(r => r.CantidadVotos).First())
|
|
.ToList();
|
|
|
|
// Ahora, obtenemos los detalles necesarios en una sola consulta adicional
|
|
var idsAgrupacionesGanadoras = ganadores.Select(g => g.AgrupacionPoliticaId).ToList();
|
|
var idsAmbitosGanadores = ganadores.Select(g => g.AmbitoGeograficoId).ToList();
|
|
|
|
var agrupacionesInfo = await _dbContext.AgrupacionesPoliticas
|
|
.AsNoTracking()
|
|
.Where(a => idsAgrupacionesGanadoras.Contains(a.Id))
|
|
.ToDictionaryAsync(a => a.Id);
|
|
|
|
var ambitosInfo = await _dbContext.AmbitosGeograficos
|
|
.AsNoTracking()
|
|
.Where(a => idsAmbitosGanadores.Contains(a.Id))
|
|
.ToDictionaryAsync(a => a.Id);
|
|
|
|
// Finalmente, unimos todo en memoria
|
|
var resultadoFinal = ganadores.Select(g => new
|
|
{
|
|
AmbitoId = g.AmbitoGeograficoId,
|
|
DepartamentoNombre = ambitosInfo.GetValueOrDefault(g.AmbitoGeograficoId)?.Nombre,
|
|
AgrupacionGanadoraId = g.AgrupacionPoliticaId,
|
|
ColorGanador = agrupacionesInfo.GetValueOrDefault(g.AgrupacionPoliticaId)?.Color
|
|
})
|
|
.Where(r => r.DepartamentoNombre != null) // Filtramos por si acaso
|
|
.ToList();
|
|
|
|
return Ok(resultadoFinal);
|
|
}
|
|
|
|
[HttpGet("secciones-electorales-con-cargos")]
|
|
public async Task<IActionResult> GetSeccionesElectoralesConCargos()
|
|
{
|
|
var secciones = await _dbContext.AmbitosGeograficos
|
|
.AsNoTracking()
|
|
.Where(a => a.NivelId == 20) // Nivel 20 = Sección Electoral Provincial
|
|
.Select(a => new
|
|
{
|
|
Id = a.SeccionProvincialId, // Usamos el ID que el frontend espera
|
|
Nombre = a.Nombre,
|
|
// Obtenemos los CategoriaId de las relaciones que tiene esta sección.
|
|
// Esto asume que tienes una tabla que relaciona Ámbitos con Cargos.
|
|
// Por ejemplo, a través de la tabla de Proyecciones o Resultados.
|
|
Cargos = _dbContext.ProyeccionesBancas
|
|
.Where(p => p.AmbitoGeograficoId == a.Id)
|
|
.Select(p => p.CategoriaId)
|
|
.Distinct()
|
|
.ToList()
|
|
})
|
|
.ToListAsync();
|
|
|
|
// Mapeamos los CategoriaId a los nombres que usa el frontend
|
|
var resultado = secciones.Select(s => new
|
|
{
|
|
s.Id,
|
|
s.Nombre,
|
|
// Convertimos la lista de IDs de cargo a una lista de strings ("diputados", "senadores")
|
|
CamarasDisponibles = s.Cargos.Select(CategoriaId =>
|
|
CategoriaId == 6 ? "diputados" : // Asume 5 = Diputados
|
|
CategoriaId == 5 ? "senadores" : // Asume 6 = Senadores
|
|
null
|
|
).Where(c => c != null).ToList()
|
|
});
|
|
|
|
return Ok(resultado);
|
|
}
|
|
|
|
// En src/Elecciones.Api/Controllers/ResultadosController.cs
|
|
|
|
[HttpGet("tabla-ranking-seccion/{seccionId}")]
|
|
public async Task<IActionResult> GetTablaRankingPorSeccion(string seccionId)
|
|
{
|
|
// 1. Obtener los ámbitos de los municipios de la sección
|
|
var municipios = await _dbContext.AmbitosGeograficos.AsNoTracking()
|
|
.Where(a => a.NivelId == 30 && a.SeccionProvincialId == seccionId)
|
|
.OrderBy(a => a.Nombre).Select(a => new { a.Id, a.Nombre }).ToListAsync();
|
|
|
|
if (!municipios.Any())
|
|
{
|
|
return Ok(new
|
|
{
|
|
Categorias = new List<object>(),
|
|
PartidosPrincipales = new Dictionary<int, List<object>>(),
|
|
ResultadosPorMunicipio = new List<object>()
|
|
});
|
|
}
|
|
|
|
var municipiosIds = municipios.Select(m => m.Id).ToList();
|
|
|
|
// 2. Obtener todos los resultados de votos para esos municipios en una sola consulta
|
|
var resultadosCrudos = await _dbContext.ResultadosVotos.AsNoTracking()
|
|
.Include(r => r.AgrupacionPolitica)
|
|
.Where(r => municipiosIds.Contains(r.AmbitoGeograficoId))
|
|
.ToListAsync();
|
|
|
|
var categoriasMap = await _dbContext.CategoriasElectorales.AsNoTracking().ToDictionaryAsync(c => c.Id);
|
|
|
|
// 3. Determinar las categorías activas en la sección
|
|
var categoriasActivas = resultadosCrudos.Select(r => r.CategoriaId).Distinct()
|
|
.Select(id => categoriasMap.GetValueOrDefault(id)).Where(c => c != null)
|
|
.OrderBy(c => c!.Orden).Select(c => new { c!.Id, c.Nombre }).ToList();
|
|
|
|
// 4. Determinar los 2 partidos principales POR CATEGORÍA a nivel SECCIÓN
|
|
var partidosPorCategoria = categoriasActivas.ToDictionary(
|
|
c => c.Id,
|
|
c =>
|
|
{
|
|
var resultadosCategoriaSeccion = resultadosCrudos.Where(r => r.CategoriaId == c.Id);
|
|
var totalVotosSeccionCategoria = (decimal)resultadosCategoriaSeccion.Sum(r => r.CantidadVotos);
|
|
|
|
return resultadosCategoriaSeccion
|
|
// --- CAMBIO CLAVE: Agrupamos por el ID (string), no por el objeto ---
|
|
.GroupBy(r => r.AgrupacionPolitica.Id)
|
|
.Select(g => new
|
|
{
|
|
// g.Key ahora es el AgrupacionPoliticaId
|
|
// Tomamos la entidad completa del primer elemento del grupo
|
|
Agrupacion = g.First().AgrupacionPolitica,
|
|
TotalVotos = g.Sum(r => r.CantidadVotos)
|
|
})
|
|
.OrderByDescending(x => x.TotalVotos)
|
|
.Take(2)
|
|
.Select((x, index) => new
|
|
{
|
|
Puesto = index + 1,
|
|
x.Agrupacion.Id,
|
|
Nombre = x.Agrupacion.NombreCorto ?? x.Agrupacion.Nombre,
|
|
PorcentajeTotalSeccion = totalVotosSeccionCategoria > 0 ? (x.TotalVotos / totalVotosSeccionCategoria) * 100 : 0
|
|
})
|
|
.ToList();
|
|
}
|
|
);
|
|
|
|
// 5. Construir los datos para las filas de la tabla (resultados por municipio)
|
|
var resultadosPorMunicipio = municipios.Select(municipio =>
|
|
{
|
|
var resultadosDelMunicipio = resultadosCrudos.Where(r => r.AmbitoGeograficoId == municipio.Id);
|
|
|
|
var celdas = resultadosDelMunicipio
|
|
.GroupBy(r => r.CategoriaId)
|
|
.ToDictionary(
|
|
g => g.Key, // CategoriaId
|
|
g =>
|
|
{
|
|
var totalVotosMunicipioCategoria = (decimal)g.Sum(r => r.CantidadVotos);
|
|
return g.ToDictionary(
|
|
r => r.AgrupacionPoliticaId, // PartidoId
|
|
r => totalVotosMunicipioCategoria > 0 ? (r.CantidadVotos / totalVotosMunicipioCategoria) * 100 : 0
|
|
);
|
|
}
|
|
);
|
|
|
|
return new
|
|
{
|
|
MunicipioId = municipio.Id,
|
|
MunicipioNombre = municipio.Nombre,
|
|
Celdas = celdas
|
|
};
|
|
}).ToList();
|
|
|
|
return Ok(new
|
|
{
|
|
Categorias = categoriasActivas,
|
|
PartidosPorCategoria = partidosPorCategoria,
|
|
ResultadosPorMunicipio = resultadosPorMunicipio
|
|
});
|
|
}
|
|
|
|
[HttpGet("ranking-municipios-por-seccion/{seccionId}")]
|
|
public async Task<IActionResult> GetRankingMunicipiosPorSeccion(string seccionId)
|
|
{
|
|
// 1. Obtener los municipios de la sección
|
|
var municipios = await _dbContext.AmbitosGeograficos.AsNoTracking()
|
|
.Where(a => a.NivelId == 30 && a.SeccionProvincialId == seccionId)
|
|
.OrderBy(a => a.Nombre)
|
|
.Select(a => new { a.Id, a.Nombre })
|
|
.ToListAsync();
|
|
|
|
if (!municipios.Any())
|
|
{
|
|
return Ok(new List<object>());
|
|
}
|
|
|
|
var municipiosIds = municipios.Select(m => m.Id).ToList();
|
|
|
|
// 2. Obtener todos los resultados de esos municipios en una sola consulta
|
|
var resultadosCrudos = await _dbContext.ResultadosVotos.AsNoTracking()
|
|
.Include(r => r.AgrupacionPolitica)
|
|
.Where(r => municipiosIds.Contains(r.AmbitoGeograficoId))
|
|
.ToListAsync();
|
|
|
|
// 3. Procesar los datos por cada municipio
|
|
var resultadosPorMunicipio = municipios.Select(municipio =>
|
|
{
|
|
var resultadosDelMunicipio = resultadosCrudos.Where(r => r.AmbitoGeograficoId == municipio.Id);
|
|
|
|
var resultadosPorCategoria = resultadosDelMunicipio
|
|
.GroupBy(r => r.CategoriaId)
|
|
.Select(g =>
|
|
{
|
|
var totalVotosCategoria = (decimal)g.Sum(r => r.CantidadVotos);
|
|
|
|
var ranking = g
|
|
.OrderByDescending(r => r.CantidadVotos)
|
|
.Take(2)
|
|
.Select(r => new
|
|
{
|
|
NombreCorto = r.AgrupacionPolitica.NombreCorto ?? r.AgrupacionPolitica.Nombre,
|
|
Porcentaje = totalVotosCategoria > 0 ? (r.CantidadVotos / totalVotosCategoria) * 100 : 0,
|
|
Votos = r.CantidadVotos // <-- AÑADIR ESTE CAMPO
|
|
})
|
|
.ToList();
|
|
|
|
return new { CategoriaId = g.Key, Ranking = ranking };
|
|
})
|
|
.ToDictionary(r => r.CategoriaId); // Lo convertimos a diccionario para fácil acceso
|
|
|
|
return new
|
|
{
|
|
MunicipioId = municipio.Id,
|
|
MunicipioNombre = municipio.Nombre,
|
|
ResultadosPorCategoria = resultadosPorCategoria
|
|
};
|
|
}).ToList();
|
|
|
|
// Devolvemos las categorías que tuvieron resultados en esta sección para construir la cabecera
|
|
var categoriasMap = await _dbContext.CategoriasElectorales.AsNoTracking().ToDictionaryAsync(c => c.Id);
|
|
var categoriasActivas = resultadosCrudos
|
|
.Select(r => r.CategoriaId).Distinct()
|
|
.Select(id => categoriasMap.GetValueOrDefault(id)).Where(c => c != null)
|
|
.OrderBy(c => c!.Orden)
|
|
.Select(c => new { Id = c!.Id, Nombre = c.Nombre })
|
|
.ToList();
|
|
|
|
return Ok(new
|
|
{
|
|
Categorias = categoriasActivas,
|
|
Resultados = resultadosPorMunicipio
|
|
});
|
|
}
|
|
|
|
[HttpGet("panel/{ambitoId?}")]
|
|
public async Task<IActionResult> GetPanelElectoral(int eleccionId, string? ambitoId, [FromQuery] int categoriaId)
|
|
{
|
|
if (string.IsNullOrEmpty(ambitoId))
|
|
{
|
|
// CASO 1: No hay ID -> Vista Nacional
|
|
return await GetPanelNacional(eleccionId, categoriaId);
|
|
}
|
|
|
|
// CASO 2: El ID es un número (y no un string corto como "02") -> Vista Municipal
|
|
// La condición clave es que los IDs de distrito son cortos. Los IDs de BD son más largos.
|
|
// O simplemente, un ID de distrito nunca será un ID de municipio.
|
|
if (int.TryParse(ambitoId, out int idNumerico) && ambitoId.Length > 2)
|
|
{
|
|
return await GetPanelMunicipal(eleccionId, idNumerico, categoriaId);
|
|
}
|
|
else
|
|
{
|
|
// CASO 3: El ID es un string corto como "02" o "06" -> Vista Provincial
|
|
return await GetPanelProvincial(eleccionId, ambitoId, categoriaId);
|
|
}
|
|
}
|
|
|
|
private async Task<IActionResult> GetPanelMunicipal(int eleccionId, int ambitoId, int categoriaId)
|
|
{
|
|
// 1. Obtener la entidad del municipio y, a partir de ella, la de su provincia.
|
|
var municipio = await _dbContext.AmbitosGeograficos.AsNoTracking()
|
|
.FirstOrDefaultAsync(a => a.Id == ambitoId && a.NivelId == 30);
|
|
|
|
if (municipio == null) return NotFound($"No se encontró el municipio con ID {ambitoId}.");
|
|
|
|
var provincia = await _dbContext.AmbitosGeograficos.AsNoTracking()
|
|
.FirstOrDefaultAsync(a => a.DistritoId == municipio.DistritoId && a.NivelId == 10);
|
|
|
|
// Si por alguna razón no encontramos la provincia, no podemos continuar.
|
|
if (provincia == null) return NotFound($"No se pudo determinar la provincia para el municipio con ID {ambitoId}.");
|
|
|
|
// 2. Cargar todos los overrides de candidatos y logos relevantes (igual que en la vista provincial).
|
|
var todosLosOverrides = await _dbContext.CandidatosOverrides.AsNoTracking()
|
|
.Where(c => c.EleccionId == eleccionId || c.EleccionId == 0).ToListAsync();
|
|
var todosLosLogos = await _dbContext.LogosAgrupacionesCategorias.AsNoTracking()
|
|
.Where(l => l.EleccionId == eleccionId || l.EleccionId == 0).ToListAsync();
|
|
|
|
// 3. Obtener los votos solo para ESE municipio (esto no cambia).
|
|
var resultadosCrudos = await _dbContext.ResultadosVotos.AsNoTracking()
|
|
.Include(r => r.AgrupacionPolitica)
|
|
.Where(r => r.EleccionId == eleccionId &&
|
|
r.CategoriaId == categoriaId &&
|
|
r.AmbitoGeograficoId == ambitoId)
|
|
.ToListAsync();
|
|
|
|
// El resto de la lógica es muy similar, pero ahora usamos los helpers con el ID de la provincia.
|
|
if (!resultadosCrudos.Any())
|
|
{
|
|
return Ok(new PanelElectoralDto
|
|
{
|
|
AmbitoNombre = municipio.Nombre,
|
|
MapaData = new List<ResultadoMapaDto>(),
|
|
ResultadosPanel = new List<AgrupacionResultadoDto>(),
|
|
EstadoRecuento = new EstadoRecuentoDto()
|
|
});
|
|
}
|
|
|
|
var totalVotosMunicipio = (decimal)resultadosCrudos.Sum(r => r.CantidadVotos);
|
|
var resultadosPanel = resultadosCrudos
|
|
.Select(g =>
|
|
{
|
|
// 4. ¡LA CLAVE! Usamos el ID de la PROVINCIA para buscar el override.
|
|
var candidatoMatch = FindBestCandidatoMatch(todosLosOverrides, g.AgrupacionPolitica.Id, categoriaId, provincia.Id, eleccionId);
|
|
var logoMatch = FindBestLogoMatch(todosLosLogos, g.AgrupacionPolitica.Id, categoriaId, provincia.Id, eleccionId);
|
|
|
|
return new AgrupacionResultadoDto
|
|
{
|
|
Id = g.AgrupacionPolitica.Id,
|
|
Nombre = g.AgrupacionPolitica.Nombre,
|
|
NombreCorto = g.AgrupacionPolitica.NombreCorto,
|
|
Color = g.AgrupacionPolitica.Color,
|
|
Votos = g.CantidadVotos,
|
|
Porcentaje = totalVotosMunicipio > 0 ? (g.CantidadVotos / totalVotosMunicipio) * 100 : 0,
|
|
// Asignamos los datos del override encontrado
|
|
NombreCandidato = candidatoMatch?.NombreCandidato,
|
|
LogoUrl = logoMatch?.LogoUrl
|
|
};
|
|
})
|
|
.OrderByDescending(r => r.Votos)
|
|
.ToList();
|
|
|
|
var estadoRecuento = await _dbContext.EstadosRecuentos
|
|
.AsNoTracking()
|
|
.FirstOrDefaultAsync(e => e.EleccionId == eleccionId && e.AmbitoGeograficoId == ambitoId && e.CategoriaId == categoriaId);
|
|
|
|
var respuesta = new PanelElectoralDto
|
|
{
|
|
AmbitoNombre = municipio.Nombre,
|
|
MapaData = new List<ResultadoMapaDto>(),
|
|
ResultadosPanel = resultadosPanel,
|
|
EstadoRecuento = new EstadoRecuentoDto
|
|
{
|
|
ParticipacionPorcentaje = estadoRecuento?.ParticipacionPorcentaje ?? 0,
|
|
MesasTotalizadasPorcentaje = estadoRecuento?.MesasTotalizadasPorcentaje ?? 0
|
|
}
|
|
};
|
|
|
|
return Ok(respuesta);
|
|
}
|
|
|
|
// Este método se ejecutará cuando la URL sea, por ejemplo, /api/elecciones/2/panel/02?categoriaId=2
|
|
private async Task<IActionResult> GetPanelProvincial(int eleccionId, string distritoId, int categoriaId)
|
|
{
|
|
var provincia = await _dbContext.AmbitosGeograficos.AsNoTracking()
|
|
.FirstOrDefaultAsync(a => a.DistritoId == distritoId && a.NivelId == 10);
|
|
if (provincia == null) return NotFound($"No se encontró la provincia con DistritoId {distritoId}.");
|
|
|
|
// --- INICIO DE LA MODIFICACIÓN ---
|
|
var todosLosOverrides = await _dbContext.CandidatosOverrides.AsNoTracking().Where(c => c.EleccionId == eleccionId || c.EleccionId == 0).ToListAsync();
|
|
var todosLosLogos = await _dbContext.LogosAgrupacionesCategorias.AsNoTracking().Where(l => l.EleccionId == eleccionId || l.EleccionId == 0).ToListAsync();
|
|
// --- FIN DE LA MODIFICACIÓN ---
|
|
|
|
// ... (la lógica de agregación de votos no cambia)
|
|
var resultadosAgregados = await _dbContext.ResultadosVotos.AsNoTracking()
|
|
.Where(r => r.EleccionId == eleccionId && r.CategoriaId == categoriaId && r.AmbitoGeografico.DistritoId == distritoId && r.AmbitoGeografico.NivelId == 30)
|
|
.GroupBy(r => r.AgrupacionPolitica)
|
|
.Select(g => new { Agrupacion = g.Key, TotalVotos = g.Sum(r => r.CantidadVotos) })
|
|
.ToListAsync();
|
|
|
|
var totalVotosProvincia = (decimal)resultadosAgregados.Sum(r => r.TotalVotos);
|
|
|
|
var resultadosPanel = resultadosAgregados
|
|
.Select(g =>
|
|
{
|
|
// Aplicamos la misma lógica de búsqueda de overrides
|
|
var candidatoMatch = FindBestCandidatoMatch(todosLosOverrides, g.Agrupacion.Id, categoriaId, provincia.Id, eleccionId);
|
|
var logoMatch = FindBestLogoMatch(todosLosLogos, g.Agrupacion.Id, categoriaId, provincia.Id, eleccionId);
|
|
|
|
return new AgrupacionResultadoDto
|
|
{
|
|
Id = g.Agrupacion.Id,
|
|
Nombre = g.Agrupacion.Nombre,
|
|
NombreCorto = g.Agrupacion.NombreCorto,
|
|
Color = g.Agrupacion.Color,
|
|
Votos = g.TotalVotos,
|
|
Porcentaje = totalVotosProvincia > 0 ? (g.TotalVotos / totalVotosProvincia) * 100 : 0,
|
|
NombreCandidato = candidatoMatch?.NombreCandidato, // <-- DATO AÑADIDO
|
|
LogoUrl = logoMatch?.LogoUrl // <-- DATO AÑADIDO
|
|
};
|
|
})
|
|
.OrderByDescending(r => r.Votos)
|
|
.ToList();
|
|
|
|
var estadoRecuento = await _dbContext.EstadosRecuentosGenerales.AsNoTracking()
|
|
.FirstOrDefaultAsync(e => e.EleccionId == eleccionId && e.AmbitoGeograficoId == provincia.Id && e.CategoriaId == categoriaId);
|
|
|
|
var respuesta = new PanelElectoralDto
|
|
{
|
|
AmbitoNombre = provincia.Nombre,
|
|
MapaData = new List<ResultadoMapaDto>(), // Se carga por separado
|
|
ResultadosPanel = resultadosPanel,
|
|
EstadoRecuento = new EstadoRecuentoDto
|
|
{
|
|
ParticipacionPorcentaje = estadoRecuento?.ParticipacionPorcentaje ?? 0,
|
|
MesasTotalizadasPorcentaje = estadoRecuento?.MesasTotalizadasPorcentaje ?? 0
|
|
}
|
|
};
|
|
return Ok(respuesta);
|
|
}
|
|
|
|
private async Task<IActionResult> GetPanelNacional(int eleccionId, int categoriaId)
|
|
{
|
|
var todosLosOverrides = await _dbContext.CandidatosOverrides.AsNoTracking().Where(c => c.EleccionId == eleccionId || c.EleccionId == 0).ToListAsync();
|
|
var todosLosLogos = await _dbContext.LogosAgrupacionesCategorias.AsNoTracking().Where(l => l.EleccionId == eleccionId || l.EleccionId == 0).ToListAsync();
|
|
|
|
|
|
var resultadosAgregados = await _dbContext.ResultadosVotos.AsNoTracking()
|
|
.Where(r => r.EleccionId == eleccionId && r.CategoriaId == categoriaId)
|
|
.GroupBy(r => r.AgrupacionPolitica)
|
|
.Select(g => new { Agrupacion = g.Key, TotalVotos = g.Sum(r => r.CantidadVotos) })
|
|
.ToListAsync();
|
|
|
|
var totalVotosNacional = (decimal)resultadosAgregados.Sum(r => r.TotalVotos);
|
|
|
|
var resultadosPanel = resultadosAgregados
|
|
.Select(g =>
|
|
{
|
|
var candidatoMatch = FindBestCandidatoMatch(todosLosOverrides, g.Agrupacion.Id, categoriaId, null, eleccionId);
|
|
var logoMatch = FindBestLogoMatch(todosLosLogos, g.Agrupacion.Id, categoriaId, null, eleccionId);
|
|
|
|
return new AgrupacionResultadoDto
|
|
{
|
|
Id = g.Agrupacion.Id,
|
|
Nombre = g.Agrupacion.Nombre,
|
|
NombreCorto = g.Agrupacion.NombreCorto,
|
|
Color = g.Agrupacion.Color,
|
|
Votos = g.TotalVotos,
|
|
Porcentaje = totalVotosNacional > 0 ? (g.TotalVotos / totalVotosNacional) * 100 : 0,
|
|
NombreCandidato = null,
|
|
LogoUrl = logoMatch?.LogoUrl
|
|
};
|
|
})
|
|
.OrderByDescending(r => r.Votos)
|
|
.ToList();
|
|
|
|
var estadoRecuento = await _dbContext.EstadosRecuentosGenerales.AsNoTracking()
|
|
.FirstOrDefaultAsync(e => e.EleccionId == eleccionId && e.CategoriaId == categoriaId && e.AmbitoGeografico.NivelId == 0);
|
|
|
|
var respuesta = new PanelElectoralDto
|
|
{
|
|
AmbitoNombre = "Argentina",
|
|
MapaData = new List<ResultadoMapaDto>(),
|
|
ResultadosPanel = resultadosPanel,
|
|
EstadoRecuento = new EstadoRecuentoDto
|
|
{
|
|
ParticipacionPorcentaje = estadoRecuento?.ParticipacionPorcentaje ?? 0,
|
|
MesasTotalizadasPorcentaje = estadoRecuento?.MesasTotalizadasPorcentaje ?? 0
|
|
}
|
|
};
|
|
return Ok(respuesta);
|
|
}
|
|
|
|
[HttpGet("mapa-resultados")]
|
|
public async Task<IActionResult> GetResultadosMapaPorMunicipio(
|
|
[FromRoute] int eleccionId,
|
|
[FromQuery] int categoriaId,
|
|
[FromQuery] string? distritoId = null)
|
|
{
|
|
if (string.IsNullOrEmpty(distritoId))
|
|
{
|
|
// PASO 1: Agrupar y sumar los votos por provincia y partido directamente en la BD.
|
|
// Esto crea una lista con los totales, que es mucho más pequeña que los datos crudos.
|
|
var votosAgregadosPorProvincia = await _dbContext.ResultadosVotos
|
|
.AsNoTracking()
|
|
.Where(r => r.EleccionId == eleccionId
|
|
&& r.CategoriaId == categoriaId
|
|
&& r.AmbitoGeografico.NivelId == 30
|
|
&& r.AmbitoGeografico.DistritoId != null)
|
|
.GroupBy(r => new { r.AmbitoGeografico.DistritoId, r.AgrupacionPoliticaId })
|
|
.Select(g => new
|
|
{
|
|
DistritoId = g.Key.DistritoId!, // Sabemos que no es nulo por el .Where()
|
|
AgrupacionPoliticaId = g.Key.AgrupacionPoliticaId,
|
|
TotalVotos = g.Sum(r => r.CantidadVotos)
|
|
})
|
|
.ToListAsync();
|
|
|
|
// PASO 2: Encontrar el ganador para cada provincia en la memoria de la aplicación.
|
|
// Esto es muy rápido porque se hace sobre la lista ya agregada.
|
|
var ganadoresPorProvincia = votosAgregadosPorProvincia
|
|
.GroupBy(r => r.DistritoId)
|
|
.Select(g => g.OrderByDescending(x => x.TotalVotos).First())
|
|
.ToList();
|
|
|
|
// PASO 3: Obtener los datos adicionales (nombres, colores) para construir la respuesta final.
|
|
var agrupacionesInfo = await _dbContext.AgrupacionesPoliticas.AsNoTracking().ToDictionaryAsync(a => a.Id);
|
|
var provinciasInfo = await _dbContext.AmbitosGeograficos.AsNoTracking().Where(a => a.NivelId == 10).ToListAsync();
|
|
|
|
var mapaDataNacional = ganadoresPorProvincia.Select(g => new ResultadoMapaDto
|
|
{
|
|
AmbitoId = g.DistritoId,
|
|
AmbitoNombre = provinciasInfo.FirstOrDefault(p => p.DistritoId == g.DistritoId)?.Nombre ?? "Desconocido",
|
|
AgrupacionGanadoraId = g.AgrupacionPoliticaId,
|
|
ColorGanador = agrupacionesInfo.GetValueOrDefault(g.AgrupacionPoliticaId)?.Color ?? "#808080"
|
|
}).ToList();
|
|
|
|
return Ok(mapaDataNacional);
|
|
}
|
|
else
|
|
{
|
|
// --- VISTA PROVINCIAL ---
|
|
var votosAgregadosPorMunicipio = await _dbContext.ResultadosVotos
|
|
.AsNoTracking()
|
|
.Where(r => r.EleccionId == eleccionId
|
|
&& r.CategoriaId == categoriaId
|
|
&& r.AmbitoGeografico.DistritoId == distritoId
|
|
&& r.AmbitoGeografico.NivelId == 30)
|
|
.GroupBy(r => new { r.AmbitoGeograficoId, r.AgrupacionPoliticaId })
|
|
.Select(g => new
|
|
{
|
|
g.Key.AmbitoGeograficoId,
|
|
g.Key.AgrupacionPoliticaId,
|
|
TotalVotos = g.Sum(r => r.CantidadVotos)
|
|
})
|
|
.ToListAsync();
|
|
|
|
var ganadoresPorMunicipio = votosAgregadosPorMunicipio
|
|
.GroupBy(r => r.AmbitoGeograficoId)
|
|
.Select(g => g.OrderByDescending(x => x.TotalVotos).First())
|
|
.ToList();
|
|
|
|
var idsMunicipios = ganadoresPorMunicipio.Select(g => g.AmbitoGeograficoId).ToList();
|
|
var idsAgrupaciones = ganadoresPorMunicipio.Select(g => g.AgrupacionPoliticaId).ToList();
|
|
|
|
var municipiosInfo = await _dbContext.AmbitosGeograficos.AsNoTracking()
|
|
.Where(a => idsMunicipios.Contains(a.Id)).ToDictionaryAsync(a => a.Id);
|
|
|
|
var agrupacionesInfo = await _dbContext.AgrupacionesPoliticas.AsNoTracking()
|
|
.Where(a => idsAgrupaciones.Contains(a.Id)).ToDictionaryAsync(a => a.Id);
|
|
|
|
var mapaDataProvincial = ganadoresPorMunicipio.Select(g => new ResultadoMapaDto
|
|
{
|
|
AmbitoId = g.AmbitoGeograficoId.ToString(),
|
|
AmbitoNombre = municipiosInfo.GetValueOrDefault(g.AmbitoGeograficoId)?.Nombre ?? "Desconocido",
|
|
AgrupacionGanadoraId = g.AgrupacionPoliticaId,
|
|
ColorGanador = agrupacionesInfo.GetValueOrDefault(g.AgrupacionPoliticaId)?.Color ?? "#808080"
|
|
}).ToList();
|
|
|
|
return Ok(mapaDataProvincial);
|
|
}
|
|
}
|
|
|
|
[HttpGet("composicion-nacional")]
|
|
public async Task<IActionResult> GetComposicionNacional([FromRoute] int eleccionId)
|
|
{
|
|
// 1. Obtener todas las configuraciones relevantes en una sola consulta.
|
|
var config = await _dbContext.Configuraciones.AsNoTracking().ToDictionaryAsync(c => c.Clave, c => c.Valor);
|
|
|
|
// 2. Obtener todas las agrupaciones políticas en una sola consulta.
|
|
var todasAgrupaciones = await _dbContext.AgrupacionesPoliticas.AsNoTracking().ToDictionaryAsync(a => a.Id);
|
|
|
|
// 3. Obtener las bancas PREVIAS (las que no están en juego).
|
|
var bancasPrevias = await _dbContext.BancasPrevias
|
|
.AsNoTracking()
|
|
.Where(b => b.EleccionId == eleccionId)
|
|
.ToListAsync();
|
|
|
|
// 4. Obtener las bancas EN JUEGO (proyectadas por provincia).
|
|
var proyecciones = await _dbContext.ProyeccionesBancas
|
|
.AsNoTracking()
|
|
.Where(p => p.EleccionId == eleccionId && p.AmbitoGeografico.NivelId == 10) // Nivel 10 = Ámbito Provincial
|
|
.ToListAsync();
|
|
|
|
//Calculamos la fecha de la última proyección.
|
|
// Si no hay proyecciones aún, usamos la fecha y hora actual como un fallback seguro.
|
|
var ultimaActualizacion = proyecciones.Any()
|
|
? proyecciones.Max(p => p.FechaTotalizacion)
|
|
: DateTime.UtcNow;
|
|
|
|
// 5. Combinar los datos para obtener la composición final de cada partido.
|
|
var composicionFinal = todasAgrupaciones.Values.Select(agrupacion => new
|
|
{
|
|
Agrupacion = agrupacion,
|
|
DiputadosFijos = bancasPrevias.FirstOrDefault(b => b.AgrupacionPoliticaId == agrupacion.Id && b.Camara == Core.Enums.TipoCamara.Diputados)?.Cantidad ?? 0,
|
|
DiputadosGanados = proyecciones.Where(p => p.AgrupacionPoliticaId == agrupacion.Id && p.CategoriaId == 2).Sum(p => p.NroBancas),
|
|
SenadoresFijos = bancasPrevias.FirstOrDefault(b => b.AgrupacionPoliticaId == agrupacion.Id && b.Camara == Core.Enums.TipoCamara.Senadores)?.Cantidad ?? 0,
|
|
SenadoresGanados = proyecciones.Where(p => p.AgrupacionPoliticaId == agrupacion.Id && p.CategoriaId == 1).Sum(p => p.NroBancas)
|
|
})
|
|
.Select(r => new
|
|
{
|
|
r.Agrupacion,
|
|
r.DiputadosFijos,
|
|
r.DiputadosGanados,
|
|
DiputadosTotales = r.DiputadosFijos + r.DiputadosGanados,
|
|
r.SenadoresFijos,
|
|
r.SenadoresGanados,
|
|
SenadoresTotales = r.SenadoresFijos + r.SenadoresGanados
|
|
})
|
|
.ToList();
|
|
|
|
// 6. Determinar la información de la presidencia para cada cámara.
|
|
config.TryGetValue("PresidenciaDiputadosNacional", out var idPartidoPresDip);
|
|
var partidoPresidenteDiputados = !string.IsNullOrEmpty(idPartidoPresDip) ? todasAgrupaciones.GetValueOrDefault(idPartidoPresDip) : null;
|
|
config.TryGetValue("PresidenciaDiputadosNacional_TipoBanca", out var tipoBancaDip);
|
|
|
|
config.TryGetValue("PresidenciaSenadoNacional", out var idPartidoPresSen);
|
|
var partidoPresidenteSenadores = !string.IsNullOrEmpty(idPartidoPresSen) ? todasAgrupaciones.GetValueOrDefault(idPartidoPresSen) : null;
|
|
config.TryGetValue("PresidenciaSenadoNacional_TipoBanca", out var tipoBancaSen);
|
|
|
|
|
|
// 7. Construir el objeto de respuesta final para D Diputados
|
|
var diputados = new
|
|
{
|
|
CamaraNombre = "Cámara de Diputados",
|
|
TotalBancas = 257,
|
|
BancasEnJuego = 127,
|
|
UltimaActualizacion = ultimaActualizacion,
|
|
Partidos = composicionFinal
|
|
.Where(p => p.DiputadosTotales > 0)
|
|
.OrderByDescending(p => p.DiputadosTotales)
|
|
.Select(p => new
|
|
{
|
|
p.Agrupacion.Id,
|
|
p.Agrupacion.Nombre,
|
|
p.Agrupacion.NombreCorto,
|
|
p.Agrupacion.Color,
|
|
BancasFijos = p.DiputadosFijos,
|
|
BancasGanadas = p.DiputadosGanados,
|
|
BancasTotales = p.DiputadosTotales,
|
|
p.Agrupacion.OrdenDiputadosNacionales,
|
|
p.Agrupacion.OrdenSenadoresNacionales
|
|
}).ToList(),
|
|
PresidenteBancada = partidoPresidenteDiputados != null
|
|
? new { Color = partidoPresidenteDiputados.Color, TipoBanca = tipoBancaDip ?? "ganada" }
|
|
: null
|
|
};
|
|
|
|
// 8. Construir el objeto de respuesta final para Senadores
|
|
var senadores = new
|
|
{
|
|
CamaraNombre = "Senado de la Nación",
|
|
TotalBancas = 72,
|
|
BancasEnJuego = 24,
|
|
UltimaActualizacion = ultimaActualizacion,
|
|
Partidos = composicionFinal
|
|
.Where(p => p.SenadoresTotales > 0)
|
|
.OrderByDescending(p => p.SenadoresTotales)
|
|
.Select(p => new
|
|
{
|
|
p.Agrupacion.Id,
|
|
p.Agrupacion.Nombre,
|
|
p.Agrupacion.NombreCorto,
|
|
p.Agrupacion.Color,
|
|
BancasFijos = p.SenadoresFijos,
|
|
BancasGanadas = p.SenadoresGanados,
|
|
BancasTotales = p.SenadoresTotales,
|
|
p.Agrupacion.OrdenDiputadosNacionales,
|
|
p.Agrupacion.OrdenSenadoresNacionales
|
|
}).ToList(),
|
|
PresidenteBancada = partidoPresidenteSenadores != null
|
|
? new { Color = partidoPresidenteSenadores.Color, TipoBanca = tipoBancaSen ?? "ganada" }
|
|
: null
|
|
};
|
|
|
|
return Ok(new { Diputados = diputados, Senadores = senadores });
|
|
}
|
|
|
|
// --- INICIO DE FUNCIONES DE AYUDA ---
|
|
private CandidatoOverride? FindBestCandidatoMatch(
|
|
List<CandidatoOverride> overrides, string agrupacionId, int categoriaId, int? ambitoId, int eleccionId)
|
|
{
|
|
return overrides.FirstOrDefault(c => c.EleccionId == eleccionId && c.AgrupacionPoliticaId == agrupacionId && c.CategoriaId == categoriaId && c.AmbitoGeograficoId == ambitoId)
|
|
?? overrides.FirstOrDefault(c => c.EleccionId == eleccionId && c.AgrupacionPoliticaId == agrupacionId && c.CategoriaId == categoriaId && c.AmbitoGeograficoId == null)
|
|
?? overrides.FirstOrDefault(c => c.EleccionId == 0 && c.AgrupacionPoliticaId == agrupacionId && c.CategoriaId == categoriaId && c.AmbitoGeograficoId == ambitoId)
|
|
?? overrides.FirstOrDefault(c => c.EleccionId == 0 && c.AgrupacionPoliticaId == agrupacionId && c.CategoriaId == categoriaId && c.AmbitoGeograficoId == null);
|
|
}
|
|
private LogoAgrupacionCategoria? FindBestLogoMatch(
|
|
List<LogoAgrupacionCategoria> logos, string agrupacionId, int categoriaId, int? ambitoId, int eleccionId)
|
|
{
|
|
// Prioridad 1: Coincidencia exacta (Elección, Categoría, Ámbito)
|
|
return logos.FirstOrDefault(l => l.EleccionId == eleccionId && l.AgrupacionPoliticaId == agrupacionId && l.CategoriaId == categoriaId && l.AmbitoGeograficoId == ambitoId)
|
|
// Prioridad 2: Coincidencia por Elección y Categoría (Ámbito genérico)
|
|
?? logos.FirstOrDefault(l => l.EleccionId == eleccionId && l.AgrupacionPoliticaId == agrupacionId && l.CategoriaId == categoriaId && l.AmbitoGeograficoId == null)
|
|
// Prioridad 3: Coincidencia de Fallback por Ámbito (Elección genérica)
|
|
?? logos.FirstOrDefault(l => l.EleccionId == 0 && l.AgrupacionPoliticaId == agrupacionId && l.CategoriaId == categoriaId && l.AmbitoGeograficoId == ambitoId)
|
|
// Prioridad 4: Coincidencia de Fallback por Categoría (Elección y Ámbito genéricos)
|
|
?? logos.FirstOrDefault(l => l.EleccionId == 0 && l.AgrupacionPoliticaId == agrupacionId && l.CategoriaId == categoriaId && l.AmbitoGeograficoId == null)
|
|
// Prioridad 5: LOGO GLOBAL. Coincidencia solo por Partido (Elección y Categoría genéricas)
|
|
?? logos.FirstOrDefault(l => l.EleccionId == 0 && l.AgrupacionPoliticaId == agrupacionId && l.CategoriaId == 0 && l.AmbitoGeograficoId == null);
|
|
}
|
|
|
|
[HttpGet("resumen-por-provincia")]
|
|
public async Task<IActionResult> GetResumenPorProvincia(
|
|
[FromRoute] int eleccionId,
|
|
[FromQuery] string? focoDistritoId = null,
|
|
[FromQuery] int? focoCategoriaId = null,
|
|
[FromQuery] int cantidadResultados = 2)
|
|
{
|
|
if (cantidadResultados < 1) cantidadResultados = 1;
|
|
|
|
const int catDiputadosNac = 2;
|
|
const int catSenadoresNac = 1;
|
|
|
|
var provinciasQueRenuevanSenadores = new HashSet<string> { "01", "06", "08", "15", "16", "17", "22", "23" };
|
|
var todasLasProyecciones = await _dbContext.ProyeccionesBancas.AsNoTracking().Where(p => p.EleccionId == eleccionId && (p.CategoriaId == catDiputadosNac || p.CategoriaId == catSenadoresNac)).ToDictionaryAsync(p => p.AmbitoGeograficoId + "_" + p.AgrupacionPoliticaId + "_" + p.CategoriaId);
|
|
var todosLosOverrides = await _dbContext.CandidatosOverrides.AsNoTracking().Where(c => c.EleccionId == eleccionId || c.EleccionId == 0).ToListAsync();
|
|
var todosLosLogos = await _dbContext.LogosAgrupacionesCategorias.AsNoTracking().Where(l => l.EleccionId == eleccionId || l.EleccionId == 0).ToListAsync();
|
|
var todosLosEstados = await _dbContext.EstadosRecuentosGenerales.AsNoTracking().Include(e => e.CategoriaElectoral).Where(e => e.EleccionId == eleccionId && (e.CategoriaId == catDiputadosNac || e.CategoriaId == catSenadoresNac)).ToDictionaryAsync(e => e.AmbitoGeograficoId + "_" + e.CategoriaId);
|
|
var mapaMunicipioADistrito = await _dbContext.AmbitosGeograficos.AsNoTracking().Where(a => a.NivelId == 30 && a.DistritoId != null).ToDictionaryAsync(a => a.Id, a => a.DistritoId!);
|
|
var todosLosVotos = await _dbContext.ResultadosVotos.AsNoTracking().Where(r => r.EleccionId == eleccionId && (r.CategoriaId == catDiputadosNac || r.CategoriaId == catSenadoresNac)).Select(r => new { r.AmbitoGeograficoId, r.CategoriaId, r.AgrupacionPoliticaId, r.CantidadVotos }).ToListAsync();
|
|
var votosAgregados = todosLosVotos.Where(v => mapaMunicipioADistrito.ContainsKey(v.AmbitoGeograficoId)).GroupBy(v => new { DistritoId = mapaMunicipioADistrito[v.AmbitoGeograficoId], v.CategoriaId, v.AgrupacionPoliticaId }).Select(g => new { g.Key.DistritoId, g.Key.CategoriaId, g.Key.AgrupacionPoliticaId, Votos = g.Sum(v => v.CantidadVotos) }).ToList();
|
|
|
|
var agrupaciones = await _dbContext.AgrupacionesPoliticas.AsNoTracking().ToDictionaryAsync(a => a.Id);
|
|
var resultadosFinales = new List<ResumenProvinciaDto>();
|
|
var provinciasQuery = _dbContext.AmbitosGeograficos.AsNoTracking().Where(a => a.NivelId == 10);
|
|
if (!string.IsNullOrEmpty(focoDistritoId)) { provinciasQuery = provinciasQuery.Where(p => p.DistritoId == focoDistritoId); }
|
|
var provincias = await provinciasQuery.ToListAsync();
|
|
if (!provincias.Any()) { return Ok(resultadosFinales); }
|
|
|
|
foreach (var provincia in provincias.OrderBy(p => p.Nombre))
|
|
{
|
|
var categoriasDeLaProvincia = new List<int>();
|
|
if (focoCategoriaId.HasValue) { if ((focoCategoriaId.Value == catDiputadosNac) || (focoCategoriaId.Value == catSenadoresNac && provinciasQueRenuevanSenadores.Contains(provincia.DistritoId!))) categoriasDeLaProvincia.Add(focoCategoriaId.Value); }
|
|
else { categoriasDeLaProvincia.Add(catDiputadosNac); if (provinciasQueRenuevanSenadores.Contains(provincia.DistritoId!)) categoriasDeLaProvincia.Add(catSenadoresNac); }
|
|
|
|
var dtoProvincia = new ResumenProvinciaDto { ProvinciaId = provincia.DistritoId!, ProvinciaNombre = provincia.Nombre, Categorias = new List<CategoriaResumenDto>() };
|
|
|
|
foreach (var categoriaId in categoriasDeLaProvincia)
|
|
{
|
|
var resultadosCategoriaCompleta = votosAgregados.Where(r => r.DistritoId == provincia.DistritoId && r.CategoriaId == categoriaId);
|
|
var totalVotosCategoria = (decimal)resultadosCategoriaCompleta.Sum(r => r.Votos);
|
|
var votosAgrupados = resultadosCategoriaCompleta.OrderByDescending(x => x.Votos).Take(cantidadResultados).Select(r => new { Agrupacion = agrupaciones[r.AgrupacionPoliticaId], r.Votos }).ToList();
|
|
todosLosEstados.TryGetValue(provincia.Id + "_" + categoriaId, out var estado);
|
|
|
|
dtoProvincia.Categorias.Add(new CategoriaResumenDto
|
|
{
|
|
CategoriaId = categoriaId,
|
|
CategoriaNombre = estado?.CategoriaElectoral.Nombre ?? (categoriaId == catDiputadosNac ? "DIPUTADOS NACIONALES" : "SENADORES NACIONALES"),
|
|
EstadoRecuento = estado != null ? new EstadoRecuentoDto
|
|
{
|
|
ParticipacionPorcentaje = estado.ParticipacionPorcentaje,
|
|
MesasTotalizadasPorcentaje = estado.MesasTotalizadasPorcentaje,
|
|
CantidadVotantes = estado.CantidadVotantes
|
|
} : null,
|
|
Resultados = votosAgrupados.Select(r =>
|
|
{
|
|
var provinciaAmbitoId = provincia.Id;
|
|
var candidatoMatch = FindBestCandidatoMatch(todosLosOverrides, r.Agrupacion.Id, categoriaId, provinciaAmbitoId, eleccionId);
|
|
|
|
string? logoFinal =
|
|
// Prioridad 1: Override Específico (EleccionId, CategoriaId, AmbitoId)
|
|
todosLosLogos.FirstOrDefault(l => l.EleccionId == eleccionId && l.AgrupacionPoliticaId == r.Agrupacion.Id && l.CategoriaId == categoriaId && l.AmbitoGeograficoId == provinciaAmbitoId)?.LogoUrl
|
|
// Prioridad 2: Override por Elección y Categoría (general a ámbitos)
|
|
?? todosLosLogos.FirstOrDefault(l => l.EleccionId == eleccionId && l.AgrupacionPoliticaId == r.Agrupacion.Id && l.CategoriaId == categoriaId && l.AmbitoGeograficoId == null)?.LogoUrl
|
|
// Prioridad 3: Fallback Global para la CATEGORÍA (EleccionId = 0, CategoriaId)
|
|
?? todosLosLogos.FirstOrDefault(l => l.EleccionId == 0 && l.AgrupacionPoliticaId == r.Agrupacion.Id && l.CategoriaId == categoriaId && l.AmbitoGeograficoId == null)?.LogoUrl
|
|
// Prioridad 4: Fallback "Super Global" (EleccionId = 0, CategoriaId = 0)
|
|
?? todosLosLogos.FirstOrDefault(l => l.EleccionId == 0 && l.AgrupacionPoliticaId == r.Agrupacion.Id && l.CategoriaId == 0 && l.AmbitoGeograficoId == null)?.LogoUrl;
|
|
|
|
return new ResultadoCandidatoDto
|
|
{
|
|
AgrupacionId = r.Agrupacion.Id,
|
|
NombreAgrupacion = r.Agrupacion.Nombre,
|
|
NombreCortoAgrupacion = r.Agrupacion.NombreCorto,
|
|
NombreCandidato = candidatoMatch?.NombreCandidato,
|
|
Color = r.Agrupacion.Color,
|
|
Votos = r.Votos,
|
|
FotoUrl = logoFinal,
|
|
BancasObtenidas = todasLasProyecciones.TryGetValue(provinciaAmbitoId + "_" + r.Agrupacion.Id + "_" + categoriaId, out var proyeccion) ? proyeccion.NroBancas : 0,
|
|
Porcentaje = totalVotosCategoria > 0 ? (r.Votos / totalVotosCategoria) * 100 : 0
|
|
};
|
|
}).ToList()
|
|
});
|
|
}
|
|
if (dtoProvincia.Categorias.Any()) { resultadosFinales.Add(dtoProvincia); }
|
|
}
|
|
return Ok(resultadosFinales);
|
|
}
|
|
|
|
[HttpGet("~/api/elecciones/home-resumen")]
|
|
public async Task<IActionResult> GetHomeResumen(
|
|
[FromQuery] int eleccionId,
|
|
[FromQuery] string distritoId,
|
|
[FromQuery] int categoriaId)
|
|
{
|
|
var provincia = await _dbContext.AmbitosGeograficos.AsNoTracking()
|
|
.FirstOrDefaultAsync(a => a.DistritoId == distritoId && a.NivelId == 10);
|
|
|
|
if (provincia == null) return NotFound($"No se encontró la provincia con DistritoId {distritoId}.");
|
|
|
|
var votosAgregados = await _dbContext.ResultadosVotos.AsNoTracking()
|
|
.Where(r => r.EleccionId == eleccionId &&
|
|
r.CategoriaId == categoriaId &&
|
|
r.AmbitoGeografico.DistritoId == distritoId)
|
|
.GroupBy(r => r.AgrupacionPolitica)
|
|
.Select(g => new { Agrupacion = g.Key, Votos = g.Sum(r => r.CantidadVotos) })
|
|
.OrderByDescending(x => x.Votos)
|
|
.ToListAsync();
|
|
|
|
var todosLosOverrides = await _dbContext.CandidatosOverrides.AsNoTracking().Where(c => c.EleccionId == eleccionId || c.EleccionId == 0).ToListAsync();
|
|
var todosLosLogos = await _dbContext.LogosAgrupacionesCategorias.AsNoTracking().Where(l => l.EleccionId == eleccionId || l.EleccionId == 0).ToListAsync();
|
|
var estado = await _dbContext.EstadosRecuentosGenerales.AsNoTracking().Include(e => e.CategoriaElectoral).FirstOrDefaultAsync(e => e.EleccionId == eleccionId && e.CategoriaId == categoriaId && e.AmbitoGeograficoId == provincia.Id);
|
|
var votosNoPositivosAgregados = await _dbContext.EstadosRecuentos.AsNoTracking().Where(e => e.EleccionId == eleccionId && e.CategoriaId == categoriaId && e.AmbitoGeografico.DistritoId == distritoId).GroupBy(e => 1).Select(g => new { VotosEnBlanco = g.Sum(e => e.VotosEnBlanco), VotosNulos = g.Sum(e => e.VotosNulos), VotosRecurridos = g.Sum(e => e.VotosRecurridos) }).FirstOrDefaultAsync();
|
|
|
|
var totalVotosPositivos = (decimal)votosAgregados.Sum(r => r.Votos);
|
|
var votosEnBlanco = votosNoPositivosAgregados?.VotosEnBlanco ?? 0;
|
|
var votosTotales = totalVotosPositivos + votosEnBlanco + (votosNoPositivosAgregados?.VotosNulos ?? 0) + (votosNoPositivosAgregados?.VotosRecurridos ?? 0);
|
|
|
|
var respuesta = new CategoriaResumenHomeDto
|
|
{
|
|
CategoriaId = categoriaId,
|
|
CategoriaNombre = estado?.CategoriaElectoral.Nombre ?? (categoriaId == 2 ? "DIPUTADOS NACIONALES" : "SENADORES NACIONALES"),
|
|
UltimaActualizacion = estado?.FechaTotalizacion ?? DateTime.UtcNow,
|
|
EstadoRecuento = estado != null ? new EstadoRecuentoDto
|
|
{
|
|
ParticipacionPorcentaje = estado.ParticipacionPorcentaje,
|
|
MesasTotalizadasPorcentaje = estado.MesasTotalizadasPorcentaje,
|
|
CantidadVotantes = estado.CantidadVotantes
|
|
} : null,
|
|
VotosEnBlanco = votosEnBlanco,
|
|
VotosEnBlancoPorcentaje = votosTotales > 0 ? (votosEnBlanco / votosTotales) * 100 : 0,
|
|
VotosTotales = (long)votosTotales,
|
|
Resultados = votosAgregados.Select(r =>
|
|
{
|
|
var provinciaAmbitoId = provincia.Id;
|
|
var candidatoMatch = FindBestCandidatoMatch(todosLosOverrides, r.Agrupacion.Id, categoriaId, provinciaAmbitoId, eleccionId);
|
|
|
|
string? logoFinal =
|
|
// Prioridad 1: Override Específico (EleccionId, CategoriaId, AmbitoId)
|
|
todosLosLogos.FirstOrDefault(l => l.EleccionId == eleccionId && l.AgrupacionPoliticaId == r.Agrupacion.Id && l.CategoriaId == categoriaId && l.AmbitoGeograficoId == provinciaAmbitoId)?.LogoUrl
|
|
// Prioridad 2: Override por Elección y Categoría (general a ámbitos)
|
|
?? todosLosLogos.FirstOrDefault(l => l.EleccionId == eleccionId && l.AgrupacionPoliticaId == r.Agrupacion.Id && l.CategoriaId == categoriaId && l.AmbitoGeograficoId == null)?.LogoUrl
|
|
// Prioridad 3: Fallback Global para la CATEGORÍA (EleccionId = 0, CategoriaId)
|
|
?? todosLosLogos.FirstOrDefault(l => l.EleccionId == 0 && l.AgrupacionPoliticaId == r.Agrupacion.Id && l.CategoriaId == categoriaId && l.AmbitoGeograficoId == null)?.LogoUrl
|
|
// Prioridad 4: Fallback "Super Global" (EleccionId = 0, CategoriaId = 0)
|
|
?? todosLosLogos.FirstOrDefault(l => l.EleccionId == 0 && l.AgrupacionPoliticaId == r.Agrupacion.Id && l.CategoriaId == 0 && l.AmbitoGeograficoId == null)?.LogoUrl;
|
|
|
|
return new ResultadoCandidatoDto
|
|
{
|
|
AgrupacionId = r.Agrupacion.Id,
|
|
NombreAgrupacion = r.Agrupacion.Nombre,
|
|
NombreCortoAgrupacion = r.Agrupacion.NombreCorto,
|
|
NombreCandidato = candidatoMatch?.NombreCandidato,
|
|
Color = r.Agrupacion.Color,
|
|
Votos = r.Votos,
|
|
Porcentaje = totalVotosPositivos > 0 ? (r.Votos / totalVotosPositivos) * 100 : 0,
|
|
FotoUrl = logoFinal
|
|
};
|
|
}).ToList()
|
|
};
|
|
|
|
return Ok(respuesta);
|
|
}
|
|
} |