From 1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1 Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Mon, 30 Mar 2026 10:38:18 -0300 Subject: chore: location --- .../SaldoRepository.cs | 136 +++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/SaldoRepository.cs (limited to 'Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/SaldoRepository.cs') diff --git a/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/SaldoRepository.cs b/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/SaldoRepository.cs new file mode 100644 index 0000000..5249fcf --- /dev/null +++ b/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/SaldoRepository.cs @@ -0,0 +1,136 @@ +using AutoMapper; +using Gestor.Infrastructure.Entities.Financeiro; +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.Financeiro; +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 SaldoRepository : GenericRepository, ISaldoRepository, IGenericRepository + { + private readonly GenericUnitOfWork _unitOfWork; + + public SaldoRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session) + { + this._unitOfWork = unitOfWork; + } + + public Saldo BuscarAberto(long id) + { + string str = string.Format(" AND idconta = {0} AND dtfim IS NULL", id); + return this.Select(str).FirstOrDefault(); + } + + public List BuscarPorConta(long id) + { + return this.Select(string.Format(" AND idconta = {0}", id)); + } + + public Saldo BuscarPorData(DateTime inicio, long id) + { + string str = string.Format(" AND idconta = {0}", id); + return ( + from x in this.Select(str) + orderby x.DataInicio descending + select x).FirstOrDefault((Saldo x) => { + DateTime? dataInicio = x.DataInicio; + DateTime dateTime = inicio; + if (!dataInicio.HasValue) + { + return false; + } + return dataInicio.GetValueOrDefault() <= dateTime; + }); + } + + public Saldo BuscarPorMenorData(long id) + { + string str = string.Format(" AND idconta = {0}", id); + return ( + from x in this.Select(str) + orderby x.DataInicio + select x).FirstOrDefault(); + } + + public void Delete(long id) + { + SaldoDb saldoDb = base.FindEntityById(id); + if (saldoDb == null) + { + return; + } + base.Delete(saldoDb); + } + + public Saldo FindById(long id) + { + SaldoDb saldoDb = base.FindEntityById(id); + return ApplicationMapper.Mapper.Map(saldoDb); + } + + public Saldo Merge(Saldo saldo) + { + SaldoDb saldoDb = ApplicationMapper.Mapper.Map(saldo); + base.Merge(saldoDb); + return ApplicationMapper.Mapper.Map(saldoDb); + } + + public Saldo SaveOrUpdate(Saldo saldo) + { + SaldoDb saldoDb = ApplicationMapper.Mapper.Map(saldo); + this.SaveOrUpdate(saldoDb); + return ApplicationMapper.Mapper.Map(saldoDb); + } + + private List Select(string condition) + { + List saldos; + 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) + { + using (SqlCommand sqlCommand = sqlConnection.CreateCommand()) + { + AuxiliarFinanceiro.Criar(sqlCommand); + sqlCommand.CommandText = string.Concat("SELECT * FROM saldo WHERE 1=1 ", condition); + using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter()) + { + sqlDataAdapter.SelectCommand = sqlCommand; + sqlDataAdapter.Fill(dataTable); + } + if (dataTable.Rows.Count != 0) + { + return CustomMap.MapSaldo(dataTable); + } + else + { + saldos = new List(); + } + } + } + return saldos; + } + } +} \ No newline at end of file -- cgit v1.2.3