From 8ca526e4f263a10819daf308cb03dab26ec1dabd Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Thu, 29 May 2025 21:58:08 +0700 Subject: [PATCH] refactor(webstatement): optimalkan pemrosesan batch data bill detail - Mengubah mekanisme pemrosesan batch `billDetailBatch` menjadi pemrosesan per chunk untuk manajemen memori yang lebih baik. - Menambahkan langkah penghapusan data eksisting berdasarkan `stmt_entry_ids` sebelum melakukan insert untuk menghindari konflik data. - Mengganti metode `upsert` dengan kombinasi `delete` dan `insert` untuk tiap chunk data. - Menyisipkan komentar untuk memberikan penjelasan yang lebih rinci terkait proses chunking. - Memastikan batch `billDetailBatch` di-reset setelah selesai diproses. Signed-off-by: Daeng Deni Mardaeni --- app/Jobs/ProcessBillDetailDataJob.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/app/Jobs/ProcessBillDetailDataJob.php b/app/Jobs/ProcessBillDetailDataJob.php index 0afe94e..d04ad33 100644 --- a/app/Jobs/ProcessBillDetailDataJob.php +++ b/app/Jobs/ProcessBillDetailDataJob.php @@ -182,14 +182,19 @@ { try { if (!empty($this->billDetailBatch)) { - // Bulk insert/update bill details - TempBillDetail::upsert( - $this->billDetailBatch, - ['_id'], // Unique key - array_diff((new TempBillDetail())->getFillable(), ['_id']) // Update columns - ); + // Process in smaller chunks for better memory management + foreach ($this->billDetailBatch as $entry) { + // Extract all stmt_entry_ids from the current chunk + $entryIds = array_column($entry, '_id'); - // Reset batch after processing + // Delete existing records with these IDs to avoid conflicts + TempBillDetail::whereIn('_id', $entryIds)->delete(); + + // Insert all records in the chunk at once + TempBillDetail::insert($entry); + } + + // Reset entry batch after processing $this->billDetailBatch = []; } } catch (Exception $e) {