OrderHandleJob.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Order;
  4. use App\Services\OrderService;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. class OrderHandleJob implements ShouldQueue
  11. {
  12. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  13. protected $order;
  14. public $tries = 3;
  15. public $timeout = 5;
  16. /**
  17. * Create a new job instance.
  18. *
  19. * @return void
  20. */
  21. public function __construct($tradeNo)
  22. {
  23. $this->onQueue('order_handle');
  24. $this->order = Order::where('trade_no', $tradeNo)
  25. ->lockForUpdate()
  26. ->first();
  27. }
  28. /**
  29. * Execute the job.
  30. *
  31. * @return void
  32. */
  33. public function handle()
  34. {
  35. if (!$this->order) return;
  36. $orderService = new OrderService($this->order);
  37. switch ($this->order->status) {
  38. // cancel
  39. case 0:
  40. if ($this->order->created_at <= (time() - 1800)) {
  41. $orderService->cancel();
  42. }
  43. break;
  44. case 1:
  45. $orderService->open();
  46. break;
  47. }
  48. }
  49. }