Update: 2026-07-02 17:44:58

This commit is contained in:
Hamza-Ayed
2026-07-02 17:44:59 +03:00
parent 5d14493ff4
commit 6876d9bc54
6 changed files with 405 additions and 90 deletions
+69 -25
View File
@@ -291,32 +291,76 @@ try {
// Direct dispatch للسائقين القريبين
$driversData = findBestDrivers($con, $startLat, $startLng, $carType, $endLat, $endLng);
// 🆕 Destination Matching Priority
if ($is_destination_match && $matched_driver_id) {
$found = false;
foreach ($driversData as $k => $d) {
if ($d['captain_id'] == $matched_driver_id) {
// Remove from current position and move to the very front
unset($driversData[$k]);
$d['is_destination_match'] = 1;
array_unshift($driversData, $d);
$found = true;
break;
}
}
// If driver wasn't nearby but we still want to give it to them because it's their route
if (!$found) {
$stmtD = $con->prepare("SELECT token FROM driverToken WHERE captain_id = :d");
$stmtD->execute([':d' => $matched_driver_id]);
$dT = $stmtD->fetchColumn();
if ($dT) {
array_unshift($driversData, [
'captain_id' => $matched_driver_id,
'token' => $dT,
'is_destination_match' => 1
]);
// ═══════════════════════════════════════════════════════════════
// 🆕 Destination Matching Priority — Multi-Driver Support
// نبحث عن كل السائقين الذين وجهتهم تطابق منطقة الإنزال
// (ليس فقط الأول، بل جميعهم) ونضعهم في مقدمة القائمة
// الخصم 14% تم تطبيقه مسبقاً في التسعير — لا خصم إضافي هنا
// ═══════════════════════════════════════════════════════════════
if ($is_destination_match && isset($redis)) {
try {
// Query ALL drivers whose destination is near the drop-off (up to 10)
$allMatchedIds = $redis->geoRadius(
'geo:driver:destinations',
(float)$destLng,
(float)$destLat,
3.5,
'km',
['COUNT' => 10, 'ASC']
);
if (!empty($allMatchedIds)) {
// Build a lookup set for fast de-duplication
$alreadyInList = [];
foreach ($driversData as $d) {
$alreadyInList[$d['captain_id'] ?? $d['driver_id'] ?? ''] = true;
}
$matchedToFront = [];
foreach ($allMatchedIds as $mid) {
$mid = (string)$mid;
// Validate still active today
$detailJson = $redis->get("driver:destination:{$mid}");
$detail = $detailJson ? json_decode($detailJson, true) : null;
if (!$detail || empty($detail['is_active']) || ($detail['usage_date'] ?? '') !== date('Y-m-d')) {
$redis->zRem('geo:driver:destinations', $mid);
continue;
}
// Mark with destination flag for special FCM title "في طريقك 📍"
if (isset($alreadyInList[$mid])) {
// Already in list — mark it and move to front
foreach ($driversData as $k => $d) {
$did = $d['captain_id'] ?? $d['driver_id'] ?? '';
if ((string)$did === $mid) {
$driversData[$k]['is_destination_match'] = 1;
$matchedToFront[] = $driversData[$k];
unset($driversData[$k]);
break;
}
}
} else {
// Not nearby but their route matches — fetch token and add
$stmtD = $con->prepare("SELECT token FROM driverToken WHERE captain_id = :d LIMIT 1");
$stmtD->execute([':d' => $mid]);
$dT = $stmtD->fetchColumn();
if ($dT) {
$matchedToFront[] = [
'captain_id' => $mid,
'driver_id' => $mid,
'token' => $dT,
'is_destination_match' => 1
];
}
}
}
// Place all destination-matched drivers at the front
$driversData = array_values($driversData);
$driversData = array_merge($matchedToFront, $driversData);
error_log("[add_ride] " . count($matchedToFront) . " destination-matched driver(s) moved to front.");
}
} catch (Exception $e) {
error_log("[add_ride] Destination priority error: " . $e->getMessage());
}
}