File manager - Edit - /var/www/html/portal/database/seeders/SmartLegacyOrgaosSeeder.php
Back
<?php namespace Database\Seeders; use Illuminate\Database\Seeder; use App\Models\Structure; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; class SmartLegacyOrgaosSeeder extends Seeder { private array $orgaoMap = []; private array $usedOrgaos = []; private array $configMapping = []; public function run(): void { $this->command->info('==========================================='); $this->command->info('Migração Inteligente de Órgãos'); $this->command->info('==========================================='); // Load config mapping $this->configMapping = config('legacy_orgaos_mapping.orgao_mapping', []); // Step 1: Identify which orgaos are actually used in projects $this->identifyUsedOrgaos(); // Step 2: Map orgaos (using config mapping or creating new ones) $this->mapOrgaos(); // Step 3: Store mapping in cache cache()->put('legacy_orgao_map', $this->orgaoMap, now()->addHours(24)); $this->showStatistics(); } private function identifyUsedOrgaos(): void { $this->command->info(''); $this->command->info('Identificando órgãos usados nos projetos...'); $csvPath = base_path('Projetos.csv'); if (!file_exists($csvPath)) { $this->command->error("Arquivo não encontrado: {$csvPath}"); return; } $handle = fopen($csvPath, 'r'); if (!$handle) { $this->command->error("Não foi possível abrir o arquivo: {$csvPath}"); return; } // Read header $header = fgetcsv($handle); if (!$header) { fclose($handle); return; } // Remove BOM if present $header[0] = preg_replace('/^\x{FEFF}/u', '', $header[0]); while (($row = fgetcsv($handle)) !== false) { if (count($row) < count($header)) { continue; } $data = array_combine($header, $row); // Collect gestor orgao if (!empty($data['pro_dimen_orgao'])) { $this->usedOrgaos[$data['pro_dimen_orgao']] = true; } // Collect orgaos envolvidos if (!empty($data['pro_dados_orgaos_envolvidos'])) { $orgaosEnvolvidos = preg_split('/[,;]/', $data['pro_dados_orgaos_envolvidos']); foreach ($orgaosEnvolvidos as $orgaoId) { $orgaoId = trim($orgaoId); if (!empty($orgaoId) && is_numeric($orgaoId)) { $this->usedOrgaos[$orgaoId] = true; } } } } fclose($handle); $this->command->info("✓ Identificados " . count($this->usedOrgaos) . " órgãos únicos usados nos projetos"); } private function mapOrgaos(): void { $this->command->info(''); $this->command->info('Mapeando órgãos...'); $csvPath = base_path('Orgaos.csv'); if (!file_exists($csvPath)) { $this->command->error("Arquivo não encontrado: {$csvPath}"); return; } $handle = fopen($csvPath, 'r'); if (!$handle) { $this->command->error("Não foi possível abrir o arquivo: {$csvPath}"); return; } // Read header $header = fgetcsv($handle); if (!$header) { fclose($handle); return; } // Remove BOM if present $header[0] = preg_replace('/^\x{FEFF}/u', '', $header[0]); $created = 0; $mapped = 0; $skipped = 0; while (($row = fgetcsv($handle)) !== false) { if (count($row) < count($header)) { continue; } $data = array_combine($header, $row); $idOrg = trim($data['id_org'] ?? ''); $sigla = trim($data['org_sigla'] ?? ''); $nome = trim($data['org_nome'] ?? ''); if (empty($idOrg) || empty($sigla) || empty($nome)) { continue; } // Skip if not used in projects if (!isset($this->usedOrgaos[$idOrg])) { $skipped++; continue; } // Check if there's a manual mapping in config if (isset($this->configMapping[$idOrg])) { $this->orgaoMap[$idOrg] = $this->configMapping[$idOrg]; $mapped++; $this->command->info("→ Mapeado (config): {$idOrg} => {$sigla}"); continue; } // Try to find existing structure by abbreviation $structure = Structure::where('abbreviation', $sigla)->first(); if ($structure) { $this->orgaoMap[$idOrg] = $structure->id; $mapped++; $this->command->info("→ Mapeado (existente): {$idOrg} => {$sigla}"); } else { // Create new structure only if used in projects $structure = Structure::create([ 'id' => Str::uuid(), 'structure_id' => null, 'type' => 'SECRETARIA', 'name' => $nome, 'abbreviation' => $sigla, 'slug' => Str::slug($sigla), 'address' => '', 'phoneNumber' => '', 'email1' => 'contato@angra.rj.gov.br', 'manager' => '', 'status' => 'ATIVADO', 'order' => (int) ($data['org_numero'] ?? 0), ]); $this->orgaoMap[$idOrg] = $structure->id; $created++; $this->command->warn("✓ Criado (necessário): {$sigla} - {$nome}"); } } fclose($handle); $this->command->info(''); $this->command->info("Criados: {$created}"); $this->command->info("Mapeados (existentes ou config): {$mapped}"); $this->command->info("Ignorados (não usados): {$skipped}"); } private function showStatistics(): void { $this->command->info(''); $this->command->info('==========================================='); $this->command->info('Estatísticas'); $this->command->info('==========================================='); $this->command->info("Órgãos usados nos projetos: " . count($this->usedOrgaos)); $this->command->info("Órgãos mapeados: " . count($this->orgaoMap)); $this->command->info("Mapeamentos do config: " . count($this->configMapping)); $this->command->info('==========================================='); } public function getOrgaoMap(): array { return $this->orgaoMap; } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.05 |
proxy
|
phpinfo
|
Settings