Integracion_DGA/DAL/JobsDgaRepository.cs

160 lines
5.7 KiB
C#
Raw Normal View History

using Microsoft.Data.SqlClient;
using Microsoft.Win32;
using Shared.DTO.Integracion_DGA;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using Microsoft.Extensions.Configuration;
namespace DAL
{
public class JobsDgaRepository
{
private static string connectionString = string.Empty;
private readonly IConfiguration _configuration;
public JobsDgaRepository(IConfiguration configuration)
{
_configuration = configuration;
connectionString = _configuration.GetConnectionString("DefaultConnection") ?? "";
}
public async Task<bool> InsertarDgaMacroResultado(List<DgaMacroResultado> dgaMacroResultados)
{
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
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(connectionString))
{
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(connectionString))
{
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(connectionString))
{
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(connectionString))
{
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(connectionString))
{
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}");
}
}
}
}