OrderReceivedEventQueueJob.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shenyang
  5. * Date: 2017/9/18
  6. * Time: 下午3:46
  7. */
  8. namespace app\Jobs;
  9. use app\common\events\order\AfterOrderReceivedEvent;
  10. use app\common\facades\Setting;
  11. use app\common\facades\SiteSetting;
  12. use app\common\models\Order;
  13. use app\common\models\OrderPaidJob;
  14. use app\common\models\OrderReceivedJob;
  15. use app\common\modules\shop\ShopConfig;
  16. use Illuminate\Contracts\Queue\ShouldQueue;
  17. use Illuminate\Bus\Queueable;
  18. use Illuminate\Queue\SerializesModels;
  19. use Illuminate\Queue\InteractsWithQueue;
  20. use Illuminate\Support\Facades\DB;
  21. class OrderReceivedEventQueueJob implements ShouldQueue
  22. {
  23. use InteractsWithQueue, Queueable, SerializesModels;
  24. /**
  25. * @var Order
  26. */
  27. protected $orderId;
  28. /**
  29. * OrderReceivedEventQueueJob constructor.
  30. * @param $orderId
  31. */
  32. public function __construct($orderId)
  33. {
  34. $queueCount = Order::queueCount();
  35. if($queueCount){
  36. $this->queue = 'order:' . ($orderId % Order::queueCount());
  37. }
  38. $this->orderId = $orderId;
  39. }
  40. /**
  41. * Execute the job.
  42. *
  43. * @return void
  44. */
  45. public function handle()
  46. {
  47. \Log::info('订单'.$this->orderId.'received任务开始执行');
  48. DB::transaction(function () {
  49. $orderId = $this->orderId;
  50. \YunShop::app()->uniacid = null;
  51. $order = Order::find($orderId);
  52. \YunShop::app()->uniacid = $order->uniacid;
  53. Setting::$uniqueAccountId = $order->uniacid;
  54. if(!$order->orderReceivedJob){
  55. $order->setRelation('orderReceivedJob',new OrderReceivedJob(['order_id'=>$order->id]));
  56. $order->orderReceivedJob->save();
  57. }
  58. if($order->orderReceivedJob->status == 'finished'){
  59. \Log::error('订单收货事件触发失败',"{$orderId}orderReceivedJob记录已");
  60. return;
  61. }
  62. $order->orderReceivedJob->status = 'finished';
  63. $order->orderReceivedJob->save();
  64. $event = new AfterOrderReceivedEvent($order);
  65. app('events')->safeFire($event,$order->id);
  66. });
  67. \Log::info('订单'.$this->orderId.'received任务执行完成');
  68. }
  69. }