summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/PlanosRepository.cs
blob: d76619de48684fca9ddbc42aeb294b3fc433adc2 (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
using AutoMapper;
using Gestor.Infrastructure.Entities.Financeiro;
using Gestor.Infrastructure.Entities.Generic;
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.Financeiro;
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.Reflection;
using System.Runtime.CompilerServices;

namespace Gestor.Infrastructure.Repository.Logic
{
	public class PlanosRepository : GenericRepository<PlanosDb>, IPlanosRepository, IGenericRepository<PlanosDb>
	{
		private readonly GenericUnitOfWork _unitOfWork;

		public PlanosRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session)
		{
			this._unitOfWork = unitOfWork;
		}

		public List<Planos> Find(string description)
		{
			PlanosRepository.u003cu003ec__DisplayClass3_0 variable = null;
			IQueryable<PlanosDb> planosDbs = base.All();
			ParameterExpression parameterExpression = Expression.Parameter(typeof(PlanosDb), "x");
			List<PlanosDb> list = planosDbs.Where<PlanosDb>(Expression.Lambda<Func<PlanosDb, bool>>(Expression.Call(Expression.Call(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(PlanosDb).GetMethod("get_Descricao").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(PlanosRepository.u003cu003ec__DisplayClass3_0)), FieldInfo.GetFieldFromHandle(typeof(PlanosRepository.u003cu003ec__DisplayClass3_0).GetField("description").FieldHandle)) }), new ParameterExpression[] { parameterExpression })).ToList<PlanosDb>();
			return ApplicationMapper.Mapper.Map<List<PlanosDb>, List<Planos>>(list);
		}

		public List<Planos> Find()
		{
			object connection;
			SessionFactoryImpl sessionFactory = this._unitOfWork.Session.SessionFactory as SessionFactoryImpl;
			DataTable dataTable = new DataTable();
			if (sessionFactory != null)
			{
				connection = sessionFactory.ConnectionProvider.GetConnection();
			}
			else
			{
				connection = null;
			}
			using (SqlConnection sqlConnection = connection as SqlConnection)
			{
				using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
				{
					AuxiliarFinanceiro.Criar(sqlCommand);
					sqlCommand.CommandText = "SELECT DISTINCT cs.idcplanos as id, cs.descricao as nome, cs.ativo, cs.idcplano, cs.idtipoconta, cs.ctipo, cp.descricao FROM cplanos cs INNER JOIN cplano cp ON cs.idcplano = cp.idcplano;";
					using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter())
					{
						sqlDataAdapter.SelectCommand = sqlCommand;
						sqlDataAdapter.Fill(dataTable);
					}
				}
			}
			return CustomMap.MapPlanos(dataTable);
		}

		public Planos FindById(long id)
		{
			PlanosDb planosDb = base.FindEntityById(id);
			return ApplicationMapper.Mapper.Map<PlanosDb, Planos>(planosDb);
		}

		public List<Planos> FindByPlanoId(long id)
		{
			List<PlanosDb> list = (
				from x in base.All()
				where x.Plano.Id == id
				select x).ToList<PlanosDb>();
			return ApplicationMapper.Mapper.Map<List<PlanosDb>, List<Planos>>(list);
		}

		public Planos Merge(Planos planos)
		{
			PlanosDb planosDb = ApplicationMapper.Mapper.Map<Planos, PlanosDb>(planos);
			base.Merge(planosDb);
			return ApplicationMapper.Mapper.Map<PlanosDb, Planos>(planosDb);
		}

		public Planos SaveOrUpdate(Planos planos)
		{
			PlanosDb planosDb = ApplicationMapper.Mapper.Map<Planos, PlanosDb>(planos);
			this.SaveOrUpdate(planosDb);
			return ApplicationMapper.Mapper.Map<PlanosDb, Planos>(planosDb);
		}
	}
}