fix handle null on scraping google sheet data and add detail data building

This commit is contained in:
arifal
2025-06-23 13:54:26 +07:00
parent ccff82bd22
commit 200b398868
6 changed files with 160 additions and 129 deletions

View File

@@ -36,7 +36,7 @@ class ServiceGoogleSheet
public function run_service(){
try{
$this->sync_big_data();
// $this->sync_big_data();
$this->sync_google_sheet_data();
}catch(Exception $e){
throw $e;
@@ -135,7 +135,11 @@ class ServiceGoogleSheet
}
// Count occurrences of each no_registrasi
$registrasiCounts = array_count_values(array_column($mapUpsert, 'no_registrasi'));
// Filter out null values before counting to avoid array_count_values error
$registrationNumbers = array_filter(array_column($mapUpsert, 'no_registrasi'), function($value) {
return $value !== null && $value !== '';
});
$registrasiCounts = array_count_values($registrationNumbers);
// Filter duplicates (those appearing more than once)
$duplicates = array_filter($registrasiCounts, function ($count) {
@@ -146,8 +150,14 @@ class ServiceGoogleSheet
Log::warning("Duplicate no_registrasi found", ['duplicates' => array_keys($duplicates)]);
}
// Remove duplicates before upsert
$mapUpsert = collect($mapUpsert)->unique('no_registrasi')->values()->all();
// Remove duplicates before upsert - filter out entries with null no_registrasi
$mapUpsert = collect($mapUpsert)
->filter(function($item) {
return !empty($item['no_registrasi']);
})
->unique('no_registrasi')
->values()
->all();
$batchSize = 1000;
$chunks = array_chunk($mapUpsert, $batchSize);
@@ -200,9 +210,19 @@ class ServiceGoogleSheet
}
foreach ($data_setting_result as $key => $value) {
// Ensure value is not null before saving to database
$processedValue = 0; // Default to 0 instead of null
if ($value !== null && $value !== '') {
if (strpos($key, '_COUNT') !== false) {
$processedValue = $this->convertToInteger($value) ?? 0;
} else {
$processedValue = $this->convertToDecimal($value) ?? 0;
}
}
DataSetting::updateOrInsert(
["key" => $key], // Find by key
["value" => $value] // Update or insert value
["value" => $processedValue] // Update or insert value
);
}
@@ -294,9 +314,22 @@ class ServiceGoogleSheet
];
foreach ($dataSettings as $key => $value) {
// Ensure value is not null before saving to database
$processedValue = null;
if ($value !== null && $value !== '') {
// Try to convert to appropriate type based on key name
if (strpos($key, '_COUNT') !== false) {
$processedValue = $this->convertToInteger($value) ?? 0;
} else {
$processedValue = $this->convertToDecimal($value) ?? 0;
}
} else {
$processedValue = 0; // Default to 0 instead of null
}
DataSetting::updateOrInsert(
['key' => $key],
['value' => $this->convertToInteger($value) ?? 0]
['value' => $processedValue]
);
}