Commit inicial

This commit is contained in:
Giuliano Paschoalino 2025-07-11 18:04:42 -03:00
parent 81b1779c1d
commit 4c72ad5cd5
12 changed files with 1276 additions and 353 deletions

Binary file not shown.

Binary file not shown.

168
Models/Tarifa.cs Normal file
View File

@ -0,0 +1,168 @@
using System.Diagnostics.Eventing.Reader;
using System.Globalization;
using System.Text.Json.Nodes;
using TarifasANEEL.Services;
using TarifasANEEL.Utilities;
namespace TarifasANEEL.Models
{
public class Tarifa
{
private const string DISTRIBUTOR_KEY = "NumCPFCNPJ";
private const string SUBGROUP_KEY = "DscSubGrupoTarifario";
private const string MODALITY_KEY = "DscModalidadeTarifaria";
public required int ID_dist { get; set; }
public required string Grupo { get; set; }
public required string Perfil { get; set; }
public required string Ciclo { get; set; }
public DateTime? DatInicioVigencia { get; set;}
public DateTime? DatFimVigencia { get; set;}
public double Energia_P { get; set;}
public double Energia_FP { get; set;}
public double Enc_P { get; set;}
public double Enc_FP { get; set;}
public double Dem_P { get; set;}
public double Dem_FP { get; set;}
public double ER_P { get; set;}
public double ER_FP { get; set;}
public double Megaflex { get; set;}
public double Covid_P { get; set;}
public double Covid_FP { get; set;}
public double Desc_Rural { get; set;}
public double EH_P { get; set;}
public double EH_FP { get; set;}
public static Tarifa FromRecord(JsonNode record)
{
ArgumentNullException.ThrowIfNull(record);
return new Tarifa
{
ID_dist = DatabaseService.GetDistribuidoraId(record[DISTRIBUTOR_KEY]?.ToString() ?? ""),
Grupo = record[SUBGROUP_KEY]?.ToString() ?? "",
Perfil = TarifaHelper.BuildProfileName(record, record[MODALITY_KEY]?.ToString() ?? ""),
Ciclo = TarifaHelper.ParseResolution(record),
DatInicioVigencia = DateTime.Parse(record[nameof(DatInicioVigencia)]?.ToString()!),
DatFimVigencia = DateTime.Parse(record[nameof(DatFimVigencia)]?.ToString()!)
};
}
public void UpdateComponentValue(JsonNode record)
{
int test = 0;
var subgroupSuffix = Grupo[0].ToString();
var component = record["DscComponenteTarifario"]?.ToString() ?? "";
var unit = record["DscUnidade"]?.ToString() ?? "";
var rawpeak = record["DscPostoTarifario"]?.ToString() ?? "";
var modality = record["DscModalidadeTarifaria"]?.ToString().ToUpper() ?? "";
var value = double.TryParse(record["VlrComponenteTarifario"]?.ToString(), out var parsedValue)
? parsedValue
: 0;
var peakSuffix = rawpeak[0].ToString();
switch ((subgroupSuffix, modality, component, unit, peakSuffix))
{
// Group A cases
case ("A", "GERAÇÃO", "TUSD", "R$/kW", "N"):
Dem_P = Dem_FP = value;
break;
case ("A", "CONVENCIONAL", "TE", "R$/MWh", var peak):
switch (peak)
{
case "N": Energia_P = Energia_FP = value; break;
case "F": Energia_FP = value; break;
case "P": Energia_P = value; break;
}
break;
case ("A", "CONVENCIONAL", "TUSD", "R$/MWh", "N"):
Enc_P = Enc_FP = value;
break;
case ("A", "AZUL", "TE", "R$/MWh", var peak) when peak is "F" or "P":
if (peak == "F") Energia_FP = value;
else Energia_P = value;
break;
case ("A", "AZUL", "TUSD", "R$/MWh", var peak) when peak is "F" or "P":
if (peak == "F") Enc_FP = value;
else Enc_P = value;
break;
case ("A", "AZUL", "TUSD", "R$/kW", var peak) when peak is "F" or "P":
if (peak == "F") Dem_FP = value;
else Dem_P = value;
break;
case ("A", "VERDE", "TE", "R$/MWh", var peak) when peak is "F" or "P":
if (peak == "P") Energia_P = value;
else Energia_FP = value;
break;
case ("A", "VERDE", "TUSD", "R$/kW", "N"):
Dem_FP = value;
Dem_P = 0;
break;
case ("A", "VERDE", "TUSD", "R$/MWh", var peak) when peak is "F" or "P":
if (peak == "P") Enc_P = value;
else Enc_FP = value;
break;
// Group B cases
case ("B", "CONVENCIONAL" or "PRÉ-PAGAMENTO", var comp, "R$/MWh", "N"):
if (comp == "TUSD") Enc_P = Enc_FP = value;
else if (comp == "TE") Energia_P = Energia_FP = value;
break;
case ("B", "BRANCA", var comp, "R$/MWh", var peak):
if (comp == "TE")
if (peak == "P") Energia_P = value;
else Energia_FP += value;
else if (comp == "TUSD")
if (peak == "P") Enc_P = value;
else Enc_FP += value;
break;
case ("B", "GERAÇÃO", "TUSD", "R$/kW", "N"):
Dem_P = Dem_FP = value;
break;
default:
if ((component == "TE" && unit == "R$/kW" && value != 0) ||
!((subgroupSuffix == "A" || subgroupSuffix == "B") &&
(modality is "GERAÇÃO" or "CONVENCIONAL" or "AZUL" or "VERDE" or "BRANCA" or "PRÉ-PAGAMENTO" or "DISTRIBUIÇÃO" or "ENERGIA SUPRIMENTO")))
{
test++;
}
break;
}
if (test > 0) { Console.WriteLine("Sem lógica:\t{0}",string.Join("\t",new string[] { ID_dist.ToString(), Ciclo, Grupo, Perfil, component, peakSuffix, unit, value.ToString("C", CultureInfo.CreateSpecificCulture("pt-BR")) })); }
}
public bool HasAllEmptyValues()
{
// Adjust this method based on your Tarifa properties
return Energia_P == 0 &&
Energia_FP == 0 &&
Enc_P == 0 &&
Enc_FP == 0 &&
Dem_P == 0 &&
Dem_FP == 0 &&
ER_P == 0 &&
ER_FP == 0 &&
Megaflex == 0 &&
Covid_P == 0 &&
Covid_FP == 0 &&
Desc_Rural == 0 &&
EH_P == 0 &&
EH_FP == 0;
}
}
}

View File

@ -1,366 +1,501 @@
 using Microsoft.AspNetCore.Builder;
