summaryrefslogtreecommitdiff
path: root/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/RepasseRepository.cs
blob: 861ac8509d04aa25a68cb089fcd0d7683df85c52 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using AutoMapper;
using Gestor.Infrastructure.Entities.Ferramentas;
using Gestor.Infrastructure.Entities.Seguros;
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.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;
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 RepasseRepository : GenericRepository<RepasseDb>, IRepasseRepository, IGenericRepository<RepasseDb>
	{
		private readonly GenericUnitOfWork _unitOfWork;

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

		public List<Repasse> DefaultSelect(long idempresa = 0L)
		{
			List<Repasse> repasses;
			object connection;
			List<long> nums = null;
			Func<DataRow, VinculoRepasse> func1 = null;
			SessionFactoryImpl sessionFactory = this._unitOfWork.Session.SessionFactory as SessionFactoryImpl;
			DataTable dataTable = new DataTable();
			DataTable dataTable1 = new DataTable();
			if (sessionFactory != null)
			{
				connection = sessionFactory.ConnectionProvider.GetConnection();
			}
			else
			{
				connection = null;
			}
			using (SqlConnection sqlConnection = connection as SqlConnection)
			{
				using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
				{
					Auxiliar.CriarVendedor(sqlCommand, true);
					Auxiliar.LoadTipoVendedor(sqlCommand);
					List<Condicao> condicaos = new List<Condicao>()
					{
						new Condicao()
						{
							Campo = "idempresa",
							Valores = idempresa.CriarValor<long>()
						}
					};
					string str = "SELECT * FROM repasse WHERE ";
					string str1 = "SELECT * FROM VinculoRepasse WHERE ";
					List<Condicao> condicaos1 = new List<Condicao>()
					{
						new Condicao()
						{
							Campo = "1",
							Valores = "1".CriarValor<string>()
						}
					};
					if (idempresa > (long)0)
					{
						condicaos1.AddRange(condicaos);
					}
					dataTable = sqlCommand.Select(condicaos1.CreateParameters(0), str, "");
					if (dataTable.Rows.Count != 0)
					{
						for (List<long> i = dataTable.AsEnumerable().Select<DataRow, long>((DataRow x) => x.Field<long>("idrepasse")).Distinct<long>().ToList<long>(); i.Count > 0; i = nums)
						{
							List<long> nums1 = i.Take<long>(2000).ToList<long>();
							List<Condicao> condicaos2 = new List<Condicao>()
							{
								new Condicao()
								{
									Campo = "IdRepasse",
									Valores = nums1.CriarValor<long>()
								}
							};
							DataTable dataTable2 = sqlCommand.Select(condicaos2.CreateParameters(0), str1, "");
							dataTable1.Merge(dataTable2);
							nums = (nums1.Count < 2000 ? new List<long>() : i.Except<long>(nums1).ToList<long>());
						}
						List<Repasse> repasses1 = dataTable.AsEnumerable().Select<DataRow, Repasse>((DataRow x) => {
							long? nullable1;
							long? nullable2;
							Repasse repasse = new Repasse()
							{
								Id = x.Field<long>("idrepasse"),
								Ativo = (x.Field<object>("ativo") == null ? true : x.Field<object>("ativo").ToString() != "0"),
								Base = (x.Field<object>("base") == null ? null : new BaseRepasse?((BaseRepasse)Enum.Parse(typeof(BaseRepasse), x.Field<object>("base").ToString()))),
								Forma = (x.Field<object>("forma") == null ? null : new FormaRepasse?((FormaRepasse)Enum.Parse(typeof(FormaRepasse), x.Field<object>("forma").ToString()))),
								Incidencia = (x.Field<object>("incidencia") == null ? null : new TipoIncidencia?((TipoIncidencia)Enum.Parse(typeof(TipoIncidencia), x.Field<object>("incidencia").ToString()))),
								Tipo = (x.Field<object>("tipo") == null ? null : new TipoRepasse?((TipoRepasse)Enum.Parse(typeof(TipoRepasse), x.Field<object>("tipo").ToString())))
							};
							decimal? nullable3 = x.Field<decimal?>("vlrnovo");
							repasse.ValorNovo = nullable3.GetValueOrDefault();
							nullable3 = x.Field<decimal?>("vlrrenovacao");
							repasse.ValorRenovacao = nullable3.GetValueOrDefault();
							repasse.Vendedor = Auxiliar.Vendedores.FirstOrDefault<Vendedor>((Vendedor z) => z.Id == x.Field<long>("idvendedor"));
							repasse.Ramo = (x.Field<object>("idramo") == null ? null : Auxiliar.Ramos.FirstOrDefault<Ramo>((Ramo z) => {
								long id = z.Id;
								long? nullable = x.Field<long?>("idramo");
								return id == nullable.GetValueOrDefault() & nullable.HasValue;
							}));
							if (x.Field<object>("idciaseg") == null)
							{
								nullable1 = null;
								nullable2 = nullable1;
							}
							else
							{
								Seguradora seguradora = Auxiliar.Seguradoras.FirstOrDefault<Seguradora>((Seguradora z) => {
									long id = z.Id;
									long? nullable = x.Field<long?>("idciaseg");
									return id == nullable.GetValueOrDefault() & nullable.HasValue;
								});
								if (seguradora != null)
								{
									nullable2 = new long?(seguradora.Id);
								}
								else
								{
									nullable1 = null;
									nullable2 = nullable1;
								}
							}
							repasse.Seguradora = nullable2;
							return repasse;
						}).ToList<Repasse>();
						List<Repasse> repasses2 = (
							from x in repasses1
							where x.Ativo
							select x).ToList<Repasse>();
						repasses2.ForEach((Repasse x) => {
							Repasse list = x;
							EnumerableRowCollection<DataRow> dataRows = dataTable1.AsEnumerable().Where<DataRow>((DataRow r) => r.Field<long>("IdRepasse") == x.Id);
							Func<DataRow, VinculoRepasse> u003cu003e9_8 = func1;
							if (u003cu003e9_8 == null)
							{
								Func<DataRow, VinculoRepasse> vinculoRepasse = (DataRow v) => new VinculoRepasse()
								{
									Id = v.Field<long>("Id"),
									IdRepasse = v.Field<long>("IdRepasse"),
									RepasseVinculo = repasses1.Find((Repasse z) => z.Id == v.Field<long>("RepasseVinculo_id")),
									TipoVendedor = Auxiliar.TipoVendedores.Find((TipoVendedor z) => z.Id == v.Field<long>("TipoVendedor_id")),
									TipoVendedorVinculo = Auxiliar.TipoVendedores.Find((TipoVendedor z) => z.Id == v.Field<long>("TipoVendedorVinculo_id"))
								};
								Func<DataRow, VinculoRepasse> func = vinculoRepasse;
								func1 = vinculoRepasse;
								u003cu003e9_8 = func;
							}
							list.Vinculo = dataRows.Select<DataRow, VinculoRepasse>(u003cu003e9_8).ToList<VinculoRepasse>();
						});
						repasses = repasses2;
					}
					else
					{
						repasses = new List<Repasse>();
					}
				}
			}
			return repasses;
		}

		public void Delete(long id)
		{
			RepasseDb repasseDb = base.FindEntityById(id);
			if (repasseDb == null)
			{
				return;
			}
			base.Delete(repasseDb);
		}

		public void DeleteVinculo(long id)
		{
			VinculoRepasseDb vinculoRepasseDb = this._unitOfWork.Repository<VinculoRepasseDb>().FindEntityById(id);
			if (vinculoRepasseDb == null)
			{
				return;
			}
			this._unitOfWork.Repository<VinculoRepasseDb>().Delete(vinculoRepasseDb);
		}

		public Repasse FindById(long id)
		{
			List<VinculoRepasse> vinculoRepasses;
			RepasseDb repasseDb = base.FindEntityById(id);
			List<VinculoRepasse> vinculoRepasses1 = this.FindByRepasse(id);
			Repasse repasse = ApplicationMapper.Mapper.Map<RepasseDb, Repasse>(repasseDb);
			if (vinculoRepasses1 == null || vinculoRepasses1.Count <= 0)
			{
				vinculoRepasses = null;
			}
			else
			{
				vinculoRepasses = vinculoRepasses1;
			}
			repasse.Vinculo = vinculoRepasses;
			return repasse;
		}

		public List<VendedorParcela> FindByIdRepasse(long id)
		{
			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())
				{
					Auxiliar.CriarVendedor(sqlCommand, false);
					sqlCommand.CommandText = string.Format("SELECT idvendedorparcela FROM vendedorparcela WHERE idrepasse = {0}", id);
					using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter())
					{
						sqlDataAdapter.SelectCommand = sqlCommand;
						sqlDataAdapter.Fill(dataTable);
					}
				}
			}
			return dataTable.AsEnumerable().Select<DataRow, VendedorParcela>((DataRow x) => new VendedorParcela()
			{
				Id = x.Field<long>("idvendedorparcela")
			}).ToList<VendedorParcela>();
		}

		public List<Repasse> FindByIdVendedor(long id)
		{
			List<RepasseDb> list = this._unitOfWork.Session.CreateQuery(string.Format("from RepasseDb \r\n                      where IdVendedor = {0}", id)).List<RepasseDb>().ToList<RepasseDb>();
			List<Repasse> repasses = ApplicationMapper.Mapper.Map<List<RepasseDb>, List<Repasse>>(list);
			repasses.ForEach((Repasse x) => {
				List<VinculoRepasse> vinculoRepasses = this.FindByRepasse(x.Id);
				x.Vinculo = (vinculoRepasses == null || vinculoRepasses.Count <= 0 ? null : vinculoRepasses);
			});
			return repasses;
		}

		public List<VinculoRepasse> FindByRepasse(long id)
		{
			List<VinculoRepasseDb> list = (
				from x in this._unitOfWork.Query<VinculoRepasseDb>()
				where x.IdRepasse == id
				select x).ToList<VinculoRepasseDb>();
			return ApplicationMapper.Mapper.Map<List<VinculoRepasseDb>, List<VinculoRepasse>>(list);
		}

		public Repasse Merge(Repasse repasse)
		{
			RepasseDb repasseDb = ApplicationMapper.Mapper.Map<Repasse, RepasseDb>(repasse);
			base.Merge(repasseDb);
			return ApplicationMapper.Mapper.Map<RepasseDb, Repasse>(repasseDb);
		}

		public VinculoRepasse Merge(VinculoRepasse vinculo)
		{
			VinculoRepasseDb vinculoRepasseDb = ApplicationMapper.Mapper.Map<VinculoRepasse, VinculoRepasseDb>(vinculo);
			this._unitOfWork.Repository<VinculoRepasseDb>().Merge(vinculoRepasseDb);
			return ApplicationMapper.Mapper.Map<VinculoRepasseDb, VinculoRepasse>(vinculoRepasseDb);
		}

		public Repasse SaveOrUpdate(Repasse repasse)
		{
			RepasseDb repasseDb = ApplicationMapper.Mapper.Map<Repasse, RepasseDb>(repasse);
			this.SaveOrUpdate(repasseDb);
			return ApplicationMapper.Mapper.Map<RepasseDb, Repasse>(repasseDb);
		}

		public VinculoRepasse SaveOrUpdate(VinculoRepasse vinculo)
		{
			VinculoRepasseDb vinculoRepasseDb = ApplicationMapper.Mapper.Map<VinculoRepasse, VinculoRepasseDb>(vinculo);
			this._unitOfWork.Repository<VinculoRepasseDb>().SaveOrUpdate(vinculoRepasseDb);
			return ApplicationMapper.Mapper.Map<VinculoRepasseDb, VinculoRepasse>(vinculoRepasseDb);
		}
	}
}