121 lines
3.8 KiB
PHP
121 lines
3.8 KiB
PHP
<?php
|
|
/**
|
|
* get_competitor_context.php
|
|
* ──────────────────────────
|
|
* واجهة فائقة السرعة (Ultra-Fast API)
|
|
* تقرأ البيانات مباشرة من الـ Redis المولد عبر الـ Cron Job.
|
|
* زمن الاستجابة: O(1).
|
|
*/
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
require_once __DIR__ . '/../../connect.php'; // Web-safe
|
|
|
|
$lat = filterRequest('lat');
|
|
$lng = filterRequest('lng');
|
|
$countryCode = filterRequest('country_code');
|
|
|
|
if (!$lat || !$lng || !$countryCode) {
|
|
echo json_encode(["status" => "error", "message" => "Missing parameters"]);
|
|
exit;
|
|
}
|
|
|
|
$lat = (float)$lat;
|
|
$lng = (float)$lng;
|
|
$countryCode = strtoupper($countryCode);
|
|
|
|
try {
|
|
$redis = getRedisConnection();
|
|
$cacheJson = $redis->get('siro:cache:pricing:grids');
|
|
} catch (Exception $e) {
|
|
echo json_encode(["status" => "error", "message" => "Redis connection failed"]);
|
|
exit;
|
|
}
|
|
|
|
if (!$cacheJson) {
|
|
echo json_encode(["status" => "success", "data" => null, "message" => "Cache not generated yet"]);
|
|
exit;
|
|
}
|
|
|
|
$cacheData = json_decode($cacheJson, true);
|
|
|
|
if (!$cacheData || !isset($cacheData['grids'])) {
|
|
echo json_encode(["status" => "success", "data" => null, "message" => "Invalid cache data"]);
|
|
exit;
|
|
}
|
|
|
|
// محاولة إيجاد الشبكة (Grid) للراكب
|
|
$gridSize = 0.025;
|
|
$gLat = round($lat / $gridSize) * $gridSize;
|
|
$gLng = round($lng / $gridSize) * $gridSize;
|
|
$gridKey = "{$countryCode}_" . number_format($gLat, 3) . "_" . number_format($gLng, 3);
|
|
|
|
$grids = $cacheData['grids'];
|
|
|
|
if (isset($grids[$gridKey])) {
|
|
$compData = $grids[$gridKey];
|
|
} else {
|
|
// إذا لم نجد، نستخدم الـ Fallback للدولة
|
|
$fallbackKey = "{$countryCode}_FALLBACK";
|
|
if (isset($grids[$fallbackKey])) {
|
|
$compData = $grids[$fallbackKey];
|
|
} else {
|
|
echo json_encode(["status" => "success", "data" => null, "message" => "No data for this region"]);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$avgPrice = $compData['avg_price'];
|
|
$topComp = $compData['top_competitor'] ?? 'Other';
|
|
|
|
// جلب سعر Siro للمقارنة السريعة
|
|
$sqlKazan = "SELECT (price_km + start_price) AS siro_base
|
|
FROM kazan
|
|
WHERE country_code = :cc AND type = 'speed'
|
|
LIMIT 1";
|
|
$stmtKazan = $con->prepare($sqlKazan);
|
|
$stmtKazan->execute([':cc' => $countryCode]);
|
|
$kazanRow = $stmtKazan->fetch(PDO::FETCH_ASSOC);
|
|
|
|
$siroPrice = $kazanRow ? (float)$kazanRow['siro_base'] : $avgPrice * 0.9;
|
|
|
|
$savingsPct = 0;
|
|
if ($avgPrice > 0 && $siroPrice < $avgPrice) {
|
|
$savingsPct = (($avgPrice - $siroPrice) / $avgPrice) * 100;
|
|
}
|
|
|
|
$passengerMessage = null;
|
|
$driverMessage = null;
|
|
$extraEarnings = 0;
|
|
|
|
if ($savingsPct > 2) {
|
|
$compNameAr = match(strtolower($topComp)) {
|
|
'careem' => 'كريم',
|
|
'uber' => 'أوبر',
|
|
'yallago' => 'يلا غو',
|
|
'jenny' => 'جيني',
|
|
default => 'التطبيقات الأخرى'
|
|
};
|
|
|
|
$passengerMessage = "أوفر بـ " . number_format($savingsPct, 1) . "% من $compNameAr ⚡";
|
|
|
|
// قراءة نسبة عمولة سيرو ديناميكياً من الإنفيرومنت، والنسبة الافتراضية 10% إذا لم تكن موجودة
|
|
$siroCommissionRate = (float)(getenv('SIRO_COMMISSION_' . $countryCode) ?: 0.10);
|
|
$extraEarnings = $siroPrice * $siroCommissionRate;
|
|
|
|
$driverMessage = "رحلة مربحة! تكسب أكثر مقارنة بـ $compNameAr 💰";
|
|
}
|
|
|
|
echo json_encode([
|
|
"status" => "success",
|
|
"data" => [
|
|
"competitor_avg_price" => $avgPrice,
|
|
"top_competitor" => $topComp,
|
|
"savings_percent" => $savingsPct,
|
|
"passenger_badge_text" => $passengerMessage,
|
|
"driver_badge_text" => $driverMessage,
|
|
"driver_extra_earnings" => round($extraEarnings, 2),
|
|
"source" => "redis_cache"
|
|
]
|
|
]);
|
|
?>
|