From 674ca83ba9243a9e95a7568c797668dab6aee26a Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Mon, 30 Mar 2026 10:35:25 -0300 Subject: feat: upload files --- .../TarefaRepository.cs | 462 +++++++++++++++++++++ 1 file changed, 462 insertions(+) create mode 100644 Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/TarefaRepository.cs (limited to 'Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/TarefaRepository.cs') diff --git a/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/TarefaRepository.cs b/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/TarefaRepository.cs new file mode 100644 index 0000000..bfcdd72 --- /dev/null +++ b/Gestor.Infrastructure/Gestor.Infrastructure.Repository.Logic/TarefaRepository.cs @@ -0,0 +1,462 @@ +using AutoMapper; +using Gestor.Common.Validation; +using Gestor.Infrastructure.Entities.Ferramentas; +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.Tarefa; +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 TarefaRepository : GenericRepository, ITarefaRepository, IGenericRepository + { + private readonly GenericUnitOfWork _unitOfWork; + + public TarefaRepository(GenericUnitOfWork unitOfWork) : base(unitOfWork.Session) + { + this._unitOfWork = unitOfWork; + } + + public Gestor.Model.Domain.Ferramentas.Tarefa Atualizar(Gestor.Model.Domain.Ferramentas.Tarefa tarefa) + { + List list; + Gestor.Model.Domain.Ferramentas.Tarefa tarefa1 = tarefa; + List responsaveis = tarefa1.Responsaveis; + if (responsaveis != null) + { + list = ( + from x in responsaveis + where x.Usuario.Id != tarefa1.Usuario.Id + select x).ToList(); + } + else + { + list = null; + } + List responsavelTarefas = list; + TarefaDb tarefaDb = ApplicationMapper.Mapper.Map(tarefa1); + List responsavelTarefaDbs = ( + from x in this._unitOfWork.Query() + where x.IdTarefa == tarefa1.Id + select x).ToList(); + this._unitOfWork.Repository().DeleteRange(responsavelTarefaDbs); + base.Merge(tarefaDb); + tarefa1 = ApplicationMapper.Mapper.Map(tarefaDb); + if (responsavelTarefas != null) + { + responsavelTarefas.ForEach((ResponsavelTarefa x) => { + x.Id = (long)0; + x.IdTarefa = tarefa1.Id; + x = this.Save(x); + }); + } + tarefa1.Responsaveis = responsavelTarefas; + return tarefa1; + } + + public Trilha Atualizar(Trilha trilha) + { + TrilhaDb trilhaDb = ApplicationMapper.Mapper.Map(trilha); + this._unitOfWork.Repository().Merge(trilhaDb); + return ApplicationMapper.Mapper.Map(trilhaDb); + } + + public Fase Atualizar(Fase fase) + { + FaseDb faseDb = ApplicationMapper.Mapper.Map(fase); + this._unitOfWork.Repository().Merge(faseDb); + return ApplicationMapper.Mapper.Map(faseDb); + } + + public List BuscarFases(long id) + { + List list = ( + from x in this._unitOfWork.Query() + where x.Trilha.Id == id + select x).ToList(); + return ApplicationMapper.Mapper.Map, List>(list); + } + + public Gestor.Model.Domain.Ferramentas.Tarefa BuscarTarefa(long id) + { + TarefaDb tarefaDb = base.FindEntityById(id); + List list = ( + from x in this._unitOfWork.Query() + where x.IdTarefa == id + select x).ToList(); + Gestor.Model.Domain.Ferramentas.Tarefa tarefa = ApplicationMapper.Mapper.Map(tarefaDb); + tarefa.Responsaveis = ApplicationMapper.Mapper.Map, List>(list); + return tarefa; + } + + public List BuscarTarefa(TipoTarefa entidade, long id, bool? concluido = false) + { + string str; + if (!concluido.HasValue) + { + str = ""; + } + else + { + str = (concluido.GetValueOrDefault() ? " AND t.Conclusao IS NOT NULL" : " AND t.Conclusao IS NULL"); + } + string str1 = str; + return this.Select(string.Format(" AND t.Entidade = {0} AND t.IdEntidade = {1} {2}", (int)entidade, id, str1)); + } + + public List BuscarTarefaConcluidaPorUsuario(long id, TipoTarefa tipo) + { + return this.Select(string.Format(" AND t.Entidade = {0} AND t.Conclusao IS NOT NULL AND (t.IdUsuario = {1} OR r.Usuario_id = {2})", (int)tipo, id, id)); + } + + public List BuscarTarefaPorCliente(long id) + { + return this.Select(string.Format(" AND t.Entidade != 1 AND t.IdCliente = {0}", id)); + } + + public List BuscarTarefaPorData(DateTime data, long idUsuario, bool? concluido = false) + { + string str; + if (!concluido.HasValue) + { + str = ""; + } + else + { + str = (concluido.GetValueOrDefault() ? "AND t.Conclusao IS NOT NULL" : " AND t.Conclusao IS NULL"); + } + string str1 = str; + return this.Select(string.Format(" AND t.Entidade != 1 AND CAST(t.Agendamento AS DATE) == '{0:yyyy-MM-dd}' {1} AND (t.IdUsuario = {2} OR r.Usuario_id = {3})", new object[] { data, str1, idUsuario, idUsuario })); + } + + public List BuscarTarefaPorFiltro(DateTime inicio, DateTime fim) + { + return ( + from x in this.Select(string.Format(" AND t.Entidade != 1 AND CAST(t.agendamento AS DATE) BETWEEN '{0:yyyy-MM-dd}' AND '{1:yyyy-MM-dd}'", inicio, fim)) + select new Gestor.Model.Domain.Relatorios.Tarefa.Tarefa() + { + Id = x.Id, + IdCliente = x.IdCliente, + Cliente = x.Cliente, + Agendamento = x.Agendamento, + Conclusao = x.Conclusao, + Referencia = x.Referencia, + Titulo = x.Titulo, + Usuario = (x.Usuario != null ? x.Usuario.Nome : ""), + IdEntidade = x.IdEntidade, + Entidade = x.Entidade, + TipoTarefa = (x.TipoDeTarefa != null ? x.TipoDeTarefa.Nome : ""), + Status = x.Status.GetDescription() + }).ToList(); + } + + public List BuscarTarefaPorTrilha(long id) + { + return this.Select(string.Format(" AND t.IdTrilha = {0}", id)); + } + + public List BuscarTarefaPorUsuario(long id, bool? concluido = false) + { + string str; + if (!concluido.HasValue) + { + str = ""; + } + else + { + str = (concluido.GetValueOrDefault() ? " AND t.Conclusao IS NOT NULL" : " AND t.Conclusao IS NULL"); + } + string str1 = str; + return this.Select(string.Format(" AND {0} AND (t.IdUsuario = {1} OR r.Usuario_id = {2})", str1, id, id)); + } + + public List BuscarTarefaPorUsuario(long id, DateTime inicio, DateTime fim, bool? concluido) + { + string str; + fim = fim.AddDays(1); + if (!concluido.HasValue) + { + str = ""; + } + else + { + str = (concluido.GetValueOrDefault() ? " AND t.Conclusao IS NOT NULL" : " AND t.Conclusao IS NULL"); + } + string str1 = str; + string str2 = (id == 0 ? "" : string.Format(" AND (t.IdUsuario = {0} OR r.Usuario_id = {1})", id, id)); + return this.Select(string.Format(" AND t.Entidade != 1 AND t.Agendamento >= '{0:yyyy-MM-dd}' AND t.Agendamento <= '{1:yyyy-MM-dd}' {2} {3}", new object[] { inicio, fim, str1, str2 })); + } + + public List BuscarTarefaPorUsuario(long id, TipoTarefa tipo) + { + return this.Select(string.Format(" AND t.Entidade = {0} AND t.Conclusao IS NULL AND (t.IdUsuario = {1} OR r.Usuario_id = {2})", (int)tipo, id, id)); + } + + public List BuscarTarefasPorTipo(long id) + { + return this.Select(string.Format(" AND t.IdTipoTarefa = {0}", id)); + } + + public Trilha BuscarTrilha(long id) + { + TrilhaDb trilhaDb = this._unitOfWork.Query().FirstOrDefault((TrilhaDb x) => x.Id == id); + return ApplicationMapper.Mapper.Map(trilhaDb); + } + + public List BuscarTrilhas(bool ativo = true) + { + List list = ( + from x in this._unitOfWork.Query() + where x.Ativo == ativo + select x).ToList(); + return ApplicationMapper.Mapper.Map, List>(list); + } + + public List BuscarTrilhas(string titulo, bool ativo = true) + { + List list = this._unitOfWork.Query().ToList().Where((TrilhaDb x) => { + if (x.Ativo != ativo) + { + return false; + } + return x.Titulo.Normalized() == titulo.Normalized(); + }).ToList(); + return ApplicationMapper.Mapper.Map, List>(list); + } + + public int[] ContarTarefas(long id) + { + int[] numArray = new int[] { this.Count(string.Format(" AND t.Entidade = 1 AND t.Conclusao IS NULL AND (t.IdUsuario = {0} OR r.Usuario_id = {1})", id, id)), 0 }; + DateTime date = Funcoes.GetNetworkTime().Date; + numArray[1] = this.Count(string.Format(" AND t.Entidade != 1 AND t.Conclusao IS NULL AND t.Agendamento < '{0:yyyy-MM-dd}' AND (t.IdUsuario = {1} OR r.Usuario_id = {2})", date.AddDays(1), id, id)); + return numArray; + } + + private int Count(string condition) + { + int num; + 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()) + { + sqlCommand.CommandText = string.Concat("SELECT Count(DISTINCT t.Id) as quantidade FROM Tarefa t LEFT OUTER JOIN ResponsavelTarefa r on r.IdTarefa = t.id WHERE 1=1 ", condition); + SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); + sqlDataReader.Read(); + num = int.Parse(sqlDataReader["quantidade"].ToString()); + } + } + return num; + } + + public void Excluir(long id) + { + TarefaDb tarefaDb = base.FindEntityById(id); + if (tarefaDb == null) + { + return; + } + List list = ( + from x in this._unitOfWork.Query() + where x.IdTarefa == id + select x).ToList(); + this._unitOfWork.Repository().DeleteRange(list); + ( + from x in this._unitOfWork.Query() + where x.Tarefa.Id == tarefaDb.Id + select x).ToList().ForEach((ProspeccaoDb x) => { + x.Tarefa = null; + this._unitOfWork.Repository().Merge(x); + }); + base.Delete(tarefaDb); + } + + public void ExcluirFase(long id) + { + FaseDb faseDb = this._unitOfWork.Query().FirstOrDefault((FaseDb x) => x.Id == id); + if (faseDb == null) + { + return; + } + this._unitOfWork.Repository().Delete(faseDb); + } + + public void ExcluirTrilha(long id) + { + TrilhaDb trilhaDb = this._unitOfWork.Query().FirstOrDefault((TrilhaDb x) => x.Id == id); + if (trilhaDb == null) + { + return; + } + this._unitOfWork.Repository().Delete(trilhaDb); + } + + public Gestor.Model.Domain.Ferramentas.Tarefa Salvar(Gestor.Model.Domain.Ferramentas.Tarefa tarefa) + { + List list; + Gestor.Model.Domain.Ferramentas.Tarefa tarefa1 = tarefa; + List responsaveis = tarefa1.Responsaveis; + if (responsaveis != null) + { + list = ( + from x in responsaveis + where x.Usuario.Id != tarefa1.Usuario.Id + select x).ToList(); + } + else + { + list = null; + } + List responsavelTarefas = list; + TarefaDb tarefaDb = ApplicationMapper.Mapper.Map(tarefa1); + this.SaveOrUpdate(tarefaDb); + tarefa1 = ApplicationMapper.Mapper.Map(tarefaDb); + if (responsavelTarefas != null) + { + responsavelTarefas.ForEach((ResponsavelTarefa x) => { + x.IdTarefa = tarefa1.Id; + x = this.Save(x); + }); + } + tarefa1.Responsaveis = responsavelTarefas; + return tarefa1; + } + + public List Salvar(List tarefas) + { + tarefas.ForEach((Gestor.Model.Domain.Ferramentas.Tarefa x) => x = this.Salvar(x)); + return tarefas; + } + + public Trilha Salvar(Trilha trilha) + { + TrilhaDb trilhaDb = ApplicationMapper.Mapper.Map(trilha); + this._unitOfWork.Repository().SaveOrUpdate(trilhaDb); + return ApplicationMapper.Mapper.Map(trilhaDb); + } + + public Fase Salvar(Fase fase) + { + FaseDb faseDb = ApplicationMapper.Mapper.Map(fase); + this._unitOfWork.Repository().SaveOrUpdate(faseDb); + return ApplicationMapper.Mapper.Map(faseDb); + } + + public List Salvar(List fases) + { + List faseDbs = new List(); + List list = ( + from x in fases + where x.Id == (long)0 + select x).ToList(); + List list1 = fases.Except(list).ToList(); + if (list.Count > 0) + { + faseDbs = ApplicationMapper.Mapper.Map, List>(list); + this._unitOfWork.Repository().AddRange(faseDbs); + } + if (list1 != null) + { + list1.ForEach((Fase x) => { + FaseDb faseDb = ApplicationMapper.Mapper.Map(x); + this._unitOfWork.Repository().SaveOrUpdate(faseDb); + faseDbs.Add(faseDb); + }); + } + else + { + } + return ApplicationMapper.Mapper.Map, List>(faseDbs); + } + + public ResponsavelTarefa Save(ResponsavelTarefa responsavel) + { + ResponsavelTarefaDb responsavelTarefaDb = ApplicationMapper.Mapper.Map(responsavel); + this._unitOfWork.Repository().SaveOrUpdate(responsavelTarefaDb); + return ApplicationMapper.Mapper.Map(responsavelTarefaDb); + } + + private List Select(string condition) + { + object connection; + List nums = null; + DataTable dataTable = new DataTable(); + DataTable dataTable1 = 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()) + { + sqlCommand.CommandTimeout = 15000; + Auxiliar.LoadTipoTarefa(sqlCommand); + Auxiliar.CriarAuxiliar(sqlCommand, false); + sqlCommand.CommandText = string.Concat("SELECT DISTINCT t.* FROM Tarefa t LEFT OUTER JOIN ResponsavelTarefa r on r.IdTarefa = t.id WHERE 1=1 ", condition); + using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter()) + { + sqlDataAdapter.SelectCommand = sqlCommand; + sqlDataAdapter.Fill(dataTable); + } + for (List i = dataTable.AsEnumerable().Select((DataRow x) => x.Field("Id")).ToList(); i.Count > 0; i = nums) + { + List list = i.Take(2100).ToList(); + string str = string.Concat(" AND IdTarefa IN (", string.Join(",", list), ")"); + DataTable dataTable2 = new DataTable(); + sqlCommand.CommandText = string.Concat("SELECT * FROM ResponsavelTarefa WHERE 1=1 ", str); + using (SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter()) + { + sqlDataAdapter1.SelectCommand = sqlCommand; + sqlDataAdapter1.Fill(dataTable2); + } + dataTable1.Merge(dataTable2); + nums = (list.Count < 2100 ? new List() : i.Except(list).ToList()); + } + } + } + return CustomMap.MapTarefa(dataTable, dataTable1); + } + + public List Validar(List ids, long trilha) + { + return ( + from x in base.All() + where ids.Contains(x.IdEntidade) && x.Trilha != null && x.Trilha.Id == trilha + select x.Id).ToList(); + } + } +} \ No newline at end of file -- cgit v1.2.3