OrderSentEventQueueJob.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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\AfterOrderSentEvent;
  10. use app\common\facades\Setting;
  11. use app\common\facades\SiteSetting;
  12. use app\common\models\Order;
  13. use app\common\models\OrderReceivedJob;
  14. use app\common\models\OrderSentJob;
  15. use Illuminate\Contracts\Queue\ShouldQueue;
  16. use Illuminate\Bus\Queueable;
  17. use Illuminate\Queue\SerializesModels;
  18. use Illuminate\Queue\InteractsWithQueue;
  19. use Illuminate\Support\Facades\DB;
  20. class OrderSentEventQueueJob implements ShouldQueue
  21. {
  22. use InteractsWithQueue, Queueable, SerializesModels;
  23. /**
  24. * @var Order
  25. */
  26. protected $orderId;
  27. /**
  28. * OrderReceivedEventQueueJob constructor.
  29. * @param $orderId
  30. */
  31. public function __construct($orderId)
  32. {
  33. $queueCount = Order::queueCount();
  34. if($queueCount){
  35. $this->queue = 'order:' . ($orderId % Order::queueCount());
  36. }
  37. $this->orderId = $orderId;
  38. }
  39. /**
  40. * Execute the job.
  41. *
  42. * @return void
  43. */
  44. public function handle()
  45. {
  46. \Log::info('订单'.$this->orderId.'sent任务开始执行');
  47. DB::transaction(function () {
  48. $orderId = $this->orderId;
  49. \YunShop::app()->uniacid = null;
  50. $order = Order::find($orderId);
  51. \YunShop::app()->uniacid = $order->uniacid;
  52. Setting::$uniqueAccountId = $order->uniacid;
  53. if(!$order->orderSentJob){
  54. $order->setRelation('orderSentJob',new OrderSentJob(['order_id'=>$order->id]));
  55. $order->orderSentJob->save();
  56. }
  57. if($order->orderSentJob->status == 'finished'){
  58. \Log::error('订单发货事件触发失败',"{$orderId}orderSentJob已完成");
  59. return;
  60. }
  61. $order->orderSentJob->status = 'finished';
  62. $order->orderSentJob->save();
  63. $event = new AfterOrderSentEvent($order);
  64. app('events')->safeFire($event,$order->id);
  65. });
  66. \Log::info('订单'.$this->orderId.'sent任务执行完成');
  67. }
  68. }