OrderCreateInstantBill.php 1.4 KB

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