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 --- .../UsuarioRepository.cs | 305 +++++++++++++++++++++ 1 file changed, 305 insertions(+) create mode 100644 Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/UsuarioRepository.cs (limited to 'Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/UsuarioRepository.cs') diff --git a/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/UsuarioRepository.cs b/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/UsuarioRepository.cs new file mode 100644 index 0000000..f1f9993 --- /dev/null +++ b/Codemerx/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/UsuarioRepository.cs @@ -0,0 +1,305 @@ +using AutoMapper; +using Gestor.Infrastructure.Entities.Generic; +using Gestor.Infrastructure.Entities.Seguros; +using Gestor.Infrastructure.Mappers; +using Gestor.Infrastructure.Repository.Generic; +using Gestor.Infrastructure.Repository.Interface; +using Gestor.Infrastructure.UnitOfWork.Generic; +using Gestor.Model.API; +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.Reflection; +using System.Runtime.CompilerServices; + +namespace Gestor.Infrastructure.Repository.Logic +{ + public class UsuarioRepository : GenericRepository, IUsuarioRepository, IGenericRepository + { + private readonly GenericUnitOfWork _unitOfWork; + + public UsuarioRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session) + { + this._unitOfWork = unitOfWork; + } + + public Usuario AddUsuarioFromSso(UserSso userSso) + { + return this.SaveOrUpdate(new Usuario() + { + SsoId = userSso.Id, + IdEmpresa = userSso.IdEmpresa, + Nome = userSso.Name, + Login = userSso.Username, + Senha = userSso.Password, + Administrador = false, + Excluido = userSso.IsDeleted, + PermissaoAggilizador = new long?((long)1) + }); + } + + public void Delete(long id) + { + UsuarioDb nullable = base.FindEntityById(id); + if (nullable.Login.IndexOf("_REM", StringComparison.InvariantCultureIgnoreCase) == -1) + { + UsuarioDb usuarioDb = nullable; + usuarioDb.Login = string.Concat(usuarioDb.Login, "_REM"); + } + nullable.Excluido = new bool?(true); + base.Merge(nullable); + } + + public List Find(string filter, long idempresa = 0L) + { + UsuarioRepository.u003cu003ec__DisplayClass11_0 variable = null; + ParameterExpression parameterExpression; + bool? nullable; + IQueryable usuarioDbs; + if (idempresa == 0) + { + IQueryable usuarioDbs1 = base.All(); + parameterExpression = Expression.Parameter(typeof(UsuarioDb), "x"); + nullable = null; + usuarioDbs = usuarioDbs1.Where(Expression.Lambda>(Expression.OrElse(Expression.AndAlso(Expression.AndAlso(Expression.NotEqual(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Excluido").MethodHandle)), Expression.Constant(nullable, typeof(bool?))), Expression.Equal(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Excluido").MethodHandle)), Expression.Convert(Expression.Constant(false, typeof(bool)), typeof(bool?)))), Expression.Call(Expression.Call(Expression.Call(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Nome").MethodHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("ToUpper").MethodHandle), Array.Empty()), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Trim").MethodHandle), Array.Empty()), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Contains", new Type[] { typeof(string) }).MethodHandle), new Expression[] { Expression.Field(Expression.Constant(variable, typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0)), FieldInfo.GetFieldFromHandle(typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0).GetField("filter").FieldHandle)) })), Expression.Call(Expression.Call(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Documento").MethodHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Trim").MethodHandle), Array.Empty()), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Contains", new Type[] { typeof(string) }).MethodHandle), new Expression[] { Expression.Field(Expression.Constant(variable, typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0)), FieldInfo.GetFieldFromHandle(typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0).GetField("filter").FieldHandle)) })), new ParameterExpression[] { parameterExpression })); + } + else + { + IQueryable usuarioDbs2 = base.All(); + parameterExpression = Expression.Parameter(typeof(UsuarioDb), "x"); + nullable = null; + usuarioDbs = usuarioDbs2.Where(Expression.Lambda>(Expression.AndAlso(Expression.AndAlso(Expression.AndAlso(Expression.NotEqual(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Excluido").MethodHandle)), Expression.Constant(nullable, typeof(bool?))), Expression.Equal(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Excluido").MethodHandle)), Expression.Convert(Expression.Constant(false, typeof(bool)), typeof(bool?)))), Expression.Equal(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_IdEmpresa").MethodHandle)), Expression.Field(Expression.Constant(variable, typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0)), FieldInfo.GetFieldFromHandle(typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0).GetField("idempresa").FieldHandle)))), Expression.OrElse(Expression.Call(Expression.Call(Expression.Call(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Nome").MethodHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("ToUpper").MethodHandle), Array.Empty()), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Trim").MethodHandle), Array.Empty()), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Contains", new Type[] { typeof(string) }).MethodHandle), new Expression[] { Expression.Field(Expression.Constant(variable, typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0)), FieldInfo.GetFieldFromHandle(typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0).GetField("filter").FieldHandle)) }), Expression.Call(Expression.Call(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Documento").MethodHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Trim").MethodHandle), Array.Empty()), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Contains", new Type[] { typeof(string) }).MethodHandle), new Expression[] { Expression.Field(Expression.Constant(variable, typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0)), FieldInfo.GetFieldFromHandle(typeof(UsuarioRepository.u003cu003ec__DisplayClass11_0).GetField("filter").FieldHandle)) }))), new ParameterExpression[] { parameterExpression })); + } + parameterExpression = Expression.Parameter(typeof(UsuarioDb), "x"); + return usuarioDbs.Select(Expression.Lambda>(Expression.MemberInit(Expression.New(typeof(Usuario)), new MemberBinding[] { Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(Usuario).GetMethod("set_IdEmpresa", new Type[] { typeof(long) }).MethodHandle), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_IdEmpresa").MethodHandle))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(DomainBase).GetMethod("set_Id", new Type[] { typeof(long) }).MethodHandle), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(EntityBase).GetMethod("get_Id").MethodHandle))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(Usuario).GetMethod("set_Nome", new Type[] { typeof(string) }).MethodHandle), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Nome").MethodHandle))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(Usuario).GetMethod("set_Documento", new Type[] { typeof(string) }).MethodHandle), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Documento").MethodHandle))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(Usuario).GetMethod("set_Excluido", new Type[] { typeof(bool) }).MethodHandle), Expression.Coalesce(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Excluido").MethodHandle)), Expression.Constant(false, typeof(bool)))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(Usuario).GetMethod("set_PermissaoAggilizador", new Type[] { typeof(long?) }).MethodHandle), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_PermissaoAggilizador").MethodHandle))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(Usuario).GetMethod("set_Administrador", new Type[] { typeof(bool) }).MethodHandle), Expression.Coalesce(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Administrador").MethodHandle)), Expression.Constant(false, typeof(bool)))), Expression.Bind((MethodInfo)MethodBase.GetMethodFromHandle(typeof(Usuario).GetMethod("set_Visita", new Type[] { typeof(string) }).MethodHandle), Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Visita").MethodHandle))) }), new ParameterExpression[] { parameterExpression })).ToList(); + } + + public List Find(long idempresa = 0L) + { + List usuarios; + object connection; + DataTable dataTable = new DataTable(); + SessionFactoryImpl sessionFactory = this._unitOfWork.Session.SessionFactory as SessionFactoryImpl; + string str = (idempresa == 0 ? "SELECT * FROM usuario" : "SELECT * FROM usuario WHERE idempresa = @idempresa"); + try + { + 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 = str; + sqlCommand.Parameters.AddWithValue("@idempresa", idempresa); + using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter()) + { + sqlDataAdapter.SelectCommand = sqlCommand; + sqlDataAdapter.Fill(dataTable); + } + } + } + else + { + usuarios = null; + return usuarios; + } + } + usuarios = CustomMap.MapUsuario(dataTable); + } + catch (Exception exception) + { + usuarios = null; + } + return usuarios; + } + + public Usuario FindById(long id) + { + UsuarioDb usuarioDb = base.FindEntityById(id); + if (usuarioDb == null) + { + return null; + } + return ApplicationMapper.Mapper.Map(usuarioDb); + } + + public List FindByLoginInteiro(string filter) + { + return ( + from x in ( + from x in base.All() + where x.Excluido != null && x.Excluido == (bool?)false && x.Login.Equals(filter) + select x).ToList() + select new Usuario() + { + IdEmpresa = x.IdEmpresa, + Id = x.Id, + Nome = x.Nome, + Documento = x.Documento, + Excluido = x.Excluido.GetValueOrDefault(), + PermissaoAggilizador = x.PermissaoAggilizador, + Administrador = x.Administrador.GetValueOrDefault(), + Visita = x.Visita + } into x + where !x.Excluido + select x).ToList(); + } + + private Usuario FindBySso(string ssoId) + { + UsuarioDb usuarioDb = base.All().FirstOrDefault((UsuarioDb u) => u.SsoId == ssoId); + if (usuarioDb == null) + { + return null; + } + return ApplicationMapper.Mapper.Map(usuarioDb); + } + + public bool FindDocUsedByDocumento(string filter, long id, long idempresa) + { + return base.All().Any((UsuarioDb u) => u.IdEmpresa == idempresa && u.Excluido != null && u.Excluido == (bool?)false && u.Id != id && u.Documento != null && u.Documento.Replace(".", string.Empty).Replace("-", string.Empty).Replace("/", string.Empty).Equals(filter)); + } + + public Usuario FindFromSso(UserSso userSso) + { + UsuarioRepository.u003cu003ec__DisplayClass4_0 variable = null; + if (userSso.IsDeleted) + { + return null; + } + IQueryable usuarioDbs = base.All(); + ParameterExpression parameterExpression = Expression.Parameter(typeof(UsuarioDb), "u"); + UsuarioDb usuarioDb = usuarioDbs.FirstOrDefault(Expression.Lambda>(Expression.Call(Expression.Call(Expression.Property(parameterExpression, (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UsuarioDb).GetMethod("get_Login").MethodHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("ToUpper").MethodHandle), Array.Empty()), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("Equals", new Type[] { typeof(string) }).MethodHandle), new Expression[] { Expression.Call(Expression.Property(Expression.Field(Expression.Constant(variable, typeof(UsuarioRepository.u003cu003ec__DisplayClass4_0)), FieldInfo.GetFieldFromHandle(typeof(UsuarioRepository.u003cu003ec__DisplayClass4_0).GetField("userSso").FieldHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(UserSso).GetMethod("get_Username").MethodHandle)), (MethodInfo)MethodBase.GetMethodFromHandle(typeof(string).GetMethod("ToUpper").MethodHandle), Array.Empty()) }), new ParameterExpression[] { parameterExpression })); + if (usuarioDb == null) + { + return null; + } + return ApplicationMapper.Mapper.Map(usuarioDb); + } + + public Usuario FindUsuario(string login) + { + UsuarioDb usuarioDb = ( + from x in base.All() + where x.Excluido != null && x.Excluido == (bool?)false + select x).FirstOrDefault((UsuarioDb x) => x.Login == login); + return ApplicationMapper.Mapper.Map(usuarioDb); + } + + public Usuario Merge(Usuario usuario) + { + UsuarioDb usuarioDb = ApplicationMapper.Mapper.Map(usuario); + base.Merge(usuarioDb); + return ApplicationMapper.Mapper.Map(usuarioDb); + } + + public List PermissaoAggilizador() + { + List list; + object connection; + SessionFactoryImpl sessionFactory = this._unitOfWork.Session.SessionFactory as SessionFactoryImpl; + DataTable dataTable = new DataTable(); + try + { + 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 Permissao where IdPermissao != 4"; + using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter()) + { + sqlDataAdapter.SelectCommand = sqlCommand; + sqlDataAdapter.Fill(dataTable); + } + } + } + } + list = ( + from x in dataTable.AsEnumerable().ToList() + select new PermissaoAggilizador() + { + Id = x.Field("IdPermissao"), + Descricao = x.Field("Nome").ToUpper() + }).ToList(); + } + catch (Exception exception) + { + list = new List() + { + new PermissaoAggilizador() + { + Id = (long)1, + Descricao = "ADMINISTRADOR" + }, + new PermissaoAggilizador() + { + Id = (long)2, + Descricao = "NORMAL" + }, + new PermissaoAggilizador() + { + Id = (long)3, + Descricao = "LIMITADO" + } + }; + } + return list; + } + + public Usuario SaveOrUpdate(Usuario usuario) + { + UsuarioDb usuarioDb = ApplicationMapper.Mapper.Map(usuario); + this.SaveOrUpdate(usuarioDb); + return ApplicationMapper.Mapper.Map(usuarioDb); + } + + public Usuario SsoId(AuthSso sso) + { + if (sso.Code != 200) + { + return null; + } + return this.FindBySso(sso.Data.User.Id); + } + + public Usuario ValidateLogin(string login, string password) + { + UsuarioDb usuarioDb = this._unitOfWork.Session.CreateQuery("FROM UsuarioDb WHERE (removido IS NULL OR removido != '1') AND (inativo = '0' OR inativo IS NULL)").List().FirstOrDefault((UsuarioDb u) => { + if (!string.Equals(u.Login.Trim(), login.Trim(), StringComparison.CurrentCultureIgnoreCase)) + { + return false; + } + return u.Senha == password; + }); + return ApplicationMapper.Mapper.Map(usuarioDb); + } + } +} \ No newline at end of file -- cgit v1.2.3