= NOW() - INTERVAL :freshSeconds SECOND ORDER BY updated_at DESC LIMIT 100; -- نجلب 100 مرشح محتمل "; $stmt_locations = $con_tracking->prepare($sql_locations); $stmt_locations->bindValue(':boundingBox', $boundingBoxWKT); $stmt_locations->bindValue(':freshSeconds', $freshSeconds, PDO::PARAM_INT); $stmt_locations->execute(); $locations = $stmt_locations->fetchAll(PDO::FETCH_ASSOC); if (!$locations) { jsonError("No car locations found in the specified area."); exit; } // ================================================================= // الخطوة 2: تجميع معرفات السائقين (driver_id) // ================================================================= $driver_ids = array_column($locations, 'driver_id'); // ================================================================= // الخطوة 3: جلب البيانات الثابتة من القاعدة الأساسية وتطبيق الفلاتر الإضافية // ================================================================= $drivers_info = []; if (!empty($driver_ids)) { $placeholders = implode(',', array_fill(0, count($driver_ids), '?')); // هنا نطبق الشروط الخاصة بهذا السكريبت (موديل السيارة > 2000) $sql_drivers_info = " SELECT d.id AS driver_id, d.phone, d.email, d.birthdate, d.first_name, d.last_name, d.gender, d.maritalStatus, cr.make, cr.model, cr.color, cr.color_hex, cr.year, dt.token, COALESCE(rdAvg.ratingDriver, 0) AS ratingDriver, COALESCE(rdAvg.ratingCount, 0) AS ratingCount FROM driver d LEFT JOIN CarRegistration cr ON cr.driverID = d.id LEFT JOIN driverToken dt ON dt.captain_id = d.id LEFT JOIN ( SELECT driver_id, AVG(rating) AS ratingDriver, COUNT(id) AS ratingCount FROM ratingDriver GROUP BY driver_id ) rdAvg ON rdAvg.driver_id = d.id WHERE d.id IN ($placeholders) -- AND COALESCE(cr.year, 0) > 2000 -- ⭐ الشرط الخاص بهذا السكريبت -- AND (cr.make NOT LIKE '%دراج%' AND cr.model NOT LIKE '%دراج%') AND (cr.model NOT LIKE '%Van%' AND cr.make NOT LIKE '%Van%') "; $stmt_drivers_info = $con->prepare($sql_drivers_info); $stmt_drivers_info->execute($driver_ids); $drivers_info_raw = $stmt_drivers_info->fetchAll(PDO::FETCH_ASSOC); // تحويل المصفوفة لتسهيل عملية الدمج لاحقاً foreach ($drivers_info_raw as $driver) { $drivers_info[$driver['driver_id']] = $driver; } } // ================================================================= // الخطوة 4: دمج النتائج في PHP // ================================================================= $final_results = []; foreach ($locations as $location) { $driver_id = $location['driver_id']; if (isset($drivers_info[$driver_id])) { $final_results[] = array_merge($location, $drivers_info[$driver_id]); } } // ================================================================= // الخطوة 5: تطبيق الترتيب والحد النهائي في PHP // ================================================================= usort($final_results, function ($a, $b) { if ($a['ratingDriver'] != $b['ratingDriver']) { return $b['ratingDriver'] <=> $a['ratingDriver']; } if ($a['ratingCount'] != $b['ratingCount']) { return $b['ratingCount'] <=> $a['ratingCount']; } return strtotime($b['updated_at']) <=> strtotime($a['updated_at']); }); $limited_results = array_slice($final_results, 0, 10); if (empty($limited_results)) { jsonError("No cars matching the specific criteria (year > 2000) found."); exit; } // ================================================================= // الخطوة 6: فك التشفير وحساب العمر (بدون تغيير) // ================================================================= $fieldsToDecrypt = [ 'phone','email','gender','birthdate', 'first_name','last_name', 'token','car_plate','vin' ]; foreach ($limited_results as &$row) { foreach ($fieldsToDecrypt as $field) { if (isset($row[$field]) && !empty($row[$field])) { try { $row[$field] = $encryptionHelper->decryptData($row[$field]); } catch (Exception $e) { $row[$field] = null; } } } if (!empty($row['birthdate'])) { try { $birthDate = new DateTime($row['birthdate']); $today = new DateTime(); $row['age'] = $today->diff($birthDate)->y; } catch (Exception $e) { $row['age'] = null; } } else { $row['age'] = null; } } unset($row); jsonSuccess($limited_results); } catch (PDOException $e) { error_log("[getSpeed.php] " . $e->getMessage()); jsonError("An internal error occurred. Please try again later."); } catch (Throwable $e) { error_log("[getSpeed.php] " . $e->getMessage()); jsonError("An internal error occurred. Please try again later."); }