ajustes
This commit is contained in:
parent
657fd50ac9
commit
b52be74bfa
9 changed files with 129 additions and 42 deletions
|
@ -1,9 +1,10 @@
|
|||
using System.Text.Json;
|
||||
using DAL;
|
||||
using DAL;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Shared.DTO;
|
||||
using Shared.DTO.Integracion_DGA;
|
||||
using Shared.DTO.VariablesEntorno;
|
||||
using Shared.Helper;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace BLL.Integracion_DGA
|
||||
{
|
||||
|
@ -12,17 +13,25 @@ namespace BLL.Integracion_DGA
|
|||
private readonly IConfiguration _configuration;
|
||||
private readonly ApiService _apiService;
|
||||
private readonly JobsDgaRepository _jobs;
|
||||
private readonly LogEnvioRepository _logEnvioRepository;
|
||||
|
||||
public ObtencionDatosDga(IConfiguration configuration, ApiService apiService, JobsDgaRepository jobs)
|
||||
public ObtencionDatosDga(IConfiguration configuration, ApiService apiService, JobsDgaRepository jobs, LogEnvioRepository logEnvioRepository)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_apiService = apiService;
|
||||
_jobs = jobs;
|
||||
FileLoggerHelper.ConfigureLogger(_configuration);
|
||||
_logEnvioRepository = logEnvioRepository;
|
||||
}
|
||||
|
||||
public async Task ObtenerDatosDga()
|
||||
{
|
||||
await _logEnvioRepository.InsertarLogProcesoAsync(new LogProceso
|
||||
{
|
||||
FechaEjecucion = DateTime.UtcNow,
|
||||
NombreProceso = "OBTENCION DATOS DGA"
|
||||
});
|
||||
|
||||
DateTimeOffset dateEnd = DateTimeOffset.Now;
|
||||
DateTimeOffset dateStart = dateEnd.AddHours(-1);
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
using System.Text.Json;
|
||||
using DAL;
|
||||
using DAS;
|
||||
using Shared.DTO;
|
||||
using Shared.DTO.Envios_DGA;
|
||||
using Shared.DTO.VariablesEntorno;
|
||||
using Shared.Helper;
|
||||
|
@ -11,17 +12,25 @@ namespace BLL.Recuperacion_DGA
|
|||
{
|
||||
private readonly MedicionDGARepository _dGAMedicionRepository;
|
||||
private readonly RegistrarMedicion _registrarMedicion;
|
||||
private readonly LogEnvioRepository _logEnvioRepository;
|
||||
|
||||
public EnvioDGA(MedicionDGARepository dGAMedicionRepository, RegistrarMedicion registrarMedicion)
|
||||
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);
|
||||
|
||||
|
@ -51,9 +60,10 @@ namespace BLL.Recuperacion_DGA
|
|||
Totalizador = medicion.TOTALIZADOR_CAUDAL ?? "",
|
||||
}
|
||||
};
|
||||
await _registrarMedicion.EnviarMedicionAsync(medicion, body, fechaEnvio);
|
||||
|
||||
listaMediciones.Add(new {Id = medicion.ID,FechaEnvio = fechaEnvio.ToString("yyyy-MM-dd HH:mm:ss"), Enviado = medicion.ENVIADO + 1});
|
||||
var response = await _registrarMedicion.EnviarMedicionAsync(medicion, body, fechaEnvio);
|
||||
|
||||
listaMediciones.Add(new {Id = medicion.ID,FechaEnvio = fechaEnvio.ToString("yyyy-MM-dd HH:mm:ss"), Enviado = 1});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
@ -76,6 +86,7 @@ namespace BLL.Recuperacion_DGA
|
|||
ConsoleLoggerHelper.WriteLineAndLogInfo($"Error al procesar las mediciones.", ConsoleColor.Red);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
using Dapper;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Serilog;
|
||||
using Shared.DTO;
|
||||
using Shared.DTO.Envios_DGA;
|
||||
using Shared.DTO.VariablesEntorno;
|
||||
|
||||
|
@ -11,22 +13,63 @@ namespace DAL
|
|||
{
|
||||
try
|
||||
{
|
||||
using (var connection = new SqlConnection(BdConexion.StringConnection))
|
||||
await using (var connection = new SqlConnection(BdConexion.StringConnection))
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
// Insertar todos los datos de la lista usando Dapper
|
||||
var sql = @"INSERT INTO dbo.DGA_LOGS_OPERACION
|
||||
(estado_envio, json_enviado, json_recibido, comprobante, fecha_envio, id_medicion_smartscada_operacion)
|
||||
VALUES
|
||||
(@EstadoEnvio, @JsonEnviado, @JsonRecibido, @Comprobante, @FechaEnvio, @IdMedicionSmartscadaOperacion)";
|
||||
await connection.ExecuteAsync(sql, logMedicionScada);
|
||||
// No es necesario hacer OpenAsync: Dapper lo abre si hace falta
|
||||
var sql = @"
|
||||
INSERT INTO dbo.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
|
||||
);";
|
||||
|
||||
return true; // Éxito
|
||||
await connection.ExecuteAsync(sql, logMedicionScada);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception($"Error: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> InsertarLogProcesoAsync(LogProceso log)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var connection = new SqlConnection(BdConexion.StringConnection);
|
||||
|
||||
var sql = @"
|
||||
INSERT INTO dbo.DGA_LOGS_PROCESOS
|
||||
(
|
||||
NOMBRE_PROCESO,
|
||||
FECHA_EJECUCION
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
@NombreProceso,
|
||||
@FechaEjecucion
|
||||
);";
|
||||
|
||||
await connection.ExecuteAsync(sql, log);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ namespace DAL
|
|||
|
||||
public static async Task<bool> ActualizarMedicionesAsync(string medicionesJson)
|
||||
{
|
||||
//TODO: ACTUALIZAR REGISTROS
|
||||
try
|
||||
{
|
||||
await using var connection = new SqlConnection(BdConexion.StringConnection);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using DAL;
|
||||
using Serilog;
|
||||
using Shared.DTO.Envios_DGA;
|
||||
using Shared.DTO.VariablesEntorno;
|
||||
using System.Globalization;
|
||||
|
@ -21,41 +22,53 @@ namespace DAS
|
|||
|
||||
public async Task<LogMedicionEnvio> EnviarMedicionAsync(DatoDGA medicion, MedicionSubterraneaRequest request, DateTime fechaEnvio)
|
||||
{
|
||||
var timeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss-0000", CultureInfo.InvariantCulture);
|
||||
var json = JsonSerializer.Serialize(request);
|
||||
|
||||
var req = new HttpRequestMessage(HttpMethod.Post, $"{SubterraneaApiUrl.BaseUrl}{SubterraneaApiUrl.EndPoint}SSSSS");
|
||||
req.Headers.Add("codigoObra", medicion.CODIGO_DGA);
|
||||
req.Headers.Add("timeStampOrigen", timeStamp);
|
||||
req.Content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
var log = new LogMedicionEnvio();
|
||||
|
||||
try
|
||||
{
|
||||
var timeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss-0000", CultureInfo.InvariantCulture);
|
||||
var json = JsonSerializer.Serialize(request);
|
||||
|
||||
var req = new HttpRequestMessage(HttpMethod.Post, $"{SubterraneaApiUrl.BaseUrl}{SubterraneaApiUrl.EndPoint}SSSSS");
|
||||
req.Headers.Add("codigoObra", medicion.CODIGO_DGA);
|
||||
req.Headers.Add("timeStampOrigen", timeStamp);
|
||||
req.Content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
log.JSON_ENVIO = json;
|
||||
log.FECHA_ENVIO = DateTime.UtcNow;
|
||||
log.ID_DGA_DATO = medicion.ID;
|
||||
|
||||
using var response = await _httpClient.SendAsync(req);
|
||||
string jsonRecibido = await response.Content.ReadAsStringAsync();
|
||||
log.JSON_RESPUESTA = jsonRecibido;
|
||||
|
||||
if (response == null || !response.IsSuccessStatusCode)
|
||||
{
|
||||
|
||||
log.COMPROBANTE = null;
|
||||
log.ESTADO_ENVIO = "ERROR";
|
||||
}
|
||||
|
||||
var apiResponse = JsonSerializer.Deserialize<ApiResponse<MedicionSubterraneaResponse>>(jsonRecibido, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
|
||||
if (apiResponse == null || string.IsNullOrEmpty(apiResponse.Status) || apiResponse.Status != "00")
|
||||
{
|
||||
log.COMPROBANTE = null;
|
||||
log.ESTADO_ENVIO = "ERROR";
|
||||
}
|
||||
|
||||
|
||||
|
||||
log.COMPROBANTE = apiResponse.Data.NumeroComprobante ?? null;
|
||||
log.ESTADO_ENVIO = "EXITO";
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
|
||||
log.COMPROBANTE = null;
|
||||
log.ESTADO_ENVIO = "ERROR";
|
||||
}
|
||||
finally
|
||||
{
|
||||
await _logMedicionScadaRepository.InsertarLogEnvioAsync(log);
|
||||
}
|
||||
|
||||
return log;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,11 +2,11 @@
|
|||
{
|
||||
public class LogMedicionEnvio
|
||||
{
|
||||
public string EstadoEnvio { get; set; }
|
||||
public string? JsonEnviado { get; set; }
|
||||
public string? JsonRecibido { get; set; }
|
||||
public string? Comprobante { get; set; }
|
||||
public DateTime FechaEnvio { get; set; }
|
||||
public long IdDgaDato { get; set; }
|
||||
public string ESTADO_ENVIO { get; set; } = string.Empty;
|
||||
public string? JSON_ENVIO { get; set; }
|
||||
public string? JSON_RESPUESTA { get; set; }
|
||||
public string? COMPROBANTE { get; set; }
|
||||
public DateTime FECHA_ENVIO { get; set; }
|
||||
public int ID_DGA_DATO { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
14
SHARED/DTO/LogProceso.cs
Normal file
14
SHARED/DTO/LogProceso.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Shared.DTO
|
||||
{
|
||||
public class LogProceso
|
||||
{
|
||||
public string NombreProceso { get; set; } = string.Empty;
|
||||
public DateTime FechaEjecucion { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,6 +1,4 @@
|
|||
using Dapper;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Serilog;
|
||||
using Shared.DTO.VariablesEntorno;
|
||||
|
||||
|
|
|
@ -7,8 +7,6 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.66" />
|
||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.6" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
|
||||
|
|
Loading…
Add table
Reference in a new issue