summaryrefslogtreecommitdiff
path: root/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/AutoRepository.cs
blob: 7cbb775cf916a4c9765b03d81f2d5e2450464db2 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
using AutoMapper;
using Gestor.Infrastructure.Entities.Generic;
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.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 AutoRepository : GenericRepository<AutoDb>, IAutoRepository, IGenericRepository<AutoDb>
	{
		private readonly GenericUnitOfWork _unitOfWork;

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

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

		public void DeleteRange(List<long> ids)
		{
			List<AutoDb> list = (
				from x in base.All()
				where ids.Contains(x.Item.Id)
				select x).ToList<AutoDb>();
			base.DeleteRange(list);
		}

		public Auto Find(long id)
		{
			AutoDb autoDb = base.All().FirstOrDefault<AutoDb>((AutoDb x) => x.Item.Id == id) ?? new AutoDb();
			return ApplicationMapper.Mapper.Map<AutoDb, Auto>(autoDb);
		}

		public List<PesquisaAvancada> FindAuto(string pesquisa, FiltroStatusDocumento status, List<VendedorUsuario> vendedorVinculado, TipoPesquisa tipo, bool tipobusca = false)
		{
			List<PesquisaAvancada> pesquisaAvancadas;
			object connection;
			DataTable dataTable = new DataTable();
			DataTable dataTable1 = new DataTable();
			DataTable dataTable2 = new DataTable();
			DataTable dataTable3 = new DataTable();
			DateTime date = Funcoes.GetNetworkTime().Date;
			List<Condicao> condicaos = new List<Condicao>()
			{
				new Condicao()
				{
					Campo = "d.excluido",
					Valores = null,
					Grupo = 1,
					Operacao = Operacao.Or
				},
				new Condicao()
				{
					Campo = "d.excluido",
					Valores = "0".CriarValor<string>(),
					Grupo = 1,
					Operacao = Operacao.Or
				}
			};
			switch (status)
			{
				case FiltroStatusDocumento.Vencidos:
				{
					condicaos.AddRange(new List<Condicao>()
					{
						new Condicao()
						{
							Campo = "d.situacao",
							Valores = new List<object>()
							{
								3,
								7
							},
							Operador = Operador.Diferente
						},
						new Condicao()
						{
							Campo = "d.vigencia2",
							Valores = date.AddDays(-5).CriarValor<DateTime>(),
							Operador = Operador.Menor
						}
					});
					goto case FiltroStatusDocumento.Todos;
				}
				case FiltroStatusDocumento.Cancelados:
				{
					condicaos.Add(new Condicao()
					{
						Campo = "d.situacao",
						Valores = 3.CriarValor<int>()
					});
					goto case FiltroStatusDocumento.Todos;
				}
				case FiltroStatusDocumento.Recusados:
				{
					condicaos.Add(new Condicao()
					{
						Campo = "d.situacao",
						Valores = 7.CriarValor<int>()
					});
					goto case FiltroStatusDocumento.Todos;
				}
				case FiltroStatusDocumento.Todos:
				{
					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())
						{
							List<Condicao> condicaos1 = new List<Condicao>();
							if (tipo == TipoPesquisa.Chassi)
							{
								condicaos1.Add(new Condicao()
								{
									Campo = "a.chassi",
									Valores = pesquisa.CriarValor<string>(),
									Operador = (tipobusca ? Operador.Igual : Operador.Like)
								});
							}
							else if (tipo == TipoPesquisa.Item)
							{
								condicaos1.Add(new Condicao()
								{
									Campo = "i.descricao",
									Valores = pesquisa.CriarValor<string>(),
									Operador = (tipobusca ? Operador.Igual : Operador.Like)
								});
							}
							else if (tipo == TipoPesquisa.Placa)
							{
								condicaos1.Add(new Condicao()
								{
									Campo = "REPLACE(a.placa, '-','')",
									Valores = pesquisa.Replace("-", "").Replace("?", "%").CriarValor<string>(),
									Operador = (tipobusca ? Operador.Igual : Operador.Like)
								});
							}
							dataTable = sqlCommand.Select(condicaos1.CreateParameters(0), "SELECT TOP 1000 i.iditem as id FROM item i LEFT OUTER JOIN auto a on i.iditem = a.iditem WHERE ", "");
							if (dataTable.Rows.Count != 0)
							{
								List<Condicao> condicaos2 = new List<Condicao>()
								{
									new Condicao()
									{
										Campo = "iditem",
										Valores = dataTable.AsEnumerable().Select<DataRow, long>((DataRow v) => v.Field<long>("id")).ToList<long>().CriarValor<long>()
									}
								};
								dataTable1 = sqlCommand.Select(condicaos2.CreateParameters(0), "SELECT iddocumento as id, descricao, iditem FROM item WHERE", "");
								condicaos.Add(new Condicao()
								{
									Campo = "d.iddocumento",
									Valores = dataTable1.AsEnumerable().Select<DataRow, long>((DataRow v) => v.Field<long>("id")).ToList<long>().CriarValor<long>()
								});
								dataTable2 = sqlCommand.Select(condicaos.CreateParameters(0), "SELECT DISTINCT cl.nome as cliente, c.idcliente, d.idcontrole, d.iddocumento, situacao, ISNULL(vigencia1, GETDATE()) AS vigencia1, vigencia2, proposta, contrato as apolice, aditamento as endosso, CAST(tipo as INTEGER) as tipo FROM documento d INNER JOIN controle c on c.idcontrole = d.idcontrole INNER JOIN cliente cl on cl.idcliente = c.idcliente WHERE ", "");
								if (dataTable2.Rows.Count != 0)
								{
									List<long> list = (
										from x in dataTable2.AsEnumerable().ToList<DataRow>()
										select x.Field<long>("idcontrole")).ToList<long>();
									List<Condicao> condicaos3 = new List<Condicao>()
									{
										new Condicao()
										{
											Campo = "d.idcontrole",
											Valores = list.CriarValor<long>()
										}
									};
									dataTable3 = sqlCommand.Select(condicaos3.CreateParameters(0), "SELECT DISTINCT vp.idvendedor, d.iddocumento, d.idcontrole, vp.idtipovendedor FROM vendedorparcela vp INNER JOIN documento d on d.iddocumento = vp.iddocumento WHERE ", "");
									goto Label1;
								}
								else
								{
									pesquisaAvancadas = new List<PesquisaAvancada>();
								}
							}
							else
							{
								pesquisaAvancadas = new List<PesquisaAvancada>();
							}
						}
					}
					return pesquisaAvancadas;
				}
				default:
				{
					condicaos.AddRange(new List<Condicao>()
					{
						new Condicao()
						{
							Campo = "d.situacao",
							Valores = new List<object>()
							{
								1,
								2,
								4
							}
						},
						new Condicao()
						{
							Campo = "d.vigencia2",
							Valores = null,
							Grupo = 2,
							Operacao = Operacao.Or
						},
						new Condicao()
						{
							Campo = "d.vigencia2",
							Valores = date.AddDays(-5).CriarValor<DateTime>(),
							Grupo = 2,
							Operacao = Operacao.Or,
							Operador = Operador.Maior
						}
					});
					goto case FiltroStatusDocumento.Todos;
				}
			}
		Label1:
			List<long> nums = null;
			if (vendedorVinculado != null && vendedorVinculado.Count > 0)
			{
				nums = dataTable3.AsEnumerable().Where<DataRow>((DataRow x) => {
					List<VendedorUsuario> vendedorUsuarios = vendedorVinculado;
					Func<VendedorUsuario, long> u003cu003e9_107 = AutoRepository.u003cu003ec.u003cu003e9__10_7;
					if (u003cu003e9_107 == null)
					{
						u003cu003e9_107 = (VendedorUsuario v) => v.Vendedor.Id;
						AutoRepository.u003cu003ec.u003cu003e9__10_7 = u003cu003e9_107;
					}
					return vendedorUsuarios.Select<VendedorUsuario, long>(u003cu003e9_107).Contains<long>(x.Field<long>("idvendedor"));
				}).Select<DataRow, long>((DataRow x) => x.Field<long>("idcontrole")).ToList<long>();
			}
			if (nums != null && nums.Count == 0)
			{
				return new List<PesquisaAvancada>();
			}
			return dataTable2.AsEnumerable().Where<DataRow>((DataRow x) => {
				if (nums == null)
				{
					return true;
				}
				return nums.Contains(x.Field<long>("idcontrole"));
			}).Select<DataRow, PesquisaAvancada>((DataRow x) => new PesquisaAvancada()
			{
				IdCliente = x.Field<long>("idcliente"),
				IdDocumento = x.Field<long>("iddocumento"),
				IdItem = dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<long>("iditem"),
				Nome = x.Field<string>("cliente"),
				Pesquisa = (x.Field<int>("tipo") == 0 ? string.Format("ITEM: {0} - NÚMERO DA APÓLICE: {1} - NÚMERO DA PROPOSTA: {2} - VIGÊNCIA INICIAL: {3:d} ", new object[] { dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<string>("descricao"), x.Field<string>("apolice"), x.Field<string>("proposta"), x.Field<DateTime>("vigencia1") }) : string.Format("ITEM: {0} - NÚMERO DA APÓLICE: {1} - NÚMERO DA PROPOSTA: {2} - NÚMERO DO ENDOSSO: {3} VIGÊNCIA INICIAL: {4:d}", new object[] { dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<string>("descricao"), x.Field<string>("apolice"), x.Field<string>("proposta"), x.Field<string>("endosso"), x.Field<DateTime>("vigencia1") }))
			}).ToList<PesquisaAvancada>();
		}

		public Documento FindByChassi(string chassi, long id, DateTime vigencia1, DateTime? vigencia2, long idSeguradora)
		{
			DocumentoDb documento;
			List<TipoSeguro> tipoSeguros = new List<TipoSeguro>()
			{
				TipoSeguro.Novo,
				TipoSeguro.Renovacao,
				TipoSeguro.Reabilitado
			};
			DateTime dateTime3 = Convert.ToDateTime(vigencia2);
			AutoDb autoDb = (
				from a in base.All()
				select new AutoDb()
				{
					Id = a.Id,
					Chassi = a.Chassi,
					Item = new ItemDb()
					{
						Cancelado = a.Item.Cancelado,
						Substituido = a.Item.Substituido,
						Documento = new DocumentoDb()
						{
							Vigencia1 = a.Item.Documento.Vigencia1,
							Vigencia2 = a.Item.Documento.Vigencia2,
							Situacao = a.Item.Documento.Situacao,
							Excluido = a.Item.Documento.Excluido,
							Controle = new ControleDb()
							{
								Id = a.Item.Documento.Controle.Id,
								Seguradora = new SeguradoraDb()
								{
									Id = a.Item.Documento.Controle.Seguradora.Id,
									Nome = a.Item.Documento.Controle.Seguradora.Nome,
									NomeSocial = a.Item.Documento.Controle.Seguradora.NomeSocial
								},
								Cliente = new ClienteDb()
								{
									Id = a.Item.Documento.Controle.Cliente.Id,
									Nome = a.Item.Documento.Controle.Cliente.Nome
								}
							}
						}
					}
				} into x
				where x.Id != id && x.Chassi == chassi && tipoSeguros.Contains(x.Item.Documento.Situacao)
				select x).ToList<AutoDb>().AsEnumerable<AutoDb>().ToList<AutoDb>().Where<AutoDb>((AutoDb x) => {
				DateTime dateTime = dateTime3;
				DateTime dateTime4 = x.Item.Documento.Vigencia1;
				if (dateTime >= dateTime4.AddDays(5))
				{
					DateTime dateTime1 = dateTime3;
					dateTime4 = Convert.ToDateTime(x.Item.Documento.Vigencia2);
					if (dateTime1 <= dateTime4.AddDays(5))
					{
						return true;
					}
				}
				DateTime dateTime2 = vigencia1;
				dateTime4 = x.Item.Documento.Vigencia1;
				if (dateTime2 < dateTime4.AddDays(5))
				{
					return false;
				}
				dateTime4 = vigencia1.AddDays(5);
				DateTime? nullable1 = x.Item.Documento.Vigencia2;
				if (!nullable1.HasValue)
				{
					return false;
				}
				return dateTime4 <= nullable1.GetValueOrDefault();
			}).ToList<AutoDb>().FirstOrDefault<AutoDb>((AutoDb x) => {
				long? nullable;
				if (!x.Item.Cancelado)
				{
					long? substituido = x.Item.Substituido;
					if (!substituido.HasValue && !x.Item.Documento.Excluido)
					{
						long num = idSeguradora;
						ControleDb controle = x.Item.Documento.Controle;
						if (controle != null)
						{
							nullable = new long?(controle.Seguradora.Id);
						}
						else
						{
							nullable = null;
						}
						substituido = nullable;
						return num == substituido.GetValueOrDefault() & substituido.HasValue;
					}
				}
				return false;
			});
			if (autoDb != null)
			{
				documento = autoDb.Item.Documento;
			}
			else
			{
				documento = null;
			}
			return ApplicationMapper.Mapper.Map<DocumentoDb, Documento>(documento);
		}

		public Auto FindById(long id)
		{
			AutoDb autoDb = base.FindEntityById(id);
			return ApplicationMapper.Mapper.Map<AutoDb, Auto>(autoDb);
		}

		public string FindChassi(long id)
		{
			List<Condicao> condicaos = new List<Condicao>()
			{
				new Condicao()
				{
					Campo = "iditem",
					Valores = id.CriarValor<long>()
				}
			};
			DataTable dataTable = this._unitOfWork.Select(condicaos.CreateParameters(0), "SELECT chassi FROM auto WHERE ", "");
			if (dataTable == null)
			{
				return null;
			}
			DataRow dataRow = dataTable.AsEnumerable().FirstOrDefault<DataRow>();
			if (dataRow == null)
			{
				return null;
			}
			return dataRow.Field<string>("chassi");
		}

		public List<PesquisaAvancada> FindPlaca(string placa, FiltroStatusDocumento status, List<VendedorUsuario> vendedorVinculado)
		{
			List<PesquisaAvancada> pesquisaAvancadas;
			object connection;
			string str = placa.Replace("-", "");
			string str1 = str.Insert(3, "-");
			DataTable dataTable = new DataTable();
			DataTable dataTable1 = new DataTable();
			DataTable dataTable2 = new DataTable();
			DataTable dataTable3 = 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(new string[] { "SELECT iditem as id FROM auto WHERE placa ='", str, "' OR placa ='", str1, "'" });
					using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter())
					{
						sqlDataAdapter.SelectCommand = sqlCommand;
						sqlDataAdapter.Fill(dataTable);
					}
					if (dataTable.Rows.Count != 0)
					{
						string str2 = string.Concat(" AND iditem IN (", string.Join<long>(",", dataTable.AsEnumerable().Select<DataRow, long>((DataRow v) => v.Field<long>("id"))), ")");
						sqlCommand.CommandText = string.Concat("SELECT iddocumento as id, descricao, iditem FROM item WHERE 1=1 ", str2);
						using (SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter())
						{
							sqlDataAdapter1.SelectCommand = sqlCommand;
							sqlDataAdapter1.Fill(dataTable1);
						}
						sqlCommand.CommandText = string.Concat("SELECT DISTINCT cl.nome as cliente, c.idcliente, d.idcontrole, d.iddocumento, situacao, vigencia1, vigencia2, proposta, contrato as apolice, aditamento as endosso, CAST(tipo as INTEGER) as tipo FROM documento d INNER JOIN controle c on c.idcontrole = d.idcontrole INNER JOIN cliente cl on cl.idcliente = c.idcliente WHERE (d.excluido IS NULL OR d.excluido != '1') AND d.iddocumento IN (", string.Join<long>(",", dataTable1.AsEnumerable().Select<DataRow, long>((DataRow v) => v.Field<long>("id"))), ")");
						using (SqlDataAdapter sqlDataAdapter2 = new SqlDataAdapter())
						{
							sqlDataAdapter2.SelectCommand = sqlCommand;
							sqlDataAdapter2.Fill(dataTable2);
						}
						string str3 = string.Concat(" AND d.idcontrole IN (", string.Join<long>(",", (
							from x in dataTable2.AsEnumerable().ToList<DataRow>()
							select x.Field<long>("idcontrole")).ToList<long>()), ")");
						sqlCommand.CommandText = string.Concat("SELECT DISTINCT vp.idvendedor, d.iddocumento, d.idcontrole, vp.idtipovendedor FROM vendedorparcela vp INNER JOIN documento d on d.iddocumento = vp.iddocumento WHERE 1=1 ", str3);
						using (SqlDataAdapter sqlDataAdapter3 = new SqlDataAdapter())
						{
							sqlDataAdapter3.SelectCommand = sqlCommand;
							sqlDataAdapter3.Fill(dataTable3);
							goto Label0;
						}
					}
					else
					{
						pesquisaAvancadas = new List<PesquisaAvancada>();
					}
				}
			}
			return pesquisaAvancadas;
		Label0:
			List<long> list = null;
			if (vendedorVinculado != null && vendedorVinculado.Count > 0)
			{
				list = dataTable3.AsEnumerable().Where<DataRow>((DataRow x) => {
					List<VendedorUsuario> vendedorUsuarios = vendedorVinculado;
					Func<VendedorUsuario, long> u003cu003e9_915 = AutoRepository.u003cu003ec.u003cu003e9__9_15;
					if (u003cu003e9_915 == null)
					{
						u003cu003e9_915 = (VendedorUsuario v) => v.Vendedor.Id;
						AutoRepository.u003cu003ec.u003cu003e9__9_15 = u003cu003e9_915;
					}
					return vendedorUsuarios.Select<VendedorUsuario, long>(u003cu003e9_915).Contains<long>(x.Field<long>("idvendedor"));
				}).Select<DataRow, long>((DataRow x) => x.Field<long>("idcontrole")).ToList<long>();
			}
			if (list != null && list.Count == 0)
			{
				return new List<PesquisaAvancada>();
			}
			DateTime date = Funcoes.GetNetworkTime().Date;
			switch (status)
			{
				case FiltroStatusDocumento.Vencidos:
				{
					return dataTable2.AsEnumerable().Where<DataRow>((DataRow x) => {
						if ((list == null || list.Contains(x.Field<long>("idcontrole"))) && (TipoSeguro)Enum.Parse(typeof(TipoSeguro), x.Field<object>("situacao").ToString()) == TipoSeguro.Renovado)
						{
							return true;
						}
						return x.Field<DateTime>("vigencia2") < date.AddDays(-5);
					}).Select<DataRow, PesquisaAvancada>((DataRow x) => new PesquisaAvancada()
					{
						IdCliente = x.Field<long>("idcliente"),
						IdDocumento = x.Field<long>("iddocumento"),
						IdItem = dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<long>("iditem"),
						Nome = x.Field<string>("cliente"),
						Pesquisa = (x.Field<int>("tipo") == 0 ? string.Concat(new string[] { "ITEM: ", dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<string>("descricao"), " - NÚMERO DA APÓLICE: ", x.Field<string>("apolice"), " - NÚMERO DA PROPOSTA: ", x.Field<string>("proposta") }) : string.Concat(new string[] { "ITEM: ", dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<string>("descricao"), " - NÚMERO DA APÓLICE: ", x.Field<string>("apolice"), " - NÚMERO DA PROPOSTA: ", x.Field<string>("proposta"), " - NÚMERO DO ENDOSSO: ", x.Field<string>("endosso") }))
					}).ToList<PesquisaAvancada>();
				}
				case FiltroStatusDocumento.Cancelados:
				{
					return dataTable2.AsEnumerable().Where<DataRow>((DataRow x) => {
						if (list != null && !list.Contains(x.Field<long>("idcontrole")))
						{
							return false;
						}
						return (TipoSeguro)Enum.Parse(typeof(TipoSeguro), x.Field<object>("situacao").ToString()) == TipoSeguro.Cancelado;
					}).Select<DataRow, PesquisaAvancada>((DataRow x) => new PesquisaAvancada()
					{
						IdCliente = x.Field<long>("idcliente"),
						IdDocumento = x.Field<long>("iddocumento"),
						IdItem = dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<long>("iditem"),
						Nome = x.Field<string>("cliente"),
						Pesquisa = (x.Field<int>("tipo") == 0 ? string.Concat(new string[] { "ITEM: ", dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<string>("descricao"), " - NÚMERO DA APÓLICE: ", x.Field<string>("apolice"), " - NÚMERO DA PROPOSTA: ", x.Field<string>("proposta") }) : string.Concat(new string[] { "ITEM: ", dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<string>("descricao"), " - NÚMERO DA APÓLICE: ", x.Field<string>("apolice"), " - NÚMERO DA PROPOSTA: ", x.Field<string>("proposta"), " - NÚMERO DO ENDOSSO: ", x.Field<string>("endosso") }))
					}).ToList<PesquisaAvancada>();
				}
				case FiltroStatusDocumento.Recusados:
				{
					return dataTable2.AsEnumerable().Where<DataRow>((DataRow x) => {
						if (list != null && !list.Contains(x.Field<long>("idcontrole")))
						{
							return false;
						}
						return (TipoSeguro)Enum.Parse(typeof(TipoSeguro), x.Field<object>("situacao").ToString()) == TipoSeguro.Recusado;
					}).Select<DataRow, PesquisaAvancada>((DataRow x) => new PesquisaAvancada()
					{
						IdCliente = x.Field<long>("idcliente"),
						IdDocumento = x.Field<long>("iddocumento"),
						IdItem = dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<long>("iditem"),
						Nome = x.Field<string>("cliente"),
						Pesquisa = (x.Field<int>("tipo") == 0 ? string.Concat(new string[] { "ITEM: ", dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<string>("descricao"), " - NÚMERO DA APÓLICE: ", x.Field<string>("apolice"), " - NÚMERO DA PROPOSTA: ", x.Field<string>("proposta") }) : string.Concat(new string[] { "ITEM: ", dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<string>("descricao"), " - NÚMERO DA APÓLICE: ", x.Field<string>("apolice"), " - NÚMERO DA PROPOSTA: ", x.Field<string>("proposta"), " - NÚMERO DO ENDOSSO: ", x.Field<string>("endosso") }))
					}).ToList<PesquisaAvancada>();
				}
				case FiltroStatusDocumento.Todos:
				{
					return dataTable2.AsEnumerable().Where<DataRow>((DataRow x) => {
						if (list == null)
						{
							return true;
						}
						return list.Contains(x.Field<long>("idcontrole"));
					}).Select<DataRow, PesquisaAvancada>((DataRow x) => new PesquisaAvancada()
					{
						IdCliente = x.Field<long>("idcliente"),
						IdDocumento = x.Field<long>("iddocumento"),
						IdItem = dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<long>("iditem"),
						Nome = x.Field<string>("cliente"),
						Pesquisa = (x.Field<int>("tipo") == 0 ? string.Concat(new string[] { "ITEM: ", dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<string>("descricao"), " - NÚMERO DA APÓLICE: ", x.Field<string>("apolice"), " - NÚMERO DA PROPOSTA: ", x.Field<string>("proposta") }) : string.Concat(new string[] { "ITEM: ", dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<string>("descricao"), " - NÚMERO DA APÓLICE: ", x.Field<string>("apolice"), " - NÚMERO DA PROPOSTA: ", x.Field<string>("proposta"), " - NÚMERO DO ENDOSSO: ", x.Field<string>("endosso") }))
					}).ToList<PesquisaAvancada>();
				}
				default:
				{
					return dataTable2.AsEnumerable().Where<DataRow>((DataRow x) => {
						if (list != null && !list.Contains(x.Field<long>("idcontrole")) || (TipoSeguro)Enum.Parse(typeof(TipoSeguro), x.Field<object>("situacao").ToString()) != TipoSeguro.Novo && (TipoSeguro)Enum.Parse(typeof(TipoSeguro), x.Field<object>("situacao").ToString()) != TipoSeguro.Renovacao)
						{
							return false;
						}
						return x.Field<DateTime>("vigencia2") > date.AddDays(-5);
					}).Select<DataRow, PesquisaAvancada>((DataRow x) => new PesquisaAvancada()
					{
						IdCliente = x.Field<long>("idcliente"),
						IdDocumento = x.Field<long>("iddocumento"),
						IdItem = dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<long>("iditem"),
						Nome = x.Field<string>("cliente"),
						Pesquisa = (x.Field<int>("tipo") == 0 ? string.Concat(new string[] { "ITEM: ", dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<string>("descricao"), " - NÚMERO DA APÓLICE: ", x.Field<string>("apolice"), " - NÚMERO DA PROPOSTA: ", x.Field<string>("proposta") }) : string.Concat(new string[] { "ITEM: ", dataTable1.AsEnumerable().First<DataRow>((DataRow i) => i.Field<long>("id") == x.Field<long>("iddocumento")).Field<string>("descricao"), " - NÚMERO DA APÓLICE: ", x.Field<string>("apolice"), " - NÚMERO DA PROPOSTA: ", x.Field<string>("proposta"), " - NÚMERO DO ENDOSSO: ", x.Field<string>("endosso") }))
					}).ToList<PesquisaAvancada>();
				}
			}
		}

		public Auto Merge(Auto auto)
		{
			AutoDb autoDb = ApplicationMapper.Mapper.Map<Auto, AutoDb>(auto);
			base.Merge(autoDb);
			return ApplicationMapper.Mapper.Map<AutoDb, Auto>(autoDb);
		}

		public Auto SaveOrUpdate(Auto auto)
		{
			AutoDb autoDb = ApplicationMapper.Mapper.Map<Auto, AutoDb>(auto);
			this.SaveOrUpdate(autoDb);
			return ApplicationMapper.Mapper.Map<AutoDb, Auto>(autoDb);
		}
	}
}