diff options
Diffstat (limited to 'Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/VinculoDocumentoRepository.cs')
| -rw-r--r-- | Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/VinculoDocumentoRepository.cs | 345 |
1 files changed, 345 insertions, 0 deletions
diff --git a/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/VinculoDocumentoRepository.cs b/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/VinculoDocumentoRepository.cs new file mode 100644 index 0000000..4ad03aa --- /dev/null +++ b/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/VinculoDocumentoRepository.cs @@ -0,0 +1,345 @@ +using AutoMapper;
+using Gestor.Infrastructure.Entities.Aggilizador;
+using Gestor.Infrastructure.Entities.Generic;
+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.Aggilizador;
+using Gestor.Model.Domain.Relatorios;
+using Gestor.Model.Helper;
+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.Runtime.CompilerServices;
+
+namespace Gestor.Infrastructure.Repository.Logic
+{
+ public class VinculoDocumentoRepository : GenericRepository<VinculoDocumentoDb>, IVinculoDocumentoRepository, IGenericRepository<VinculoDocumentoDb>
+ {
+ private readonly GenericUnitOfWork _unitOfWork;
+
+ public VinculoDocumentoRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session)
+ {
+ this._unitOfWork = unitOfWork;
+ }
+
+ public List<ArquivoVinculo> BuscarArquivos(List<long> ids, TipoArquivoVinculo type)
+ {
+ string str;
+ str = (type == TipoArquivoVinculo.Proposta ? "IdPropostaDigital" : "IdApoliceDigital");
+ return this.Select(string.Concat(new string[] { " AND ", str, " IN (", string.Join<long>(",", ids), ")" }), type);
+ }
+
+ public void Delete(long id)
+ {
+ VinculoDocumentoDb vinculoDocumentoDb = base.FindEntityById(id);
+ if (vinculoDocumentoDb == null)
+ {
+ return;
+ }
+ base.Delete(vinculoDocumentoDb);
+ }
+
+ public VinculoDocumento FindById(long id)
+ {
+ VinculoDocumentoDb vinculoDocumentoDb = base.FindEntityById(id);
+ return ApplicationMapper.Mapper.Map<VinculoDocumentoDb, VinculoDocumento>(vinculoDocumentoDb);
+ }
+
+ public VinculoDocumento Merge(VinculoDocumento vinculo)
+ {
+ VinculoDocumentoDb vinculoDocumentoDb = ApplicationMapper.Mapper.Map<VinculoDocumento, VinculoDocumentoDb>(vinculo);
+ base.Merge(vinculoDocumentoDb);
+ return ApplicationMapper.Mapper.Map<VinculoDocumentoDb, VinculoDocumento>(vinculoDocumentoDb);
+ }
+
+ public VinculoDocumento SaveOrUpdate(VinculoDocumento vinculo)
+ {
+ VinculoDocumentoDb vinculoDocumentoDb = ApplicationMapper.Mapper.Map<VinculoDocumento, VinculoDocumentoDb>(vinculo);
+ this.SaveOrUpdate(vinculoDocumentoDb);
+ return ApplicationMapper.Mapper.Map<VinculoDocumentoDb, VinculoDocumento>(vinculoDocumentoDb);
+ }
+
+ public void SaveVinculo(VinculoDocumentoDb vinculoDb)
+ {
+ if (vinculoDb.Id == 0)
+ {
+ this.SaveOrUpdate(vinculoDb);
+ return;
+ }
+ base.Merge(vinculoDb);
+ }
+
+ private List<ArquivoVinculo> Select(string condition, TipoArquivoVinculo type)
+ {
+ string str;
+ string str1;
+ 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)
+ {
+ if (sqlConnection != null)
+ {
+ using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
+ {
+ if (type == TipoArquivoVinculo.Proposta)
+ {
+ str = "IdArquivoProposta as Id, Conteudo as Arquivo, IdPropostaDigital as IdVinculo, Chave, Ano, Storage";
+ str1 = "ArquivoProposta";
+ }
+ else
+ {
+ str = "IdArquivoApolice as Id, Conteudo as Arquivo, IdApoliceDigital as IdVinculo, Chave, Ano, Storage";
+ str1 = "ArquivoApolice";
+ }
+ sqlCommand.CommandText = string.Concat(new string[] { "SELECT ", str, " FROM ", str1, " WHERE 1=1 ", condition });
+ using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter())
+ {
+ sqlDataAdapter.SelectCommand = sqlCommand;
+ sqlDataAdapter.Fill(dataTable);
+ }
+ sqlCommand.Parameters.Clear();
+ }
+ }
+ }
+ return CustomMap.MapArquivoVinculo(dataTable);
+ }
+
+ public int Sincronize(DateTime inicio, List<DadosVinculo> dados)
+ {
+ int num1;
+ object connection;
+ int num2;
+ int num3 = 0;
+ int num4 = 0;
+ try
+ {
+ DataTable dataTable = new DataTable();
+ DataTable dataTable1 = 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)
+ {
+ if (sqlConnection != null)
+ {
+ using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
+ {
+ sqlCommand.CommandText = "SELECT * FROM PropostaDigital p INNER JOIN ClienteDigital c on c.idcliente = p.idcliente WHERE Data >= @Data";
+ sqlCommand.Parameters.AddWithValue("@Data", inicio.AddMonths(-2));
+ using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter())
+ {
+ sqlDataAdapter.SelectCommand = sqlCommand;
+ sqlDataAdapter.Fill(dataTable);
+ }
+ sqlCommand.CommandText = "SELECT * FROM ApoliceDigital p INNER JOIN ClienteDigital c on c.idcliente = p.idcliente WHERE DataEmissao >= @Data";
+ using (SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter())
+ {
+ sqlDataAdapter1.SelectCommand = sqlCommand;
+ sqlDataAdapter1.Fill(dataTable1);
+ }
+ sqlCommand.Parameters.Clear();
+ }
+ }
+ }
+ dataTable = dataTable.AsEnumerable().Where<DataRow>((DataRow x) => !string.IsNullOrWhiteSpace(x.Field<string>("Numero"))).CopyToDataTable<DataRow>();
+ dados.ForEach((DadosVinculo x) => {
+ num3++;
+ if (x.IdSeguradora != (long)1)
+ {
+ x.Proposta = x.Proposta.Clear();
+ }
+ x.Documento = x.Documento.Clear();
+ x.Vinculo = x.Vinculo ?? new VinculoDocumento()
+ {
+ IdDocumento = x.Id
+ };
+ if (!string.IsNullOrWhiteSpace(x.Proposta) && string.IsNullOrWhiteSpace(x.Endosso))
+ {
+ DataRow dataRow = (x.Vinculo.IdPropostaDigital > (long)0 ? dataTable.AsEnumerable().FirstOrDefault<DataRow>((DataRow p) => p.Field<long>("IdProposta") == x.Vinculo.IdPropostaDigital) : dataTable.AsEnumerable().FirstOrDefault<DataRow>((DataRow p) => {
+ if (x.IdSeguradora == (long)1 && !p.IsNull("Identificacao"))
+ {
+ return p.Field<string>("Identificacao").Equals(x.Proposta);
+ }
+ if (!p.Field<string>("Numero").Equals(x.Proposta))
+ {
+ return false;
+ }
+ return p.Field<int>("Seguradora").Equals((int)x.IdSeguradora);
+ }));
+ if (dataRow != null)
+ {
+ x.Vinculo.IdPropostaDigital = dataRow.Field<long>("IdProposta");
+ DataRow dataRow1 = dataTable1.AsEnumerable().FirstOrDefault<DataRow>((DataRow y) => {
+ if (!y.Field<long?>("IdProposta").HasValue)
+ {
+ return false;
+ }
+ return y.Field<long>("IdProposta") == x.Vinculo.IdPropostaDigital;
+ });
+ x.Vinculo.IdApoliceDigital = (dataRow1 != null ? dataRow1.Field<long>("IdApolice") : (long)0);
+ this.SaveVinculo(ApplicationMapper.Mapper.Map<VinculoDocumento, VinculoDocumentoDb>(x.Vinculo));
+ num4++;
+ return;
+ }
+ EnumerableRowCollection<DataRow> dataRows = dataTable.AsEnumerable().Where<DataRow>((DataRow p) => {
+ if (!string.IsNullOrEmpty(p.Field<string>("Documento")) && !p.Field<string>("Documento").Equals(x.Documento) || !p.Field<int>("Seguradora").Equals((int)x.IdSeguradora))
+ {
+ return false;
+ }
+ if (x.Proposta.Contains(p.Field<string>("Numero")))
+ {
+ return true;
+ }
+ return p.Field<string>("Numero").Contains(x.Proposta);
+ });
+ Func<DataRow, long> u003cu003e9_38 = VinculoDocumentoRepository.u003cu003ec.u003cu003e9__3_8;
+ if (u003cu003e9_38 == null)
+ {
+ u003cu003e9_38 = (DataRow y) => y.Field<long>("idProposta");
+ VinculoDocumentoRepository.u003cu003ec.u003cu003e9__3_8 = u003cu003e9_38;
+ }
+ List<long> list = dataRows.Select<DataRow, long>(u003cu003e9_38).ToList<long>();
+ if (list.Count != 0)
+ {
+ DataRow dataRow2 = dataTable1.AsEnumerable().Where<DataRow>((DataRow y) => {
+ if (!y.Field<long?>("IdProposta").HasValue)
+ {
+ return false;
+ }
+ return list.Contains(y.Field<long>("IdProposta"));
+ }).FirstOrDefault<DataRow>((DataRow y) => {
+ if (y.Field<string>("Endosso") != null && (!int.TryParse(y.Field<string>("Endosso"), out num2) || num2 != 0))
+ {
+ return false;
+ }
+ DateTime? nullable = y.Field<DateTime?>("VigenciaInicial");
+ DateTime vigenciaInicial = x.VigenciaInicial;
+ if ((nullable.HasValue ? nullable.GetValueOrDefault() != vigenciaInicial : true))
+ {
+ nullable = y.Field<DateTime?>("VigenciaFinal");
+ DateTime? vigenciafinal = x.Vigenciafinal;
+ if ((nullable.HasValue == vigenciafinal.HasValue ? (nullable.HasValue ? nullable.GetValueOrDefault() != vigenciafinal.GetValueOrDefault() : false) : true))
+ {
+ vigenciafinal = y.Field<DateTime?>("DataEmissao");
+ if (!vigenciafinal.HasValue)
+ {
+ return false;
+ }
+ vigenciaInicial = y.Field<DateTime>("DataEmissao");
+ return vigenciaInicial.AddMonths(1) >= x.VigenciaInicial;
+ }
+ }
+ return true;
+ });
+ if (dataRow2 != null)
+ {
+ dataRow = dataTable.AsEnumerable().First<DataRow>((DataRow p) => p.Field<long>("IdProposta") == dataRow2.Field<long>("IdProposta"));
+ x.Vinculo.IdPropostaDigital = dataRow.Field<long>("IdProposta");
+ x.Vinculo.IdApoliceDigital = dataRow2.Field<long>("IdApolice");
+ this.SaveVinculo(ApplicationMapper.Mapper.Map<VinculoDocumento, VinculoDocumentoDb>(x.Vinculo));
+ num4++;
+ return;
+ }
+ }
+ }
+ num2 = 0;
+ DataRow dataRow3 = dataTable1.AsEnumerable().Where<DataRow>((DataRow p) => {
+ if (p.Field<string>("Documento") == null || !p.Field<string>("Documento").Equals(x.Documento))
+ {
+ return false;
+ }
+ return p.Field<int>("Seguradora").Equals((int)x.IdSeguradora);
+ }).ToList<DataRow>().FirstOrDefault<DataRow>((DataRow y) => {
+ DateTime vigenciaInicial;
+ DateTime? vigenciafinal;
+ if (!string.IsNullOrWhiteSpace(x.Endosso))
+ {
+ if (y.Field<string>("Endosso") == null || !int.TryParse(y.Field<string>("Endosso"), out num2) || num2 <= 0)
+ {
+ return false;
+ }
+ vigenciafinal = y.Field<DateTime?>("VigenciaInicial");
+ vigenciaInicial = x.VigenciaInicial;
+ if (!vigenciafinal.HasValue)
+ {
+ return false;
+ }
+ return vigenciafinal.GetValueOrDefault() == vigenciaInicial;
+ }
+ if (y.Field<string>("Endosso") == null)
+ {
+ return true;
+ }
+ if (!int.TryParse(y.Field<string>("Endosso"), out num2) || num2 != 0)
+ {
+ return false;
+ }
+ DateTime? nullable = y.Field<DateTime?>("VigenciaInicial");
+ vigenciaInicial = x.VigenciaInicial;
+ if ((nullable.HasValue ? nullable.GetValueOrDefault() != vigenciaInicial : true))
+ {
+ nullable = y.Field<DateTime?>("VigenciaFinal");
+ vigenciafinal = x.Vigenciafinal;
+ if ((nullable.HasValue == vigenciafinal.HasValue ? (nullable.HasValue ? nullable.GetValueOrDefault() != vigenciafinal.GetValueOrDefault() : false) : true))
+ {
+ vigenciafinal = y.Field<DateTime?>("DataEmissao");
+ if (!vigenciafinal.HasValue)
+ {
+ return false;
+ }
+ vigenciaInicial = y.Field<DateTime>("DataEmissao");
+ return vigenciaInicial.AddMonths(1) >= x.VigenciaInicial;
+ }
+ }
+ return true;
+ });
+ if (dataRow3 == null)
+ {
+ return;
+ }
+ DataRow dataRow4 = dataTable.AsEnumerable().FirstOrDefault<DataRow>((DataRow p) => {
+ if (!dataRow3.Field<long?>("IdProposta").HasValue)
+ {
+ return false;
+ }
+ return p.Field<long>("IdProposta") == dataRow3.Field<long>("IdProposta");
+ });
+ x.Vinculo.IdPropostaDigital = (dataRow4 != null ? dataRow4.Field<long>("IdProposta") : (long)0);
+ x.Vinculo.IdApoliceDigital = dataRow3.Field<long>("IdApolice");
+ this.SaveVinculo(ApplicationMapper.Mapper.Map<VinculoDocumento, VinculoDocumentoDb>(x.Vinculo));
+ num4++;
+ });
+ num1 = num4;
+ }
+ catch (Exception exception)
+ {
+ num1 = num4;
+ }
+ return num1;
+ }
+ }
+}
\ No newline at end of file |