using AutoMapper; using Gestor.Infrastructure.Entities.Generic; using Gestor.Infrastructure.Entities.Seguros; using Gestor.Infrastructure.Mappers; using Gestor.Infrastructure.Repository.Generic; using Gestor.Infrastructure.Repository.Interface; using Gestor.Infrastructure.UnitOfWork.Generic; using Gestor.Model.Common; using Gestor.Model.Domain.Seguros; 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; using System.Linq.Expressions; using System.Runtime.CompilerServices; namespace Gestor.Infrastructure.Repository.Logic { public class PerfilRepository : GenericRepository, IPerfilRepository, IGenericRepository { private readonly GenericUnitOfWork _unitOfWork; public PerfilRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session) { this._unitOfWork = unitOfWork; } public void Delete(long id) { PerfilDb perfilDb = base.FindEntityById(id); if (perfilDb == null) { return; } base.Delete(perfilDb); } public Perfil Find(long id) { PerfilDb perfilDb = base.All().FirstOrDefault((PerfilDb x) => x.Id == id); return ApplicationMapper.Mapper.Map(perfilDb); } public List FindByControleId(long id) { List list = ( from x in base.All() where x.Controle.Id == id select x).ToList(); return ApplicationMapper.Mapper.Map, List>(list); } public List FindByControleIds(string ids) { 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()) { sqlCommand.CommandText = string.Concat("SELECT * FROM perfil WHERE idcontrole IN (", ids, ");"); using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter()) { sqlDataAdapter.SelectCommand = sqlCommand; sqlDataAdapter.Fill(dataTable); } } } if (dataTable.Rows.Count == 0) { return new List(); } return dataTable.AsEnumerable().Select((DataRow p) => { GaragemTrabalhoEstudo? nullable; bool? nullable1; GaragemTrabalhoEstudo? nullable2; GaragemTrabalhoEstudo? nullable3; bool? nullable4; bool? nullable5; bool? nullable6; Perfil perfil = new Perfil() { Cliente = new Cliente() { Id = long.Parse(p["idcliente"].ToString()) }, Controle = new Controle() { Id = long.Parse(p["idcontrole"].ToString()) }, Nome = p["nomecompleto"].ToString(), Cpf = p["cpf"].ToString(), Nascimento = (string.IsNullOrEmpty(p["datanascimento"].ToString()) ? null : new DateTime?(DateTime.Parse(p["datanascimento"].ToString()))), EstadoCivil = (string.IsNullOrEmpty(p["estadocivil"].ToString()) ? null : new EstadoCivil?((EstadoCivil)int.Parse(p["estadocivil"].ToString()))), Sexo = (string.IsNullOrEmpty(p["sexo"].ToString()) ? null : new Sexo?((Sexo)int.Parse(p["sexo"].ToString()))), Relacao = (string.IsNullOrEmpty(p["relacao"].ToString()) ? null : new Relacao?((Relacao)int.Parse(p["relacao"].ToString()))), Habilitacao = p["numerohabilitacao"].ToString(), TempoHabilitacao = (string.IsNullOrEmpty(p["tempohabilitacao"].ToString()) ? null : new TempoHabilitacao?((TempoHabilitacao)int.Parse(p["tempohabilitacao"].ToString()))), VeiculoResidencia = (string.IsNullOrEmpty(p["veiculosresidencia"].ToString()) ? null : new int?(int.Parse(p["veiculosresidencia"].ToString()))), GaragemResidencia = (string.IsNullOrEmpty(p["garagemresidencia"].ToString()) ? null : new GaragemResidencia?((GaragemResidencia)int.Parse(p["garagemresidencia"].ToString()))) }; if (string.IsNullOrEmpty(p["garagemtrabalho"].ToString())) { nullable = null; nullable2 = nullable; } else { nullable2 = new GaragemTrabalhoEstudo?((GaragemTrabalhoEstudo)int.Parse(p["garagemtrabalho"].ToString())); } perfil.GaragemTrabalho = nullable2; if (string.IsNullOrEmpty(p["garagemestudo"].ToString())) { nullable = null; nullable3 = nullable; } else { nullable3 = new GaragemTrabalhoEstudo?((GaragemTrabalhoEstudo)int.Parse(p["garagemestudo"].ToString())); } perfil.GaragemEstudo = nullable3; perfil.TipoResidencia = (string.IsNullOrEmpty(p["tiporesidencia"].ToString()) ? null : new TipoResidencia?((TipoResidencia)int.Parse(p["tiporesidencia"].ToString()))); perfil.KmMensal = p["quilometragemmensal"].ToString(); perfil.DistanciaResidenciaTrabalho = (string.IsNullOrEmpty(p["distanciaresidenciatrabalho"].ToString()) ? null : new DistanciaTrabalho?((DistanciaTrabalho)int.Parse(p["distanciaresidenciatrabalho"].ToString()))); if (string.IsNullOrEmpty(p["usoprofissional"].ToString())) { nullable1 = null; nullable4 = nullable1; } else { nullable4 = new bool?(p["usoprofissional"].ToString() == "1"); } perfil.UsoProfissional = nullable4; perfil.UsoDependentes = (string.IsNullOrEmpty(p["usodependentes"].ToString()) ? null : new UsoDependetes?((UsoDependetes)int.Parse(p["usodependentes"].ToString()))); perfil.Ocupacao = (string.IsNullOrEmpty(p["ocupacao"].ToString()) ? null : new Ocupacao?((Ocupacao)int.Parse(p["ocupacao"].ToString()))); if (string.IsNullOrEmpty(p["segurovida"].ToString())) { nullable1 = null; nullable5 = nullable1; } else { nullable5 = new bool?(p["segurovida"].ToString() == "1"); } perfil.SeguroVida = nullable5; perfil.EstenderCobertura = (bool?)p["EstenderCobertura"]; if (string.IsNullOrEmpty(p["isencao"].ToString())) { nullable1 = null; nullable6 = nullable1; } else { nullable6 = new bool?(p["isencao"].ToString() == "1"); } perfil.Isencao = nullable6; perfil.AntiFurto = (string.IsNullOrEmpty(p["antifurto"].ToString()) ? null : new Antifurto?((Antifurto)int.Parse(p["antifurto"].ToString()))); return perfil; }).ToList(); } public Perfil Merge(Perfil perfil) { PerfilDb perfilDb = ApplicationMapper.Mapper.Map(perfil); base.Merge(perfilDb); return ApplicationMapper.Mapper.Map(perfilDb); } public Perfil SaveOrUpdate(Perfil perfil) { PerfilDb perfilDb = ApplicationMapper.Mapper.Map(perfil); this.SaveOrUpdate(perfilDb); return ApplicationMapper.Mapper.Map(perfilDb); } } }