Pipefy/Program.cs

96 lines
4.7 KiB
C#

using System.Data.OleDb;
using Pipefy;
using Pipefy.Models;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Text;
using System.Timers;
using Pipefy.Services;
using Microsoft.Extensions.DependencyInjection;
class Program
{
static DateTime endTime;
static void SetEndTime(DateTime date)
{
endTime = date;
}
static async Task Main(string[] args)
{
// Setup DI
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<IConfigurationService, ConfigurationService>();
serviceCollection.AddSingleton<IPipefyApiService>(provider => {
var config = provider.GetRequiredService<IConfigurationService>().LoadAppSettings();
return new PipefyApiService(config.PIPEFY_API_URL, config.PIPEFY_API_TOKEN);
});
serviceCollection.AddSingleton<IDatabaseService, DatabaseService>();
serviceCollection.AddSingleton<IDataMapper, DataMapper>();
serviceCollection.AddSingleton<IBusinessLogicService, BusinessLogicService>();
var serviceProvider = serviceCollection.BuildServiceProvider();
var configService = serviceProvider.GetRequiredService<IConfigurationService>();
var AppSettings = configService.LoadAppSettings();
if (AppSettings is null){ Environment.Exit(1); }
Console.Clear();
var pipefyApi = serviceProvider.GetRequiredService<IPipefyApiService>();
JArray allRecords = await pipefyApi.GetRecordsAsync(AppSettings.PIPEFY_TABLE_ID);
JArray allGestores = await pipefyApi.GetGestoresAsync(AppSettings.PIPEFY_TABLE_ID_GESTORES);
Console.Clear();
if (allRecords is not null)
{
string strGestores = JsonConvert.SerializeObject(allGestores);
var jsonGestores = JsonConvert.DeserializeObject<List<RootGestor>>(strGestores)!;
var mapper = serviceProvider.GetRequiredService<IDataMapper>();
List<ClasseGestores> jsonListGestores = mapper.ConvertGestoresJson(jsonGestores);
string strJson = JsonConvert.SerializeObject(allRecords);
var jsonData = JsonConvert.DeserializeObject<List<RootObject>>(strJson)!;
List<ClasseEmpresas> jsonList = mapper.ConvertEmpresasJson(jsonData);
var databaseService = serviceProvider.GetRequiredService<IDatabaseService>();
List<ClasseEmpresas> databaseData = databaseService.GetDataFromDatabase(AppSettings.DB_PATH);
var businessLogic = serviceProvider.GetRequiredService<IBusinessLogicService>();
List<ClasseEmpresas> recordsMissingInJson = businessLogic.CompareData(databaseData, jsonList, jsonListGestores);
if (recordsMissingInJson.Count != 0 && recordsMissingInJson != null)
{
await pipefyApi.CreateRecordsAsync(AppSettings.PIPEFY_TABLE_ID, recordsMissingInJson);
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.");
}
var timer = new System.Timers.Timer(100);
double remainingTime;
int interval = 60;
SetEndTime(System.DateTime.Now.AddSeconds(interval));
timer.Elapsed += OnTimerElapsed!;
timer.AutoReset = true;
timer.Enabled = true;
Console.WriteLine("");
Console.WriteLine("Pressione qualquer tecla para encerrar o programa...");
Console.WriteLine("");
Task.Factory.StartNew(
() => {
Console.ReadKey();
remainingTime = DateTime.Now.Subtract(endTime).TotalSeconds;
}
).Wait(
TimeSpan.FromSeconds(interval)
);
}
private static void OnTimerElapsed(object sender, ElapsedEventArgs e)
{
Console.Write($"\rEncerrando em {endTime.Subtract(e.SignalTime).Seconds.ToString()}");
}
}