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

139 lines
3.9 KiB

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