diff --git a/ANEEL.accdb b/ANEEL.accdb new file mode 100644 index 0000000..a4c7ffa Binary files /dev/null and b/ANEEL.accdb differ diff --git a/ANEEL.laccdb b/ANEEL.laccdb new file mode 100644 index 0000000..6610406 Binary files /dev/null and b/ANEEL.laccdb differ diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..0c85edc --- /dev/null +++ b/Program.cs @@ -0,0 +1,268 @@ + +using System.Text.Json; +using System.Data.OleDb; +using TarifasANEEL; +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; + +public class Program +{ + private static async Task Main(string[] args) + { + string AneelURL = "https://dadosabertos.aneel.gov.br/api/3/action/datastore_search?resource_id=fcf2906c-7c32-4b9b-a637-054e7a5234f4&limit=32000"; + string AccessPath = @"C:\Users\contratos\Documents\Giuliano\Code\TarifasANEEL\ANEEL.accdb"; + + Rootobject? rootObject = await PullDataAsync(AneelURL); + + List? records = rootObject?.result?.records?.ToList(); + List? 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) + { + rootObject = await PullDataAsync(AneelURL + "&offset=" + records.Last()._id); + Console.WriteLine(rootObject?.result?.records?.Last()._id); + + tempRecords = rootObject?.result?.records?.ToList(); + if (tempRecords != null) + { + records.AddRange(tempRecords); + } + } + } + if (records != null) + { + UpdateData(records, "tblTarifas"); + } + + } + + private static async Task 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(await response.Content.ReadAsStringAsync(), options); + + return rootObject; + } + public static DataTable ConvertToDataTable(IList 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(List 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 + { + conn.Open(); + + //Creating temp table on database + command.CommandText = "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)"; + command.ExecuteNonQuery(); + + //Bulk insert into temp table + using (SqlBulkCopy bulkcopy = new SqlBulkCopy(conn)) + { + bulkcopy.BulkCopyTimeout = 660; + bulkcopy.DestinationTableName = "TmpTable"; + 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.[DscREH] = T.[DscREH], + P.[SigAgente] = T.[SigAgente], + P.[NumCNPJDistribuidora] = T.[NumCNPJDistribuidora], + P.[DatInicioVigencia] = T.[DatInicioVigencia], + P.[DatFimVigencia] = T.[DatFimVigencia], + P.[DscBaseTarifaria] = T.[DscBaseTarifaria], + P.[DscSubGrupo] = T.[DscSubGrupo], + P.[DscModalidadeTarifaria] = T.[DscModalidadeTarifaria], + P.[DscClasse] = T.[DscClasse], + P.[DscSubClasse] = T.[DscSubClasse], + P.[DscDetalhe] = T.[DscDetalhe], + P.[NomPostoTarifario] = T.[NomPostoTarifario], + P.[DscUnidadeTerciaria] = T.[DscUnidadeTerciaria], + P.[SigAgenteAcessante] = T.[SigAgenteAcessante], + P.[VlrTUSD] = T.[VlrTUSD], + P.[VlrTE] = T.[VlrTE] + 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(); + } + } + } + } + private static void AdicionarDadosAoBanco(List records, string accessPath) + { + try + { + using (OleDbConnection connection = new OleDbConnection($"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={accessPath};Persist Security Info=False;")) + { + connection.Open(); + + // Verificar se a tabela temporária já existe + if (!TmpTableExiste(connection)) + { + CriarTmpTable(connection); + } + + using (OleDbTransaction transaction = connection.BeginTransaction()) + { + try + { + 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("@DscREH", record.DscREH); + command.Parameters.AddWithValue("@SigAgente", record.SigAgente); + command.Parameters.AddWithValue("@NumCNPJDistribuidora", record.NumCNPJDistribuidora); + command.Parameters.AddWithValue("@DatInicioVigencia", record.DatInicioVigencia); + command.Parameters.AddWithValue("@DatFimVigencia", record.DatFimVigencia); + command.Parameters.AddWithValue("@DatFimVigencia", record.DatFimVigencia); + command.Parameters.AddWithValue("@DscBaseTarifaria", record.DscBaseTarifaria); + command.Parameters.AddWithValue("@DscSubGrupo", record.DscSubGrupo); + command.Parameters.AddWithValue("@DscModalidadeTarifaria", record.DscModalidadeTarifaria); + command.Parameters.AddWithValue("@DscClasse", record.DscClasse); + command.Parameters.AddWithValue("@DscSubClasse", record.DscSubClasse); + command.Parameters.AddWithValue("@DscDetalhe", record.DscDetalhe); + command.Parameters.AddWithValue("@NomPostoTarifario", record.NomPostoTarifario); + command.Parameters.AddWithValue("@DscUnidadeTerciaria", record.DscUnidadeTerciaria); + command.Parameters.AddWithValue("@SigAgenteAcessante", record.SigAgenteAcessante); + command.Parameters.AddWithValue("@VlrTUSD", record.VlrTUSD); + command.Parameters.AddWithValue("@VlrTE", record.VlrTE); + + 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 (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)) + { + command.ExecuteNonQuery(); + } + } + + private static void AdicionarDadosATmpTable(OleDbConnection connection, List records) + { + 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)) + { + // Adicionar parâmetros + command.Parameters.AddWithValue("@_id", record._id); + command.Parameters.AddWithValue("@DatGeracaoConjuntoDados", record.DatGeracaoConjuntoDados); + command.Parameters.AddWithValue("@DscREH", record.DscREH); + command.Parameters.AddWithValue("@SigAgente", record.SigAgente); + command.Parameters.AddWithValue("@NumCNPJDistribuidora", record.NumCNPJDistribuidora); + command.Parameters.AddWithValue("@DatInicioVigencia", record.DatInicioVigencia); + command.Parameters.AddWithValue("@DatFimVigencia", record.DatFimVigencia); + command.Parameters.AddWithValue("@DatFimVigencia", record.DatFimVigencia); + command.Parameters.AddWithValue("@DscBaseTarifaria",record.DscBaseTarifaria); + command.Parameters.AddWithValue("@DscSubGrupo", record.DscSubGrupo); + command.Parameters.AddWithValue("@DscModalidadeTarifaria", record.DscModalidadeTarifaria); + command.Parameters.AddWithValue("@DscClasse", record.DscClasse); + command.Parameters.AddWithValue("@DscSubClasse", record.DscSubClasse); + command.Parameters.AddWithValue("@DscDetalhe", record.DscDetalhe); + command.Parameters.AddWithValue("@NomPostoTarifario", record.NomPostoTarifario); + command.Parameters.AddWithValue("@DscUnidadeTerciaria", record.DscUnidadeTerciaria); + command.Parameters.AddWithValue("@SigAgenteAcessante", record.SigAgenteAcessante); + command.Parameters.AddWithValue("@VlrTUSD", record.VlrTUSD); + command.Parameters.AddWithValue("@VlrTE", record.VlrTE); + + // Adicionar outros parâmetros conforme necessário + + command.ExecuteNonQuery(); + } + } + } +} \ No newline at end of file diff --git a/Rootobject.cs b/Rootobject.cs new file mode 100644 index 0000000..5c836f3 --- /dev/null +++ b/Rootobject.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TarifasANEEL +{ + + + public class Rootobject + { + public string? help { get; set; } + public bool success { get; set; } + public required Result result { get; set; } + } + + public class Result + { + public bool include_total { get; set; } + public int limit { get; set; } + public string? records_format { get; set; } + public string? resource_id { get; set; } + public object? total_estimation_threshold { get; set; } + public Record[]? records { get; set; } + public Field[]? fields { get; set; } + public _Links? _links { get; set; } + public int total { get; set; } + public bool total_was_estimated { get; set; } + } + + public class _Links + { + public string? start { get; set; } + public string? next { get; set; } + } + + public class Record + { + public int _id { get; set; } + public string? DatGeracaoConjuntoDados { get; set; } + public string? DscREH { get; set; } + public string? SigAgente { get; set; } + public string? NumCNPJDistribuidora { get; set; } + public string? DatInicioVigencia { get; set; } + public string? DatFimVigencia { get; set; } + public string? DscBaseTarifaria { get; set; } + public string? DscSubGrupo { get; set; } + public string? DscModalidadeTarifaria { get; set; } + public string? DscClasse { get; set; } + public string? DscSubClasse { get; set; } + public string? DscDetalhe { get; set; } + public string? NomPostoTarifario { get; set; } + public string? DscUnidadeTerciaria { get; set; } + public string? SigAgenteAcessante { get; set; } + public string? VlrTUSD { get; set; } + public string? VlrTE { get; set; } + } + + public class Field + { + public string? id { get; set; } + public string? type { get; set; } + } + + +} diff --git a/SQLQuery5.sql b/SQLQuery5.sql new file mode 100644 index 0000000..1dac889 --- /dev/null +++ b/SQLQuery5.sql @@ -0,0 +1,19 @@ +UPDATE P SET +P.[DatGeracaoConjuntoDados] = T.[DatGeracaoConjuntoDados], +P.[DscREH] = T.[DscREH], +P.[SigAgente] = T.[SigAgente], +P.[NumCNPJDistribuidora] = T.[NumCNPJDistribuidora], +P.[DatInicioVigencia] = T.[DatInicioVigencia], +P.[DatFimVigencia] = T.[DatFimVigencia], +P.[DscBaseTarifaria] = T.[DscBaseTarifaria], +P.[DscSubGrupo] = T.[DscSubGrupo], +P.[DscModalidadeTarifaria] = T.[DscModalidadeTarifaria], +P.[DscClasse] = T.[DscClasse], +P.[DscSubClasse] = T.[DscSubClasse], +P.[DscDetalhe] = T.[DscDetalhe], +P.[NomPostoTarifario] = T.[NomPostoTarifario], +P.[DscUnidadeTerciaria] = T.[DscUnidadeTerciaria], +P.[SigAgenteAcessante] = T.[SigAgenteAcessante], +P.[VlrTUSD] = T.[VlrTUSD], +P.[VlrTE] = T.[VlrTE] +FROM [TmpTable] AS P INNER JOIN TmpTable AS T ON P.[_id] = T.[_id] ;DROP TABLE TmpTable; \ No newline at end of file diff --git a/TarifasANEEL.csproj b/TarifasANEEL.csproj new file mode 100644 index 0000000..a2ad7db --- /dev/null +++ b/TarifasANEEL.csproj @@ -0,0 +1,15 @@ + + + + Exe + net7.0-windows10.0.17763.0 + enable + enable + + + + + + + + diff --git a/TarifasANEEL.sln b/TarifasANEEL.sln new file mode 100644 index 0000000..886f708 --- /dev/null +++ b/TarifasANEEL.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34221.43 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TarifasANEEL", "TarifasANEEL.csproj", "{1F607620-F794-43F3-A6DD-3CFF4649251D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1F607620-F794-43F3-A6DD-3CFF4649251D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1F607620-F794-43F3-A6DD-3CFF4649251D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1F607620-F794-43F3-A6DD-3CFF4649251D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1F607620-F794-43F3-A6DD-3CFF4649251D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {232BF577-9134-43C9-91D2-46118A657BA0} + EndGlobalSection +EndGlobal