Ejemplo n.º 1
0
        public async Task ExecuteAsync()
        {
            for (int i = 1; i < 6; i++)
            {
                var defaultValue       = (TermoOuEgressoAlunoDefaultValues)i;
                var stringDefaultValue = TermoOuEgressoAlunoDefaultValuesAccess.GetValue(defaultValue);

                var termoOuEgressoAlunoDb = _termoOuEgressoAlunoRepository.GetByDescricao(stringDefaultValue);

                if (termoOuEgressoAlunoDb == null)
                {
                    termoOuEgressoAlunoDb = new TermoOuEgressoAluno(stringDefaultValue);

                    _termoOuEgressoAlunoRepository.Create(termoOuEgressoAlunoDb);
                    await _termoOuEgressoAlunoRepository.UnitOfWork.SaveDbChanges();
                }
            }
        }
        public async Task <IActionResult> ImportExcelAlunos([FromForm] ExcelGenericInput file)
        {
            IWorkbook excel = WorkbookFactory.Create(file.Arq.OpenReadStream());

            ISheet tabelaAlunos = excel.GetSheet("Table");

            if (tabelaAlunos == null)
            {
                throw new Exception("A planilha [Table] não foi encontrada");
            }

            // Pega o index da primeira linha (Titulos) dessa tabela
            int primeiraLinhaIndexTitulos = tabelaAlunos.FirstRowNum;

            // Pega o index da seguna linha (1° Registro) dessa tabela
            int segundaLinhaIndex = (tabelaAlunos.FirstRowNum + 1);

            // Pega o index da ultima linha (Ultimo Registro) dessa tabela
            int ultimaLinhaIndex = tabelaAlunos.LastRowNum;

            ICell numeroMatriculaCell = null;
            ICell nomeCell            = null;
            ICell dataNascimentoCell  = null;
            ICell sexoCell            = null;
            ICell emailCell           = null;
            ICell cursoCell           = null;
            ICell termoCell           = null;
            ICell dataMatriculaCell   = null;

            ICell nivelCell = null;

            List <Aluno> alunos = new List <Aluno>();

            IRow linhasTitulos = tabelaAlunos.GetRow(primeiraLinhaIndexTitulos);

            foreach (ICell cell in linhasTitulos.Cells)
            {
                switch (cell.StringCellValue)
                {
                case "Nº de Matrícula":
                    numeroMatriculaCell = cell;
                    break;

                case "Nome":
                    nomeCell = cell;
                    break;

                case "Data de Nascimento":
                    dataNascimentoCell = cell;
                    break;

                case "Sexo":
                    sexoCell = cell;
                    break;

                case "Email":
                    emailCell = cell;
                    break;

                case "Curso":
                    cursoCell = cell;
                    break;

                case "Termo":
                    termoCell = cell;
                    break;

                case "Data de Matrícula":
                    dataMatriculaCell = cell;
                    break;

                case "Nível":
                    nivelCell = cell;
                    break;

                default:
                    break;
                }
            }

            string   RMA            = "";
            string   nomeCompleto   = "";
            DateTime DataNascimento = DateTime.Now;
            bool     Sexo           = true;
            string   Email          = "";
            string   Curso          = "";
            long     Termo          = 0;
            DateTime DataMatricula  = DateTime.Now;

            string Nivel = "";

            for (int rowNum = segundaLinhaIndex; rowNum <= ultimaLinhaIndex; rowNum++)
            {
                //Recupera a linha atual
                IRow linha = tabelaAlunos.GetRow(rowNum);

                foreach (ICell cell in linha.Cells)
                {
                    if (cell.ColumnIndex == numeroMatriculaCell.ColumnIndex)
                    {
                        RMA = cell.StringCellValue;
                    }

                    if (cell.ColumnIndex == nomeCell.ColumnIndex)
                    {
                        nomeCompleto = cell.StringCellValue;
                    }

                    if (cell.ColumnIndex == dataNascimentoCell.ColumnIndex)
                    {
                        DataNascimento = Convert.ToDateTime(cell.StringCellValue);
                    }

                    if (cell.ColumnIndex == sexoCell.ColumnIndex)
                    {
                        Sexo = (cell.StringCellValue == "M" ? true : false);
                    }

                    if (cell.ColumnIndex == emailCell.ColumnIndex)
                    {
                        Email = cell.StringCellValue.ToLower();
                    }

                    if (cell.ColumnIndex == cursoCell.ColumnIndex)
                    {
                        Curso = cell.StringCellValue;
                    }

                    if (cell.ColumnIndex == termoCell.ColumnIndex)
                    {
                        Termo = Convert.ToInt32(cell.NumericCellValue);
                    }

                    if (cell.ColumnIndex == nivelCell.ColumnIndex)
                    {
                        Nivel = cell.StringCellValue;
                    }

                    // Não é possível adquirir data de matrícula, pois o dado no excel foi salvo como "numérico" e não como texto

                    //if (cell.ColumnIndex == dataMatriculaCell.ColumnIndex)
                    //    DataMatricula = Convert.ToDateTime(cell.StringCellValue);
                }

                // Busca aluno por RMA
                Aluno alunoDb = _alunoRepository.BuscarPorEmailRMANome(RMA, Email, nomeCompleto);

                // Caso aluno NÃO existir, cria um novo
                if (alunoDb == null && (Nivel == "FIC" || Nivel == "Nível Técnico"))
                {
                    try
                    {
                        if (alunos.FirstOrDefault(x => x.RMA == RMA) == null &&
                            alunos.FirstOrDefault(x => x.Email == Email) == null &&
                            alunos.FirstOrDefault(x => x.NomeCompleto.ToLower() == nomeCompleto.ToLower()) == null)
                        {
                            var tipoCursoDb = _tipoCursoRepository.GetByDescricao(Curso);

                            if (tipoCursoDb == null)
                            {
                                return(StatusCode(400, $"Não foi encontrado um TipoCurso de valor [{Curso}]"));
                            }

                            var    termoDefaultValue = (TermoOuEgressoAlunoDefaultValues)Termo;
                            string termoString       = TermoOuEgressoAlunoDefaultValuesAccess.GetValue(termoDefaultValue);

                            var TermoOuEgressoAlunoDb = _termoOuEgressoAlunoRepository.GetByDescricao(termoString);

                            if (TermoOuEgressoAlunoDb == null)
                            {
                                return(StatusCode(400, $"Não foi encontrado um TermoOuEgressoAluno com o valor [{termoString}]."));
                            }

                            // Cria a entidade Aluno
                            alunoDb = new Aluno(nomeCompleto, Email, RMA, Sexo, DataNascimento, DataMatricula, TermoOuEgressoAlunoDb.Id, tipoCursoDb.Id);

                            alunos.Add(alunoDb);
                        }
                    }
                    catch (Exception)
                    {
                        return(StatusCode(500, "Houve um erro na criação do objeto Aluno ou TipoCurso"));
                    }
                }
            }

            // Cria todos os novos alunos de uma vez
            var alunosCriadosDb = _alunoRepository.CreateRange(alunos);

            // Salva alterações na DB
            await _alunoRepository.UnitOfWork.SaveDbChanges();

            // Retorna os alunos criados
            return(StatusCode(200, new
            {
                AlunosCriados = alunosCriadosDb,
            }));
        }