using System.Text.Json; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using TarifasANEEL.Services;
using TarifasANEEL.Utilities;
using System.Data.OleDb; using System.Data.OleDb;
using TarifasANEEL; using System.Text.Json.Nodes;
using System.Data;
using WinRT;
using System.Text;
using Windows.Media.Capture;
using System.Configuration;
using System.ComponentModel;
using System.Data.SqlClient;
using static System.Runtime.InteropServices.JavaScript.JSType;
using Windows.System;
using System.Security.Cryptography;
using System.Reflection.PortableExecutable;
using System.Xml.Linq;
using System.ComponentModel.DataAnnotations;
public class Program namespace TarifasANEEL
{ {
private static async Task Main(string[] args) public class Program
{ {
private static async Task Main(string[] args)
string AneelURL = "https://dadosabertos.aneel.gov.br/api/3/action/datastore_search";
string AccessPath = @"C:\Users\contratos\Documents\Giuliano\Code\TarifasANEEL\ANEEL.accdb";
int offset = 0;
int lastID;
Dictionary<string, List<object>>? WebDatas = GetResources(AccessPath, "SELECT * FROM tblResourceComponentesTarifarias") !;
foreach(object resource in WebDatas["resource_id"].ToList())
{ {
if (resource != null) try
{ {
Console.WriteLine(resource.ToString()); // Configuração inicial
}; ConfigurationService.LoadConfiguration();
}
using (OleDbConnection connection = new OleDbConnection($"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={AccessPath};Persist Security Info=False;"))
{
connection.Open();
using (OleDbCommand command = new OleDbCommand($"SELECT Max([_id]) FROM TmpTable WHERE [resource_id] = \"{WebDatas["resource_id"]}\"", connection))
{
lastID = (int)command.ExecuteScalar()!;
}
}
string webString = $@"{AneelURL}?"; // Inicialização do aplicativo
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var logger = app.Services.GetRequiredService<ILogger<Program>>();
foreach (var webData in WebDatas) // Conexão ao banco de dados
{ using var resourcesConnection = DatabaseService.CreateConnection(ConfigurationService.ResourcesDbConnectionString);
webString = $@"{webString}{webData.Key}={webData.Value}&"; resourcesConnection.Open();
}
if (!DatabaseService.TableExists(resourcesConnection, ConfigurationService.ResourcesTableName))
Rootobject? rootObject = await PullDataAsync($@"{webString}offset={offset.ToString()}");
List<Record>? records = rootObject?.result?.records?.ToList();
List<Record>? tempRecords = rootObject?.result?.records?.ToList();
Console.Clear();
Console.WriteLine(rootObject?.result?.records?.Last()._id);
if (records != null && (records.Last()._id != rootObject?.result?.total))
{
while (rootObject?.result?.total != records.Last()._id)
//while (records.Last()._id != 32000)
{
offset = records.Last()._id;
rootObject = await PullDataAsync($@"{webString}offset={offset.ToString()}");
Console.WriteLine(rootObject?.result?.records?.Last()._id);
tempRecords = rootObject?.result?.records?.ToList();
if (tempRecords != null)
{ {
records.AddRange(tempRecords); throw new Exception($"Falha ao acessar [{ConfigurationService.ResourcesTableName}]");
} }
}
}
if (records != null)
{
//UpdateData(records, "tblTarifas");
AdicionarDadosAoBanco(records,AccessPath);
}
} using var dataConnection = DatabaseService.CreateConnection(ConfigurationService.MainDbConnectionString);
dataConnection.Open();
DatabaseService.LoadDistribuidoras(dataConnection);
public static Dictionary<string, List<object>>? GetResources(string AccessPath, string sSQL) using var command = new OleDbCommand(
{ $"SELECT {ConfigurationService.ResourcesTableName}.resource_id, * " +
Dictionary<string, List<object>>? Temp = new Dictionary<string, List<object>>(); $"FROM {ConfigurationService.ResourcesTableName} " +
try $"WHERE ((Not ({ConfigurationService.ResourcesTableName}.resource_id)=\"\"))" +
{ $"ORDER BY {ConfigurationService.ResourcesTableName}.Ano DESC;",
using (OleDbConnection connection = new OleDbConnection($"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={AccessPath};Persist Security Info=False;")) resourcesConnection);
{
using (OleDbCommand command = new OleDbCommand(sSQL, connection)) using var reader = command.ExecuteReader() ?? throw new Exception("Falha ao acessar os recursos");
int iRows = 0;
var tasks = new List<Task>();
while (reader.Read())
{ {
connection.Open(); // Create a copy of the current row since reader will move to next row
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command)) var readerValues = new Dictionary<string, object>();
for (int i = 0; i < reader.FieldCount; i++)
{ {
DataTable dt = new DataTable(); readerValues[reader.GetName(i)] = reader.GetValue(i);
adapter.Fill(dt);
foreach(DataColumn dc in dt.Columns)
{
var list = dt.Rows.OfType<DataRow>().Select(dr => dr.Field<object>(dc.ColumnName)).ToList();
Temp.Add(dc.ColumnName, list !);
}
} }
//tasks.Add(ProcessResourceAsync(readerValues, resourcesConnection, dataConnection));
await ProcessResourceAsync(readerValues, resourcesConnection, dataConnection);
Console.WriteLine(iRows);
iRows++;
} }
await Task.WhenAll(tasks);
} }
return Temp; catch (Exception ex)
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
private static async Task<Rootobject?> PullDataAsync(string sURL)
{
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, sURL);
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
};
Rootobject? rootObject = JsonSerializer.Deserialize<Rootobject>(await response.Content.ReadAsStringAsync(), options);
return rootObject;
}
public static DataTable ConvertToDataTable<T>(IList<T> data)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
return table;
}
public static void UpdateData<T>(List<T> list, string TableName)
{
DataTable dt = new DataTable("MyTable");
dt = ConvertToDataTable(list);
using (SqlConnection conn = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\contratos\Documents\Giuliano\Code\TarifasANEEL\Tarifas.mdf; Integrated Security = True; Connect Timeout = 30"))
{
using (SqlCommand command = new SqlCommand("", conn))
{ {
try Console.WriteLine($"Erro: {ex.Message}");
{
conn.Open();
//Creating temp table on database
command.CommandText = @"CREATE TABLE tblTarifas (_id INT, DatGeracaoConjuntoDados TEXT,
DscResolucaoHomologatoria TEXT,
SigNomeAgente TEXT,
NumCPFCNPJ TEXT,
DatInicioVigencia TEXT,
DatFimVigencia TEXT,
DscBaseTarifaria TEXT,
DscSubGrupoTarifario TEXT,
DscModalidadeTarifaria TEXT,
DscClasseConsumidor TEXT,
DscSubClasseConsumidor TEXT,
DscDetalheConsumidor TEXT,
DscPostoTarifario TEXT,
DscUnidade TEXT,
SigNomeAgenteAcessante TEXT,
DscComponenteTarifario TEXT,
VlrComponenteTarifario TEXT";
command.ExecuteNonQuery();
//command.CommandText = @"CREATE TABLE TmpTable (_id INT, DatGeracaoConjuntoDados TEXT,
// DscResolucaoHomologatoria TEXT,
// SigNomeAgente TEXT,
// NumCPFCNPJ TEXT,
// DatInicioVigencia TEXT,
// DatFimVigencia TEXT,
// DscBaseTarifaria TEXT,
// DscSubGrupoTarifario TEXT,
// DscModalidadeTarifaria TEXT,
// DscClasseConsumidor TEXT,
// DscSubClasseConsumidor TEXT,
// DscDetalheConsumidor TEXT,
// DscPostoTarifario TEXT,
// DscUnidade TEXT,
// SigNomeAgenteAcessante TEXT,
// DscComponenteTarifario TEXT,
// VlrComponenteTarifario TEXT";
//command.ExecuteNonQuery();
//Bulk insert into temp table
using (SqlBulkCopy bulkcopy = new SqlBulkCopy(conn))
{
bulkcopy.BulkCopyTimeout = 660;
bulkcopy.DestinationTableName = "tblTarifas";
bulkcopy.WriteToServer(dt);
bulkcopy.Close();
}
// Updating destination table, and dropping temp table
command.CommandTimeout = 300;
command.CommandText = @"UPDATE P SET
P.[DatGeracaoConjuntoDados] = T.[DatGeracaoConjuntoDados],
P.[DscResolucaoHomologatoria] = T.[DscResolucaoHomologatoria],
P.[SigNomeAgente] = T.[SigNomeAgente],
P.[NumCPFCNPJ] = T.[NumCPFCNPJ],
P.[DatInicioVigencia] = T.[DatInicioVigencia],
P.[DatFimVigencia] = T.[DatFimVigencia],
P.[DscBaseTarifaria] = T.[DscBaseTarifaria],
P.[DscSubGrupoTarifario] = T.[DscSubGrupoTarifario],
P.[DscModalidadeTarifaria] = T.[DscModalidadeTarifaria],
P.[DscClasseConsumidor] = T.[DscClasseConsumidor],
P.[DscSubClasseConsumidor] = T.[DscSubClasseConsumidor],
P.[DscDetalheConsumidor] = T.[DscDetalheConsumidor],
P.[DscPostoTarifario] = T.[DscPostoTarifario],
P.[DscUnidade] = T.[DscUnidade],
P.[SigNomeAgenteAcessante] = T.[SigNomeAgenteAcessante],
P.[DscComponenteTarifario] = T.[DscComponenteTarifario],
P.[VlrComponenteTarifario] = T.[VlrComponenteTarifario]
FROM[TmpTable] AS P INNER JOIN TmpTable AS T ON P.[_id] = T.[_id]; DROP TABLE TmpTable; ";
command.ExecuteNonQuery();
}
catch (Exception)
{
// Handle exception properly
}
finally
{
conn.Close();
}
} }
} finally
}
private static void AdicionarDadosAoBanco(List<Record> records, string accessPath)
{
try
{
using (OleDbConnection connection = new OleDbConnection($"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={accessPath};Persist Security Info=False;"))
{ {
connection.Open(); TimerHelper.SetTimer(ConfigurationService.TimerDurationToClose);
}
}
// Verificar se a tabela temporária já existe private static async Task ProcessResourceAsync(Dictionary<string, object> readerValues, OleDbConnection resourcesConnection, OleDbConnection dataConnection)
if (!TmpTableExiste(connection)) {
string tableName = $"Tarifas_{readerValues["Ano"]}";
if (tableName == "Tarifas_2021") { int test1 = 0; }
int offset = DatabaseService.GetOffsetForTable(
tableName,
(string)readerValues["Ano"] == "All",
"_id",
resourcesConnection);
offset = 0;
string apiUrl = ApiService.BuildUrl(
ConfigurationService.ApiDadosAbertosUrl,
readerValues,
"Ano");
bool hasNextPage = true;
while (hasNextPage)
{
JsonNode rootObject = await ApiService.PullDataAsync($"{apiUrl}offset={offset}");
JsonNode records = rootObject["result"]?["records"] ?? new JsonArray();
if (records.AsArray().Count > 0)
{ {
CriarTmpTable(connection); var formattedData = TarifaHelper.FormatData(records);
DatabaseService.AddDataToTable(dataConnection, formattedData, ConfigurationService.DataTableName, (string)readerValues["Ano"]);
offset += records.AsArray().Count;
} }
else
using (OleDbTransaction transaction = connection.BeginTransaction())
{ {
try hasNextPage = false;
{
foreach (var record in records)
{
using (OleDbCommand command = new OleDbCommand("INSERT INTO TmpTable VALUES (@_id, @DatGeracaoConjuntoDados, @DscREH, @SigAgente, @NumCNPJDistribuidora, @DatInicioVigencia, @DatFimVigencia, @DscBaseTarifaria, @DscSubGrupo, @DscModalidadeTarifaria, @DscClasse, @DscSubClasse, @DscDetalhe, @NomPostoTarifario, @DscUnidadeTerciaria, @SigAgenteAcessante, @VlrTUSD, @VlrTE)", connection, transaction))
{
// Adicionar parâmetros
command.Parameters.AddWithValue("@_id", record._id);
command.Parameters.AddWithValue("@DatGeracaoConjuntoDados", record.DatGeracaoConjuntoDados);
command.Parameters.AddWithValue("@DscResolucaoHomologatoria", record.DscResolucaoHomologatoria);
command.Parameters.AddWithValue("@SigNomeAgente", record.SigNomeAgente);
command.Parameters.AddWithValue("@NumCPFCNPJ", record.NumCPFCNPJ);
command.Parameters.AddWithValue("@DatInicioVigencia", record.DatInicioVigencia);
command.Parameters.AddWithValue("@DatFimVigencia", record.DatFimVigencia);
command.Parameters.AddWithValue("@DscBaseTarifaria", record.DscBaseTarifaria);
command.Parameters.AddWithValue("@DscSubGrupoTarifario", record.DscSubGrupoTarifario);
command.Parameters.AddWithValue("@DscModalidadeTarifaria", record.DscModalidadeTarifaria);
command.Parameters.AddWithValue("@DscClasseConsumidor", record.DscClasseConsumidor);
command.Parameters.AddWithValue("@DscSubClasseConsumidor", record.DscSubClasseConsumidor);
command.Parameters.AddWithValue("@DscDetalheConsumidor", record.DscDetalheConsumidor);
command.Parameters.AddWithValue("@DscPostoTarifario", record.DscPostoTarifario);
command.Parameters.AddWithValue("@DscUnidade", record.DscUnidade);
command.Parameters.AddWithValue("@SigNomeAgenteAcessante", record.SigNomeAgenteAcessante);
command.Parameters.AddWithValue("@DscComponenteTarifario", record.DscComponenteTarifario);
command.Parameters.AddWithValue("@VlrComponenteTarifario", record.VlrComponenteTarifario);
command.ExecuteNonQuery();
Console.WriteLine(record._id);
}
}
transaction.Commit();
Console.WriteLine("Dados adicionados com sucesso à tabela temporária!");
}
catch (Exception ex)
{
transaction.Rollback();
Console.WriteLine($"Erro ao adicionar dados: {ex.Message}");
}
} }
} }
Console.WriteLine("Dados adicionados com sucesso à tabela temporária!");
}
catch (Exception ex)
{
Console.WriteLine($"Ocorreu um erro: {ex.Message}");
} }
} }
}
private static bool TmpTableExiste(OleDbConnection connection)
{
try
{
using (OleDbCommand command = new OleDbCommand("SELECT COUNT(*) FROM TmpTable", connection))
{
command.ExecuteScalar();
return true;
}
}
catch (OleDbException)
{
// Se ocorrer uma exceção, a tabela provavelmente não existe
return false;
}
}
private static void CriarTmpTable(OleDbConnection connection) //using System.Data;
{ //using System.Data.OleDb;
using (OleDbCommand command = new OleDbCommand("CREATE TABLE TmpTable (_id INT, DatGeracaoConjuntoDados TEXT, DscREH TEXT, SigAgente TEXT, NumCNPJDistribuidora TEXT, DatInicioVigencia TEXT, DatFimVigencia TEXT, DscBaseTarifaria TEXT, DscSubGrupo TEXT, DscModalidadeTarifaria TEXT, DscClasse TEXT, DscSubClasse TEXT, DscDetalhe TEXT, NomPostoTarifario TEXT, DscUnidadeTerciaria TEXT, SigAgenteAcessante TEXT, VlrTUSD TEXT, VlrTE TEXT)", connection)) //using System.Text.Json;
{ //using System.Text.Json.Nodes;
command.ExecuteNonQuery(); //using System.Timers;
} //using Microsoft.AspNetCore.Builder;
} //using Microsoft.Extensions.Configuration;
//using Microsoft.Extensions.DependencyInjection;
//using Microsoft.Extensions.Logging;
private static void AdicionarDadosATmpTable(OleDbConnection connection, List<Record> records) //namespace TarifasANEEL.App
{ //{
foreach (var record in records) // public class Program
{ // {
using (OleDbCommand command = new OleDbCommand("INSERT INTO TmpTable VALUES (@_id, @DatGeracaoConjuntoDados, @DscREH, @SigAgente, @NumCNPJDistribuidora, @DatInicioVigencia, @DatFimVigencia, @DscBaseTarifaria, @DscSubGrupo, @DscModalidadeTarifaria, @DscClasse, @DscSubClasse, @DscDetalhe, @NomPostoTarifario, @DscUnidadeTerciaria, @SigAgenteAcessante, @VlrTUSD, @VlrTE)", connection)) // static DateTime dtTimerEndTime;
{ // static string? sApiDadosAbertosUrl;
// Adicionar parâmetros // static string? sResourcesDbPath;
command.Parameters.AddWithValue("@_id", record._id); // static string? sDbPath;
command.Parameters.AddWithValue("@DatGeracaoConjuntoDados", record.DatGeracaoConjuntoDados); // static string? sResourcesTableName;
command.Parameters.AddWithValue("@DscResolucaoHomologatoria", record.DscResolucaoHomologatoria); // static string? sResourcesDbConnection;
command.Parameters.AddWithValue("@SigNomeAgente", record.SigNomeAgente); // static string? sDbConnection;
command.Parameters.AddWithValue("@NumCPFCNPJ", record.NumCPFCNPJ); // static DataTable? dtDistribuidoras = new();
command.Parameters.AddWithValue("@DatInicioVigencia", record.DatInicioVigencia); // static string? sTimerDurationToClose;
command.Parameters.AddWithValue("@DatFimVigencia", record.DatFimVigencia);
command.Parameters.AddWithValue("@DscBaseTarifaria", record.DscBaseTarifaria);
command.Parameters.AddWithValue("@DscSubGrupoTarifario", record.DscSubGrupoTarifario);
command.Parameters.AddWithValue("@DscModalidadeTarifaria", record.DscModalidadeTarifaria);
command.Parameters.AddWithValue("@DscClasseConsumidor", record.DscClasseConsumidor);
command.Parameters.AddWithValue("@DscSubClasseConsumidor", record.DscSubClasseConsumidor);
command.Parameters.AddWithValue("@DscDetalheConsumidor", record.DscDetalheConsumidor);
command.Parameters.AddWithValue("@DscPostoTarifario", record.DscPostoTarifario);
command.Parameters.AddWithValue("@DscUnidade", record.DscUnidade);
command.Parameters.AddWithValue("@SigNomeAgenteAcessante", record.SigNomeAgenteAcessante);
command.Parameters.AddWithValue("@DscComponenteTarifario", record.DscComponenteTarifario);
command.Parameters.AddWithValue("@VlrComponenteTarifario", record.VlrComponenteTarifario);
// Adicionar outros parâmetros conforme necessário // static void SetConfig()
// {
// IConfigurationRoot config;
// config = new ConfigurationBuilder()
// .AddUserSecrets<Program>()
// .Build();
// sApiDadosAbertosUrl = config["sApiDadosAbertosUrl"]!;
// sResourcesDbPath = config["sResourcesDbPath"]!;
// sDbPath = config["sDbPath"]!;
// sResourcesTableName = config["sResourcesTableName"]!;
// sTimerDurationToClose = config["sTimerDurationToClose"];
// sResourcesDbConnection = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={sResourcesDbPath};Persist Security Info=False;";
// sDbConnection = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={sDbPath};Jet OLEDB:Database Password=gds21";
// if (config.AsEnumerable().Where(c => c.Value == "" ^ c.Value == null).ToList().Any()) { throw new Exception("Falha ao acessar o arquivo de configuração"); }
// }
// static void SetDistTable(OleDbConnection conn)
// {
// if (!TabelaExiste(conn, "Distribuidoras_tarifas") ^ !TabelaExiste(conn, "Distribuidoras_geral"))
// {
// throw new Exception($"Falha ao acessar [Distribuidoras_tarifas] ou [Distribuidoras_geral]");
// }
// string sQueryDist = $"SELECT *\r\nFROM Distribuidoras_geral\r\nWHERE ((Not (Distribuidoras_geral.CNPJ)=\"\"));";
// using OleDbCommand cmd = new(sQueryDist, conn);
// using OleDbDataReader distReader = cmd.ExecuteReader() ?? throw new Exception();
// dtDistribuidoras!.Load(distReader);
// }
// private static bool TabelaExiste(OleDbConnection connection, string TableName)
// {
// try
// {
// using OleDbCommand command = new(@$"SELECT COUNT(*) FROM {TableName}", connection);
// command.ExecuteScalar();
// return true;
// }
// catch (OleDbException)
// {
// return false;
// }
// }
// public static int GetCountIdFromTable(string sTableName, string sIdColumnName, OleDbConnection connection)
// {
// int iOffset = 0;
// if (TabelaExiste(connection, sTableName))
// {
// using OleDbCommand cmd = new($"SELECT Count([{sIdColumnName}]) FROM {sTableName}", connection);
// var test = cmd.ExecuteScalar() ?? "0";
// if (test.ToString() != "")
// {
// iOffset = int.Parse(test!.ToString() ?? "0");
// }
// }
// return iOffset;
// }
// public static int GetLastIdFromTable(string sTableName, string sIdColumnName, OleDbConnection connection)
// {
// int iOffset = 0;
// if (TabelaExiste(connection, sTableName))
// {
// using OleDbCommand cmd = new($"SELECT Last([{sIdColumnName}]) FROM {sTableName}", connection);
// var test = cmd.ExecuteScalar() ?? "0";
// if (test.ToString() != "")
// {
// iOffset = int.Parse(test!.ToString() ?? "0");
// }
// }
// return iOffset;
// }
// public static string BuildUrl(string sBaseUrl, OleDbDataReader reader, string sReaderColumn)
// {
// string sWebString = $@"{sBaseUrl}?";
// for (int iCol = 0; iCol < reader.FieldCount; iCol++)
// {
// if (reader.GetName(iCol) != sReaderColumn)
// {
// if (reader[iCol].ToString() != "")
// {
// sWebString = $@"{sWebString}{reader.GetName(iCol)}={reader[iCol]}&";
// }
// }
// }
// return sWebString;
// }
// private static async Task<JsonNode> PullDataAsync(string sURL)
// {
// Console.WriteLine("Carregando dados da API");
// var client = new HttpClient();
// var request = new HttpRequestMessage(HttpMethod.Get, sURL);
// var response = await client.SendAsync(request);
// response.EnsureSuccessStatusCode();
// string sResponse = await response.Content.ReadAsStringAsync();
// JsonNode? rootObject = JsonSerializer.Deserialize<JsonNode?>(sResponse.ToString()
// .Replace(@"""NomPostoTarifario"":""Não se aplica""", @"""NomPostoTarifario"":""N""")
// .Replace(@"""DscPostoTarifario"":""Não se aplica""", @"""DscPostoTarifario"":""N""")
// .Replace(@"""DscSubClasseConsumidor"":""Residencial""", @"""DscSubClasseConsumidor"":""""")
// .Replace(@"""DscClasseConsumidor"":""Iluminação pública""", @"""DscClasseConsumidor"":""""")
// .Replace(@"""DscModalidadeTarifaria"":""Convencional pré-pagamento""", @"""DscModalidadeTarifaria"":""pré-pagamento""")
// .Replace(@"""Não se aplica""", @""""""))!;
// Console.WriteLine("Dados carregados.");
// return rootObject;
// }
// private static void CriarTabela(OleDbConnection connection, string TableName, string columnNames)
// {
// try
// {
// using OleDbCommand command = new(@$"CREATE TABLE {TableName} ({columnNames})", connection);
// command.ExecuteNonQuery();
// }
// catch (Exception ex)
// {
// Console.WriteLine($"Ocorreu um erro: {ex.Message}");
// }
// }
// private static void AdicionarDadosAoBanco(OleDbConnection connection, JsonNode records, string TableName)
// {
// try
// {
// string stringColumns = string.Join(", \n", records[0]!.AsObject().AsEnumerable().Select(d => "@" + d.Key).ToList());
// string createColumns = string.Join(", \n", records[0]!.AsObject().AsEnumerable().Select(d => d.Key + " TEXT").ToList());
// if (!TabelaExiste(connection, TableName))
// {
// CriarTabela(connection, TableName, createColumns);
// }
// using OleDbTransaction transaction = connection.BeginTransaction();
// try
// {
// for (int iRow = 0; iRow < records!.AsArray().Count; iRow++)
// {
// var record = records[iRow];
// using OleDbCommand command = new(@$"INSERT INTO {TableName} VALUES ({stringColumns})", connection, transaction);
// foreach (var child in record!.AsObject())
// {
// command.Parameters.AddWithValue(child.Key, (child.Value ?? "")!.ToString());
// }
// command.ExecuteNonQuery();
// }
// transaction.Commit();
// Console.WriteLine($"{records!.AsArray().Count} dados adicionados com sucesso à [{TableName}] - Last _id {records[records!.AsArray().Count - 1]!["_id"]}");
// }
// catch (Exception ex)
// {
// transaction.Rollback();
// Console.WriteLine($"Erro ao adicionar dados: {ex.Message}");
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine($"Ocorreu um erro: {ex.Message}");
// }
// }
// static void SetTimerEndTime(DateTime date) => dtTimerEndTime = date;
// private static void WhileTimerRuns(object sender, ElapsedEventArgs e)
// {
// Console.Write($"\rEncerrando em {dtTimerEndTime.Subtract(e.SignalTime).Seconds}");
// }
// static void SetTimer()
// {
// var vTimer = new System.Timers.Timer(100);
// double dRemainingTime;
// int iInterval = Convert.ToInt32(sTimerDurationToClose!);
// SetTimerEndTime(DateTime.Now.AddSeconds(iInterval));
// vTimer.Elapsed += WhileTimerRuns!;
// vTimer.AutoReset = true;
// vTimer.Enabled = true;
// Console.WriteLine("");
// Console.WriteLine("Pressione qualquer tecla para encerrar o programa...");
// Console.WriteLine("");
// Task.Factory.StartNew(
// () =>
// {
// Console.ReadKey();
// dRemainingTime = DateTime.Now.Subtract(dtTimerEndTime).TotalSeconds;
// }
// ).Wait(
// TimeSpan.FromSeconds(iInterval)
// );
// }
// static JsonNode FormatData(JsonNode jsData)
// {
// JsonNode data = (JsonNode)JsonDocument.Parse(jsData.Root.ToString()).Deserialize(typeof(JsonNode))!;
// dynamic jsNewData = new JsonObject();
// DataTable vDistTable = dtDistribuidoras!;
// int iCount = data["result"]!["records"]!.AsArray().Count;
// for (int i = 0; i < iCount; i++)
// {
// //jsNewData.Add(new JsonObject());
// var record = data["result"]!["records"]![i];
// string ID_dist = vDistTable.Select($"CNPJ LIKE '%{record!["NumCPFCNPJ"]}%'").First()[0].ToString()!;
// string Grupo = record["DscSubGrupoTarifario"]!.ToString();
// string Perfil = "";
// if (record["DscDetalheConsumidor"]!.ToString() == "SCEE")
// {
// Perfil = "SCEE - " + record["DscModalidadeTarifaria"]!.ToString().ToUpper();
// }
// else if (record["DscDetalheConsumidor"]!.ToString() == "APE")
// {
// Perfil = record["DscModalidadeTarifaria"]!.ToString().ToUpper() + " APE";
// }
// else
// {
// Perfil = string.Join('_', (new string[] { record!["DscModalidadeTarifaria"]!.ToString().ToUpper(), record!["DscDetalheConsumidor"]!.ToString().ToUpper() }).Where(x => !string.IsNullOrEmpty(x)));
// }
// Perfil = string.Join(
// '_',
// (new string[] {
// Perfil.ToString(),
// record["DscClasseConsumidor"]!.ToString().ToUpper(),
// record["DscSubClasseConsumidor"]!.ToString().ToUpper()
// }).Where(x => !string.IsNullOrEmpty(x)));
// string Ciclo = "RH" + record!["DscResolucaoHomologatoria"]!.ToString().Split(' ')[3].Replace(".", "").Replace(",", "");
// string DatInicioVigencia = DateOnly.Parse(record!["DatInicioVigencia"]!.ToString()).ToString();
// string DatFimVigencia = DateOnly.Parse(record!["DatFimVigencia"]!.ToString()).ToString();
// string Energia_P = "";
// string Energia_FP = "";
// string Enc_P = "";
// string Enc_FP = "";
// string Dem_P = "";
// string Dem_FP = "";
// string ER_P = "";
// string ER_FP = "";
// string Megaflex = "";
// string Covid_P = "";
// string Covid_FP = "";
// string Desc_Rural = "";
// string EH_P = "";
// string EH_FP = "";
// switch (record["DscComponenteTarifario"]!.ToString(), record["DscSubGrupoTarifario"]!.ToString()[0], record["DscModalidadeTarifaria"]!.ToString())
// {
// case ("TE", 'B', _):
// Energia_P = record["VlrComponenteTarifario"]!.ToString();
// Energia_FP = record["VlrComponenteTarifario"]!.ToString();
// break;
// case ("TE", 'A', "Ponta"):
// Energia_P = record["VlrComponenteTarifario"]!.ToString();
// break;
// }
// if (jsNewData[ID_dist] == null) { jsNewData[ID_dist] = new JsonObject(); }
// if (jsNewData[ID_dist][Grupo] == null) { jsNewData[ID_dist][Grupo] = new JsonObject(); }
// if (jsNewData[ID_dist][Grupo][Perfil] == null) { jsNewData[ID_dist][Grupo][Perfil] = new JsonObject(); }
// if (jsNewData[ID_dist][Grupo][Perfil][Ciclo] == null) { jsNewData[ID_dist][Grupo][Perfil][Ciclo] = new JsonObject(); }
// jsNewData[ID_dist][Grupo][Perfil][Ciclo]["DatInicioVigencia"] = DatInicioVigencia;
// jsNewData[ID_dist][Grupo][Perfil][Ciclo]["DatFimVigencia"] = DatFimVigencia;
// jsNewData[ID_dist][Grupo][Perfil][Ciclo]["Energia_P"] = Energia_P;
// jsNewData[ID_dist][Grupo][Perfil][Ciclo]["Energia_FP"] = Energia_FP;
// jsNewData[ID_dist][Grupo][Perfil][Ciclo]["Enc_P"] = Enc_P;
// jsNewData[ID_dist][Grupo][Perfil][Ciclo]["Enc_FP"] = Enc_FP;
// jsNewData[ID_dist][Grupo][Perfil][Ciclo]["Dem_P"] = Dem_P;
// jsNewData[ID_dist][Grupo][Perfil][Ciclo]["Dem_FP"] = Dem_FP;
// jsNewData[ID_dist][Grupo][Perfil][Ciclo]["ER_P"] = ER_P;
// jsNewData[ID_dist][Grupo][Perfil][Ciclo]["ER_FP"] = ER_FP;
// jsNewData[ID_dist][Grupo][Perfil][Ciclo]["Megaflex"] = Megaflex;
// jsNewData[ID_dist][Grupo][Perfil][Ciclo]["Covid_P"] = Covid_P;
// jsNewData[ID_dist][Grupo][Perfil][Ciclo]["Covid_FP"] = Covid_FP;
// jsNewData[ID_dist][Grupo][Perfil][Ciclo]["Desc_Rural"] = Desc_Rural;
// jsNewData[ID_dist][Grupo][Perfil][Ciclo]["EH_P"] = EH_P;
// jsNewData[ID_dist][Grupo][Perfil][Ciclo]["EH_FP"] = EH_FP;
// }
// return jsNewData;
// }
// private static async Task Main(string[] args)
// {
// try
// {
// SetConfig();
// int iOffset = 0;
// var builder = WebApplication.CreateBuilder(args);
// var app = builder.Build();
// var logger = app.Services.GetRequiredService<ILogger<Program>>();
// using OleDbConnection connection = new(sResourcesDbConnection);
// connection.Open();
// if (!TabelaExiste(connection, sResourcesTableName!)) { throw new Exception($"Falha ao acessar [{sResourcesTableName}]"); }
// using OleDbConnection conn = new(sDbConnection);
// conn.Open();
// SetDistTable(conn);
// using OleDbCommand command = new($"SELECT {sResourcesTableName}.resource_id, *\r\nFROM {sResourcesTableName}\r\nWHERE ((Not ({sResourcesTableName}.resource_id)=\"\"));\r\n", connection);
// using OleDbDataReader reader = command.ExecuteReader() ?? throw new Exception();
// while (reader.Read())
// {
// iOffset = 0;
// string sTableName = $"Tarifas_{reader["Ano"]}";
// if ((string)reader["Ano"] == "All")
// {
// iOffset = GetLastIdFromTable(sTableName, "_id", connection);
// }
// else
// {
// iOffset = GetCountIdFromTable(sTableName, "_id", connection);
// }
// string sWebString = BuildUrl(sApiDadosAbertosUrl!, reader, "Ano");
// bool hasNextPage = true;
// JsonNode? tempRecords = null;
// while (hasNextPage)
// {
// JsonNode? rootObject = await PullDataAsync($@"{sWebString}offset={iOffset}");
// tempRecords = rootObject["result"]!["records"]!;
// int iTotal = (int)rootObject!["result"]!["total"]!;
// if (tempRecords != null && tempRecords.AsArray().Count != 0 && iOffset < iTotal)
// {
// JsonNode jsFormatedData = FormatData(tempRecords);
// AdicionarDadosAoBanco(connection, tempRecords, sTableName);
// iOffset += tempRecords!.AsArray().Count + iOffset;
// }
// else
// {
// Console.WriteLine("Nada mais a adicionar.");
// hasNextPage = false;
// }
// }
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine(ex.Message);
// }
// finally
// {
// SetTimer();
// }
// }
// }
//}
command.ExecuteNonQuery();
}
}
}
}

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -38,24 +39,101 @@ namespace TarifasANEEL
public class Record public class Record
{ {
public int _id { get; set; } public object this[string propertyName]
public string? DatGeracaoConjuntoDados { get; set; } {
public string? DscResolucaoHomologatoria { get; set; } get { return this.GetType().GetProperty(propertyName)!.GetValue(this, null)!; }
public string? SigNomeAgente { get; set; } set { this.GetType().GetProperty(propertyName)!.SetValue(this, value, null); }
public string? NumCPFCNPJ { get; set; } }
public string? DatInicioVigencia { get; set; } public int _id {
public string? DatFimVigencia { get; set; } get; set;
public string? DscBaseTarifaria { get; set; } }
public string? DscSubGrupoTarifario { get; set; } public string? DatGeracaoConjuntoDados {
public string? DscModalidadeTarifaria { get; set; } get; set;
public string? DscClasseConsumidor { get; set; } }
public string? DscSubClasseConsumidor { get; set; } public string? DscREH {
public string? DscDetalheConsumidor { get; set; } get; set;
public string? DscPostoTarifario { get; set; } }
public string? DscUnidade { get; set; } public string? DscResolucaoHomologatoria {
public string? SigNomeAgenteAcessante { get; set; } get; set;
public string? DscComponenteTarifario { get; set; } }
public string? VlrComponenteTarifario { get; set; } public string? SigAgente {
get; set;
}
public string? SigNomeAgente {
get; set;
}
public string? NumCNPJDistribuidora {
get; set;
}
public Object? NumCPFCNPJ {
get; set;
}
public string? DatInicioVigencia {
get; set;
}
public string? DatFimVigencia {
get; set;
}
public string? DscBaseTarifaria {
get; set;
}
public string? DscSubGrupo {
get; set;
}
public string? DscSubGrupoTarifario {
get; set;
}
public string? DscModalidadeTarifaria {
get; set;
}
public string? DscClasse {
get; set;
}
public string? DscClasseConsumidor {
get; set;
}
public string? DscSubClasse {
get; set;
}
public string? DscSubClasseConsumidor {
get; set;
}
public string? DscDetalhe {
get; set;
}
public string? DscDetalheConsumidor {
get; set;
}
public string? NomPostoTarifario {
get; set;
}
public string? DscPostoTarifario {
get; set;
}
public string? DscUnidadeTerciaria {
get; set;
}
public string? DscUnidade {
get; set;
}
public string? SigAgenteAcessante {
get; set;
}
public string? SigNomeAgenteAcessante {
get; set;
}
public string? DscComponenteTarifario {
get; set;
}
public string? VlrTUSD {
get; set;
}
public string? VlrTE {
get; set;
}
public string? VlrComponenteTarifario {
get; set;
}
} }
public class Field public class Field
@ -66,4 +144,4 @@ namespace TarifasANEEL
} }

