Пример #1
0
        private int BuscarMunicipio(string NomeMunicipio)
        {
            var municipio = municipioRepositorio.ObterMunicipio(NomeMunicipio);

            if (municipio == null)
            {
                var municipioInserido = municipioRepositorio.InserirMunicipio(NomeMunicipio);

                if (municipioInserido == null)
                {
                    throw new Exception("Ocorreu um erro ao salvar um novo município!");
                }
                else
                {
                    municipio = municipioRepositorio.ObterMunicipio(NomeMunicipio);
                }
            }

            return(municipio.Codigo);
        }
Пример #2
0
        public void PopularBancoComDadosAnp(string caminhoExcelAnp)
        {
            string file = string.Format("{0}/{1}", Constantes.CAMINHO_DOWNLOAD_ARQUIVO, caminhoExcelAnp);

            FileStream       stream = File.Open(file, FileMode.Open, FileAccess.Read);
            IExcelDataReader excelReader;

            if (file.ToLower().EndsWith("xls"))
            {
                //1. Reading from a binary Excel file ('97-2003 format; *.xls)
                excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
            }
            else
            {
                //2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
                excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            }

            DataSet   result             = excelReader.AsDataSet();
            DataTable tabelas            = result.Tables[0];
            int       indiceLinhaInicial = 0;

            foreach (DataRow linha in tabelas.Rows)
            {
                if (linha?.ItemArray?.GetValue(0)?.ToString() == "MÊS" && linha?.ItemArray?.GetValue(1)?.ToString() == "PRODUTO")
                {
                    break;
                }
                indiceLinhaInicial++;
            }

            for (int a = 0; a < indiceLinhaInicial; a++)
            {
                var dataRow = tabelas.Rows[a];
                dataRow.Delete();
            }

            tabelas.AcceptChanges();

            int contador = 0;

            if (tabelas.Rows[0]?.ItemArray?.GetValue(0)?.ToString() != "MÊS" && tabelas.Rows[0]?.ItemArray?.GetValue(1)?.ToString() != "PRODUTO")
            {
                throw new Exception("Erro ao ler planilha da ANP");
            }


            foreach (DataColumn coluna in tabelas.Columns)
            {
                if (!string.IsNullOrWhiteSpace(tabelas.Rows[0]?.ItemArray?.GetValue(contador)?.ToString()))
                {
                    coluna.ColumnName = tabelas.Rows[0]?.ItemArray?.GetValue(contador)?.ToString();
                    tabelas.AcceptChanges();
                }
                contador++;
            }

            tabelas.Rows[0].Delete();
            tabelas.AcceptChanges();

            DataView tabelaInsercao = new DataView(tabelas)
            {
                RowFilter = " REGIÃO = 'SUDESTE' AND ESTADO = 'MINAS GERAIS' AND PRODUTO NOT IN ('GLP','GNV')"
            };

            tabelas = tabelaInsercao.ToTable();

            DataTable municipiosDistintos = tabelaInsercao.ToTable(true, "MUNICÍPIO");

            foreach (DataRow municipio in municipiosDistintos.Rows)
            {
                string nomeMunicipio = municipio.ItemArray[0].ToString();

                var municipioRegistrado = municipioRepositorio.ObterMunicipio(nomeMunicipio);
                if (municipioRegistrado == null)
                {
                    municipioRepositorio.InserirMunicipio(nomeMunicipio);
                }
            }

            uploadAnpRepositorio.InserirNovaData();
            var idUploadAnp = uploadAnpRepositorio.ObterUltimoUpload().Id;

            tabelaAnpRepositorio.DeletarTodosRegistros();

            var  colunasInsert = new List <DataRow>();
            bool ultimaIteracao;

            for (int cont = 0; cont < tabelas.Rows.Count; cont++)
            {
                ultimaIteracao = cont == tabelas.Rows.Count - 1;

                if (colunasInsert.Count < 100 && !ultimaIteracao)
                {
                    if (ValidarLinha(tabelas, cont))
                    {
                        colunasInsert.Add(tabelas.Rows[cont]);
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    try
                    {
                        tabelaAnpRepositorio.InserirLoteAnp(colunasInsert, idUploadAnp);
                    }
#pragma warning disable CS0168 // A variável "ex" está declarada, mas nunca é usada
                    catch (Exception ex) { }
#pragma warning restore CS0168 // A variável "ex" está declarada, mas nunca é usada
                    finally
                    {
                        colunasInsert.Clear();
                    }
                }
            }

            stream.Close();
        }