321 lines
14 KiB
C#
321 lines
14 KiB
C#
using System;
|
|
using System.Data.OleDb;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
using System.Reflection.Metadata;
|
|
using Pipefy;
|
|
using Microsoft.VisualBasic;
|
|
using System.Net.Quic;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json.Linq;
|
|
using Newtonsoft.Json;
|
|
|
|
class Program
|
|
{
|
|
static async Task Main(string[] args)
|
|
{
|
|
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
|
|
IConfiguration configuration = configurationBuilder.AddUserSecrets<Program>().Build();
|
|
|
|
Console.Clear();
|
|
// URL da API que você deseja chamar
|
|
string ConnSourcePath = configuration.GetValue<string>("DB_PATH");
|
|
string apiUrl = configuration.GetValue<string>("PIPEFY_API_URL");
|
|
string PipefyToken = configuration.GetValue<string>("PIPEFY_API_TOKEN");
|
|
string PipefyTokenTableID = configuration.GetValue<string>("PIPEFY_TABLE_ID");
|
|
string PipefyTokenTableIDGestores = configuration.GetValue<string>("PIPEFY_TABLE_ID_GESTORES");
|
|
|
|
JArray allRecords = await GetPipefyDataAsync(apiUrl,PipefyToken,PipefyTokenTableID);
|
|
JArray allGestores = await GetPipefyDataAsync(apiUrl, PipefyToken, PipefyTokenTableIDGestores);
|
|
|
|
Console.Clear();
|
|
|
|
if (allRecords is not null)
|
|
{
|
|
string strGestores = JsonConvert.SerializeObject(allGestores);
|
|
// Desserialize o JSON em objetos C#
|
|
var jsonGestores = JsonConvert.DeserializeObject<List<Pipefy.RootGestor>>(strGestores);
|
|
|
|
List<Pipefy.ClasseGestores> jsonListGestores = convertGestoresJson(jsonGestores);
|
|
|
|
string strJson = JsonConvert.SerializeObject(allRecords);
|
|
// Desserialize o JSON em objetos C#
|
|
var jsonData = JsonConvert.DeserializeObject<List<Pipefy.RootObject>>(strJson);
|
|
|
|
List<Pipefy.ClasseEmpresas> jsonList = convertEmpresasJson(jsonData);
|
|
|
|
// Consulte os dados da tabela no banco de dados Access
|
|
List<Pipefy.ClasseEmpresas> databaseData = GetDataFromDatabase(ConnSourcePath);
|
|
|
|
// Compare os dados e encontre os registros ausentes no JSON
|
|
List<Pipefy.ClasseEmpresas> recordsMissingInJson = CompareData(databaseData, jsonList, jsonListGestores);
|
|
|
|
if (recordsMissingInJson.Count != 0)
|
|
{
|
|
string strQuery = "{\"query\":\"mutation {\\r\\n ";
|
|
for (int i = 0; i < recordsMissingInJson.Count; i++)
|
|
{
|
|
if (i % 49 == 0) { strQuery = "{\"query\":\"mutation {\\r\\n "; }
|
|
|
|
strQuery = strQuery + $"N{i}: createTableRecord(input: {{ table_id: \\\"{PipefyTokenTableID}\\\", fields_attributes: [ {{ field_id: \\\"nome_da_empresa\\\", field_value: \\\"{recordsMissingInJson[i].nome_da_empresa.ToString()}\\\" }} {{ field_id: \\\"c_digo_smart\\\", field_value: \\\"{recordsMissingInJson[i].c_digo_smart.ToString()}\\\" }} {{ field_id: \\\"modalidade\\\", field_value: \\\"{recordsMissingInJson[i].modalidade.ToString()}\\\" }} {{ field_id: \\\"gestores\\\", field_value: \\\"{recordsMissingInJson[i].gestores.ToString()}\\\" }}]}}) {{ table_record {{ id }}}}\\r\\n ";
|
|
|
|
if (i % 48 == 0 && i != 0) {
|
|
|
|
strQuery = strQuery + "}\",\"variables\":{}}";
|
|
|
|
bool success = await CreatePipefyDataAsync(apiUrl, PipefyToken, PipefyTokenTableID, strQuery);
|
|
|
|
//if (!success) {
|
|
// int test2 = 10;
|
|
//}
|
|
}
|
|
}
|
|
|
|
strQuery = strQuery + "}\",\"variables\":{}}";
|
|
|
|
bool success1 = await CreatePipefyDataAsync(apiUrl, PipefyToken, PipefyTokenTableID, strQuery);
|
|
|
|
// Faça algo com os registros ausentes
|
|
int maxCId = recordsMissingInJson.OrderByDescending(s => s.c_digo_smart.Length).First().c_digo_smart.Length;
|
|
int maxCNome = recordsMissingInJson.OrderByDescending(s => s.nome_da_empresa.Length).First().nome_da_empresa.Length;
|
|
int maxCMod = recordsMissingInJson.OrderByDescending(s => s.modalidade.Length).First().modalidade.Length;
|
|
int maxCGestao = recordsMissingInJson.OrderByDescending(s => s.gestores.Length).First().gestores.Length;
|
|
|
|
foreach (var record in recordsMissingInJson)
|
|
{
|
|
Console.WriteLine(String.Format($"| ID: {{0,{maxCId}}} | Nome: {{1,{maxCNome}}} | Modalidade: {{2,{maxCMod}}} | Gestão: {{3,{maxCGestao}}} |",record.c_digo_smart,record.nome_da_empresa,record.modalidade,record.gestores));
|
|
}
|
|
|
|
Console.WriteLine($"");
|
|
|
|
}
|
|
|
|
Console.WriteLine($"{recordsMissingInJson.Count} registros encontrados.");
|
|
|
|
}
|
|
}
|
|
private static async Task<bool> CreatePipefyDataAsync(string apiUrl, string PipefyToken, string PipefyTokenTableID, string query)
|
|
{
|
|
|
|
var httpClient = new HttpClient();
|
|
// Defina os headers da requisição (opcional)
|
|
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + PipefyToken);
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Post, apiUrl);
|
|
request.Headers.Add("Accept", "application/json");
|
|
|
|
var content = new StringContent(query, null, "application/json");
|
|
request.Content = content;
|
|
var response = await httpClient.SendAsync(request);
|
|
var body = JsonConvert.SerializeObject(response.Content);
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
private static async Task<JArray> GetPipefyDataAsync(string apiUrl, string PipefyToken, string PipefyTokenTableID)
|
|
{
|
|
JArray allRecords = new JArray();
|
|
|
|
string cursor = "null";
|
|
|
|
string query = $"{{\"query\":\"query GetRecords($cursor: String){{ table_records(table_id: \\\"{PipefyTokenTableID}\\\",first:50,after:$cursor){{ pageInfo{{ hasNextPage endCursor }} edges{{ node{{ id record_fields{{ field {{ id }} value array_value }} }} }} }}}}\",\"variables\":{{\"cursor\":{cursor}}}}}";
|
|
|
|
bool hasNextPage = true;
|
|
|
|
while (hasNextPage)
|
|
{
|
|
var httpClient = new HttpClient();
|
|
// Defina os headers da requisição (opcional)
|
|
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + PipefyToken);
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Post, apiUrl);
|
|
request.Headers.Add("Accept", "application/json");
|
|
|
|
var content = new StringContent(query, null, "application/json");
|
|
request.Content = content;
|
|
var response = await httpClient.SendAsync(request);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var responseContent = await response.Content.ReadAsStringAsync();
|
|
var responseData = JObject.Parse(responseContent);
|
|
|
|
Console.Clear();
|
|
|
|
var records = responseData["data"]["table_records"]["edges"];
|
|
foreach (var record in records)
|
|
{
|
|
//Console.WriteLine(record);
|
|
allRecords.Add(record);
|
|
}
|
|
|
|
hasNextPage = responseData["data"]["table_records"]["pageInfo"]["hasNextPage"].Value<bool>();
|
|
cursor = responseData["data"]["table_records"]["pageInfo"]["endCursor"].Value<string>();
|
|
query = $"{{\"query\":\"query GetRecords($cursor: String){{ table_records(table_id: \\\"{PipefyTokenTableID}\\\",first:50,after:$cursor){{ pageInfo{{ hasNextPage endCursor }} edges{{ node{{ record_fields{{ field {{ id }} value array_value }} }} }} }}}}\",\"variables\":{{\"cursor\":\"{cursor}\"}}}}";
|
|
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine($"Erro na solicitação GraphQL: {response.StatusCode}");
|
|
allRecords.Clear();
|
|
return allRecords;
|
|
}
|
|
}
|
|
|
|
return allRecords;
|
|
}
|
|
|
|
static List<Pipefy.ClasseEmpresas> GetDataFromDatabase(string ConnSourcePath)
|
|
{
|
|
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ConnSourcePath;
|
|
List<Pipefy.ClasseEmpresas> data = new List<Pipefy.ClasseEmpresas>();
|
|
|
|
using (OleDbConnection connection = new OleDbConnection(connectionString))
|
|
{
|
|
connection.Open();
|
|
|
|
// Execute uma consulta SQL para recuperar dados da tabela no banco de dados Access
|
|
string sqlQuery = "SELECT * FROM tblEmpresas";
|
|
using (OleDbCommand command = new OleDbCommand(sqlQuery, connection))
|
|
{
|
|
using (OleDbDataReader reader = command.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
Pipefy.ClasseEmpresas record = new Pipefy.ClasseEmpresas
|
|
{
|
|
c_digo_smart = (string)reader["Código Smart"].ToString(),
|
|
nome_da_empresa = (string)reader["Nome da empresa"],
|
|
modalidade = (string)reader["modalidade"],
|
|
gestores = (string)reader["gestores"],
|
|
rec_id = ""
|
|
|
|
// Adicione outras propriedades conforme necessário
|
|
};
|
|
data.Add(record);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return data;
|
|
}
|
|
static List<Pipefy.ClasseEmpresas> convertEmpresasJson(List<Pipefy.RootObject> jsonData)
|
|
{
|
|
List<Pipefy.ClasseEmpresas> data = new List<Pipefy.ClasseEmpresas>();
|
|
|
|
for (int i = 0;i<jsonData.Count;i++)
|
|
{
|
|
Pipefy.ClasseEmpresas record = new Pipefy.ClasseEmpresas();
|
|
record.rec_id = jsonData[i].node.id;
|
|
|
|
for (int j = 0;j < jsonData[i].node.record_fields.Length;j++)
|
|
{
|
|
switch (jsonData[i].node.record_fields[j].field.id)
|
|
{
|
|
case "nome_da_empresa":
|
|
record.nome_da_empresa = jsonData[i].node.record_fields[j].value;
|
|
break;
|
|
case "c_digo_smart":
|
|
record.c_digo_smart = jsonData[i].node.record_fields[j].value.Replace(".0","");
|
|
break;
|
|
case "modalidade":
|
|
record.modalidade = jsonData[i].node.record_fields[j].value;
|
|
break;
|
|
case "gestores":
|
|
record.gestores = jsonData[i].node.record_fields[j].array_value.FirstOrDefault().ToString();
|
|
break;
|
|
|
|
}
|
|
}
|
|
|
|
// Adicione outras propriedades conforme necessário
|
|
data.Add(record);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
static List<Pipefy.ClasseGestores> convertGestoresJson(List<Pipefy.RootGestor> jsonData)
|
|
{
|
|
List<Pipefy.ClasseGestores> data = new List<Pipefy.ClasseGestores>();
|
|
|
|
for (int i = 0; i < jsonData.Count; i++)
|
|
{
|
|
|
|
Pipefy.ClasseGestores record = new Pipefy.ClasseGestores();
|
|
record.id = jsonData[i].node.id.ToString();
|
|
|
|
for (int j = 0; j < jsonData[i].node.record_fields.Length; j++)
|
|
{
|
|
if (jsonData[i].node.record_fields[j].field.id == "gest_o")
|
|
{
|
|
record.gestores = jsonData[i].node.record_fields[j].value;
|
|
|
|
}
|
|
}
|
|
|
|
// Adicione outras propriedades conforme necessário
|
|
data.Add(record);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
static List<Pipefy.ClasseEmpresas> CompareData(List<Pipefy.ClasseEmpresas> databaseData, List<Pipefy.ClasseEmpresas> jsonList, List<Pipefy.ClasseGestores> jsonListGestores)
|
|
{
|
|
if (jsonList == null | jsonListGestores == null)
|
|
{
|
|
return databaseData;
|
|
}
|
|
|
|
List<Pipefy.ClasseEmpresas> recordsMissingInJson = new List<Pipefy.ClasseEmpresas>();
|
|
var exists = false;
|
|
|
|
foreach (var record in databaseData)
|
|
{
|
|
exists = false;
|
|
|
|
for (var j = 0; j < jsonList.Count; j++) {
|
|
|
|
//Console.WriteLine(jsonList[j].c_digo_smart.ToString().Replace(".0", "") + " - " + record.c_digo_smart.ToString() + " - " + (jsonList[j].c_digo_smart.ToString().Replace(".0", "") == record.c_digo_smart.ToString()));
|
|
|
|
if (jsonList[j].c_digo_smart.ToString().Replace(".0", "") == record.c_digo_smart.ToString())
|
|
{
|
|
exists = true;
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
Console.Clear();
|
|
|
|
if (exists == false) {
|
|
record.gestores = findGestores(record.gestores,jsonListGestores);
|
|
|
|
if (record.gestores != "0")
|
|
{
|
|
recordsMissingInJson.Add(record);
|
|
}
|
|
//else
|
|
//{
|
|
// int test = 10;
|
|
//}
|
|
}
|
|
}
|
|
|
|
return recordsMissingInJson;
|
|
}
|
|
static string findGestores(string sCodigo, List<Pipefy.ClasseGestores> jsonListGestores)
|
|
{
|
|
for (var i = 0; i < jsonListGestores.Count; i++)
|
|
{
|
|
if (sCodigo == jsonListGestores[i].gestores.ToString())
|
|
{
|
|
return jsonListGestores[i].id;
|
|
}
|
|
}
|
|
return "0";
|
|
}
|
|
} |