using AutoMapper; using Gestor.Infrastructure.Entities.Ferramentas; using Gestor.Infrastructure.Helpers; using Gestor.Infrastructure.Mappers; using Gestor.Infrastructure.Repository.Generic; using Gestor.Infrastructure.Repository.Interface; using Gestor.Infrastructure.UnitOfWork.Generic; using Gestor.Model.Domain.Ferramentas; 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; namespace Gestor.Infrastructure.Repository.Logic { public class ImpostoRepository : GenericRepository, IImpostoRepository, IGenericRepository { private readonly GenericUnitOfWork _unitOfWork; public ImpostoRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session) { this._unitOfWork = unitOfWork; } public List DefaultSelect(bool? ativo) { string str; if (!ativo.HasValue) { str = ""; } else { str = (ativo.Value ? " AND Ativo = 1" : " AND Ativo = 0"); } return this.Select(str); } public Imposto FindById(long id) { ImpostoDb impostoDb = base.FindEntityById(id); return ApplicationMapper.Mapper.Map(impostoDb); } public List FindByRamo(long id) { return this.Select(string.Format(" AND idramo = {0}", id)); } public List FindBySeguradora(long id) { return this.Select(string.Format(" AND idciaseg = {0}", id)); } public Imposto Merge(Imposto imposto) { ImpostoDb impostoDb = ApplicationMapper.Mapper.Map(imposto); base.Merge(impostoDb); return ApplicationMapper.Mapper.Map(impostoDb); } public Imposto SaveOrUpdate(Imposto imposto) { ImpostoDb impostoDb = ApplicationMapper.Mapper.Map(imposto); this.SaveOrUpdate(impostoDb); return ApplicationMapper.Mapper.Map(impostoDb); } private List Select(string condition) { 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()) { Auxiliar.CriarAuxiliar(sqlCommand, false); sqlCommand.CommandText = string.Concat("SELECT * FROM Imposto WHERE 1=1 ", condition); using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter()) { sqlDataAdapter.SelectCommand = sqlCommand; sqlDataAdapter.Fill(dataTable); } } } return CustomMap.MapImposto(dataTable); } } }