68 lines
2.4 KiB
PHP
68 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
|
use Illuminate\Database\Seeder;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class NoSpjSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function run()
|
|
{
|
|
// Ambil data dari tabel api_logs
|
|
$apiLogs = DB::connection('oldlms')->table('api_logs')
|
|
->selectRaw("`sResponse` as sResponse")
|
|
->where('sTarget', 'INHPostSJPdanRujukan')
|
|
->orderByDesc('nID')
|
|
->limit(1000)
|
|
->get();
|
|
|
|
// Proses setiap log untuk mengambil nosjp dan refasalsjp
|
|
$berhasil = 0;
|
|
foreach ($apiLogs as $log) {
|
|
// Decode JSON string
|
|
$decodedResponse = json_decode($log->sResponse, true);
|
|
|
|
// Cek apakah decode berhasil
|
|
if (json_last_error() === JSON_ERROR_NONE) {
|
|
// Ambil data dari field 'data' yang berisi JSON string
|
|
$data = json_decode($decodedResponse['data'], true);
|
|
|
|
// Pastikan data yang diambil ada dan formatnya benar
|
|
if (isset($data['data'][0]['nosjp'], $data['data'][0]['refasalsjp'])) {
|
|
$nosjp = $data['data'][0]['nosjp'];
|
|
$refasalsjp = $data['data'][0]['refasalsjp'];
|
|
$nIDLiveChat = substr($refasalsjp, -5);
|
|
|
|
// // Tampilkan hasil atau lakukan penyimpanan ke tabel lain
|
|
// echo "nosjp: " . $nosjp . ", refasalsjp: " . $refasalsjp . ", 5 karakter terakhir: " . $lastFiveCharacters . "\n";
|
|
|
|
// Contoh penyimpanan ke tabel lain (misal, table `processed_logs`)
|
|
DB::connection('oldlms')->table('tx_livechat')
|
|
->where('nID', $nIDLiveChat) // Kondisi where ditempatkan sebelum update
|
|
->update([
|
|
'sNoSpj' => $nosjp,
|
|
]
|
|
);
|
|
|
|
echo "Berhasil data ke: " .$berhasil ++ . "\n";;
|
|
|
|
} else {
|
|
// Tangani kasus jika data tidak sesuai
|
|
echo "Data tidak ditemukan dalam response: " . $log->sResponse . "\n";
|
|
}
|
|
} else {
|
|
// Tangani kesalahan JSON decode
|
|
echo "Error decoding JSON: " . json_last_error_msg() . " in response: " . $log->sResponse . "\n";
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|