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
|
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.Ferramentas;
using Gestor.Model.Domain.Generic;
using Gestor.Model.Domain.Relatorios;
using Gestor.Model.Domain.Seguros;
using NHibernate;
using NHibernate.Connection;
using NHibernate.Impl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
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 VendedorParcelaRepository : GenericRepository<VendedorParcelaDb>, IVendedorParcelaRepository, IGenericRepository<VendedorParcelaDb>
{
private readonly GenericUnitOfWork _unitOfWork;
public VendedorParcelaRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session)
{
this._unitOfWork = unitOfWork;
}
public List<VendedorParcela> AddRange(List<VendedorParcela> repasses)
{
List<VendedorParcelaDb> vendedorParcelaDbs = ApplicationMapper.Mapper.Map<List<VendedorParcela>, List<VendedorParcelaDb>>(repasses);
base.AddRange(vendedorParcelaDbs);
return ApplicationMapper.Mapper.Map<List<VendedorParcelaDb>, List<VendedorParcela>>(vendedorParcelaDbs);
}
public Vendedor BuscarVendedorPorControle(long id)
{
Vendedor vendedor;
object connection;
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())
{
Auxiliar.CriarVendedor(sqlCommand, false);
sqlCommand.CommandText = string.Format("SELECT TOP 1 idvendedor as id FROM vendedorparcela vp INNER JOIN parcela p on p.idparcela = vp.idparcela INNER JOIN documento d on d.iddocumento = vp.iddocumento WHERE vp.idtipovendedor = 1 AND p.idsubtipo = 1 AND d.idcontrole = {0};", id);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
sqlDataReader.Read();
long num = (sqlDataReader["id"] == null ? (long)0 : long.Parse(sqlDataReader["id"].ToString()));
sqlDataReader.Close();
return Auxiliar.Vendedores.FirstOrDefault<Vendedor>((Vendedor x) => {
if (x == null)
{
return false;
}
return x.Id == num;
});
}
else
{
vendedor = null;
}
}
}
return vendedor;
}
public bool Delete(long id)
{
VendedorParcelaDb vendedorParcelaDb = base.FindEntityById(id);
if (vendedorParcelaDb == null)
{
return true;
}
base.Delete(vendedorParcelaDb);
return true;
}
public void DeleteRange(long id)
{
List<VendedorParcelaDb> list = (
from vp in base.All()
where vp.Documento.Id == id && (int)vp.Parcela.SubTipo == 1
select vp).ToList<VendedorParcelaDb>();
if (list.Count == 0)
{
return;
}
VendedorParcelaRepository vendedorParcelaRepository = this;
list.ForEach(new Action<VendedorParcelaDb>(vendedorParcelaRepository.Delete));
}
public void DeleteRange(List<VendedorParcela> repasses)
{
List<long> list = (
from y in repasses
select y.Id).ToList<long>();
List<VendedorParcelaDb> vendedorParcelaDbs = (
from x in base.All()
where list.Contains(x.Id)
select x).ToList<VendedorParcelaDb>();
if (vendedorParcelaDbs.Count == 0)
{
return;
}
VendedorParcelaRepository vendedorParcelaRepository = this;
vendedorParcelaDbs.ForEach(new Action<VendedorParcelaDb>(vendedorParcelaRepository.Delete));
}
public List<ManutencaoPagamentos> FindByDate(Filtros filtro)
{
List<ManutencaoPagamentos> manutencaoPagamentos;
object connection;
DataTable dataTable = new DataTable();
string str = (filtro.Referencia == "DATA PAGAMENTO" ? "CAST(vp.datapgt AS DATE)" : "CAST(vp.dataprepagto AS DATE)");
List<Condicao> condicaos = new List<Condicao>()
{
new Condicao()
{
Campo = "vp.datapgt",
Valores = null,
Operador = Operador.Diferente,
Operacao = Operacao.And
},
new Condicao()
{
Campo = str,
Valores = filtro.Inicio.CriarValor<DateTime>(),
Operador = Operador.MaiorEIgual
},
new Condicao()
{
Campo = str,
Valores = filtro.Fim.CriarValor<DateTime>(),
Operador = Operador.MenorEIgual
}
};
if (filtro.Vendedores != null && filtro.Vendedores.Any<long>())
{
condicaos.Add(new Condicao()
{
Campo = "vp.idvendedor",
Valores = filtro.Vendedores.CriarValor<long>()
});
}
if (filtro.IdEmpresa > (long)0)
{
condicaos.Add(new Condicao()
{
Campo = "c.idempresa",
Valores = filtro.IdEmpresa.CriarValor<long>()
});
}
SessionFactoryImpl sessionFactory = this._unitOfWork.Session.SessionFactory as SessionFactoryImpl;
if (sessionFactory != null)
{
connection = sessionFactory.ConnectionProvider.GetConnection();
}
else
{
connection = null;
}
using (SqlConnection sqlConnection = connection as SqlConnection)
{
if (sqlConnection != null)
{
using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
{
Auxiliar.CriarAuxiliar(sqlCommand, false);
string str1 = "SELECT p.idparcela, vp.idvendedorparcela as idvendedorparcela, vp.iddocumento as iddocumento, c.idciaseg as idseguradora, cl.nome as nomecliente, c.idramo, d.contrato as apolice, d.aditamento as endosso, v.nome as nomevendedor, p.parcela as numeroparcela, p.valor as valor, p.datarec as datarecebimento, vp.datapgt as datapagamento, vp.dataprepagto as dataprepagamento, p.idsubtipo, c.idempresa, vp.vlrrep, c.idproduto FROM vendedorparcela vp INNER JOIN documento d on d.iddocumento = vp.iddocumento INNER JOIN controle c on c.idcontrole = d.idcontrole INNER JOIN vendedor v on v.idvendedor = vp.idvendedor INNER JOIN parcela p on p.idparcela = vp.idparcela INNER JOIN cliente cl on cl.idcliente = c.idcliente WHERE ";
dataTable = sqlCommand.Select(condicaos.CreateParameters(0), str1, "");
return dataTable.MapManutencao();
}
}
else
{
manutencaoPagamentos = null;
}
}
return manutencaoPagamentos;
}
public List<VendedorParcela> FindByDocumentId(long documentId)
{
return this.Select(string.Format(" AND vp.iddocumento = {0}", documentId));
}
public VendedorParcela FindById(long id)
{
VendedorParcelaDb vendedorParcelaDb = base.FindEntityById(id);
return ApplicationMapper.Mapper.Map<VendedorParcelaDb, VendedorParcela>(vendedorParcelaDb);
}
public Vendedor FindByMainDocumentId(long id)
{
Vendedor vendedor;
object connection;
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())
{
Auxiliar.CriarVendedor(sqlCommand, false);
sqlCommand.CommandText = string.Format("SELECT TOP 1 idvendedor as id FROM vendedorparcela vp INNER JOIN parcela p on p.idparcela = vp.idparcela WHERE vp.idtipovendedor = 1 AND p.idsubtipo = 1 AND vp.iddocumento = {0};", id);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
sqlDataReader.Read();
long num = (sqlDataReader["id"] == null ? (long)0 : long.Parse(sqlDataReader["id"].ToString()));
sqlDataReader.Close();
return Auxiliar.Vendedores.FirstOrDefault<Vendedor>((Vendedor x) => {
if (x == null)
{
return false;
}
return x.Id == num;
});
}
else
{
vendedor = null;
}
}
}
return vendedor;
}
public List<VendedorParcela> FindByParcela(long id)
{
return this.Select(string.Format(" AND vp.idparcela = {0}", id));
}
public List<Vendedor> FindVendedoresByDocumentId(long documentId)
{
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 DISTINCT idvendedor as id FROM vendedorparcela WHERE iddocumento = {0}", documentId);
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter())
{
sqlDataAdapter.SelectCommand = sqlCommand;
sqlDataAdapter.Fill(dataTable);
}
}
}
return Auxiliar.Vendedores.Where<Vendedor>((Vendedor x) => {
List<DataRow> list = dataTable.AsEnumerable().ToList<DataRow>();
Func<DataRow, long> u003cu003e9_181 = VendedorParcelaRepository.u003cu003ec.u003cu003e9__18_1;
if (u003cu003e9_181 == null)
{
u003cu003e9_181 = (DataRow v) => v.Field<long>("id");
VendedorParcelaRepository.u003cu003ec.u003cu003e9__18_1 = u003cu003e9_181;
}
return list.Select<DataRow, long>(u003cu003e9_181).Contains<long>(x.Id);
}).ToList<Vendedor>();
}
public List<Vendedor> FindVinculoByIdCliente(long idcliente)
{
object connection;
SqlCommand sqlCommand;
List<long> list;
List<Condicao> condicaos = new List<Condicao>();
SessionFactoryImpl sessionFactory = this._unitOfWork.Session.SessionFactory as SessionFactoryImpl;
condicaos.Add(new Condicao()
{
Campo = "cl.idcliente",
Valores = idcliente.CriarValor<long>()
});
if (sessionFactory != null)
{
connection = sessionFactory.ConnectionProvider.GetConnection();
}
else
{
connection = null;
}
using (SqlConnection sqlConnection = connection as SqlConnection)
{
if (sqlConnection != null)
{
sqlCommand = sqlConnection.CreateCommand();
}
else
{
sqlCommand = null;
}
using (SqlCommand sqlCommand1 = sqlCommand)
{
Auxiliar.CriarAuxiliar(sqlCommand1, false);
string str = "SELECT DISTINCT vp.idvendedor idvendedor FROM vendedorparcela vp LEFT JOIN documento d ON vp.iddocumento = d.iddocumento LEFT JOIN controle c ON c.idcontrole = d.idcontrole LEFT JOIN cliente cl on cl.idcliente = c.idcliente WHERE";
DataTable dataTable = sqlCommand1.Select(condicaos.CreateParameters(0), str, "");
list = dataTable.AsEnumerable().Select<DataRow, long>((DataRow column) => column.Field<long>("idvendedor")).ToList<long>();
}
}
return (
from v in Auxiliar.Vendedores
where list.Contains(v.Id)
select v).ToList<Vendedor>();
}
public bool GerarPagamento(List<long> ids, DateTime data)
{
bool flag;
object connection;
try
{
SessionFactoryImpl sessionFactory = this._unitOfWork.Session.SessionFactory as SessionFactoryImpl;
if (sessionFactory != null)
{
connection = sessionFactory.ConnectionProvider.GetConnection();
}
else
{
connection = null;
}
using (SqlConnection sqlConnection = connection as SqlConnection)
{
if (sqlConnection != null)
{
SqlTransaction sqlTransaction = sqlConnection.BeginTransaction();
using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
{
sqlCommand.CommandTimeout = 15000;
sqlCommand.Transaction = sqlTransaction;
List<long> nums = ids;
while (nums.Count > 0)
{
List<long> list = nums.Take<long>(1000).ToList<long>();
List<Condicao> condicaos = new List<Condicao>()
{
new Condicao()
{
Campo = "idvendedorparcela",
Valores = list.CriarValor<long>()
},
new Condicao()
{
Campo = "datapgt",
Valores = null
}
};
List<SqlParameter> sqlParameters = new List<SqlParameter>()
{
new SqlParameter()
{
ParameterName = "@datapgt",
SqlDbType = Funcoes.GetDbType(typeof(DateTime)),
Value = data
}
};
if (sqlCommand.Update(condicaos.CreateParameters(0), "UPDATE vendedorparcela SET datapgt = @datapgt WHERE ", sqlParameters))
{
nums = (list.Count < 1000 ? new List<long>() : nums.Except<long>(list).ToList<long>());
}
else
{
flag = false;
return flag;
}
}
sqlTransaction.Commit();
sqlCommand.Dispose();
}
}
else
{
flag = false;
return flag;
}
}
return true;
}
catch (Exception exception)
{
flag = false;
}
return flag;
}
public VendedorParcela Merge(VendedorParcela vendedorParcela)
{
VendedorParcelaDb vendedorParcelaDb = ApplicationMapper.Mapper.Map<VendedorParcela, VendedorParcelaDb>(vendedorParcela);
base.Merge(vendedorParcelaDb);
return ApplicationMapper.Mapper.Map<VendedorParcelaDb, VendedorParcela>(vendedorParcelaDb);
}
public VendedorParcela SaveOrUpdate(VendedorParcela vendedorParcela)
{
VendedorParcelaDb vendedorParcelaDb = ApplicationMapper.Mapper.Map<VendedorParcela, VendedorParcelaDb>(vendedorParcela);
this.SaveOrUpdate(vendedorParcelaDb);
return ApplicationMapper.Mapper.Map<VendedorParcelaDb, VendedorParcela>(vendedorParcelaDb);
}
public List<VendedorParcela> Select(string condition)
{
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, true);
sqlCommand.CommandText = string.Concat("SELECT DISTINCT * FROM vendedorparcela vp INNER JOIN parcela p on p.idparcela = vp.idparcela WHERE 1=1 ", condition);
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter())
{
sqlDataAdapter.SelectCommand = sqlCommand;
sqlDataAdapter.Fill(dataTable);
}
}
}
return CustomMap.MapVendedorParcela(dataTable);
}
public bool TemPagamentoParcela(long id)
{
return base.All().Any<VendedorParcelaDb>((VendedorParcelaDb x) => x.Parcela.Id == id && (x.DataPagamento != null) && (int?)x.Repasse.Forma == (int?)FormaRepasse.Recebimento);
}
public bool TipoVendedorUtilizado(long id)
{
return base.All().Any<VendedorParcelaDb>((VendedorParcelaDb x) => x.TipoVendedor.Id == id);
}
}
}
|