summaryrefslogtreecommitdiff
path: root/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/EmpresaRepository.cs
blob: 4d51e87b7d80c4485efb9b5af1b4523f2e0bc8eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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<EmpresaDb>, IEmpresaRepository, IGenericRepository<EmpresaDb>
	{
		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>((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<Empresa> 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<Empresa>();
		}

		public List<Cliente> FindAsCliente(string filter)
		{
			EmpresaRepository.u003cu003ec__DisplayClass9_0 variable = null;
			IQueryable<EmpresaDb> empresaDbs = base.All();
			ParameterExpression parameterExpression = Expression.Parameter(typeof(EmpresaDb), "x");
			IQueryable<EmpresaDb> empresaDbs1 = empresaDbs.Where<EmpresaDb>(Expression.Lambda<Func<EmpresaDb, bool>>(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<Expression>()), (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<EmpresaDb, Cliente>(Expression.Lambda<Func<EmpresaDb, Cliente>>(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<Cliente>();
		}

		public Empresa FindByDocumento(string documento)
		{
			EmpresaDb empresaDb = base.All().FirstOrDefault<EmpresaDb>((EmpresaDb e) => e.Documento.Replace(",", string.Empty).Replace(".", string.Empty).Replace("-", string.Empty).Replace("/", string.Empty).Equals(documento)) ?? base.All().First<EmpresaDb>();
			return ApplicationMapper.Mapper.Map<EmpresaDb, Empresa>(empresaDb);
		}

		public Empresa FindById(long id)
		{
			EmpresaDb empresaDb = base.FindEntityById(id);
			return ApplicationMapper.Mapper.Map<EmpresaDb, Empresa>(empresaDb);
		}

		public Empresa FindBySerial(string serial)
		{
			EmpresaDb empresaDb = base.All().FirstOrDefault<EmpresaDb>((EmpresaDb e) => e.Serial.Equals(serial)) ?? base.All().First<EmpresaDb>();
			return ApplicationMapper.Mapper.Map<EmpresaDb, Empresa>(empresaDb);
		}

		public Empresa Merge(Empresa empresa)
		{
			EmpresaDb empresaDb = ApplicationMapper.Mapper.Map<Empresa, EmpresaDb>(empresa);
			base.Merge(empresaDb);
			return ApplicationMapper.Mapper.Map<EmpresaDb, Empresa>(empresaDb);
		}

		public Empresa SaveOrUpdate(Empresa empresa)
		{
			EmpresaDb empresaDb = ApplicationMapper.Mapper.Map<Empresa, EmpresaDb>(empresa);
			this.SaveOrUpdate(empresaDb);
			return ApplicationMapper.Mapper.Map<EmpresaDb, Empresa>(empresaDb);
		}
	}
}