62
Services/ApiService.cs Normal file
View File

@ -0,0 +1,62 @@
using System.Data.OleDb;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace TarifasANEEL.Services
{
public class ApiException : Exception
{
public ApiException(string message, Exception? innerException = null)
: base(message, innerException) { }
}
public static class ApiService
{
private static readonly HttpClient HttpClient = new();
public static string BuildUrl(string baseUrl, Dictionary<string, object> reader, string yearColumn)
{
string sWebString = $@"{baseUrl}?";
foreach (var kvp in reader)
{
if (kvp.Key != yearColumn && kvp.Value.ToString() != "")
{
sWebString = $@"{sWebString}{kvp.Key}={kvp.Value}&";
}
}
return sWebString;
}
public static async Task<JsonNode> PullDataAsync(string url)
{
try
{
using var response = await HttpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
var responseContent = await response.Content.ReadAsStringAsync();
return JsonNode.Parse(CleanJson(responseContent))
?? throw new JsonException("Failed to parse API response");
}
catch (HttpRequestException ex)
{
throw new ApiException($"API request failed: {url}", ex);
}
catch (JsonException ex)
{
throw new ApiException("Invalid JSON response", ex);
}
}
private static string CleanJson(string responseContent)
{
return responseContent.ToString()
.Replace(@"""NomPostoTarifario"":""Não se aplica""", @"""NomPostoTarifario"":""N""")
.Replace(@"""DscPostoTarifario"":""Não se aplica""", @"""DscPostoTarifario"":""N""")
.Replace(@"""DscSubClasseConsumidor"":""Residencial""", @"""DscSubClasseConsumidor"":""""")
.Replace(@"""DscClasseConsumidor"":""Iluminação pública""", @"""DscClasseConsumidor"":""""")
.Replace(@"""DscModalidadeTarifaria"":""Convencional pré-pagamento""", @"""DscModalidadeTarifaria"":""pré-pagamento""")
.Replace(@"""Não se aplica""", @"""""");
}
}
}

