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, IVinculoDocumentoRepository, IGenericRepository { private readonly GenericUnitOfWork _unitOfWork; public VinculoDocumentoRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session) { this._unitOfWork = unitOfWork; } public List BuscarArquivos(List ids, TipoArquivoVinculo type) { string str; str = (type == TipoArquivoVinculo.Proposta ? "IdPropostaDigital" : "IdApoliceDigital"); return this.Select(string.Concat(new string[] { " AND ", str, " IN (", string.Join(",", 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); } public VinculoDocumento Merge(VinculoDocumento vinculo) { VinculoDocumentoDb vinculoDocumentoDb = ApplicationMapper.Mapper.Map(vinculo); base.Merge(vinculoDocumentoDb); return ApplicationMapper.Mapper.Map(vinculoDocumentoDb); } public VinculoDocumento SaveOrUpdate(VinculoDocumento vinculo) { VinculoDocumentoDb vinculoDocumentoDb = ApplicationMapper.Mapper.Map(vinculo); this.SaveOrUpdate(vinculoDocumentoDb); return ApplicationMapper.Mapper.Map(vinculoDocumentoDb); } public void SaveVinculo(VinculoDocumentoDb vinculoDb) { if (vinculoDb.Id == 0) { this.SaveOrUpdate(vinculoDb); return; } base.Merge(vinculoDb); } private List 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 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 x) => !string.IsNullOrWhiteSpace(x.Field("Numero"))).CopyToDataTable(); 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 p) => p.Field("IdProposta") == x.Vinculo.IdPropostaDigital) : dataTable.AsEnumerable().FirstOrDefault((DataRow p) => { if (x.IdSeguradora == (long)1 && !p.IsNull("Identificacao")) { return p.Field("Identificacao").Equals(x.Proposta); } if (!p.Field("Numero").Equals(x.Proposta)) { return false; } return p.Field("Seguradora").Equals((int)x.IdSeguradora); })); if (dataRow != null) { x.Vinculo.IdPropostaDigital = dataRow.Field("IdProposta"); DataRow dataRow1 = dataTable1.AsEnumerable().FirstOrDefault((DataRow y) => { if (!y.Field("IdProposta").HasValue) { return false; } return y.Field("IdProposta") == x.Vinculo.IdPropostaDigital; }); x.Vinculo.IdApoliceDigital = (dataRow1 != null ? dataRow1.Field("IdApolice") : (long)0); this.SaveVinculo(ApplicationMapper.Mapper.Map(x.Vinculo)); num4++; return; } EnumerableRowCollection dataRows = dataTable.AsEnumerable().Where((DataRow p) => { if (!string.IsNullOrEmpty(p.Field("Documento")) && !p.Field("Documento").Equals(x.Documento) || !p.Field("Seguradora").Equals((int)x.IdSeguradora)) { return false; } if (x.Proposta.Contains(p.Field("Numero"))) { return true; } return p.Field("Numero").Contains(x.Proposta); }); Func u003cu003e9_38 = VinculoDocumentoRepository.u003cu003ec.u003cu003e9__3_8; if (u003cu003e9_38 == null) { u003cu003e9_38 = (DataRow y) => y.Field("idProposta"); VinculoDocumentoRepository.u003cu003ec.u003cu003e9__3_8 = u003cu003e9_38; } List list = dataRows.Select(u003cu003e9_38).ToList(); if (list.Count != 0) { DataRow dataRow2 = dataTable1.AsEnumerable().Where((DataRow y) => { if (!y.Field("IdProposta").HasValue) { return false; } return list.Contains(y.Field("IdProposta")); }).FirstOrDefault((DataRow y) => { if (y.Field("Endosso") != null && (!int.TryParse(y.Field("Endosso"), out num2) || num2 != 0)) { return false; } DateTime? nullable = y.Field("VigenciaInicial"); DateTime vigenciaInicial = x.VigenciaInicial; if ((nullable.HasValue ? nullable.GetValueOrDefault() != vigenciaInicial : true)) { nullable = y.Field("VigenciaFinal"); DateTime? vigenciafinal = x.Vigenciafinal; if ((nullable.HasValue == vigenciafinal.HasValue ? (nullable.HasValue ? nullable.GetValueOrDefault() != vigenciafinal.GetValueOrDefault() : false) : true)) { vigenciafinal = y.Field("DataEmissao"); if (!vigenciafinal.HasValue) { return false; } vigenciaInicial = y.Field("DataEmissao"); return vigenciaInicial.AddMonths(1) >= x.VigenciaInicial; } } return true; }); if (dataRow2 != null) { dataRow = dataTable.AsEnumerable().First((DataRow p) => p.Field("IdProposta") == dataRow2.Field("IdProposta")); x.Vinculo.IdPropostaDigital = dataRow.Field("IdProposta"); x.Vinculo.IdApoliceDigital = dataRow2.Field("IdApolice"); this.SaveVinculo(ApplicationMapper.Mapper.Map(x.Vinculo)); num4++; return; } } } num2 = 0; DataRow dataRow3 = dataTable1.AsEnumerable().Where((DataRow p) => { if (p.Field("Documento") == null || !p.Field("Documento").Equals(x.Documento)) { return false; } return p.Field("Seguradora").Equals((int)x.IdSeguradora); }).ToList().FirstOrDefault((DataRow y) => { DateTime vigenciaInicial; DateTime? vigenciafinal; if (!string.IsNullOrWhiteSpace(x.Endosso)) { if (y.Field("Endosso") == null || !int.TryParse(y.Field("Endosso"), out num2) || num2 <= 0) { return false; } vigenciafinal = y.Field("VigenciaInicial"); vigenciaInicial = x.VigenciaInicial; if (!vigenciafinal.HasValue) { return false; } return vigenciafinal.GetValueOrDefault() == vigenciaInicial; } if (y.Field("Endosso") == null) { return true; } if (!int.TryParse(y.Field("Endosso"), out num2) || num2 != 0) { return false; } DateTime? nullable = y.Field("VigenciaInicial"); vigenciaInicial = x.VigenciaInicial; if ((nullable.HasValue ? nullable.GetValueOrDefault() != vigenciaInicial : true)) { nullable = y.Field("VigenciaFinal"); vigenciafinal = x.Vigenciafinal; if ((nullable.HasValue == vigenciafinal.HasValue ? (nullable.HasValue ? nullable.GetValueOrDefault() != vigenciafinal.GetValueOrDefault() : false) : true)) { vigenciafinal = y.Field("DataEmissao"); if (!vigenciafinal.HasValue) { return false; } vigenciaInicial = y.Field("DataEmissao"); return vigenciaInicial.AddMonths(1) >= x.VigenciaInicial; } } return true; }); if (dataRow3 == null) { return; } DataRow dataRow4 = dataTable.AsEnumerable().FirstOrDefault((DataRow p) => { if (!dataRow3.Field("IdProposta").HasValue) { return false; } return p.Field("IdProposta") == dataRow3.Field("IdProposta"); }); x.Vinculo.IdPropostaDigital = (dataRow4 != null ? dataRow4.Field("IdProposta") : (long)0); x.Vinculo.IdApoliceDigital = dataRow3.Field("IdApolice"); this.SaveVinculo(ApplicationMapper.Mapper.Map(x.Vinculo)); num4++; }); num1 = num4; } catch (Exception exception) { num1 = num4; } return num1; } } }