Integracion_DGA/BLL/Recuperacion_DGA/EnvioDGA.cs
bcastrogallardo d228cf71dd cambios
2025-07-17 14:00:21 -04:00

114 lines
4.6 KiB
C#

using System;
using System.Text.Json;
using DAL;
using DAS;
using Shared.DTO;
using Shared.DTO.Envios_DGA;
using Shared.DTO.VariablesEntorno;
using Shared.Helper;
namespace BLL.Recuperacion_DGA
{
public class EnvioDGA
{
private readonly MedicionDGARepository _dGAMedicionRepository;
private readonly RegistrarMedicion _registrarMedicion;
private readonly LogEnvioRepository _logEnvioRepository;
public EnvioDGA(MedicionDGARepository dGAMedicionRepository, RegistrarMedicion registrarMedicion, LogEnvioRepository logEnvioRepository)
{
_dGAMedicionRepository = dGAMedicionRepository;
_registrarMedicion = registrarMedicion;
_logEnvioRepository = logEnvioRepository;
}
public async Task<bool> RegistrarMedicionesAsync()
{
try
{
await _logEnvioRepository.InsertarLogProcesoAsync(new LogProceso
{
FechaEjecucion = DateTime.UtcNow,
NombreProceso = "ENVIO DATOS DGA"
});
ConsoleLoggerHelper.WriteLineAndLogEventoAsync("INICIO", "Inicio proceso de recuperación DGA", "");
ConsoleLoggerHelper.WriteLineAndLogInfo("Obteniendo Mediciones Scada", ConsoleColor.Green);
var logsEnviados = new List<LogMedicionEnvio>();
var pageNumber = 1;
var fechaInicio = DateTime.UtcNow;
while (true)
{
var mediciones = await _dGAMedicionRepository.ObtenerMedicionesPorLoteAsync(pageNumber, fechaInicio);
if (mediciones == null || mediciones.Count == 0)
{
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 idMediciones = mediciones.Select(x => x.ID).ToList();
await _dGAMedicionRepository.GuardarMedicionesEnviadasAsync(idMediciones);
await _logEnvioRepository.InsertarLogRespuesta(logsEnviados);
}
catch (Exception e)
{
ConsoleLoggerHelper.WriteLineAndLogInfo($"[Error] {e.Message}");
}
logsEnviados.Clear();
mediciones.Clear();
pageNumber++;
}
ConsoleLoggerHelper.WriteLineAndLogEventoAsync("FIN","Fin proceso de recuperación DGA","");
}
catch (Exception ex)
{
FileLoggerHelper.LogError($"[Error] {ex.Message}.", ex);
ConsoleLoggerHelper.WriteLineAndLogInfo($"Error al procesar las mediciones.", ConsoleColor.Red);
return false;
}
return true;
}
}
}