View File

@ -0,0 +1,58 @@
using Microsoft.Extensions.Configuration;
namespace TarifasANEEL.Services
{
public class ConfigurationService
{
public static string ApiDadosAbertosUrl { get; private set; } = "";
public static string ResourcesDbPath { get; private set; } = "";
public static string MainDbPath { get; private set; } = "";
public static string ResourcesTableName { get; private set; } = "";
public static string DataTableName { get; private set; } = "";
public static int TimerDurationToClose { get; private set; }
public static string ResourcesDbConnectionString { get; private set; } = "";
public static string MainDbConnectionString { get; private set; } = "";
public static void LoadConfiguration()
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
ApiDadosAbertosUrl = config["sApiDadosAbertosUrl"]!;
ResourcesDbPath = config["sResourcesDbPath"]!;
MainDbPath = config["sDbPath"]!;
ResourcesTableName = config["sResourcesTableName"]!;
DataTableName = config["sDataTableName"]!;
TimerDurationToClose = int.Parse(config["sTimerDurationToClose"]!);
ResourcesDbConnectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={ResourcesDbPath};Persist Security Info=False;";
MainDbConnectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={MainDbPath};Jet OLEDB:Database Password=gds21";
ValidateConfiguration();
}
private static void ValidateConfiguration()
{
var requiredSettings = new Dictionary<string, string>
{
{ nameof(ApiDadosAbertosUrl), ApiDadosAbertosUrl },
{ nameof(ResourcesDbPath), ResourcesDbPath },
{ nameof(MainDbPath), MainDbPath },
{ nameof(ResourcesTableName), ResourcesTableName },
{ nameof(DataTableName), DataTableName }
};
var missingSettings = requiredSettings
.Where(kvp => string.IsNullOrEmpty(kvp.Value))
.Select(kvp => kvp.Key)
.ToList();
if (missingSettings.Any())
{
throw new InvalidOperationException(
$"Missing required configuration values: {string.Join(", ", missingSettings)}");
}
}
}
}

