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
|
using AutoMapper;
using Gestor.Infrastructure.Entities.Generic;
using Gestor.Infrastructure.Entities.Seguros;
using Gestor.Infrastructure.Mappers;
using Gestor.Infrastructure.Repository.Generic;
using Gestor.Infrastructure.Repository.Interface;
using Gestor.Infrastructure.UnitOfWork.Generic;
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 CoberturaRepository : GenericRepository<CoberturaDb>, ICoberturaRepository, IGenericRepository<CoberturaDb>
{
private readonly GenericUnitOfWork _unitOfWork;
public CoberturaRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session)
{
this._unitOfWork = unitOfWork;
}
public List<Cobertura> AddRange(List<Cobertura> coberturas)
{
List<CoberturaDb> coberturaDbs = ApplicationMapper.Mapper.Map<List<Cobertura>, List<CoberturaDb>>(coberturas);
base.AddRange(coberturaDbs);
return ApplicationMapper.Mapper.Map<List<CoberturaDb>, List<Cobertura>>(coberturaDbs);
}
public void Delete(long id)
{
CoberturaDb coberturaDb = base.FindEntityById(id);
if (coberturaDb == null)
{
return;
}
base.Delete(coberturaDb);
}
public void DeletebyItem(long id)
{
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())
{
sqlCommand.CommandText = string.Format("DELETE FROM cobertura WHERE iditem = {0}", id);
sqlCommand.ExecuteNonQuery();
}
}
}
public void DeleteRange(List<long> ids)
{
List<CoberturaDb> list = (
from x in base.All()
where ids.Contains(x.Item.Id)
select x).ToList<CoberturaDb>();
if (list.Count == 0)
{
return;
}
base.DeleteRange(list);
}
public Cobertura FindById(long id)
{
CoberturaDb coberturaDb = base.FindEntityById(id);
return ApplicationMapper.Mapper.Map<CoberturaDb, Cobertura>(coberturaDb);
}
public List<Cobertura> FindByItemId(long id)
{
object connection;
SessionFactoryImpl sessionFactory = this._unitOfWork.Session.SessionFactory as SessionFactoryImpl;
DataTable dataTable = new DataTable();
DataTable dataTable1 = new DataTable();
if (sessionFactory != null)
{
connection = sessionFactory.ConnectionProvider.GetConnection();
}
else
{
connection = null;
}
using (SqlConnection sqlConnection = connection as SqlConnection)
{
using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
{
sqlCommand.CommandText = "SELECT * FROM coberturapadrao;";
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter())
{
sqlDataAdapter.SelectCommand = sqlCommand;
sqlDataAdapter.Fill(dataTable);
}
sqlCommand.CommandText = string.Format("SELECT * FROM cobertura WHERE iditem = {0}", id);
using (SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter())
{
sqlDataAdapter1.SelectCommand = sqlCommand;
sqlDataAdapter1.Fill(dataTable1);
}
}
}
List<CoberturaPadrao> list = (
from x in dataTable.AsEnumerable().ToList<DataRow>()
select new CoberturaPadrao()
{
Id = x.Field<long>("idcoberturapadrao"),
IdRamo = (x.Field<object>("idramo") != null ? x.Field<long>("idramo") : (long)0),
Descricao = x.Field<string>("descricao")
}).ToList<CoberturaPadrao>();
return dataTable1.AsEnumerable().ToList<DataRow>().Select<DataRow, Cobertura>((DataRow x) => {
Cobertura cobertura = new Cobertura()
{
Id = x.Field<long>("idcobertura"),
CoberturaPadrao = (x.Field<object>("idcoberturapadrao") == null ? null : list.FirstOrDefault<CoberturaPadrao>((CoberturaPadrao c) => c.Id == x.Field<long>("idcoberturapadrao"))),
Item = new Item()
{
Id = x.Field<long>("iditem")
}
};
decimal? nullable = x.Field<decimal?>("franquia");
cobertura.Franquia = nullable.GetValueOrDefault();
nullable = x.Field<decimal?>("premio");
cobertura.Premio = nullable.GetValueOrDefault();
nullable = x.Field<decimal?>("lmi");
cobertura.Lmi = nullable.GetValueOrDefault();
cobertura.Observacao = x.Field<string>("observacao");
return cobertura;
}).ToList<Cobertura>();
}
public Cobertura Merge(Cobertura cobertura)
{
CoberturaDb coberturaDb = ApplicationMapper.Mapper.Map<Cobertura, CoberturaDb>(cobertura);
base.Merge(coberturaDb);
return ApplicationMapper.Mapper.Map<CoberturaDb, Cobertura>(coberturaDb);
}
public List<Cobertura> MergeRange(List<Cobertura> coberturas)
{
List<CoberturaDb> coberturaDbs = ApplicationMapper.Mapper.Map<List<Cobertura>, List<CoberturaDb>>(coberturas);
CoberturaRepository coberturaRepository = this;
coberturaDbs.ForEach(new Action<CoberturaDb>(coberturaRepository.Merge));
return ApplicationMapper.Mapper.Map<List<CoberturaDb>, List<Cobertura>>(coberturaDbs);
}
public Cobertura SaveOrUpdate(Cobertura cobertura)
{
CoberturaDb coberturaDb = ApplicationMapper.Mapper.Map<Cobertura, CoberturaDb>(cobertura);
this.SaveOrUpdate(coberturaDb);
return ApplicationMapper.Mapper.Map<CoberturaDb, Cobertura>(coberturaDb);
}
}
}
|