Files
verifikator/app/Console/Commands/Pegawai/UpdateNoHpPimpinanCommand.php
T
2026-09-28 15:17:03 +07:00

101 lines
2.8 KiB
PHP

<?php
namespace App\Console\Commands\Pegawai;
use App\Models\Verifikator\Dosen;
use App\Models\Verifikator\Tendik;
use App\Services\SiskaService;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class UpdateNoHpPimpinanCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'pegawai:update-no-hp-pimpinan';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Update nomor HP dosen dan tendik dari SISKA API (api/v2/user/pimpinan)';
/**
* Execute the console command.
*/
public function handle(SiskaService $siskaService)
{
$this->info('Mengambil data pimpinan dari SISKA...');
$pimpinan = $siskaService->daftarPimpinan();
if ($pimpinan === null) {
$this->error('Gagal mengambil data dari SISKA');
return Command::FAILURE;
}
$this->info('Total data pimpinan: '.count($pimpinan));
// NIP bisa muncul berkali-kali dengan no_hp berbeda, ambil yang terakhir
$noHpByNip = [];
foreach ($pimpinan as $item) {
$nip = $item['nip'] ?? null;
$noHp = $item['no_hp'] ?? null;
if (! $nip || ! $noHp) {
continue;
}
$noHpByNip[$nip] = $noHp;
}
$this->info('Total NIP unik: '.count($noHpByNip));
$this->newLine();
$updatedDosen = 0;
$updatedTendik = 0;
try {
$nipList = array_keys($noHpByNip);
$dosens = Dosen::whereIn('nip', $nipList)->get(['id', 'nip', 'no_hp']);
foreach ($dosens as $dosen) {
$noHp = $noHpByNip[$dosen->nip];
if ($dosen->no_hp !== $noHp) {
$dosen->update(['no_hp' => $noHp]);
$updatedDosen++;
}
}
$tendiks = Tendik::whereIn('nip', $nipList)->get(['id', 'nip', 'no_hp']);
foreach ($tendiks as $tendik) {
$noHp = $noHpByNip[$tendik->nip];
if ($tendik->no_hp !== $noHp) {
$tendik->update(['no_hp' => $noHp]);
$updatedTendik++;
}
}
} catch (Exception $e) {
$this->error('Terjadi error: '.$e->getMessage());
Log::error('Error saat update no_hp pimpinan', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
return Command::FAILURE;
}
$this->info("Dosen diupdate: {$updatedDosen}");
$this->info("Tendik diupdate: {$updatedTendik}");
$this->info('Selesai.');
return Command::SUCCESS;
}
}