From 747391eade1b1628f383069c98e055b628e81dc0 Mon Sep 17 00:00:00 2001 From: Leonel Toro Date: Thu, 17 Jul 2025 12:40:59 -0400 Subject: [PATCH] feat: ajustes para las mediciones --- BLL/Recuperacion_DGA/EnvioDGA.cs | 88 ++++++++++++++---------- DAL/LogEnvioRepository.cs | 29 ++++++++ DAL/MedicionDGARepository.cs | 32 +++++++++ DAS/RegistrarMedicion.cs | 2 +- SHARED/DTO/Envios_DGA/DatoDGATemporal.cs | 18 +++++ 5 files changed, 133 insertions(+), 36 deletions(-) create mode 100644 SHARED/DTO/Envios_DGA/DatoDGATemporal.cs diff --git a/BLL/Recuperacion_DGA/EnvioDGA.cs b/BLL/Recuperacion_DGA/EnvioDGA.cs index d5b30c6..96a7863 100644 --- a/BLL/Recuperacion_DGA/EnvioDGA.cs +++ b/BLL/Recuperacion_DGA/EnvioDGA.cs @@ -1,4 +1,5 @@ -using System.Text.Json; +using System; +using System.Text.Json; using DAL; using DAS; using Shared.DTO; @@ -34,49 +35,66 @@ namespace BLL.Recuperacion_DGA ConsoleLoggerHelper.WriteLineAndLogEventoAsync("INICIO", "Inicio proceso de recuperación DGA", ""); ConsoleLoggerHelper.WriteLineAndLogInfo("Obteniendo Mediciones Scada", ConsoleColor.Green); - var mediciones = await _dGAMedicionRepository.ObtenerMedicionesAsync(); - var medicionesEnviadas = new List(); - var logs = new List(); + var logsEnviados = new List(); - foreach (var medicion in mediciones) + var pageNumber = 1; + + while (true) { + var mediciones = await _dGAMedicionRepository.ObtenerMedicionesPorLoteAsync(pageNumber); + + if (mediciones == null || !mediciones.Any()){ break; } + + foreach (var medicion in mediciones) + { + try + { + var fechaEnvio = DateTime.UtcNow; + + var body = new MedicionSubterraneaRequest + { + Autenticacion = new Autenticacion + { + Password = CredencialDGA.Password, + RutEmpresa = medicion.TIPO_EMPRESA == "EV" ? CredencialDGA.RutEsval : CredencialDGA.RutAv, + RutUsuario = CredencialDGA.RutUsuario + }, + MedicionSubterranea = new Medicion + { + Caudal = medicion.CAUDAL ?? "", + FechaMedicion = medicion.FECHA_MEDICION_CAUDAL?.ToString("yyyy-MM-dd") ?? "", + HoraMedicion = medicion.FECHA_MEDICION_CAUDAL?.ToString("HH:mm:ss") ?? "", + NivelFreaticoDelPozo = medicion.NIVEL_FREATICO_DEL_POZO ?? "", + Totalizador = medicion.TOTALIZADOR_CAUDAL ?? "", + } + }; + + var response = await _registrarMedicion.EnviarMedicionAsync(medicion, body, fechaEnvio); + + logsEnviados.Add(response); + + } + catch (Exception ex) + { + ConsoleLoggerHelper.WriteLineAndLogInfo($"[Error]: medicion[{medicion.ID}], mensaje: {ex.Message}"); + } + } try { - var fechaEnvio = DateTime.UtcNow; - - var body = new MedicionSubterraneaRequest - { - Autenticacion = new Autenticacion - { - Password = CredencialDGA.Password, - RutEmpresa = medicion.TIPO_EMPRESA == "EV" ? CredencialDGA.RutEsval : CredencialDGA.RutAv, - RutUsuario = CredencialDGA.RutUsuario - }, - MedicionSubterranea = new Medicion - { - Caudal = medicion.CAUDAL ?? "", - FechaMedicion = medicion.FECHA_MEDICION_CAUDAL?.ToString("yyyy-MM-dd") ?? "", - HoraMedicion = medicion.FECHA_MEDICION_CAUDAL?.ToString("HH:mm:ss") ?? "", - NivelFreaticoDelPozo = medicion.NIVEL_FREATICO_DEL_POZO ?? "", - Totalizador = medicion.TOTALIZADOR_CAUDAL ?? "", - } - }; - - var response = await _registrarMedicion.EnviarMedicionAsync(medicion, body, fechaEnvio); - - medicionesEnviadas.Add(new {Id = medicion.ID,FechaEnvio = fechaEnvio.ToString("yyyy-MM-dd HH:mm:ss"), Enviado = 1}); - logs.Add(response); + var idMediciones = mediciones.Select(x => x.ID).ToList(); + await _dGAMedicionRepository.GuardarMedicionesEnviadasAsync(idMediciones); + await _logEnvioRepository.InsertarLogRespuesta(logsEnviados); } - catch (Exception ex) + catch (Exception e) { - FileLoggerHelper.LogError($"[Error] {ex.Message}.", ex); - ConsoleLoggerHelper.WriteLineAndLogInfo($"Error al enviar la medición con ID {medicion.CODIGO_DGA}.", ConsoleColor.Red); + ConsoleLoggerHelper.WriteLineAndLogInfo($"[Error] {e.Message}"); } + + logsEnviados.Clear(); + mediciones.Clear(); + pageNumber++; } - //INSERTAR DATOS DE LOGS, ACTUALIZAR REGISTROS - - ConsoleLoggerHelper.WriteLineAndLogEventoAsync("FIN","Fin proceso de recuperación DGA",""); } catch (Exception ex) diff --git a/DAL/LogEnvioRepository.cs b/DAL/LogEnvioRepository.cs index 363a317..a5434c4 100644 --- a/DAL/LogEnvioRepository.cs +++ b/DAL/LogEnvioRepository.cs @@ -36,5 +36,34 @@ namespace DAL } } + public async Task InsertarLogRespuesta(List logsEnviados) + { + await using var connection = new SqlConnection(BdConexion.StringConnection); + try + { + var sql = @"INSERT INTO DGA_LOGS_ENVIOS ( + ESTADO_ENVIO, + JSON_ENVIO, + JSON_RESPUESTA, + COMPROBANTE, + FECHA_ENVIO, + ID_DGA_DATO) + VALUES ( + @ESTADO_ENVIO, + @JSON_ENVIO, + @JSON_RESPUESTA, + @COMPROBANTE, + @FECHA_ENVIO, + @ID_DGA_DATO);"; + + await connection.ExecuteAsync(sql, logsEnviados); + return true; + } + catch (Exception ex) + { + throw new Exception($"Error al insertar logs de respuesta {ex.Message}"); + } + } + } } diff --git a/DAL/MedicionDGARepository.cs b/DAL/MedicionDGARepository.cs index 4d4dced..fa470f0 100644 --- a/DAL/MedicionDGARepository.cs +++ b/DAL/MedicionDGARepository.cs @@ -18,5 +18,37 @@ namespace DAL return result.ToList(); } + + public async Task> ObtenerMedicionesPorLoteAsync(int pageNumber) + { + var nroPagina = new DynamicParameters(); + nroPagina.Add("@PageNumber", pageNumber); + + await using var connection = new SqlConnection(BdConexion.StringConnection); + + var resultado = await connection.QueryAsync( + "SP_OBTENER_LOTE_DGA_DATOS", + nroPagina, + commandType: CommandType.StoredProcedure); + + return resultado.ToList(); + } + + public async Task GuardarMedicionesEnviadasAsync(List medicionesGuardadas) + { + await using var connection = new SqlConnection(BdConexion.StringConnection); + try + { + var query = @"UPDATE DGA_DATOS SET ENVIADO = 1 WHERE ID IN @Ids"; + + await connection.ExecuteAsync(query, new { Ids = medicionesGuardadas }); + return true; + } + catch(Exception ex) { + + throw new Exception($"Error {ex.Message}"); + } + + } } } diff --git a/DAS/RegistrarMedicion.cs b/DAS/RegistrarMedicion.cs index e30b35f..3b4a0ad 100644 --- a/DAS/RegistrarMedicion.cs +++ b/DAS/RegistrarMedicion.cs @@ -20,7 +20,7 @@ namespace DAS _logMedicionScadaRepository = logMedicionScadaRepository; } - public async Task EnviarMedicionAsync(DatoDGA medicion, MedicionSubterraneaRequest request, DateTime fechaEnvio) + public async Task EnviarMedicionAsync(DatoDGATemporal medicion, MedicionSubterraneaRequest request, DateTime fechaEnvio) { var log = new LogMedicionEnvio(); try diff --git a/SHARED/DTO/Envios_DGA/DatoDGATemporal.cs b/SHARED/DTO/Envios_DGA/DatoDGATemporal.cs new file mode 100644 index 0000000..d375bde --- /dev/null +++ b/SHARED/DTO/Envios_DGA/DatoDGATemporal.cs @@ -0,0 +1,18 @@ +namespace Shared.DTO.Envios_DGA +{ + public class DatoDGATemporal + { + public int ID { get; set; } + public string? CODIGO_DGA { get; set; } + + public string? CAUDAL { get; set; } + + public string? TOTALIZADOR_CAUDAL { get; set; } + + public DateTime? FECHA_MEDICION_CAUDAL { get; set; } + + public string? NIVEL_FREATICO_DEL_POZO { get; set; } + + public string? TIPO_EMPRESA { get; set; } + } +}