diff --git a/Program.cs b/Program.cs index 55e0c57..1178477 100644 --- a/Program.cs +++ b/Program.cs @@ -59,7 +59,7 @@ class Program if (!_isAuthenticated) { AnsiConsole.MarkupLine("[bold red]Nenhum usuário encontrado ou erro na autenticação. Saindo...[/]"); - Console.ReadLine(); + Thread.Sleep(3000); return; } @@ -86,7 +86,7 @@ class Program public DateTime MovedAt { get; set; } public required string Title { get; set; } public string? AcaoID { get; set; } - public string? Acao { get; set; } + public required string Acao { get; set; } } static void AtualizarDados() { @@ -108,11 +108,23 @@ class Program tableContLigacoes.AddColumn(new TableColumn("[green]UserID[/]").Centered()); tableContLigacoes.AddColumn(new TableColumn("[green]Registros Hoje[/]").Centered()); + // Montamos uma tabela com: Nome do usuário | UserID | Registros Hoje + var tableContReunioes = new Table().Border(TableBorder.Rounded).Alignment(Justify.Center); + tableContReunioes.Border(TableBorder.Rounded); + tableContReunioes.AddColumn(new TableColumn("[green]Nome[/]").Centered()); + tableContReunioes.AddColumn(new TableColumn("[green]UserID[/]").Centered()); + tableContReunioes.AddColumn(new TableColumn("[green]Agendadas[/]").Centered()); + tableContReunioes.AddColumn(new TableColumn("[green]Realizadas[/]").Centered()); + tableContReunioes.AddColumn(new TableColumn("[green]Canceladas[/]").Centered()); + // Para cada usuário, rodar uma query COUNT(*) e adicionar linha na tabela foreach (var (userId, nome) in _pipeUsers) { var userRecords = new List(); int count = 0; + int countR = 0; + int countA = 0; + int countC = 0; AnsiConsole.Status() .Spinner(Spinner.Known.Line) .Start($"[blue]Obtendo registros de [yellow]{nome}[/]...[/]", ctx => @@ -121,46 +133,51 @@ class Program SELECT * FROM public.""ActionsHistory"" WHERE ""UserID"" = @pipeUser AND ""MovedAt"" >= date_trunc('month', CURRENT_DATE)"; - //try - //{ - using var conn = new NpgsqlConnection(_connectionString); - conn.Open(); - using var cmd = new NpgsqlCommand(query, conn); - cmd.Parameters.AddWithValue("@pipeUser", userId); - var reader = cmd.ExecuteReader(); - using (reader) + try { - while (reader.Read()) + using var conn = new NpgsqlConnection(_connectionString); + conn.Open(); + using var cmd = new NpgsqlCommand(query, conn); + cmd.Parameters.AddWithValue("@pipeUser", userId); + var reader = cmd.ExecuteReader(); + using (reader) { - var temp = new Record + while (reader.Read()) { - Id = ToIntOrNull(reader, "Id"), - Action = ToStringOrNull(reader, "Action"), - User = _pipeUsers.Find(u => u.UserId == ToIntOrNull(reader, "UserID")).Nome, - FieldID = ToStringOrNull(reader, "FieldID"), - From = ToIntOrNull(reader, "From"), - To = ToIntOrNull(reader, "To"), - CardID = ToIntOrNull(reader, "CardID"), - PipeSUID = ToStringOrNull(reader, "PipeSUID"), - MovedAt = (DateTime)(reader["MovedAt"] ?? DateTime.MinValue), - Title = ToStringOrNull(reader, "Title") ?? "-", - }; - temp.AcaoID = temp.From + "-" + temp.To; - temp.Acao = _actionIds.Find(p => p.actionId == temp.AcaoID).actionName; - - userRecords.Add(temp); + var temp = new Record + { + Id = ToIntOrNull(reader, "Id"), + Action = ToStringOrNull(reader, "Action"), + User = _pipeUsers.Find(u => u.UserId == ToIntOrNull(reader, "UserID")).Nome, + FieldID = ToStringOrNull(reader, "FieldID"), + From = ToIntOrNull(reader, "From"), + To = ToIntOrNull(reader, "To"), + CardID = ToIntOrNull(reader, "CardID"), + PipeSUID = ToStringOrNull(reader, "PipeSUID"), + MovedAt = (DateTime)(reader["MovedAt"] ?? DateTime.MinValue), + Title = ToStringOrNull(reader, "Title") ?? "-", + Acao = "Desconhecida" + }; + temp.AcaoID = temp.From + "-" + temp.To; + temp.Acao = _actionIds.Find(p => p.actionId == temp.AcaoID).actionName; + + userRecords.Add(temp); + } } } - //} - //catch (Exception ex) - //{ - // // Em caso de erro, exibimos zero e continuamos - // count = 0; - // AnsiConsole.MarkupLine($"\n[red]Erro ao consultar {nome}:[/] {ex.Message}"); - //} + catch (Exception ex) + { + // Em caso de erro, exibimos zero e continuamos + count = 0; + AnsiConsole.MarkupLine($"\n[red]Erro ao consultar {nome}:[/] {ex.Message}"); + } }); count = userRecords.Where(x => (x.MovedAt > DateTime.Today && x.FieldID is not null)).ToList().Count; + countR = userRecords.Where(x => ((x.Acao ?? "").StartsWith("Realizou"))).ToList().Count; + countA = userRecords.Where(x => ((x.Acao ?? "").StartsWith("Agendou"))).ToList().Count; + countC = userRecords.Where(x => ((x.Acao ?? "").EndsWith("cancelada"))).ToList().Count; tableContLigacoes.AddRow(nome, userId.ToString(), $"[bold yellow]{count}[/]"); + tableContReunioes.AddRow(nome, userId.ToString(), $"[bold yellow]{countA}[/]", $"[bold yellow]{countR}[/]", $"[bold yellow]{countC}[/]"); records.AddRange(userRecords); } @@ -170,6 +187,11 @@ class Program .Expand(); AnsiConsole.Write(new Align(panel, HorizontalAlignment.Center, VerticalAlignment.Top)); + panel = new Panel(new Align(tableContReunioes, HorizontalAlignment.Center, VerticalAlignment.Top)) + .Header("[blue bold] Reuniões Agendadas e Realizadas [/]", Justify.Center) + .Border(BoxBorder.Double) + .Expand(); + AnsiConsole.Write(new Align(panel, HorizontalAlignment.Center, VerticalAlignment.Top)); // Montamos uma tabela com: Nome do usuário | UserID | Registros Hoje var tableReunioes = new Table().Border(TableBorder.Rounded).Alignment(Justify.Center); tableReunioes.AddColumn(new TableColumn("[green]Data[/]").Centered()); @@ -178,7 +200,7 @@ class Program tableReunioes.AddColumn(new TableColumn("[green]Card ID\n(Ctrl + click)[/]").Centered()); tableReunioes.AddColumn(new TableColumn("[green]Prospectante[/]").Centered()); - records = [.. records.OrderBy(x => x.MovedAt).OrderBy(y => y.User)]; + records = [.. records.OrderBy(x => x.MovedAt.Date).OrderBy(x => x.Acao).OrderBy(y => y.User)]; // Para cada usuário, rodar uma query COUNT(*) e adicionar linha na tabela foreach (var record in records)