File manager - Edit - /var/www/html/transparencia/database/seeders/ImportRelatorioPedidosAcessoInformacaoSeeder.php
Back
<?php namespace Database\Seeders; use Carbon\Carbon; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; class ImportRelatorioPedidosAcessoInformacaoSeeder extends Seeder { private const CSV_PATH = 'database/seeders/data/relatorio_pedidos_acesso_informacao.csv'; public function run(): void { $fullPath = base_path(self::CSV_PATH); if (!is_file($fullPath)) { $this->command?->error('Arquivo CSV não encontrado: ' . self::CSV_PATH); return; } DB::table('relatorio_pedidos_acesso_informacao')->truncate(); $handle = fopen($fullPath, 'r'); if ($handle === false) { $this->command?->error('Não foi possível abrir o CSV.'); return; } $header = fgetcsv($handle); if ($header === false) { fclose($handle); return; } $payload = []; $now = now(); while (($row = fgetcsv($handle)) !== false) { if ($this->isRowEmpty($row)) { continue; } $payload[] = [ 'id' => (string) Str::uuid(), 'situacao' => $this->trim($row[0] ?? null), 'nup' => $this->trim($row[1] ?? null), 'dataDeCadastro' => $this->parseDate($row[2] ?? null), 'prazoDeAtendimento' => $this->parseDate($row[3] ?? null), 'orgao' => $this->trim($row[4] ?? null), 'subtipoFormulario' => $this->trim($row[5] ?? null), 'usuario' => $this->trim($row[6] ?? null), 'quantidade' => $this->parseInteger($row[7] ?? null), 'situacaoReclamacao' => $this->trim($row[8] ?? null), 'dataResposta' => $this->parseDate($row[9] ?? null), 'especificacaoDecisao' => $this->trim($row[10] ?? null), 'responsavelResposta' => $this->trim($row[11] ?? null), 'destinatarioRecurso' => $this->trim($row[12] ?? null), 'prazoRecurso' => $this->parseDate($row[13] ?? null), 'anexoQtd' => $this->parseInteger($row[14] ?? null), 'situacaoRecurso' => $this->trim($row[15] ?? null), 'pessoa' => $this->trim($row[16] ?? null), 'escolaridade' => $this->trim($row[17] ?? null), 'profissao' => $this->trim($row[18] ?? null), 'genero' => $this->trim($row[19] ?? null), 'municipio' => $this->trim($row[20] ?? null), 'estado' => $this->trim($row[21] ?? null), 'created_at' => $now, 'updated_at' => $now, ]; } fclose($handle); foreach (array_chunk($payload, 100) as $chunk) { DB::table('relatorio_pedidos_acesso_informacao')->insert($chunk); } $this->command?->info(count($payload) . ' pedidos de acesso à informação importados.'); } private function isRowEmpty(array $row): bool { foreach ($row as $value) { if ($this->trim($value) !== null) { return false; } } return true; } private function trim(mixed $value): ?string { if ($value === null) { return null; } $text = trim((string) $value); return $text === '' ? null : $text; } private function parseDate(mixed $value): ?string { $text = $this->trim($value); if ($text === null) { return null; } try { return Carbon::createFromFormat('d/m/Y', $text)->format('Y-m-d'); } catch (\Throwable) { return null; } } private function parseInteger(mixed $value): ?int { $text = $this->trim($value); if ($text === null || !is_numeric($text)) { return null; } return (int) $text; } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.01 |
proxy
|
phpinfo
|
Settings