summaryrefslogtreecommitdiff
path: root/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/AdiantamentoRepository.cs
blob: aff2bf746ea25923d11219847a8fc0eea7fcf5d4 (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
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.Seguros;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;

namespace Gestor.Infrastructure.Repository.Logic
{
	public class AdiantamentoRepository : GenericRepository<AdiantamentoDb>, IAdiantamentoRepository, IGenericRepository<AdiantamentoDb>
	{
		private readonly GenericUnitOfWork _unitOfWork;

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

		public List<Adiantamento> AddRange(List<Adiantamento> adiantamentos)
		{
			List<AdiantamentoDb> adiantamentoDbs = ApplicationMapper.Mapper.Map<List<Adiantamento>, List<AdiantamentoDb>>(adiantamentos);
			base.AddRange(adiantamentoDbs);
			return ApplicationMapper.Mapper.Map<List<AdiantamentoDb>, List<Adiantamento>>(adiantamentoDbs);
		}

		public List<Adiantamento> BuscarAdiantamentos(long id, bool concluido = false)
		{
			List<AdiantamentoDb> list = (
				from x in base.All()
				where x.Vendedor.Id == id && x.Pago == concluido
				select x).ToList<AdiantamentoDb>();
			return ApplicationMapper.Mapper.Map<List<AdiantamentoDb>, List<Adiantamento>>(list);
		}

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

		public List<Adiantamento> Find(bool ativo)
		{
			return this.Select((new List<Condicao>()
			{
				new Condicao()
				{
					Campo = "ativo",
					Valores = null
				},
				new Condicao()
				{
					Campo = "pago",
					Valores = "1".CriarValor<string>()
				}
			}).CreateParameters(0));
		}

		public List<Adiantamento> Find(long id)
		{
			return this.Select((new List<Condicao>()
			{
				new Condicao()
				{
					Campo = "idvendedor",
					Valores = id.CriarValor<long>()
				}
			}).CreateParameters(0));
		}

		public List<Adiantamento> Find(string nome)
		{
			return this.Select((new List<Condicao>()
			{
				new Condicao()
				{
					Campo = "historico",
					Valores = nome.CriarValor<string>()
				}
			}).CreateParameters(0));
		}

		public List<Adiantamento> FindByDate(DateTime inicio, DateTime fim, List<long> vendedores = null, bool segundavia = false)
		{
			List<Condicao> condicaos = new List<Condicao>()
			{
				new Condicao()
				{
					Campo = (segundavia ? "CAST(Pagamento AS DATE)" : "CAST(data AS DATE)"),
					Valores = inicio.CriarValor<DateTime>(),
					Operador = Operador.MaiorEIgual
				},
				new Condicao()
				{
					Campo = (segundavia ? "CAST(Pagamento AS DATE)" : "CAST(data AS DATE)"),
					Valores = fim.CriarValor<DateTime>(),
					Operador = Operador.MenorEIgual
				}
			};
			condicaos.AddRange((!segundavia ? new List<Condicao>()
			{
				new Condicao()
				{
					Campo = "pago",
					Valores = null,
					Operacao = Operacao.Or,
					Grupo = 1
				},
				new Condicao()
				{
					Campo = "pago",
					Valores = "1".CriarValor<string>(),
					Operacao = Operacao.Or,
					Grupo = 1,
					Operador = Operador.Diferente
				}
			} : new List<Condicao>()
			{
				new Condicao()
				{
					Campo = "pago",
					Valores = "1".CriarValor<string>()
				}
			}));
			if (vendedores != null && vendedores.Count > 0)
			{
				condicaos.Add(new Condicao()
				{
					Campo = "idvendedor",
					Valores = vendedores.CriarValor<long>()
				});
			}
			return this.Select(condicaos.CreateParameters(0));
		}

		public Adiantamento FindById(long id)
		{
			AdiantamentoDb adiantamentoDb = base.FindEntityById(id);
			return ApplicationMapper.Mapper.Map<AdiantamentoDb, Adiantamento>(adiantamentoDb);
		}

		public Adiantamento Merge(Adiantamento adiantamento)
		{
			AdiantamentoDb adiantamentoDb = ApplicationMapper.Mapper.Map<Adiantamento, AdiantamentoDb>(adiantamento);
			base.Merge(adiantamentoDb);
			return ApplicationMapper.Mapper.Map<AdiantamentoDb, Adiantamento>(adiantamentoDb);
		}

		public Adiantamento SaveOrUpdate(Adiantamento adiantamento)
		{
			AdiantamentoDb adiantamentoDb = ApplicationMapper.Mapper.Map<Adiantamento, AdiantamentoDb>(adiantamento);
			this.SaveOrUpdate(adiantamentoDb);
			return ApplicationMapper.Mapper.Map<AdiantamentoDb, Adiantamento>(adiantamentoDb);
		}

		private List<Adiantamento> Select(SqlQueryCondition sqlCondition)
		{
			return this._unitOfWork.Select(sqlCondition, "SELECT DISTINCT * FROM adiantamento WHERE", "").MapAdiantamento();
		}
	}
}