StoreCreateInstantBill.php 1.3 KB

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