This commit is contained in:
Dwi Swandhana
2026-07-22 05:18:55 +07:00
parent 6d0f9a8bf6
commit 32db765b98
50 changed files with 2189 additions and 1909 deletions
@@ -0,0 +1,124 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class SyncPoliFromJson extends Command
{
protected $signature = 'poli:sync-json {--path= : Path file JSON poli, default database/seeders/data/poli.json} {--dry-run : Cek data tanpa menulis ke database}';
protected $description = 'Update atau create data poli dari file JSON berdasarkan id tanpa truncate.';
public function handle(): int
{
$path = $this->resolvePath($this->option('path') ?: database_path('seeders/data/poli.json'));
$data = $this->readJson($path);
$updated = 0;
$created = 0;
$unchanged = 0;
DB::transaction(function () use ($data, &$updated, &$created, &$unchanged) {
foreach ($data as $row) {
$id = (int) $row['id'];
$payload = [
'poli' => $row['poli'] ?? null,
'subpoli' => $row['subpoli'] ?? null,
'subsubpoli' => $row['subsubpoli'] ?? null,
'modaliti' => $row['modaliti'] ?? null,
'kelompok' => $row['kelompok'] ?? null,
'modaliti2' => isset($row['modaliti2']) ? (string) $row['modaliti2'] : null,
'created_at' => $row['created_at'] ?? now(),
'updated_at' => $row['updated_at'] ?? now(),
];
$existing = DB::table('poli')->where('id', $id)->first();
if ($existing === null) {
$created++;
if (!$this->option('dry-run')) {
DB::table('poli')->insert(array_merge(['id' => $id], $payload));
}
continue;
}
if ($this->isSameRow($existing, $payload)) {
$unchanged++;
continue;
}
$updated++;
if (!$this->option('dry-run')) {
DB::table('poli')->where('id', $id)->update($payload);
}
}
});
$mode = $this->option('dry-run') ? 'DRY RUN' : 'DONE';
$this->info("{$mode}: {$created} created, {$updated} updated, {$unchanged} unchanged.");
return self::SUCCESS;
}
private function resolvePath(string $path): string
{
if ($path !== '' && $path[0] === '/') {
return $path;
}
return base_path($path);
}
private function readJson(string $path): array
{
if (!is_file($path)) {
throw new \RuntimeException("File JSON poli tidak ditemukan: {$path}");
}
$json = file_get_contents($path);
if ($json === false) {
throw new \RuntimeException("Gagal membaca file JSON poli: {$path}");
}
$data = json_decode($json, true);
if (!is_array($data)) {
throw new \RuntimeException("Format JSON poli tidak valid: {$path}");
}
$ids = [];
foreach ($data as $index => $row) {
if (!is_array($row) || !isset($row['id'])) {
throw new \RuntimeException('Data poli baris '.($index + 1).' tidak memiliki id.');
}
$id = (int) $row['id'];
if (isset($ids[$id])) {
throw new \RuntimeException("Duplikasi id poli pada JSON: {$id}");
}
$ids[$id] = true;
}
return $data;
}
private function isSameRow(object $existing, array $payload): bool
{
foreach ($payload as $key => $value) {
if ((string) ($existing->{$key} ?? '') !== (string) ($value ?? '')) {
return false;
}
}
return true;
}
}
@@ -846,7 +846,7 @@ class DokterController extends Controller
return 2;
}
if (str_starts_with($normalized, 'data bd di terima')) {
if (str_starts_with($normalized, 'data bd di terima') || str_starts_with($normalized, 'data bd diterima')) {
return 3;
}
@@ -858,26 +858,31 @@ class DokterController extends Controller
return 5;
}
if (str_starts_with($normalized, 'otor maldi (un verified)')) {
if (str_starts_with($normalized, 'data malditof diterima') || str_starts_with($normalized, 'data malditof di terima')) {
return 6;
}
if (str_starts_with($normalized, 'preliminary results')) {
if (str_starts_with($normalized, 'otor maldi (un verified)')) {
return 7;
}
if (str_starts_with($normalized, 'data vitek di terima')) {
if (str_starts_with($normalized, 'preliminary results')) {
return 8;
}
if (str_starts_with($normalized, 'expertise saved (un verified)')) {
if (str_starts_with($normalized, 'data vitek di terima') || str_starts_with($normalized, 'data vitek diterima')) {
return 9;
}
if (in_array($normalized, ['expertise', 'selesai', 'final result'], true)) {
if (str_starts_with($normalized, 'id/ast pending result') || $normalized === 'sedang id+ast') {
return 10;
}
if (str_starts_with($normalized, 'expertise saved (un verified)')
|| in_array($normalized, ['expertise', 'selesai', 'final result'], true)) {
return 11;
}
return null;
}
protected function normalizePeriksaStatus($status): string {
@@ -897,10 +902,6 @@ class DokterController extends Controller
'menunggu kultur yg lain',
'menunggu kultur yang lain',
'proses identifikasi dan uji kepekaan',
'id/ast pending result',
'data malditof diterima',
'data malditof di terima',
'sedang id+ast',
'sedang malditof',
'pertumbuhan primer',
];
+6 -3
View File
@@ -8,11 +8,14 @@ class Poli extends Model
{
protected $table = "poli";
protected $fillable = [
'id',
'poli',
'subpoli',
'subsubpoli',
'modaliti',
'kelompok',
'modaliti2',
'modaliti',
'kelompok',
'modaliti2',
'created_at',
'updated_at',
];
}
@@ -0,0 +1,72 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
if (!Schema::hasTable('poli')) {
Schema::create('poli', function (Blueprint $table) {
$table->id();
$table->string('poli', 250)->nullable();
$table->string('subpoli', 250)->nullable();
$table->string('subsubpoli', 250)->nullable();
$table->string('modaliti', 150)->nullable();
$table->string('kelompok', 150)->nullable();
$table->string('modaliti2', 150)->nullable();
$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->nullable();
});
return;
}
Schema::table('poli', function (Blueprint $table) {
if (!Schema::hasColumn('poli', 'poli')) {
$table->string('poli', 250)->nullable();
}
if (!Schema::hasColumn('poli', 'subpoli')) {
$table->string('subpoli', 250)->nullable();
}
if (!Schema::hasColumn('poli', 'subsubpoli')) {
$table->string('subsubpoli', 250)->nullable();
}
if (!Schema::hasColumn('poli', 'modaliti')) {
$table->string('modaliti', 150)->nullable();
}
if (!Schema::hasColumn('poli', 'kelompok')) {
$table->string('kelompok', 150)->nullable();
}
if (!Schema::hasColumn('poli', 'modaliti2')) {
$table->string('modaliti2', 150)->nullable();
}
if (!Schema::hasColumn('poli', 'created_at')) {
$table->timestamp('created_at')->nullable();
}
if (!Schema::hasColumn('poli', 'updated_at')) {
$table->timestamp('updated_at')->nullable();
}
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
// Kolom poli dipakai data production, rollback tidak menghapus kolom.
}
};
File diff suppressed because it is too large Load Diff