227
Services/DatabaseService.cs Normal file
View File

@ -0,0 +1,227 @@
using System.Data;
using System.Data.OleDb;
using TarifasANEEL.Models;
using System.Text.Json.Nodes;
using System.Globalization;
namespace TarifasANEEL.Services
{
public static class DatabaseService
{
public static DataTable DistribuidorasTable { get; private set; } = new();
public static OleDbConnection CreateConnection(string connectionString)
{
return new OleDbConnection(connectionString);
}
public static bool TableExists(OleDbConnection connection, string tableName)
{
try
{
using var command = new OleDbCommand($"SELECT COUNT(*) FROM {tableName}", connection);
command.ExecuteScalar();
return true;
}
catch (OleDbException)
{
return false;
}
}
public static void LoadDistribuidoras(OleDbConnection connection)
{
if (!TableExists(connection, "Distribuidoras_geral"))
{
throw new Exception("Tabela [Distribuidoras_geral] não encontrada.");
}
using var command = new OleDbCommand(
$"SELECT *\r\nFROM Distribuidoras_geral\r\nWHERE ((Not (Distribuidoras_geral.CNPJ)=\"\"));",
connection);
using OleDbDataReader reader = command.ExecuteReader();
DistribuidorasTable = new DataTable();
DistribuidorasTable.Load(reader);
}
public static int GetOffsetForTable(string tableName, bool isAll, string idColumn, OleDbConnection connection)
{
if (TableExists(connection, tableName))
{
string query = isAll
? $"SELECT MAX([{idColumn}]) FROM {tableName}"
: $"SELECT COUNT([{idColumn}]) FROM {tableName}";
using var command = new OleDbCommand(query, connection);
object result = command.ExecuteScalar() ?? 0;
return Convert.ToInt32(result);
}
return 0;
}
public static int GetDistribuidoraId(string cnpj)
{
return int.Parse(DistribuidorasTable?.AsEnumerable().FirstOrDefault(row => row["CNPJ"].ToString() == cnpj)?["ID_dist"].ToString() ?? "0");
}
public static void AddDataToTable(OleDbConnection connection,
Dictionary<int, Dictionary<string, Dictionary<string, Dictionary<string, Tarifa>>>> data,
string tableName, string year)
{
if (!TableExists(connection, tableName))
{
// Define columns based on Tarifa model properties
var createColumns = @"
ID_tar COUNTER PRIMARY KEY,
ID_dist NUMBER,
Grupo TEXT,
Perfil TEXT,
Ciclo TEXT,
DatInicioVigencia DATETIME,
DatFimVigencia DATETIME,
Energia_P NUMBER,
Energia_FP NUMBER,
Enc_P NUMBER,
Enc_FP NUMBER,
Dem_P NUMBER,
Dem_FP NUMBER,
ER_P NUMBER,
ER_FP NUMBER,
Megaflex NUMBER,
Covid_P NUMBER,
Covid_FP NUMBER,
EH_P NUMBER,
EH_FP NUMBER";
CreateTable(connection, tableName, createColumns);
}
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
using var transaction = connection.BeginTransaction();
try
{
foreach (var (idDist, grupos) in data)
foreach (var (grupo, perfis) in grupos)
foreach (var (perfil, ciclos) in perfis)
foreach (var (ciclo, tarifa) in ciclos)
{
var selectCommand = new OleDbCommand(
$"SELECT ID_tar FROM {tableName} " +
"WHERE ID_dist = @ID_dist AND Grupo = @Grupo AND Perfil = @Perfil AND Ciclo = @Ciclo",
connection, transaction);
selectCommand.Parameters.AddWithValue("@ID_dist", idDist);
selectCommand.Parameters.AddWithValue("@Grupo", grupo);
selectCommand.Parameters.AddWithValue("@Perfil", perfil);
selectCommand.Parameters.AddWithValue("@Ciclo", ciclo);
object? result = null;
try
{
result = selectCommand.ExecuteScalar();
}
catch (Exception ex)
{
Console.WriteLine($"Error checking for existing record: {ex.Message}");
}
OleDbCommand command;
if (result != null && result != DBNull.Value)
{
var updateSql = $@"UPDATE {tableName}
SET DatInicioVigencia = @DatInicioVigencia,
DatFimVigencia = @DatFimVigencia,
Energia_P = @Energia_P,
Energia_FP = @Energia_FP,
Enc_P = @Enc_P,
Enc_FP = @Enc_FP,
Dem_P = @Dem_P,
Dem_FP = @Dem_FP,
ER_P = @ER_P,
ER_FP = @ER_FP,
Covid_P = @Covid_P,
Covid_FP = @Covid_FP,
EH_P = @EH_P,
EH_FP = @EH_FP
WHERE ID_tar = @ID_tar";
command = new OleDbCommand(updateSql, connection, transaction);
AddTarifaParameters(command, tarifa);
command.Parameters.AddWithValue("@ID_tar", result);
}
else
{
// Insert new record
var insertSql = $@"INSERT INTO {tableName}
(ID_dist, Grupo, Perfil, Ciclo, DatInicioVigencia,
DatFimVigencia, Energia_P, Energia_FP, Enc_P, Enc_FP,
Dem_P, Dem_FP, ER_P, ER_FP, Covid_P,
Covid_FP, EH_P, EH_FP)
VALUES
(@ID_dist, @Grupo, @Perfil, @Ciclo, @DatInicioVigencia,
@DatFimVigencia, @Energia_P, @Energia_FP, @Enc_P, @Enc_FP,
@Dem_P, @Dem_FP, @ER_P, @ER_FP, @Covid_P,
@Covid_FP, @EH_P, @EH_FP)";
command = new OleDbCommand(insertSql, connection, transaction);
command.Parameters.AddWithValue("@ID_dist", tarifa.ID_dist);
command.Parameters.AddWithValue("@Grupo", tarifa.Grupo);
command.Parameters.AddWithValue("@Perfil", tarifa.Perfil);
command.Parameters.AddWithValue("@Ciclo", tarifa.Ciclo);
AddTarifaParameters(command, tarifa);
}
try
{
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine($"Error executing command: {ex.Message}");
throw;
}
}
transaction.Commit();
Console.WriteLine($"Dados de {year} atualizados com sucesso em [{tableName}]");
}
catch (Exception ex)
{
Console.WriteLine($"Erro ao atualizar dados: {ex.Message}");
transaction.Rollback();
throw;
}
}
private static void AddTarifaParameters(OleDbCommand command, Tarifa tarifa)
{
command.Parameters.AddWithValue("@DatInicioVigencia", tarifa.DatInicioVigencia);
command.Parameters.AddWithValue("@DatFimVigencia", tarifa.DatFimVigencia);
command.Parameters.AddWithValue("@Energia_P", tarifa.Energia_P);
command.Parameters.AddWithValue("@Energia_FP", tarifa.Energia_FP);
command.Parameters.AddWithValue("@Enc_P", tarifa.Enc_P);
command.Parameters.AddWithValue("@Enc_FP", tarifa.Enc_FP);
command.Parameters.AddWithValue("@Dem_P", tarifa.Dem_P);
command.Parameters.AddWithValue("@Dem_FP", tarifa.Dem_FP);
command.Parameters.AddWithValue("@ER_P", tarifa.ER_P);
command.Parameters.AddWithValue("@ER_FP", tarifa.ER_FP);
command.Parameters.AddWithValue("@Covid_P", tarifa.Covid_P);
command.Parameters.AddWithValue("@Covid_FP", tarifa.Covid_FP);
command.Parameters.AddWithValue("@EH_P", tarifa.EH_P);
command.Parameters.AddWithValue("@EH_FP", tarifa.EH_FP);
}
private static void CreateTable(OleDbConnection connection, string tableName, string columnNames)
{
using var command = new OleDbCommand(
@$"CREATE TABLE {tableName} ({columnNames})",
connection);
command.ExecuteNonQuery();
}
}
}

