summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/VendedorRepository.cs
blob: 04a6532fb168595018ea76d2ec32a667c8b769dd (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
using AutoMapper;
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.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 VendedorRepository : GenericRepository<VendedorDb>, IVendedorRepository, IGenericRepository<VendedorDb>
	{
		private readonly GenericUnitOfWork _unitOfWork;

		private const string comandoVendedor = "SELECT idvendedor, nome, corretora, tipoincidenciadesconto, desconto, ativo, idempresa, idcodigobanco, agencia, conta, cpfcnpj, idtipoconta, cpfcnpjtitular, titularconta, obs FROM vendedor WHERE";

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

		public void Delete(long id)
		{
			base.Delete(base.FindEntityById(id));
		}

		public List<Vendedor> Find(bool ativo, long idempresa = 0L)
		{
			List<Condicao> condicaos = new List<Condicao>()
			{
				new Condicao()
				{
					Campo = "idempresa",
					Valores = idempresa.CriarValor<long>()
				}
			};
			List<Condicao> condicaos1 = new List<Condicao>()
			{
				new Condicao()
				{
					Campo = "ISNULL(ativo, '1')",
					Valores = (ativo ? "1".CriarValor<string>() : "0".CriarValor<string>())
				}
			};
			if (idempresa > (long)0)
			{
				condicaos1.AddRange(condicaos);
			}
			return this._unitOfWork.Select(condicaos1.CreateParameters(0), "SELECT idvendedor, nome, corretora, tipoincidenciadesconto, desconto, ativo, idempresa, idcodigobanco, agencia, conta, cpfcnpj, idtipoconta, cpfcnpjtitular, titularconta, obs FROM vendedor WHERE", "").MapVendedor();
		}

		public List<Vendedor> Find(long idempresa = 0L)
		{
			return this.Select((idempresa == 0 ? "" : string.Format(" AND idempresa = {0}", idempresa)));
		}

		public List<Vendedor> Find(string nome, long idempresa = 0L)
		{
			string str = (idempresa == 0 ? "" : string.Format(" AND idempresa = {0}", idempresa));
			string str1 = string.Concat(" AND UPPER(ISNULL(nome, '')) LIKE '%", nome.ToUpper(), "%'");
			return this.Select(string.Concat(str, str1));
		}

		public Vendedor FindById(long id)
		{
			VendedorDb vendedorDb = base.FindEntityById(id);
			return ApplicationMapper.Mapper.Map<VendedorDb, Vendedor>(vendedorDb);
		}

		public Vendedor FindCorretora()
		{
			VendedorDb vendedorDb = base.All().FirstOrDefault<VendedorDb>((VendedorDb x) => x.Corretora);
			return ApplicationMapper.Mapper.Map<VendedorDb, Vendedor>(vendedorDb);
		}

		public Vendedor Merge(Vendedor vendedor)
		{
			VendedorDb vendedorDb = ApplicationMapper.Mapper.Map<Vendedor, VendedorDb>(vendedor);
			base.Merge(vendedorDb);
			return ApplicationMapper.Mapper.Map<VendedorDb, Vendedor>(vendedorDb);
		}

		public Vendedor SaveOrUpdate(Vendedor vendedor)
		{
			VendedorDb vendedorDb = ApplicationMapper.Mapper.Map<Vendedor, VendedorDb>(vendedor);
			this.SaveOrUpdate(vendedorDb);
			return ApplicationMapper.Mapper.Map<VendedorDb, Vendedor>(vendedorDb);
		}

		public List<Vendedor> Select(string condition)
		{
			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())
				{
					Auxiliar.CriarAuxiliar(sqlCommand, false);
					sqlCommand.CommandText = string.Concat("SELECT idvendedor, nome, corretora, tipoincidenciadesconto, desconto, ativo, idempresa, idcodigobanco, agencia, conta, cpfcnpj, idtipoconta, cpfcnpjtitular, titularconta, obs FROM vendedor WHERE 1=1 ", condition, " ");
					using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter())
					{
						sqlDataAdapter.SelectCommand = sqlCommand;
						sqlDataAdapter.Fill(dataTable);
					}
				}
			}
			return dataTable.MapVendedor();
		}
	}
}