summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/PerfilRepository.cs
blob: 67375a500ba347f58486e62071deae7ecba8b357 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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<PerfilDb>, IPerfilRepository, IGenericRepository<PerfilDb>
	{
		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>((PerfilDb x) => x.Id == id);
			return ApplicationMapper.Mapper.Map<PerfilDb, Perfil>(perfilDb);
		}

		public List<Perfil> FindByControleId(long id)
		{
			List<PerfilDb> list = (
				from x in base.All()
				where x.Controle.Id == id
				select x).ToList<PerfilDb>();
			return ApplicationMapper.Mapper.Map<List<PerfilDb>, List<Perfil>>(list);
		}

		public List<Perfil> 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<Perfil>();
			}
			return dataTable.AsEnumerable().Select<DataRow, Perfil>((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<Perfil>();
		}

		public Perfil Merge(Perfil perfil)
		{
			PerfilDb perfilDb = ApplicationMapper.Mapper.Map<Perfil, PerfilDb>(perfil);
			base.Merge(perfilDb);
			return ApplicationMapper.Mapper.Map<PerfilDb, Perfil>(perfilDb);
		}

		public Perfil SaveOrUpdate(Perfil perfil)
		{
			PerfilDb perfilDb = ApplicationMapper.Mapper.Map<Perfil, PerfilDb>(perfil);
			this.SaveOrUpdate(perfilDb);
			return ApplicationMapper.Mapper.Map<PerfilDb, Perfil>(perfilDb);
		}
	}
}