erp-backend/app/Jobs/ProcessPlatformOrderJob.php
2026-04-01 17:07:04 +08:00

75 lines
2.2 KiB
PHP

<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Services\OrderPullService;
use Illuminate\Support\Facades\Log;
class ProcessPlatformOrderJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $tries = 3;
public $backoff = [10, 30, 60];
protected $platform;
protected $shopAuthId;
protected $orderData;
public function __construct(string $platform, int $shopAuthId, array $orderData)
{
$this->platform = $platform;
$this->shopAuthId = $shopAuthId;
$this->orderData = $orderData;
}
public function handle(OrderPullService $orderPullService)
{
$orderNo = $this->orderData['platform_order_sn'] ?? 'unknown';
try {
$result = $orderPullService->processSingleOrder(
$this->platform,
$this->shopAuthId,
$this->orderData
);
if ($result['status'] === 'success') {
Log::info("订单处理成功", [
'order_no' => $orderNo,
'order_id' => $result['order_id'] ?? null,
]);
} elseif ($result['status'] === 'skipped') {
Log::debug("订单跳过", [
'order_no' => $orderNo,
'reason' => $result['reason'] ?? 'unknown',
]);
}
} catch (\Exception $e) {
Log::error("订单处理失败", [
'order_no' => $orderNo,
'platform' => $this->platform,
'shop_id' => $this->shopAuthId,
'error' => $e->getMessage(),
]);
throw $e;
}
}
public function failed(\Throwable $exception)
{
Log::error("订单处理最终失败", [
'platform' => $this->platform,
'shop_id' => $this->shopAuthId,
'order_no' => $this->orderData['platform_order_sn'] ?? 'unknown',
'error' => $exception->getMessage(),
]);
}
}