Integracion_DGA/DAL/JobsDgaRepository.cs

230 lines
8.7 KiB
C#

using System.Data;
using Dapper;
using Microsoft.Data.SqlClient;
using Microsoft.Identity.Client;
using Shared.DTO.Integracion_DGA;
using Shared.DTO.VariablesEntorno;
namespace DAL
{
public class JobsDgaRepository
{
public async Task<bool> InsertarDgaMacroResultado(List<DgaMacroResultado> dgaMacroResultados)
{
try
{
using (SqlConnection connection = new SqlConnection(BdConexion.StringConnection))
{
await connection.OpenAsync();
// 1. Truncar la tabla antes de insertar
await connection.ExecuteAsync("TRUNCATE TABLE DGA_MACRO_RESULTADO");
// 2. Insertar la lista de registros
string sql = "INSERT INTO DGA_MACRO_RESULTADO (TagName, TimeStamp, Value, Quality) VALUES (@TagName, @TimeStamp, @Value, @Quality)";
// Esto inserta todos los elementos de la lista en la tabla
await connection.ExecuteAsync(sql, dgaMacroResultados);
return true;
}
}
catch (Exception ex)
{
throw new Exception($"Error: {ex.Message}");
}
}
public async Task<bool> InsertarDgaSensorResultado(List<DgaSensorResultado> dgaSensorResultado)
{
try
{
using (var connection = new SqlConnection(BdConexion.StringConnection))
{
await connection.OpenAsync();
// 1. Truncar la tabla DGA_SENSOR_RESULTADO
await connection.ExecuteAsync("TRUNCATE TABLE DGA_SENSOR_RESULTADO");
// 2. Llamar al stored procedure SP_CALCULO_DGA
await connection.ExecuteAsync("SP_CALCULO_DGA", commandType: System.Data.CommandType.StoredProcedure);
// 3. Insertar todos los registros de la lista con Dapper
string sql = "INSERT INTO DGA_SENSOR_RESULTADO (TagName, TimeStamp, Value, Quality) VALUES (@TagName, @TimeStamp, @Value, @Quality)";
await connection.ExecuteAsync(sql, dgaSensorResultado);
return true;
}
}
catch (Exception ex)
{
throw new Exception($"Error: {ex.Message}");
}
}
public async Task<bool> InsertarDgaMacroResultadoSupFlujSuma(List<DgaMacroResultadoSupFlujSuma> dgaMacroResultadoSupFlujSuma)
{
try
{
// Configurar la conexión a la base de datos
using (SqlConnection connection = new SqlConnection(BdConexion.StringConnection))
{
await connection.OpenAsync();
// Truncar la tabla antes de insertar
await connection.ExecuteAsync("TRUNCATE TABLE DGA_MACRO_RESULTADO_SUP_FLUJ_SUMA");
// Crear un adaptador de datos
SqlDataAdapter adapter = new SqlDataAdapter();
// Configurar el comando de inserción
string sql = "INSERT INTO DGA_MACRO_RESULTADO_SUP_FLUJ_SUMA (TagName, TimeStamp, Value, Quality) VALUES (@TagName, @TimeStamp, @Value, @Quality)";
await connection.ExecuteAsync(sql, dgaMacroResultadoSupFlujSuma);
return true; // Éxito
}
}
catch (Exception ex)
{
throw new Exception($"Error: {ex.Message}");
}
}
public async Task<bool> SpCalculoDga()
{
try
{
using (var connection = new SqlConnection(BdConexion.StringConnection))
{
await connection.OpenAsync();
// Ejecuta el stored procedure sin parámetros
await connection.ExecuteAsync("SP_CALCULO_DGA", commandType: CommandType.StoredProcedure);
}
return true; // Éxito
}
catch (Exception ex)
{
throw new Exception($"Error: {ex.Message}");
}
}
public async Task<bool> SpCalculoDgaSupFlujSumaSnreversibilidad()
{
try
{
using (var connection = new SqlConnection(BdConexion.StringConnection))
{
await connection.OpenAsync();
await connection.ExecuteAsync(
"SP_CALCULO_DGA_SUP_FLUJ_SUMA_SNREVERSIBILIDAD",
commandType: CommandType.StoredProcedure
);
}
return true; // Éxito
}
catch (Exception ex)
{
throw new Exception($"Error: {ex.Message}");
}
}
public async Task<bool> SpTraspasoDatosAchird()
{
try
{
using (var connection = new SqlConnection(BdConexion.StringConnection))
{
await connection.OpenAsync();
await connection.ExecuteAsync(
"SP_TRASPASO_DATOS_ACHIRD",
commandType: CommandType.StoredProcedure
);
}
return true; // Éxito
}
catch (Exception ex)
{
throw new Exception($"Error: {ex.Message}");
}
}
public async Task<bool> InsertarDgaCaudal(List<DGAInsert> dgaMacroResultados)
{
try
{
using (SqlConnection connection = new SqlConnection(BdConexion.StringConnection))
{
await connection.OpenAsync();
// 1. Truncar la tabla antes de insertar
await connection.ExecuteAsync("TRUNCATE TABLE DGA_CAUDAL");
// 2. Insertar la lista de registros
string sql = "INSERT INTO DGA_CAUDAL (TAG, CAUDAL, FECHAMEDICION) VALUES (@TAG, @VALOR, @FECHAMEDICION)";
// Esto inserta todos los elementos de la lista en la tabla
await connection.ExecuteAsync(sql, dgaMacroResultados);
return true;
}
}
catch (Exception ex)
{
throw new Exception($"Error: {ex.Message}");
}
}
public async Task<bool> InsertarDgaNivel(List<DGAInsert> dgaMacroResultados)
{
try
{
using (SqlConnection connection = new SqlConnection(BdConexion.StringConnection))
{
await connection.OpenAsync();
// 1. Truncar la tabla antes de insertar
await connection.ExecuteAsync("TRUNCATE TABLE DGA_NIVEL");
// 2. Insertar la lista de registros
string sql = "INSERT INTO DGA_NIVEL (TAG, NIVEL_FREATICO, FECHAMEDICION) VALUES (@TAG, @VALOR, @FECHAMEDICION)";
// Esto inserta todos los elementos de la lista en la tabla
await connection.ExecuteAsync(sql, dgaMacroResultados);
return true;
}
}
catch (Exception ex)
{
throw new Exception($"Error: {ex.Message}");
}
}
public async Task<bool> InsertarMedicionOperacionNivel()
{
try
{
using (SqlConnection connection = new SqlConnection(BdConexion.StringConnection))
{
await connection.OpenAsync();
await connection.ExecuteAsync(
"SP_CALCULO_NIVEL_MEDICION_SMARTSCADA_OPERACION",
commandType: CommandType.StoredProcedure
);
return true;
}
}
catch (Exception ex)
{
throw new Exception($"Error: {ex.Message}");
}
}
public async Task<bool> InsertarMedicionOperacionCaudal()
{
try
{
using (SqlConnection connection = new SqlConnection(BdConexion.StringConnection))
{
await connection.OpenAsync();
await connection.ExecuteAsync(
"SP_CALCULO_CAUDAL_MEDICION_SMARTSCADA_OPERACION",
commandType: CommandType.StoredProcedure
);
return true;
}
}
catch (Exception ex)
{
throw new Exception($"Error: {ex.Message}");
}
}
}
}