StoreCreateInstantBill.php 1.7 KB

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