View File

@ -5,11 +5,30 @@
<TargetFramework>net7.0-windows10.0.17763.0</TargetFramework> <TargetFramework>net7.0-windows10.0.17763.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<UserSecretsId>4a8b5d39-dbdf-4469-9564-cdbc2a8a445a</UserSecretsId>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="System.Data.OleDb" Version="8.0.0" /> <FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" /> <Compile Remove="Rootobject.cs" />
</ItemGroup>
<ItemGroup>
<None Remove="SQLQuery5.sql" />
<None Remove="Tarifas.mdf" />
<None Remove="Tarifas_log.ldf" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.Data.OleDb" Version="8.0.1" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>

125
Utilities/TarifaHelper.cs Normal file
View File

@ -0,0 +1,125 @@
using System.Data;
using System.Text.Json.Nodes;
using TarifasANEEL.Models;
using TarifasANEEL.Services;
namespace TarifasANEEL.Utilities
{
public static class TarifaHelper
{
private static readonly Dictionary<string, Func<JsonNode, string, string>> ProfileBuilders = new()
{
["SCEE"] = (record, modality) => $"SCEE - {modality.ToUpper()}",
["APE"] = (record, modality) => $"{modality.ToUpper()} APE"
};
public static Dictionary<int, Dictionary<string, Dictionary<string, Dictionary<string, Tarifa>>>> FormatData(JsonNode records)
{
var formattedData = new Dictionary<int, Dictionary<string, Dictionary<string, Dictionary<string, Tarifa>>>>();
foreach (var record in records.AsArray())
{
if (record == null) continue;
var tempTarifa = Tarifa.FromRecord(record);
var idDist = tempTarifa.ID_dist;
if (idDist == 0) continue;
var grupo = tempTarifa.Grupo;
var perfil = tempTarifa.Perfil;
var ciclo = tempTarifa.Ciclo;
// Get or create the Tarifa object
var tarifa = formattedData
.GetOrAdd(idDist)
.GetOrAdd(grupo)
.GetOrAdd(perfil)
.GetOrAdd(ciclo, () => tempTarifa);
// Update the component values based on the record
tarifa.UpdateComponentValue(record);
}
// Filter out Tarifas with all zero/null/empty values
foreach (var distKvp in formattedData.ToList())
{
foreach (var grupoKvp in distKvp.Value.ToList())
{
foreach (var perfilKvp in grupoKvp.Value.ToList())
{
foreach (var cicloKvp in perfilKvp.Value.ToList())
{
if (cicloKvp.Value.HasAllEmptyValues())
{
perfilKvp.Value.Remove(cicloKvp.Key);
}
}
if (perfilKvp.Value.Count == 0)
{
grupoKvp.Value.Remove(perfilKvp.Key);
}
}
if (grupoKvp.Value.Count == 0)
{
distKvp.Value.Remove(grupoKvp.Key);
}
}
if (distKvp.Value.Count == 0)
{
formattedData.Remove(distKvp.Key);
}
}
return formattedData;
}
public static string BuildProfileName(JsonNode record, string modality)
{
var consumerDetail = record["DscDetalheConsumidor"]!.ToString();
string profile = ProfileBuilders.TryGetValue(consumerDetail, out var builder)
? builder(record, modality)
: string.Join('_', new[] {
modality.ToUpper(),
consumerDetail.ToUpper()
}.Where(x => !string.IsNullOrEmpty(x)));
return string.Join('_', new[] {
profile,
record["DscClasseConsumidor"]!.ToString().ToUpper(),
record["DscSubClasseConsumidor"]!.ToString().ToUpper()
}.Where(x => !string.IsNullOrEmpty(x)));
}
public static string ParseResolution(JsonNode record)
{
return record["DscResolucaoHomologatoria"]!.ToString()
.Split(' ')[3]
.Replace(".", "")
.Replace(",", "");
}
}
// Add this extension method to help with dictionary operations
public static class DictionaryExtensions
{
public static TValue GetOrAdd<TKey, TValue>(
this Dictionary<TKey, TValue> dict,
TKey key,
Func<TValue>? valueFactory = null) where TKey : notnull
{
if (!dict.TryGetValue(key, out var value))
{
value = valueFactory != null
? valueFactory()
: (TValue)Activator.CreateInstance(typeof(TValue))!;
dict[key] = value;
}
return value;
}
}
}

