using AutoMapper; using Gestor.Infrastructure.Entities.Ferramentas; 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.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 RepasseRepository : GenericRepository, IRepasseRepository, IGenericRepository { private readonly GenericUnitOfWork _unitOfWork; public RepasseRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session) { this._unitOfWork = unitOfWork; } public List DefaultSelect(long idempresa = 0L) { List repasses; object connection; List nums = null; Func func1 = null; 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()) { Auxiliar.CriarVendedor(sqlCommand, true); Auxiliar.LoadTipoVendedor(sqlCommand); List condicaos = new List() { new Condicao() { Campo = "idempresa", Valores = idempresa.CriarValor() } }; string str = "SELECT * FROM repasse WHERE "; string str1 = "SELECT * FROM VinculoRepasse WHERE "; List condicaos1 = new List() { new Condicao() { Campo = "1", Valores = "1".CriarValor() } }; if (idempresa > (long)0) { condicaos1.AddRange(condicaos); } dataTable = sqlCommand.Select(condicaos1.CreateParameters(0), str, ""); if (dataTable.Rows.Count != 0) { for (List i = dataTable.AsEnumerable().Select((DataRow x) => x.Field("idrepasse")).Distinct().ToList(); i.Count > 0; i = nums) { List nums1 = i.Take(2000).ToList(); List condicaos2 = new List() { new Condicao() { Campo = "IdRepasse", Valores = nums1.CriarValor() } }; DataTable dataTable2 = sqlCommand.Select(condicaos2.CreateParameters(0), str1, ""); dataTable1.Merge(dataTable2); nums = (nums1.Count < 2000 ? new List() : i.Except(nums1).ToList()); } List repasses1 = dataTable.AsEnumerable().Select((DataRow x) => { long? nullable1; long? nullable2; Repasse repasse = new Repasse() { Id = x.Field("idrepasse"), Ativo = (x.Field("ativo") == null ? true : x.Field("ativo").ToString() != "0"), Base = (x.Field("base") == null ? null : new BaseRepasse?((BaseRepasse)Enum.Parse(typeof(BaseRepasse), x.Field("base").ToString()))), Forma = (x.Field("forma") == null ? null : new FormaRepasse?((FormaRepasse)Enum.Parse(typeof(FormaRepasse), x.Field("forma").ToString()))), Incidencia = (x.Field("incidencia") == null ? null : new TipoIncidencia?((TipoIncidencia)Enum.Parse(typeof(TipoIncidencia), x.Field("incidencia").ToString()))), Tipo = (x.Field("tipo") == null ? null : new TipoRepasse?((TipoRepasse)Enum.Parse(typeof(TipoRepasse), x.Field("tipo").ToString()))) }; decimal? nullable3 = x.Field("vlrnovo"); repasse.ValorNovo = nullable3.GetValueOrDefault(); nullable3 = x.Field("vlrrenovacao"); repasse.ValorRenovacao = nullable3.GetValueOrDefault(); repasse.Vendedor = Auxiliar.Vendedores.FirstOrDefault((Vendedor z) => z.Id == x.Field("idvendedor")); repasse.Ramo = (x.Field("idramo") == null ? null : Auxiliar.Ramos.FirstOrDefault((Ramo z) => { long id = z.Id; long? nullable = x.Field("idramo"); return id == nullable.GetValueOrDefault() & nullable.HasValue; })); if (x.Field("idciaseg") == null) { nullable1 = null; nullable2 = nullable1; } else { Seguradora seguradora = Auxiliar.Seguradoras.FirstOrDefault((Seguradora z) => { long id = z.Id; long? nullable = x.Field("idciaseg"); return id == nullable.GetValueOrDefault() & nullable.HasValue; }); if (seguradora != null) { nullable2 = new long?(seguradora.Id); } else { nullable1 = null; nullable2 = nullable1; } } repasse.Seguradora = nullable2; return repasse; }).ToList(); List repasses2 = ( from x in repasses1 where x.Ativo select x).ToList(); repasses2.ForEach((Repasse x) => { Repasse list = x; EnumerableRowCollection dataRows = dataTable1.AsEnumerable().Where((DataRow r) => r.Field("IdRepasse") == x.Id); Func u003cu003e9_8 = func1; if (u003cu003e9_8 == null) { Func vinculoRepasse = (DataRow v) => new VinculoRepasse() { Id = v.Field("Id"), IdRepasse = v.Field("IdRepasse"), RepasseVinculo = repasses1.Find((Repasse z) => z.Id == v.Field("RepasseVinculo_id")), TipoVendedor = Auxiliar.TipoVendedores.Find((TipoVendedor z) => z.Id == v.Field("TipoVendedor_id")), TipoVendedorVinculo = Auxiliar.TipoVendedores.Find((TipoVendedor z) => z.Id == v.Field("TipoVendedorVinculo_id")) }; Func func = vinculoRepasse; func1 = vinculoRepasse; u003cu003e9_8 = func; } list.Vinculo = dataRows.Select(u003cu003e9_8).ToList(); }); repasses = repasses2; } else { repasses = new List(); } } } return repasses; } public void Delete(long id) { RepasseDb repasseDb = base.FindEntityById(id); if (repasseDb == null) { return; } base.Delete(repasseDb); } public void DeleteVinculo(long id) { VinculoRepasseDb vinculoRepasseDb = this._unitOfWork.Repository().FindEntityById(id); if (vinculoRepasseDb == null) { return; } this._unitOfWork.Repository().Delete(vinculoRepasseDb); } public Repasse FindById(long id) { List vinculoRepasses; RepasseDb repasseDb = base.FindEntityById(id); List vinculoRepasses1 = this.FindByRepasse(id); Repasse repasse = ApplicationMapper.Mapper.Map(repasseDb); if (vinculoRepasses1 == null || vinculoRepasses1.Count <= 0) { vinculoRepasses = null; } else { vinculoRepasses = vinculoRepasses1; } repasse.Vinculo = vinculoRepasses; return repasse; } public List FindByIdRepasse(long id) { object connection; SessionFactoryImpl sessionFactory = this._unitOfWork.Session.SessionFactory as SessionFactoryImpl; DataTable dataTable = new DataTable(); if (sessionFactory != null) { connection = sessionFactory.ConnectionProvider.GetConnection(); } else { connection = null; } using (SqlConnection sqlConnection = connection as SqlConnection) { using (SqlCommand sqlCommand = sqlConnection.CreateCommand()) { Auxiliar.CriarVendedor(sqlCommand, false); sqlCommand.CommandText = string.Format("SELECT idvendedorparcela FROM vendedorparcela WHERE idrepasse = {0}", id); using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter()) { sqlDataAdapter.SelectCommand = sqlCommand; sqlDataAdapter.Fill(dataTable); } } } return dataTable.AsEnumerable().Select((DataRow x) => new VendedorParcela() { Id = x.Field("idvendedorparcela") }).ToList(); } public List FindByIdVendedor(long id) { List list = this._unitOfWork.Session.CreateQuery(string.Format("from RepasseDb \r\n where IdVendedor = {0}", id)).List().ToList(); List repasses = ApplicationMapper.Mapper.Map, List>(list); repasses.ForEach((Repasse x) => { List vinculoRepasses = this.FindByRepasse(x.Id); x.Vinculo = (vinculoRepasses == null || vinculoRepasses.Count <= 0 ? null : vinculoRepasses); }); return repasses; } public List FindByRepasse(long id) { List list = ( from x in this._unitOfWork.Query() where x.IdRepasse == id select x).ToList(); return ApplicationMapper.Mapper.Map, List>(list); } public Repasse Merge(Repasse repasse) { RepasseDb repasseDb = ApplicationMapper.Mapper.Map(repasse); base.Merge(repasseDb); return ApplicationMapper.Mapper.Map(repasseDb); } public VinculoRepasse Merge(VinculoRepasse vinculo) { VinculoRepasseDb vinculoRepasseDb = ApplicationMapper.Mapper.Map(vinculo); this._unitOfWork.Repository().Merge(vinculoRepasseDb); return ApplicationMapper.Mapper.Map(vinculoRepasseDb); } public Repasse SaveOrUpdate(Repasse repasse) { RepasseDb repasseDb = ApplicationMapper.Mapper.Map(repasse); this.SaveOrUpdate(repasseDb); return ApplicationMapper.Mapper.Map(repasseDb); } public VinculoRepasse SaveOrUpdate(VinculoRepasse vinculo) { VinculoRepasseDb vinculoRepasseDb = ApplicationMapper.Mapper.Map(vinculo); this._unitOfWork.Repository().SaveOrUpdate(vinculoRepasseDb); return ApplicationMapper.Mapper.Map(vinculoRepasseDb); } } }