using AutoMapper; using Gestor.Infrastructure.Entities.Ferramentas; 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.Domain.Ferramentas; using Gestor.Model.Domain.Generic; using Gestor.Model.Domain.Relatorios; 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 NotaFiscalRepository : GenericRepository, INotaFiscalRepository, IGenericRepository { private readonly GenericUnitOfWork _unitOfWork; public NotaFiscalRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session) { this._unitOfWork = unitOfWork; } public void Delete(long id) { NotaFiscalDb notaFiscalDb = base.FindEntityById(id); if (notaFiscalDb == null) { return; } base.Delete(notaFiscalDb); } public List FindAll() { return this.Select(""); } public List FindByDatas(Filtros filtro) { string str = (filtro.Seguradoras == null || filtro.Seguradoras.Count == 0 ? "" : string.Concat(" AND IdSeguradora IN (", string.Join(",", from v in filtro.Seguradoras select v), ")")); return this.Select(string.Format(" AND Data >= '{0:yyyy-MM-dd}' AND Data <= '{1:yyyy-MM-dd}' {2}", filtro.Inicio, filtro.Fim, str)); } public bool FindByExtrato(long id) { return base.All().FirstOrDefault((NotaFiscalDb x) => x.IdExtrato == (long?)id) != null; } public NotaFiscal Merge(NotaFiscal notaFiscal) { NotaFiscalDb notaFiscalDb = ApplicationMapper.Mapper.Map(notaFiscal); base.Merge(notaFiscalDb); return ApplicationMapper.Mapper.Map(notaFiscalDb); } public NotaFiscal SaveOrUpdate(NotaFiscal notaFiscal) { NotaFiscalDb notaFiscalDb = ApplicationMapper.Mapper.Map(notaFiscal); this.SaveOrUpdate(notaFiscalDb); return ApplicationMapper.Mapper.Map(notaFiscalDb); } private List Select(string condition) { List notaFiscals; 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()) { sqlCommand.CommandTimeout = 15000; Auxiliar.CriarAuxiliar(sqlCommand, false); sqlCommand.CommandText = string.Concat("SELECT * FROM NotaFiscal WHERE 1=1 ", condition, ";"); using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter()) { sqlDataAdapter.SelectCommand = sqlCommand; sqlDataAdapter.Fill(dataTable); } if (dataTable.Rows.Count != 0) { return ( from x in dataTable.AsEnumerable().ToList() select new NotaFiscal() { Id = x.Field("Id"), ValorLiquido = x.Field("ValorLiquido"), Obs = x.Field("obs"), IdExtrato = x.Field("IdExtrato"), Iss = x.Field("Iss"), ValorBruto = x.Field("ValorBruto"), Data = x.Field("Data"), Seguradora = Auxiliar.Seguradoras.FirstOrDefault((Seguradora v) => v.Id == x.Field("IdSeguradora")), Estipulante = (!x.Field("IdEstipulante").HasValue ? null : Auxiliar.Estipulantes.FirstOrDefault((Estipulante e) => e.Id == x.Field("IdEstipulante"))), Extrato = x.Field("Extrato"), Ir = (x.Field("Ir") != null ? x.Field("Ir") : decimal.Zero) }).ToList(); } else { notaFiscals = new List(); } } } return notaFiscals; } } }