海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.6 KiB

  1. <?php
  2. namespace App\Jobs;
  3. use App\Common\AgentType;
  4. use App\Common\UserStatus;
  5. use App\Models\Agent;
  6. use App\Models\AgentProduct;
  7. use App\Models\Product;
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Contracts\Queue\ShouldBeUnique;
  10. use Illuminate\Contracts\Queue\ShouldQueue;
  11. use Illuminate\Foundation\Bus\Dispatchable;
  12. use Illuminate\Queue\InteractsWithQueue;
  13. use Illuminate\Queue\SerializesModels;
  14. class ProductSaved implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. /**
  18. * 任务可尝试的次数
  19. * @var int
  20. */
  21. public $tries = 3;
  22. /**
  23. * 任务失败前允许的最大异常数
  24. * @var int
  25. */
  26. public $maxExceptions = 3;
  27. /**
  28. * Create a new job instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct(protected Product $product)
  33. {
  34. }
  35. /**
  36. * Execute the job.
  37. *
  38. * @return void
  39. */
  40. public function handle()
  41. {
  42. $product = $this->product;
  43. /** 同步信息到代理商产品 START */
  44. AgentProduct::query()
  45. ->where(['type' => 0, 'product_id' => $product->id])
  46. ->update([
  47. 'title' => $product->title,
  48. 'know' => $product->know,
  49. 'content' => $product->content,
  50. 'pictures' => $product->pictures,
  51. ]);
  52. /** 同步信息到代理商产品 END */
  53. /** 自动上架 START */
  54. Agent::query()->where([
  55. ['id', '>', 1], //1是总管理员,不处理
  56. ['type', '<>', AgentType::OPERATOR],
  57. ['status', '=', UserStatus::NORMAL]
  58. ])->select(['id'])->chunk(100, function ($agents) use ($product) {
  59. foreach ($agents as $agent) {
  60. AgentProductShelve::dispatch($agent['id'], $product->id);
  61. }
  62. });
  63. /** 自动上架 END */
  64. }
  65. }