OrderCreateInstantBill.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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\QueryException;
  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(QueryException $qe){
  40. if ($qe->getCode()!='23000'){
  41. LogService::log(__METHOD__,"ERROR-订单生成即时账单",$order->toJson()." | ".$qe->getMessage());
  42. $errors = ["order_".$order->id.":".$qe->getMessage()];
  43. }
  44. }catch (\Exception $e){
  45. LogService::log(__METHOD__,"ERROR-订单生成即时账单",$order->toJson()." | ".$e->getMessage());
  46. $errors = ["order_".$order->id.":".$e->getMessage()];
  47. }
  48. }
  49. if ($errors)throw new \Exception(json_encode($errors));
  50. }
  51. }
  52. }