using AutoMapper; using Gestor.Infrastructure.Entities.Common; using Gestor.Infrastructure.Entities.Generic; using Gestor.Infrastructure.Mappers; using Gestor.Infrastructure.Repository.Generic; using Gestor.Infrastructure.Repository.Interface; using Gestor.Infrastructure.UnitOfWork.Generic; using Gestor.Model.Domain.Common; using Gestor.Model.Domain.Generic; using Gestor.Model.Domain.Seguros; using NHibernate; using NHibernate.Connection; using NHibernate.Impl; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Runtime.CompilerServices; namespace Gestor.Infrastructure.Repository.Logic { public class EmpresaRepository : GenericRepository, IEmpresaRepository, IGenericRepository { private readonly GenericUnitOfWork _unitOfWork; public EmpresaRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session) { this._unitOfWork = unitOfWork; } public string BuscarSenhaAdm(long idempresa = 1L) { EmpresaDb empresaDb = base.All().FirstOrDefault((EmpresaDb x) => x.Id == idempresa); if (empresaDb != null) { return empresaDb.SenhaAdmin; } return null; } public double ConsultaEspacoBancoInGb() { double num; object connection; using (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 = "EXEC sp_spaceused @oneresultset = '1'"; SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); if (!sqlDataReader.HasRows) { return 0; } else { sqlDataReader.Read(); double num1 = double.Parse(sqlDataReader["reserved"].ToString().Replace("KB", string.Empty).Trim()); double num2 = double.Parse(sqlDataReader["unallocated space"].ToString().Replace("MB", string.Empty).Replace(".", ",").Trim()); num = (num1 / 1024 + num2) / 1024; } } } } return num; } public void Delete(long id) { base.Delete(base.FindEntityById(id)); } public List Find(long idempresa = 0L) { return ( from x in (idempresa == 0 ? base.All() : from x in base.All() where x.Id == idempresa select x) select new Empresa() { Id = x.Id, Nome = x.Nome, Documento = x.Documento, Serial = x.Serial }).ToList(); } public List FindAsCliente(string filter) { EmpresaRepository.u003cu003ec__DisplayClass9_0 variable = null; IQueryable empresaDbs = base.All(); ParameterExpression parameterExpression = Expression.Parameter(typeof(EmpresaDb), "x"); IQueryable empresaDbs1 = empresaDbs.Where(Expression.Lambda>(Expression.OrElse(Expression.OrElse(Expression.Call(Expression.Call(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(EmpresaDb).GetMethod("get_Nome").MethodHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("ToUpper").MethodHandle), Array.Empty()), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Contains", new Type[] { typeof(string) }).MethodHandle), new Expression[] { Expression.Field(Expression.Constant(variable, typeof(EmpresaRepository.u003cu003ec__DisplayClass9_0)), FieldInfo.GetFieldFromHandle(typeof(EmpresaRepository.u003cu003ec__DisplayClass9_0).GetField("filter").FieldHandle)) }), Expression.Call(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(EmpresaDb).GetMethod("get_Documento").MethodHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Contains", new Type[] { typeof(string) }).MethodHandle), new Expression[] { Expression.Field(Expression.Constant(variable, typeof(EmpresaRepository.u003cu003ec__DisplayClass9_0)), FieldInfo.GetFieldFromHandle(typeof(EmpresaRepository.u003cu003ec__DisplayClass9_0).GetField("filter").FieldHandle)) })), Expression.Call(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(EmpresaDb).GetMethod("get_Serial").MethodHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Contains", new Type[] { typeof(string) }).MethodHandle), new Expression[] { Expression.Field(Expression.Constant(variable, typeof(EmpresaRepository.u003cu003ec__DisplayClass9_0)), FieldInfo.GetFieldFromHandle(typeof(EmpresaRepository.u003cu003ec__DisplayClass9_0).GetField("filter").FieldHandle)) })), new ParameterExpression[] { parameterExpression })); parameterExpression = Expression.Parameter(typeof(EmpresaDb), "x"); return empresaDbs1.Select(Expression.Lambda>(Expression.MemberInit(Expression.New(typeof(Cliente)), new MemberBinding[] { Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(DomainBase).GetMethod("set_Id", new Type[] { typeof(long) }).MethodHandle), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(EntityBase).GetMethod("get_Id").MethodHandle))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(Cliente).GetMethod("set_Nome", new Type[] { typeof(string) }).MethodHandle), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(EmpresaDb).GetMethod("get_Nome").MethodHandle))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(Cliente).GetMethod("set_Documento", new Type[] { typeof(string) }).MethodHandle), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(EmpresaDb).GetMethod("get_Documento").MethodHandle))) }), new ParameterExpression[] { parameterExpression })).ToList(); } public Empresa FindByDocumento(string documento) { EmpresaDb empresaDb = base.All().FirstOrDefault((EmpresaDb e) => e.Documento.Replace(",", string.Empty).Replace(".", string.Empty).Replace("-", string.Empty).Replace("/", string.Empty).Equals(documento)) ?? base.All().First(); return ApplicationMapper.Mapper.Map(empresaDb); } public Empresa FindById(long id) { EmpresaDb empresaDb = base.FindEntityById(id); return ApplicationMapper.Mapper.Map(empresaDb); } public Empresa FindBySerial(string serial) { EmpresaDb empresaDb = base.All().FirstOrDefault((EmpresaDb e) => e.Serial.Equals(serial)) ?? base.All().First(); return ApplicationMapper.Mapper.Map(empresaDb); } public Empresa Merge(Empresa empresa) { EmpresaDb empresaDb = ApplicationMapper.Mapper.Map(empresa); base.Merge(empresaDb); return ApplicationMapper.Mapper.Map(empresaDb); } public Empresa SaveOrUpdate(Empresa empresa) { EmpresaDb empresaDb = ApplicationMapper.Mapper.Map(empresa); this.SaveOrUpdate(empresaDb); return ApplicationMapper.Mapper.Map(empresaDb); } } }