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 --- .../VendedorParcelaRepository.cs | 474 +++++++++++++++++++++ 1 file changed, 474 insertions(+) create mode 100644 Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/VendedorParcelaRepository.cs (limited to 'Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/VendedorParcelaRepository.cs') diff --git a/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/VendedorParcelaRepository.cs b/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/VendedorParcelaRepository.cs new file mode 100644 index 0000000..e765212 --- /dev/null +++ b/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/VendedorParcelaRepository.cs @@ -0,0 +1,474 @@ +using AutoMapper; +using Gestor.Infrastructure.Entities.Generic; +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.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.ComponentModel; +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 VendedorParcelaRepository : GenericRepository, IVendedorParcelaRepository, IGenericRepository + { + private readonly GenericUnitOfWork _unitOfWork; + + public VendedorParcelaRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session) + { + this._unitOfWork = unitOfWork; + } + + public List AddRange(List repasses) + { + List vendedorParcelaDbs = ApplicationMapper.Mapper.Map, List>(repasses); + base.AddRange(vendedorParcelaDbs); + return ApplicationMapper.Mapper.Map, List>(vendedorParcelaDbs); + } + + public Vendedor BuscarVendedorPorControle(long id) + { + Vendedor vendedor; + object connection; + 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()) + { + Auxiliar.CriarVendedor(sqlCommand, false); + sqlCommand.CommandText = string.Format("SELECT TOP 1 idvendedor as id FROM vendedorparcela vp INNER JOIN parcela p on p.idparcela = vp.idparcela INNER JOIN documento d on d.iddocumento = vp.iddocumento WHERE vp.idtipovendedor = 1 AND p.idsubtipo = 1 AND d.idcontrole = {0};", id); + SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); + if (sqlDataReader.HasRows) + { + sqlDataReader.Read(); + long num = (sqlDataReader["id"] == null ? (long)0 : long.Parse(sqlDataReader["id"].ToString())); + sqlDataReader.Close(); + return Auxiliar.Vendedores.FirstOrDefault((Vendedor x) => { + if (x == null) + { + return false; + } + return x.Id == num; + }); + } + else + { + vendedor = null; + } + } + } + return vendedor; + } + + public bool Delete(long id) + { + VendedorParcelaDb vendedorParcelaDb = base.FindEntityById(id); + if (vendedorParcelaDb == null) + { + return true; + } + base.Delete(vendedorParcelaDb); + return true; + } + + public void DeleteRange(long id) + { + List list = ( + from vp in base.All() + where vp.Documento.Id == id && (int)vp.Parcela.SubTipo == 1 + select vp).ToList(); + if (list.Count == 0) + { + return; + } + VendedorParcelaRepository vendedorParcelaRepository = this; + list.ForEach(new Action(vendedorParcelaRepository.Delete)); + } + + public void DeleteRange(List repasses) + { + List list = ( + from y in repasses + select y.Id).ToList(); + List vendedorParcelaDbs = ( + from x in base.All() + where list.Contains(x.Id) + select x).ToList(); + if (vendedorParcelaDbs.Count == 0) + { + return; + } + VendedorParcelaRepository vendedorParcelaRepository = this; + vendedorParcelaDbs.ForEach(new Action(vendedorParcelaRepository.Delete)); + } + + public List FindByDate(Filtros filtro) + { + List manutencaoPagamentos; + object connection; + DataTable dataTable = new DataTable(); + string str = (filtro.Referencia == "DATA PAGAMENTO" ? "CAST(vp.datapgt AS DATE)" : "CAST(vp.dataprepagto AS DATE)"); + List condicaos = new List() + { + new Condicao() + { + Campo = "vp.datapgt", + Valores = null, + Operador = Operador.Diferente, + Operacao = Operacao.And + }, + new Condicao() + { + Campo = str, + Valores = filtro.Inicio.CriarValor(), + Operador = Operador.MaiorEIgual + }, + new Condicao() + { + Campo = str, + Valores = filtro.Fim.CriarValor(), + Operador = Operador.MenorEIgual + } + }; + if (filtro.Vendedores != null && filtro.Vendedores.Any()) + { + condicaos.Add(new Condicao() + { + Campo = "vp.idvendedor", + Valores = filtro.Vendedores.CriarValor() + }); + } + if (filtro.IdEmpresa > (long)0) + { + condicaos.Add(new Condicao() + { + Campo = "c.idempresa", + Valores = filtro.IdEmpresa.CriarValor() + }); + } + 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()) + { + Auxiliar.CriarAuxiliar(sqlCommand, false); + string str1 = "SELECT p.idparcela, vp.idvendedorparcela as idvendedorparcela, vp.iddocumento as iddocumento, c.idciaseg as idseguradora, cl.nome as nomecliente, c.idramo, d.contrato as apolice, d.aditamento as endosso, v.nome as nomevendedor, p.parcela as numeroparcela, p.valor as valor, p.datarec as datarecebimento, vp.datapgt as datapagamento, vp.dataprepagto as dataprepagamento, p.idsubtipo, c.idempresa, vp.vlrrep, c.idproduto FROM vendedorparcela vp INNER JOIN documento d on d.iddocumento = vp.iddocumento INNER JOIN controle c on c.idcontrole = d.idcontrole INNER JOIN vendedor v on v.idvendedor = vp.idvendedor INNER JOIN parcela p on p.idparcela = vp.idparcela INNER JOIN cliente cl on cl.idcliente = c.idcliente WHERE "; + dataTable = sqlCommand.Select(condicaos.CreateParameters(0), str1, ""); + return dataTable.MapManutencao(); + } + } + else + { + manutencaoPagamentos = null; + } + } + return manutencaoPagamentos; + } + + public List FindByDocumentId(long documentId) + { + return this.Select(string.Format(" AND vp.iddocumento = {0}", documentId)); + } + + public VendedorParcela FindById(long id) + { + VendedorParcelaDb vendedorParcelaDb = base.FindEntityById(id); + return ApplicationMapper.Mapper.Map(vendedorParcelaDb); + } + + public Vendedor FindByMainDocumentId(long id) + { + Vendedor vendedor; + object connection; + 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()) + { + Auxiliar.CriarVendedor(sqlCommand, false); + sqlCommand.CommandText = string.Format("SELECT TOP 1 idvendedor as id FROM vendedorparcela vp INNER JOIN parcela p on p.idparcela = vp.idparcela WHERE vp.idtipovendedor = 1 AND p.idsubtipo = 1 AND vp.iddocumento = {0};", id); + SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); + if (sqlDataReader.HasRows) + { + sqlDataReader.Read(); + long num = (sqlDataReader["id"] == null ? (long)0 : long.Parse(sqlDataReader["id"].ToString())); + sqlDataReader.Close(); + return Auxiliar.Vendedores.FirstOrDefault((Vendedor x) => { + if (x == null) + { + return false; + } + return x.Id == num; + }); + } + else + { + vendedor = null; + } + } + } + return vendedor; + } + + public List FindByParcela(long id) + { + return this.Select(string.Format(" AND vp.idparcela = {0}", id)); + } + + public List FindVendedoresByDocumentId(long documentId) + { + 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 DISTINCT idvendedor as id FROM vendedorparcela WHERE iddocumento = {0}", documentId); + using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter()) + { + sqlDataAdapter.SelectCommand = sqlCommand; + sqlDataAdapter.Fill(dataTable); + } + } + } + return Auxiliar.Vendedores.Where((Vendedor x) => { + List list = dataTable.AsEnumerable().ToList(); + Func u003cu003e9_181 = VendedorParcelaRepository.u003cu003ec.u003cu003e9__18_1; + if (u003cu003e9_181 == null) + { + u003cu003e9_181 = (DataRow v) => v.Field("id"); + VendedorParcelaRepository.u003cu003ec.u003cu003e9__18_1 = u003cu003e9_181; + } + return list.Select(u003cu003e9_181).Contains(x.Id); + }).ToList(); + } + + public List FindVinculoByIdCliente(long idcliente) + { + object connection; + SqlCommand sqlCommand; + List list; + List condicaos = new List(); + SessionFactoryImpl sessionFactory = this._unitOfWork.Session.SessionFactory as SessionFactoryImpl; + condicaos.Add(new Condicao() + { + Campo = "cl.idcliente", + Valores = idcliente.CriarValor() + }); + if (sessionFactory != null) + { + connection = sessionFactory.ConnectionProvider.GetConnection(); + } + else + { + connection = null; + } + using (SqlConnection sqlConnection = connection as SqlConnection) + { + if (sqlConnection != null) + { + sqlCommand = sqlConnection.CreateCommand(); + } + else + { + sqlCommand = null; + } + using (SqlCommand sqlCommand1 = sqlCommand) + { + Auxiliar.CriarAuxiliar(sqlCommand1, false); + string str = "SELECT DISTINCT vp.idvendedor idvendedor FROM vendedorparcela vp LEFT JOIN documento d ON vp.iddocumento = d.iddocumento LEFT JOIN controle c ON c.idcontrole = d.idcontrole LEFT JOIN cliente cl on cl.idcliente = c.idcliente WHERE"; + DataTable dataTable = sqlCommand1.Select(condicaos.CreateParameters(0), str, ""); + list = dataTable.AsEnumerable().Select((DataRow column) => column.Field("idvendedor")).ToList(); + } + } + return ( + from v in Auxiliar.Vendedores + where list.Contains(v.Id) + select v).ToList(); + } + + public bool GerarPagamento(List ids, DateTime data) + { + bool flag; + object connection; + try + { + 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) + { + SqlTransaction sqlTransaction = sqlConnection.BeginTransaction(); + using (SqlCommand sqlCommand = sqlConnection.CreateCommand()) + { + sqlCommand.CommandTimeout = 15000; + sqlCommand.Transaction = sqlTransaction; + List nums = ids; + while (nums.Count > 0) + { + List list = nums.Take(1000).ToList(); + List condicaos = new List() + { + new Condicao() + { + Campo = "idvendedorparcela", + Valores = list.CriarValor() + }, + new Condicao() + { + Campo = "datapgt", + Valores = null + } + }; + List sqlParameters = new List() + { + new SqlParameter() + { + ParameterName = "@datapgt", + SqlDbType = Funcoes.GetDbType(typeof(DateTime)), + Value = data + } + }; + if (sqlCommand.Update(condicaos.CreateParameters(0), "UPDATE vendedorparcela SET datapgt = @datapgt WHERE ", sqlParameters)) + { + nums = (list.Count < 1000 ? new List() : nums.Except(list).ToList()); + } + else + { + flag = false; + return flag; + } + } + sqlTransaction.Commit(); + sqlCommand.Dispose(); + } + } + else + { + flag = false; + return flag; + } + } + return true; + } + catch (Exception exception) + { + flag = false; + } + return flag; + } + + public VendedorParcela Merge(VendedorParcela vendedorParcela) + { + VendedorParcelaDb vendedorParcelaDb = ApplicationMapper.Mapper.Map(vendedorParcela); + base.Merge(vendedorParcelaDb); + return ApplicationMapper.Mapper.Map(vendedorParcelaDb); + } + + public VendedorParcela SaveOrUpdate(VendedorParcela vendedorParcela) + { + VendedorParcelaDb vendedorParcelaDb = ApplicationMapper.Mapper.Map(vendedorParcela); + this.SaveOrUpdate(vendedorParcelaDb); + return ApplicationMapper.Mapper.Map(vendedorParcelaDb); + } + + public List Select(string condition) + { + 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, true); + sqlCommand.CommandText = string.Concat("SELECT DISTINCT * FROM vendedorparcela vp INNER JOIN parcela p on p.idparcela = vp.idparcela WHERE 1=1 ", condition); + using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter()) + { + sqlDataAdapter.SelectCommand = sqlCommand; + sqlDataAdapter.Fill(dataTable); + } + } + } + return CustomMap.MapVendedorParcela(dataTable); + } + + public bool TemPagamentoParcela(long id) + { + return base.All().Any((VendedorParcelaDb x) => x.Parcela.Id == id && (x.DataPagamento != null) && (int?)x.Repasse.Forma == (int?)FormaRepasse.Recebimento); + } + + public bool TipoVendedorUtilizado(long id) + { + return base.All().Any((VendedorParcelaDb x) => x.TipoVendedor.Id == id); + } + } +} \ No newline at end of file -- cgit v1.2.3