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 TitularesVidaRepository : GenericRepository, ITitularesVidaRepository, IGenericRepository { private readonly GenericUnitOfWork _unitOfWork; public TitularesVidaRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session) { this._unitOfWork = unitOfWork; } public void Delete(long id) { base.Delete(base.FindEntityById(id)); } public void DeleteRange(List ids) { for (int i = 0; i < ids.Count; i += 200) { IEnumerable nums = ids.Skip(i).Take(200); List list = ( from x in base.All() where nums.Contains(x.Id) select x).ToList(); base.DeleteRange(list); } } public List Find(long id) { 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) { if (sqlConnection != null) { using (SqlCommand sqlCommand = sqlConnection.CreateCommand()) { sqlCommand.CommandText = string.Format("SELECT * FROM TitularesVida WHERE IdItem = {0}", id); using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter()) { sqlDataAdapter.SelectCommand = sqlCommand; sqlDataAdapter.Fill(dataTable); } } } } return dataTable.AsEnumerable().ToList().Select((DataRow x) => { DateTime? nullable; DateTime? nullable1; DateTime? nullable2; DateTime? nullable3; TitularesVida titularesVida = new TitularesVida() { Id = x.Field("Id"), Codigo = (x.Field("Codigo") != null ? x.Field("Codigo").ToString() : null) }; if (x.Field("Inicio") != null) { nullable1 = new DateTime?(DateTime.Parse(x.Field("Inicio").ToString())); } else { nullable = null; nullable1 = nullable; } titularesVida.Inicio = nullable1; if (x.Field("Fim") != null) { nullable2 = new DateTime?(DateTime.Parse(x.Field("Fim").ToString())); } else { nullable = null; nullable2 = nullable; } titularesVida.Fim = nullable2; titularesVida.Fatura = (x.Field("Fatura") != null ? x.Field("Fatura").ToString() : null); titularesVida.Nome = (x.Field("Nome") != null ? x.Field("Nome").ToString() : null); titularesVida.Observacao = (x.Field("Observacao") != null ? x.Field("Observacao").ToString() : null); if (x.Field("Nascimento") != null) { nullable3 = new DateTime?(DateTime.Parse(x.Field("Nascimento").ToString())); } else { nullable = null; nullable3 = nullable; } titularesVida.Nascimento = nullable3; titularesVida.Cpf = (x.Field("Cpf") != null ? x.Field("Cpf").ToString() : null); titularesVida.Matricula = (x.Field("Matricula") != null ? x.Field("Matricula").ToString() : null); titularesVida.Premio = new decimal?((x.Field("Premio") != null ? x.Field("Premio") : decimal.Zero)); titularesVida.Capital = new decimal?((x.Field("Capital") != null ? x.Field("Capital") : decimal.Zero)); titularesVida.Tipo = (x.Field("Tipo") != null ? (TipoTitular?)Enum.Parse(typeof(TipoTitular), x.Field("Tipo").ToString()) : null); titularesVida.Dependente = (x.Field("Dependente_id") != null ? this.FindById(x.Field("Dependente_id")) : null); return titularesVida; }).ToList(); } public TitularesVida FindById(long id) { TitularesVidaDb titularesVidaDb = base.FindEntityById(id); return ApplicationMapper.Mapper.Map(titularesVidaDb); } public TitularesVida Merge(TitularesVida vida) { TitularesVidaDb titularesVidaDb = ApplicationMapper.Mapper.Map(vida); base.Merge(titularesVidaDb); return ApplicationMapper.Mapper.Map(titularesVidaDb); } public TitularesVida SaveOrUpdate(TitularesVida vida) { TitularesVidaDb titularesVidaDb = ApplicationMapper.Mapper.Map(vida); this.SaveOrUpdate(titularesVidaDb); return ApplicationMapper.Mapper.Map(titularesVidaDb); } } }