Пример #1
0
        public void ImportacaoGestores(DataSet ds)
        {
            int linha = 2;

            try
            {
                GestoresService service = new GestoresService();
                foreach (DataRow r in ds.Tables[0].Rows)
                {
                    //Condição de parada
                    if (r[0] == null || r[0].ToString() == "")
                    {
                        return;
                    }

                    Gestor g = new Gestor
                    {
                        Nome  = r[0].ToString(),
                        Email = r[1].ToString()
                    };

                    Gestor atual = service.Get(g.Nome);

                    if (atual != null)
                    {
                        g.Codigo = atual.Codigo;
                    }

                    service.Save(g);
                    linha++;
                }
            }
            catch (Exception e)
            {
                throw new ExcelException(e.Message, ds.DataSetName, linha);
            }
        }
Пример #2
0
        public List <InconsistenciaFuncionarioDTO> ImportacaoFuncionarios(DataSet ds, int codEleicao)
        {
            int l = 2;
            FuncionariosService funcService     = new FuncionariosService();
            UsuariosService     userService     = new UsuariosService();
            EleicoesService     eleicoesService = new EleicoesService();
            GestoresService     gestoresService = new GestoresService();
            //List<InconsistenciaFuncionarioDTO> inconsistencias = new List<InconsistenciaFuncionarioDTO>(); //Guarda os funcionários com a mesma matrícula com Login's diferentes
            HashSet <string> notRequiredFields = new HashSet <string> {
                "Email", "Nome do Gestor"
            };

            try
            {
                Eleicao eleicao = eleicoesService.GetEleicao(codEleicao);
                foreach (DataRow r in ds.Tables[0].Rows)
                {
                    //Condição de parada
                    if (r[1] == null || r[1].ToString() == "")
                    {
                        return(null);
                    }

                    //Validações
                    foreach (DataColumn c in ds.Tables[0].Columns)
                    {
                        if (!notRequiredFields.Contains(c.ColumnName))
                        {
                            if (r[c.ColumnName] == null || r[c.ColumnName].ToString() == "")
                            {
                                throw new ResourceNotFoundException(ds.DataSetName, l, c.ColumnName);
                            }
                        }
                    }

                    if (!DateTime.TryParse(r[5].ToString(), out DateTime dataAdmissao))
                    {
                        throw new FormatoDataInvalidoException(ds.DataSetName, l, r[5].ToString());
                    }

                    if (!DateTime.TryParse(r[6].ToString(), out DateTime dataNascimento))
                    {
                        throw new FormatoDataInvalidoException(ds.DataSetName, l, r[6].ToString());
                    }

                    Gestor gestor = null;
                    if (r[8].ToString() == "")
                    {
                        gestor = null;
                    }
                    else
                    {
                        gestor = gestoresService.Get(r[8].ToString());
                        if (gestor == null)
                        {
                            throw new GestorNaoEncontradoException(r[8].ToString());
                        }
                    }

                    string      login     = r[1].ToString().Trim().ToLower();
                    string      matricula = r[0].ToString();
                    Funcionario func      = new Funcionario
                    {
                        MatriculaFuncionario = matricula,
                        Login          = login,
                        Nome           = r[2].ToString(),
                        Cargo          = r[3].ToString(),
                        Area           = r[4].ToString(),
                        DataAdmissao   = dataAdmissao,
                        DataNascimento = dataNascimento,
                        CodigoGestor   = gestor?.Codigo,
                        Email          = r[7].ToString(),
                        CodigoEmpresa  = eleicao.Unidade.CodigoEmpresa
                    };

                    Funcionario funcMatricula = null;
                    try
                    {
                        funcMatricula = funcService.GetFuncionario(matricula, eleicao.Unidade.CodigoEmpresa);

                        if (funcMatricula.Login != login)
                        {
                            throw new Exception($"O funcionario {func.Nome} (matrícula {matricula}) já está cadastrado com o login {funcMatricula.Login}. Altere a planilha ou altere o registro atual em \"Base Geral\".");
                        }
                    }
                    catch (FuncionarioNaoEncontradoException) { }

                    Funcionario atual = funcService.GetByLogin(login);

                    if (atual == null || atual.MatriculaFuncionario == matricula)
                    {
                        if (atual != null)
                        {
                            func.Id = atual.Id;
                        }
                        funcService.AddOrUpdateFuncionario(func);
                    }
                    else
                    {
                        throw new Exception($"Já existe um funcionário cadastrado com o login {login}! Matrícula: {atual.MatriculaFuncionario}.");
                    }

                    Usuario usuario = null;
                    try
                    {
                        usuario = userService.GetUsuario(login);
                        usuario.FuncionarioId = atual.Id;
                        usuario.Nome          = func.Nome;
                        userService.AddOrUpdateUsuario(usuario);
                    }
                    catch (UsuarioNaoEncontradoException) { }
                    eleicoesService.AddFuncionario(codEleicao, func.Id);

                    l++;
                }
            }
            catch (ResourceNotFoundException e)
            {
                throw e;
            }
            catch (FormatoDataInvalidoException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw new ExcelException(e.Message, ds.DataSetName, l);
            }

            return(null);
        }