StoreCreateInstantBill.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Jobs;
  3. use App\Services\LogService;
  4. use App\Services\StoreService;
  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 StoreCreateInstantBill implements ShouldQueue
  12. {
  13. use Dispatchable, InteractsWithQueue, Queueable;
  14. protected $stores;
  15. /**
  16. * Create a new job instance.
  17. *
  18. * @param Collection $stores
  19. * @return void
  20. */
  21. public function __construct($stores)
  22. {
  23. $this->stores = $stores;
  24. }
  25. /**
  26. * Execute the job.
  27. *
  28. * @param StoreService $service
  29. * @return void
  30. * @throws
  31. */
  32. public function handle(StoreService $service)
  33. {
  34. if (!$this->stores)return;
  35. $errors = [];
  36. foreach ($this->stores as $store){
  37. try{
  38. $service->createInstantBill($store);
  39. }catch (\Error|\Exception $e){
  40. LogService::log(__METHOD__,"ERROR-入库生成即时账单",(is_subclass_of($store,Model::class) ? $store->toJson() : json_encode($this->stores))." | ".$e->getMessage());
  41. $errors = ["store_".$store->id.":".$e->getMessage()];
  42. }
  43. }
  44. if ($errors)throw new \Exception(json_encode($errors));
  45. }
  46. }