File manager - Edit - /var/www/html/portalHomolog/app/Console/Commands/ImportLicitacoesProcedimentosCommand.php
Back
<?php namespace App\Console\Commands; use App\Http\Services\LicitacaoProcedimentoImportReport; use App\Http\Services\LicitacaoProcedimentoImportService; use Illuminate\Console\Command; class ImportLicitacoesProcedimentosCommand extends Command { /** * @var string */ protected $signature = 'licitacoes:import {--only= : Importar apenas "licitacoes" (registros principais) ou "uploads"}'; /** * @var string */ protected $description = 'Importa licitacoes e procedimentos a partir dos arquivos CSV legados.'; public function handle(LicitacaoProcedimentoImportService $service): int { $only = $this->option('only'); $storagePath = storage_path('app' . DIRECTORY_SEPARATOR . 'importacoes_licitacoes_procedimentos'); $legacyPath = base_path('temp' . DIRECTORY_SEPARATOR . 'importacoes_licitacoes_procedimentos'); if ($only && !in_array($only, ['licitacoes', 'uploads'], true)) { $this->error('Valor invalido para --only. Use "licitacoes" ou "uploads".'); return self::FAILURE; } $basePath = $this->resolveBasePath($storagePath, $legacyPath, $only); if ($basePath === null) { $this->error('Arquivos CSV nao encontrados. Verifique os diretorios:'); $this->line(" - {$storagePath}"); $this->line(" - {$legacyPath}"); return self::FAILURE; } if ($basePath === $legacyPath) { $this->warn('Usando diretorio legado em /temp. Mova os CSVs para storage/app/importacoes_licitacoes_procedimentos.'); } $this->info('Limpando dados importados anteriormente (reimportacao idempotente)...'); $removed = $service->cleanImportedData($only); $this->line(" Removidos: {$removed['uploads']} upload(s), {$removed['processos']} processo(s)."); $totalLines = $this->countLines($basePath, $only); $bar = $this->output->createProgressBar($totalLines); $bar->setFormat(' %current%/%max% [%bar%] %percent:3s%% — %elapsed:6s%'); $bar->start(); $service->setProgressCallback(fn () => $bar->advance()); $inicio = microtime(true); try { $results = match ($only) { 'licitacoes' => $service->importOnlyLicitacoes($basePath), 'uploads' => $service->importOnlyUploads($basePath), default => $service->importAll($basePath), }; $bar->finish(); $this->newLine(2); $duracao = microtime(true) - $inicio; $relatorio = LicitacaoProcedimentoImportReport::persist( $results, $service->getIssues(), $basePath, $duracao, $only, ); $this->info('Importacao concluida com sucesso!'); $this->newLine(); $this->printSummaryTable($results); $this->printReportSummary($service->getIssues(), $relatorio); return self::SUCCESS; } catch (\Throwable $e) { $bar->finish(); $this->newLine(2); $this->error('Importacao falhou: ' . $e->getMessage()); return self::FAILURE; } } /** * @param array<string, array{imported: int, skipped: int, errors: int}> $results */ private function printSummaryTable(array $results): void { $rows = []; foreach ($results as $key => $stats) { $rows[] = [$key, $stats['imported'], $stats['skipped'], $stats['errors']]; } $this->table( ['Etapa', 'Importados', 'Ignorados', 'Erros'], $rows, ); } /** * @param array<int, array<string, mixed>> $issues * @param array{json: string, md: string, timestamp: string, resumo_console?: string[]} $relatorio */ private function printReportSummary(array $issues, array $relatorio): void { $this->newLine(); $this->line(str_repeat('─', 60)); foreach ($relatorio['resumo_console'] ?? [] as $linha) { if (str_contains($linha, 'ressalvas') || str_contains($linha, 'erro')) { $this->warn($linha); } else { $this->info($linha); } } $this->line(str_repeat('─', 60)); $this->info('Relatório completo (MD): ' . $relatorio['md']); $this->info('Relatório completo (JSON): ' . $relatorio['json']); } /** * @return string[] */ private function getRequiredCsvFileNames(?string $only): array { $files = []; if ($only !== 'uploads') { $files[] = 'TB_PMAR_LICITACAO-29052026.csv'; $files[] = 'TB_PMAR_PROCEDIMENTOS-29052026.csv'; } if ($only !== 'licitacoes') { $files[] = 'TB_PMAR_LICITACAO-UPLOAD-29052026.csv'; $files[] = 'TB_PMAR_PROCEDIMENTO_UPLOAD-29052026.csv'; } return $files; } private function resolveBasePath(string $storagePath, string $legacyPath, ?string $only): ?string { if ($this->hasRequiredCsvFiles($storagePath, $only)) { return $storagePath; } if ($this->hasRequiredCsvFiles($legacyPath, $only)) { return $legacyPath; } return null; } private function hasRequiredCsvFiles(string $basePath, ?string $only): bool { if (!is_dir($basePath)) { return false; } $ds = DIRECTORY_SEPARATOR; foreach ($this->getRequiredCsvFileNames($only) as $fileName) { if (!is_readable($basePath . $ds . $fileName)) { return false; } } return true; } private function countLines(string $basePath, ?string $only): int { $service = app(LicitacaoProcedimentoImportService::class); $ds = DIRECTORY_SEPARATOR; $total = 0; foreach ($this->getRequiredCsvFileNames($only) as $fileName) { $total += $service->countCsvLines($basePath . $ds . $fileName); } return $total; } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings