diff --git a/.gitignore b/.gitignore index 7a39ba9..e3a1d00 100644 --- a/.gitignore +++ b/.gitignore @@ -360,4 +360,6 @@ MigrationBackup/ .ionide/ # Fody - auto-generated XML schema -FodyWeavers.xsd \ No newline at end of file +FodyWeavers.xsd + +.history \ No newline at end of file diff --git a/App.xaml b/App.xaml index fc1f6f4..d667cee 100644 --- a/App.xaml +++ b/App.xaml @@ -4,6 +4,9 @@ xmlns:local="clr-namespace:BD_empresa" StartupUri="MainWindow.xaml"> - + + + + diff --git a/BD_empresa.csproj b/BD_empresa.csproj index defb4d1..c01d888 100644 --- a/BD_empresa.csproj +++ b/BD_empresa.csproj @@ -8,4 +8,11 @@ true + + + + + + + diff --git a/Converters.cs b/Converters.cs new file mode 100644 index 0000000..2eb0955 --- /dev/null +++ b/Converters.cs @@ -0,0 +1,33 @@ +using System; +using System.Globalization; +using System.Windows; +using System.Windows.Data; + +namespace BD_empresa +{ + public class BoolToVisibilityConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + if (value is bool b && b) + return Visibility.Visible; + return Visibility.Collapsed; + } + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + return (value is Visibility v && v == Visibility.Visible); + } + } + + public class StringToVisibilityConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + return string.IsNullOrWhiteSpace(value as string) ? Visibility.Collapsed : Visibility.Visible; + } + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/Data/AccessService.cs b/Data/AccessService.cs new file mode 100644 index 0000000..42af733 --- /dev/null +++ b/Data/AccessService.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.OleDb; +using System.Linq; +using System.Threading.Tasks; + +namespace BD_empresa.Data +{ + public class AccessService(string connectionString) : IClienteRepository + { + private readonly string _connectionString = connectionString; + + public async Task> SearchClientesAsync(string query) + { + return await Task.Run(() => + { + var clientes = new List(); + using (var connection = new OleDbConnection(_connectionString)) + { + connection.Open(); + string sql = @"SELECT Gestao, Cliente, Unidade, CNPJ_CPF, Codigo_Instalacao, Razao_Social FROM Dados_cadastrais WHERE CNPJ_CPF LIKE ? OR Codigo_Instalacao LIKE ? OR Razao_Social LIKE ? OR Cliente LIKE ?"; + using var cmd = new OleDbCommand(sql, connection); + string likeQuery = $"%{query}%"; + cmd.Parameters.AddWithValue("?", likeQuery); + cmd.Parameters.AddWithValue("?", likeQuery); + cmd.Parameters.AddWithValue("?", likeQuery); + cmd.Parameters.AddWithValue("?", likeQuery); + using var reader = cmd.ExecuteReader(); + while (reader.Read()) + { + clientes.Add(new ClienteSmart + { + Gestao = reader["Gestao"].ToString(), + Cliente = reader["Cliente"].ToString(), + Unidade = reader["Unidade"].ToString(), + CNPJ_CPF = reader["CNPJ_CPF"].ToString(), + Codigo_Instalacao = reader["Codigo_Instalacao"].ToString(), + Razao_Social = reader["Razao_Social"].ToString() + }); + } + } + // Unifica clientes por CNPJ, retorna no máximo 3 + return clientes + .GroupBy(c => c.CNPJ_CPF) + .Select(g => g.First()) + .Take(3) + .ToList(); + }); + } + + public async Task> GetAllClientesAsync() + { + return await Task.Run(() => + { + var clientes = new List(); + using (var connection = new OleDbConnection(_connectionString)) + { + connection.Open(); + string sql = "SELECT Gestao, Cliente, Unidade, CNPJ_CPF, Codigo_Instalacao, Razao_Social FROM Dados_cadastrais"; + using var cmd = new OleDbCommand(sql, connection); + using var reader = cmd.ExecuteReader(); + while (reader.Read()) + { + clientes.Add(new ClienteSmart + { + Gestao = reader["Gestao"].ToString(), + Cliente = reader["Cliente"].ToString(), + Unidade = reader["Unidade"].ToString(), + CNPJ_CPF = reader["CNPJ_CPF"].ToString(), + Codigo_Instalacao = reader["Codigo_Instalacao"].ToString(), + Razao_Social = reader["Razao_Social"].ToString() + }); + } + } + // Unifica clientes por CNPJ + return clientes + .GroupBy(c => c.CNPJ_CPF) + .Select(g => g.First()) + .Take(3) + .ToList(); + }); + } + } +} diff --git a/Data/ClienteSmart.cs b/Data/ClienteSmart.cs new file mode 100644 index 0000000..4245916 --- /dev/null +++ b/Data/ClienteSmart.cs @@ -0,0 +1,20 @@ +using System.ComponentModel; + +namespace BD_empresa.Data +{ + public class ClienteSmart : INotifyPropertyChanged + { + public string? Gestao { get; set; } + public string? Cliente { get; set; } + public string? Unidade { get; set; } + public string? CNPJ_CPF { get; set; } + public string? Codigo_Instalacao { get; set; } + public string? Razao_Social { get; set; } + + public event PropertyChangedEventHandler? PropertyChanged; + protected void OnPropertyChanged(string propertyName) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + } +} diff --git a/Data/IClienteRepository.cs b/Data/IClienteRepository.cs new file mode 100644 index 0000000..ef6ce08 --- /dev/null +++ b/Data/IClienteRepository.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace BD_empresa.Data +{ + public interface IClienteRepository + { + Task> SearchClientesAsync(string query); + Task> GetAllClientesAsync(); + } +} diff --git a/MainWindow.xaml b/MainWindow.xaml index ed01b74..d3f8fd7 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -8,5 +8,40 @@ Title="MainWindow" Height="450" Width="800"> + + + + + + + +