using DAL; using DALL; using MLL; using System; using System.Collections.Generic; using System.Data; using System.Linq; namespace BLL { public class BLLClientes { private readonly DALClientes dal; public BLLClientes(string conexao) { dal = new DALClientes(conexao); } #region CRUD public bool Inserir(ModeloCliente c) { string erro = Validar(c); if (!string.IsNullOrWhiteSpace(erro)) throw new Exception(erro); ModeloCliente? existente = CarregarPorDocumento(c.EmpresaId, c.Documento); if (existente != null) throw new Exception("Já existe um cliente com esse documento."); return dal.Inserir(c); } public bool Alterar(ModeloCliente c) { if (c.Id <= 0) throw new Exception("Cliente inválido."); string erro = Validar(c); if (!string.IsNullOrWhiteSpace(erro)) throw new Exception(erro); ModeloCliente? existente = CarregarPorDocumento(c.EmpresaId, c.Documento); if (existente != null && existente.Id != c.Id) throw new Exception("Já existe outro cliente com esse documento."); return dal.Alterar(c); } public bool Excluir(int id) { if (id <= 0) throw new Exception("ID inválido."); return dal.Excluir(id); } public ModeloCliente? Carregar(int id) { if (id <= 0) return null; return dal.Carregar(id); } public List Listar() { return dal.Listar(); } #endregion #region BUSCAS public ModeloCliente? CarregarPorDocumento( int empresaId, string documento) { if (string.IsNullOrWhiteSpace(documento)) return null; documento = LimparNumeros(documento); return dal.Listar() .FirstOrDefault(x => x.EmpresaId == empresaId && LimparNumeros(x.Documento) == documento); } public List BuscarPorNome(string nome) { if (string.IsNullOrWhiteSpace(nome)) return []; return dal.Listar() .Where(x => x.Nome.Contains( nome, StringComparison.OrdinalIgnoreCase)) .ToList(); } public List BuscarPorCidade(string cidade) { if (string.IsNullOrWhiteSpace(cidade)) return []; return dal.Listar() .Where(x => !string.IsNullOrWhiteSpace(x.Cidade) && x.Cidade.Contains( cidade, StringComparison.OrdinalIgnoreCase)) .ToList(); } public List BuscarPorTelefone(string telefone) { if (string.IsNullOrWhiteSpace(telefone)) return []; telefone = LimparNumeros(telefone); return dal.Listar() .Where(x => LimparNumeros(x.Telefone1).Contains(telefone) || LimparNumeros(x.Telefone2).Contains(telefone) || LimparNumeros(x.Celular).Contains(telefone) || LimparNumeros(x.Whatsapp).Contains(telefone)) .ToList(); } #endregion #region VIEWS public List ListarTodos() { return dal.ListarTodos(); } public List ListarAtivos() { return dal.ListarAtivos(); } public List ListarInativos() { return dal.ListarInativos(); } public List ListarBloqueados() { return dal.ListarBloqueados(); } public List ListarComLimite() { return dal.ListarComLimite(); } public List ListarSemLimite() { return dal.ListarSemLimite(); } public List ListarPF() { return dal.ListarPF(); } public List ListarPJ() { return dal.ListarPJ(); } public List ListarWhatsapp() { return dal.ListarWhatsapp(); } public List ListarEmail() { return dal.ListarEmail(); } public List ListarEmailNFe() { return dal.ListarEmailNFe(); } public List ListarCripto() { return dal.ListarCripto(); } public List ListarUltimaCompra() { return dal.ListarUltimaCompra(); } public List ListarSemCompra() { return dal.ListarSemCompra(); } public List ListarRecentes() { return dal.ListarRecentes(); } public List ListarCobranca() { return dal.ListarCobranca(); } #endregion #region DASHBOARD public DataTable ObterClientesPorCidade() { return dal.ObterClientesPorCidade(); } public DataTable ObterClientesPorGrupo() { return dal.ObterClientesPorGrupo(); } public DataTable ObterDashboard() { return dal.ObterDashboard(); } public DataTable ObterClientesCompleto() { return dal.ObterClientesCompleto(); } #endregion #region VALIDAÇÃO private string? Validar(ModeloCliente c) { if (c == null) return "Objeto cliente inválido."; if (c.EmpresaId <= 0) return "Empresa inválida."; if (string.IsNullOrWhiteSpace(c.Nome)) return "Nome é obrigatório."; if (string.IsNullOrWhiteSpace(c.TipoPessoa)) return "Tipo de pessoa é obrigatório."; if (string.IsNullOrWhiteSpace(c.Documento)) return "Documento é obrigatório."; c.Documento = LimparNumeros(c.Documento); if (c.TipoPessoa == "PF" && c.Documento.Length != 11) { return "CPF inválido."; } if (c.TipoPessoa == "PJ" && c.Documento.Length != 14) { return "CNPJ inválido."; } if (!string.IsNullOrWhiteSpace(c.Email)) { if (!c.Email.Contains("@")) return "Email inválido."; } if (!string.IsNullOrWhiteSpace(c.UF)) { if (c.UF.Length != 2) return "UF inválida."; } return null; } #endregion #region AUXILIARES private string LimparNumeros(string? texto) { if (string.IsNullOrWhiteSpace(texto)) return string.Empty; return new string( texto.Where(char.IsDigit).ToArray()); } #endregion } }