OrderCreateInstantBill.php 1.8 KB

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