海南旅游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.

134 lines
3.6 KiB

  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Agent;
  4. use App\Models\AgentProduct;
  5. use App\Models\AgentProductItem;
  6. use App\Models\AgentSetting;
  7. use App\Models\Category;
  8. use App\Models\Product;
  9. use App\Models\ProductSpec;
  10. use App\Service\SmsService;
  11. use App\Traits\SmsTraits;
  12. use Illuminate\Bus\Queueable;
  13. use Illuminate\Contracts\Queue\ShouldQueue;
  14. use Illuminate\Foundation\Bus\Dispatchable;
  15. use Illuminate\Queue\InteractsWithQueue;
  16. use Illuminate\Queue\SerializesModels;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Log;
  19. /**
  20. * 代理商产品自动上架
  21. */
  22. class AgentProductShelve implements ShouldQueue
  23. {
  24. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  25. /**
  26. * 任务可尝试的次数
  27. * @var int
  28. */
  29. public $tries = 3;
  30. /**
  31. * 任务失败前允许的最大异常数
  32. * @var int
  33. */
  34. public $maxExceptions = 3;
  35. /**
  36. * Create a new job instance.
  37. *
  38. * @return void
  39. */
  40. public function __construct(private int $agent_id, private int $product_id)
  41. {
  42. //
  43. }
  44. /**
  45. * Execute the job.
  46. *
  47. * @return void
  48. */
  49. public function handle()
  50. {
  51. $agent_id = $this->agent_id;
  52. $product = Product::find($this->product_id);
  53. //如果没开启自动上架 滚蛋
  54. if (empty(AgentSetting::val($agent_id, 'auto_shelves'))) {
  55. return;
  56. }
  57. //如果已经有这个产品 滚蛋
  58. if(AgentProductItem::query()->where(['product_id' => $product->id,'agent_id' => $agent_id])->exists()) {
  59. return;
  60. }
  61. //啥有没有 自动添加商品
  62. DB::beginTransaction();
  63. try {
  64. $agentProduct = new AgentProduct();
  65. $agentProduct->title = $product->title;
  66. $agentProduct->agent_id = $agent_id;
  67. $agentProduct->product_id = $product->id;
  68. $agentProduct->product_ids = $product->id;
  69. $agentProduct->stock = $product->stock;
  70. $agentProduct->status = 1;
  71. $agentProduct->pictures = $product->pictures;
  72. $agentProduct->content = $product->content;
  73. $agentProduct->know = $product->know;
  74. $agentProduct->price = $product->price;
  75. $agentProduct->original_price = $product->original_price;
  76. //自动添加分类
  77. $autoCategory = AgentSetting::val($agent_id, 'auto_category') ?? 0;
  78. if (!empty($autoCategory)) {
  79. $categoryName = Category::query()->where('id', $product->category_id)->value('name');
  80. $category = Category::query()->firstOrCreate(['agent_id' => $agent_id, 'name' => $categoryName]);
  81. $agentProduct->category_id = $category->id;
  82. }
  83. $agentProduct->save();
  84. //维护关联表
  85. AgentProductItem::query()->create([
  86. 'agent_id' => $agent_id,
  87. 'supplier_id' => $product->supplier_id,
  88. 'agent_product_id' => $agentProduct->id,
  89. 'product_id' => $product->id,
  90. ]);
  91. //规格
  92. $productSpec = ProductSpec::query()->where('product_id',$product->id)->get();
  93. if (!empty($productSpec)) {
  94. $agentSpecArr = [];
  95. foreach ($productSpec as $spec) {
  96. $agentSpecArr[] = [
  97. 'agent_product_id' => $agentProduct->id,
  98. 'product_spec_id' => $spec->id,
  99. 'stock' => $spec->stock,
  100. 'original_price' => $spec->original_price,
  101. 'price' => $spec->price,
  102. ];
  103. }
  104. $agentProduct->spec()->createMany($agentSpecArr);
  105. }
  106. DB::commit();
  107. Log::info($agent_id . "自动上架{$product->id}成功!");
  108. if(env('SMS_SWITCH','') == true) {
  109. $phone = Agent::query()->where('id',$agent_id)->value('contact_mobile');
  110. (new SmsService)->send('auto_shelves',[$product->supplier->name,now(),SmsTraits::$systeaNameText['agent']],[$phone]);
  111. }
  112. } catch (\Exception $e) {
  113. DB::rollBack();
  114. Log::error('自动上架失败::' . $e->getTraceAsString());
  115. throw new \Exception('自动上架失败::' . $e->getTraceAsString());
  116. }
  117. }
  118. }