summaryrefslogtreecommitdiff
path: root/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/ImpostoRepository.cs
blob: 004432acb0e78b4f79c8c67388b0504cc5f429af (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
using AutoMapper;
using Gestor.Infrastructure.Entities.Ferramentas;
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.Domain.Ferramentas;
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;

namespace Gestor.Infrastructure.Repository.Logic
{
	public class ImpostoRepository : GenericRepository<ImpostoDb>, IImpostoRepository, IGenericRepository<ImpostoDb>
	{
		private readonly GenericUnitOfWork _unitOfWork;

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

		public List<Imposto> DefaultSelect(bool? ativo)
		{
			string str;
			if (!ativo.HasValue)
			{
				str = "";
			}
			else
			{
				str = (ativo.Value ? " AND Ativo = 1" : " AND Ativo = 0");
			}
			return this.Select(str);
		}

		public Imposto FindById(long id)
		{
			ImpostoDb impostoDb = base.FindEntityById(id);
			return ApplicationMapper.Mapper.Map<ImpostoDb, Imposto>(impostoDb);
		}

		public List<Imposto> FindByRamo(long id)
		{
			return this.Select(string.Format(" AND idramo = {0}", id));
		}

		public List<Imposto> FindBySeguradora(long id)
		{
			return this.Select(string.Format(" AND idciaseg = {0}", id));
		}

		public Imposto Merge(Imposto imposto)
		{
			ImpostoDb impostoDb = ApplicationMapper.Mapper.Map<Imposto, ImpostoDb>(imposto);
			base.Merge(impostoDb);
			return ApplicationMapper.Mapper.Map<ImpostoDb, Imposto>(impostoDb);
		}

		public Imposto SaveOrUpdate(Imposto imposto)
		{
			ImpostoDb impostoDb = ApplicationMapper.Mapper.Map<Imposto, ImpostoDb>(imposto);
			this.SaveOrUpdate(impostoDb);
			return ApplicationMapper.Mapper.Map<ImpostoDb, Imposto>(impostoDb);
		}

		private List<Imposto> 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 * FROM Imposto WHERE 1=1 ", condition);
					using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter())
					{
						sqlDataAdapter.SelectCommand = sqlCommand;
						sqlDataAdapter.Fill(dataTable);
					}
				}
			}
			return CustomMap.MapImposto(dataTable);
		}
	}
}