43
Utilities/TimerHelper.cs Normal file
View File

@ -0,0 +1,43 @@
using System.Timers;
namespace TarifasANEEL.Utilities
{
public static class TimerHelper
{
static DateTime dtTimerEndTime;
static void SetTimerEndTime(DateTime date) => dtTimerEndTime = date;
static void WhileTimerRuns(object sender, ElapsedEventArgs e)
{
Console.Write($"\rEncerrando em {dtTimerEndTime.Subtract(e.SignalTime).Seconds}");
}
public static void SetTimer(int durationInSeconds)
{
var vTimer = new System.Timers.Timer(100);
double dRemainingTime;
SetTimerEndTime(DateTime.Now.AddSeconds(durationInSeconds));
vTimer.Elapsed += WhileTimerRuns!;
vTimer.AutoReset = true;
vTimer.Enabled = true;
Console.WriteLine("");
Console.WriteLine("Pressione qualquer tecla para encerrar o programa...");
Console.WriteLine("");
Task.Factory.StartNew(
() =>
{
Console.ReadKey();
dRemainingTime = DateTime.Now.Subtract(dtTimerEndTime).TotalSeconds;
}
).Wait(
TimeSpan.FromSeconds(durationInSeconds)
);
}
private static void OnTimedEvent(object? source, ElapsedEventArgs e)
{
Console.WriteLine("Encerrando o aplicativo.");
Environment.Exit(0);
}
}
}

8
appsettings.json Normal file
View File

@ -0,0 +1,8 @@
{
"sApiDadosAbertosUrl": "https://dadosabertos.aneel.gov.br/api/3/action/datastore_search",
"sResourcesDbPath": "X:/Middle/Informativo Setorial/Modelo Word/ANEEL.accdb",
"sDbPath": "X:/Middle/Informativo Setorial/Modelo Word/BD1_dados cadastrais e faturas.accdb",
"sResourcesTableName": "tblResourceComponentesTarifarias",
"sDataTableName": "Distribuidoras_tarifas",
"sTimerDurationToClose": 10
}