fix scadule for getting price

This commit is contained in:
2026-09-02 18:14:32 +03:30
parent 0b1e5c687b
commit 92d8f2e9b3
5 changed files with 149 additions and 67 deletions
+45 -30
View File
@@ -52,42 +52,27 @@ class ColleagueOrderCallbackService
return $this->applyProviderSts($order, $sts, $body);
}
public function pollDueOrders(): int
public function refreshIfDue(Order $order): Order
{
if (!Schema::hasTable('orders') || !Schema::hasColumn('orders', 'sts')) {
return 0;
if ((int) $order->sts !== 4) {
return $order;
}
$cutoff = now()->subSeconds(self::POLL_SECONDS);
$handled = 0;
$query = Order::query()->where('sts', 4)->whereNotNull('provider_id');
if (Schema::hasColumn('orders', 'colleague_last_poll_at')) {
$query->where(function ($q) use ($cutoff) {
$q->where(function ($inner) use ($cutoff) {
$inner->whereNull('colleague_last_poll_at')
->where('created_at', '<=', $cutoff);
})->orWhere('colleague_last_poll_at', '<=', $cutoff);
});
} else {
$query->where('created_at', '<=', $cutoff);
if (!$this->isPollDue($order)) {
return $order;
}
$orders = $query->orderBy('id')->limit(80)->get();
foreach ($orders as $order) {
try {
$this->pollOne($order);
$handled++;
} catch (\Throwable $e) {
Log::warning('Colleague pending poll failed', [
'order_id' => $order->id,
'error' => $e->getMessage(),
]);
$this->touchPollAt($order);
}
try {
$this->pollOne($order);
} catch (\Throwable $e) {
Log::warning('Colleague pending poll failed', [
'order_id' => $order->id,
'error' => $e->getMessage(),
]);
$this->touchPollAt($order);
}
return $handled;
return Order::query()->find($order->id) ?? $order;
}
public function pollOne(Order $order): array
@@ -110,7 +95,17 @@ class ColleagueOrderCallbackService
return ['ok' => true, 'state' => 'wait'];
}
return $this->applyProviderSts($order, $sts, $body);
// جدول سفارش همکار همان sts مدل است (۲ موفق / ۳ ناموفق / ۴ هنوز باز).
// کالبک جداست: آن‌جا ۴ و ۶ یعنی رد.
if (in_array($sts, [3, 6], true)) {
return $this->rejectPending($order, $body);
}
if ($sts === 2) {
return $this->approvePending($order, $body);
}
return ['ok' => true, 'state' => 'wait', 'sts' => (int) $order->sts];
}
/**
@@ -286,12 +281,32 @@ class ColleagueOrderCallbackService
$order->save();
}
private function isPollDue(Order $order): bool
{
$cutoff = now()->subSeconds(self::POLL_SECONDS);
$last = $order->colleague_last_poll_at ?? null;
if ($last) {
return $last <= $cutoff;
}
$created = $order->created_at;
return !$created || $created <= $cutoff;
}
/**
* @param array<string, mixed> $body
*/
public function providerStsFromBody(array $body): ?int
{
$rows = [$body, $body['data'] ?? null, $body['order'] ?? null];
if (isset($body['rows']) && is_array($body['rows'])) {
foreach ($body['rows'] as $row) {
if (is_array($row)) {
$rows[] = $row;
}
}
}
foreach ($body as $item) {
if (!is_array($item)) {
continue;