using AutoMapper; using Gestor.Infrastructure.Entities.Financeiro; using Gestor.Infrastructure.Mappers; using Gestor.Infrastructure.Repository.Generic; using Gestor.Infrastructure.Repository.Interface; using Gestor.Infrastructure.UnitOfWork.Generic; using Gestor.Model.Domain.Financeiro; using NHibernate; using NHibernate.Connection; using NHibernate.Impl; using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Data.SqlClient; using System.Linq; namespace Gestor.Infrastructure.Repository.Logic { public class FornecedorRepository : GenericRepository, IFornecedorRepository, IGenericRepository { private readonly GenericUnitOfWork _unitOfWork; public FornecedorRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session) { this._unitOfWork = unitOfWork; } public void Delete(long id) { FornecedorDb fornecedorDb = base.FindEntityById(id); if (fornecedorDb == null) { return; } base.Delete(fornecedorDb); } public List Find(string filter, bool ativo = false) { List fornecedors; object connection; DataTable dataTable = new DataTable(); SessionFactoryImpl sessionFactory = this._unitOfWork.Session.SessionFactory as SessionFactoryImpl; if (sessionFactory != null) { connection = sessionFactory.ConnectionProvider.GetConnection(); } else { connection = null; } using (SqlConnection sqlConnection = connection as SqlConnection) { using (SqlCommand sqlCommand = sqlConnection.CreateCommand()) { string str = string.Concat(new string[] { "(UPPER(nome) COLLATE Latin1_General_CI_AI LIKE '%", filter, "%' OR UPPER(cpfcnpj) LIKE '%", filter, "%' OR UPPER(fone1) LIKE '%", filter, "%' OR UPPER(fone2) LIKE '%", filter, "%' OR UPPER(email) LIKE '%", filter, "%')" }); sqlCommand.CommandText = string.Concat("SELECT top 200 * FROM fornecedor WHERE ", str, " ", (ativo ? "AND Ativo = 1" : string.Empty)); using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter()) { sqlDataAdapter.SelectCommand = sqlCommand; sqlDataAdapter.Fill(dataTable); } if (dataTable.Rows.Count != 0) { return CustomMap.MapFornecedor(dataTable); } else { fornecedors = new List(); } } } return fornecedors; } public List Find() { List list = this._unitOfWork.Session.CreateQuery("from FornecedorDb").List().ToList(); return ApplicationMapper.Mapper.Map, List>(list); } public Fornecedor FindById(long id) { FornecedorDb fornecedorDb = base.FindEntityById(id); return ApplicationMapper.Mapper.Map(fornecedorDb); } public Fornecedor Merge(Fornecedor fornecedor) { FornecedorDb fornecedorDb = ApplicationMapper.Mapper.Map(fornecedor); base.Merge(fornecedorDb); return ApplicationMapper.Mapper.Map(fornecedorDb); } public Fornecedor SaveOrUpdate(Fornecedor fornecedor) { FornecedorDb fornecedorDb = ApplicationMapper.Mapper.Map(fornecedor); this.SaveOrUpdate(fornecedorDb); return ApplicationMapper.Mapper.Map(fornecedorDb); } } }