OrderCreateInstantBill.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Jobs;
  3. use App\Services\LogService;
  4. use App\Services\OrderService;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Database\Eloquent\Collection;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. class OrderCreateInstantBill implements ShouldQueue
  12. {
  13. use Dispatchable, InteractsWithQueue, Queueable;
  14. protected $orders;
  15. /**
  16. * Create a new job instance.
  17. *
  18. * @param Collection $orders
  19. * @return void
  20. */
  21. public function __construct($orders)
  22. {
  23. $this->orders = $orders;
  24. }
  25. /**
  26. *
  27. * @param OrderService $service
  28. * @return void
  29. * @throws
  30. */
  31. public function handle(OrderService $service)
  32. {
  33. if ($this->orders){
  34. $errors = [];
  35. foreach ($this->orders as $order){
  36. try{
  37. if (!$service->createInstantBill($order))
  38. LogService::log(__METHOD__,"ERROR-订单生成即时账单",$order->toJson());
  39. }catch (\Error|\Exception $e){
  40. LogService::log(__METHOD__,"ERROR-订单生成即时账单",(is_subclass_of($order,Model::class) ? $order->toJson() : json_encode($order))." | ".$e->getMessage());
  41. $errors = ["order_".$order->id.":".$e->getMessage()];
  42. }
  43. }
  44. if ($errors)throw new \Exception(json_encode($errors));
  45. }
  46. }
  47. }