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

143 lines
4.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Providers;
  3. use App\Common\AgentType;
  4. use App\Common\ProductStatus;
  5. use App\Models\AgentProduct;
  6. use App\Models\AgentProductItem;
  7. use App\Models\AgentProductSpec;
  8. use App\Models\AgentSetting;
  9. use App\Models\Category;
  10. use App\Models\Agent;
  11. use App\Models\Product;
  12. use App\Models\ProductSpec;
  13. use App\Service\SmsService;
  14. use App\Traits\SmsTraits;
  15. use Illuminate\Database\Eloquent\Model;
  16. use Illuminate\Support\Facades\DB;
  17. use Illuminate\Support\Facades\Log;
  18. use Illuminate\Support\ServiceProvider;
  19. class ProductServiceProvider extends ServiceProvider
  20. {
  21. /**
  22. * Register services.
  23. *
  24. * @return void
  25. */
  26. public function register()
  27. {
  28. //
  29. }
  30. /**
  31. * Bootstrap services.
  32. *
  33. * @return void
  34. */
  35. public function boot()
  36. {
  37. Product::updated(function ($product) {
  38. if ($product->isDirty('status') && $product->status == ProductStatus::ON_SALE) {
  39. /** 同步信息到代理商产品 START */
  40. AgentProduct::query()
  41. ->where(['type' => 0, 'product_id' => $product->id])
  42. ->update([
  43. 'title' => $product->title,
  44. 'know' => $product->know,
  45. 'content' => $product->content,
  46. 'pictures' => $product->pictures,
  47. ]);
  48. /** 同步信息到代理商产品 END */
  49. /** 自动上架 START */
  50. $agentIds = AgentProductItem::query()
  51. ->withoutGlobalScope('orderById')
  52. ->where('supplier_id', $product->supplier_id)
  53. ->whereHas('agent',function($query) {
  54. $query->where('type','!=',AgentType::OPERATOR);
  55. })
  56. ->distinct()
  57. ->pluck('agent_id');
  58. foreach ($agentIds as $v) {
  59. //如果没开启自动上架 滚蛋
  60. if (empty(AgentSetting::val($v, 'auto_shelves'))) {
  61. continue;
  62. }
  63. //如果已经有这个产品 滚蛋
  64. if(AgentProductItem::query()->where(['product_id' => $product->id,'agent_id' => $v])->exists()){
  65. continue;
  66. }
  67. //啥有没有 自动添加商品
  68. DB::beginTransaction();
  69. try {
  70. $agentProduct = new AgentProduct();
  71. $agentProduct->title = $product->title;
  72. $agentProduct->agent_id = $v;
  73. $agentProduct->product_id = $product->id;
  74. $agentProduct->product_ids = $product->id;
  75. $agentProduct->stock = $product->stock;
  76. $agentProduct->status = 1;
  77. $agentProduct->pictures = $product->pictures;
  78. $agentProduct->content = $product->content;
  79. $agentProduct->know = $product->know;
  80. //计算价格
  81. // $profit = AgentSetting::val($v, 'profit') ?? 0;
  82. // $price = bcmul($product->price, bcdiv($profit + 100, 100, 6), 6);
  83. $agentProduct->price = $product->price;
  84. $agentProduct->original_price = $product->original_price;
  85. //自动添加分类
  86. $autoCategory = AgentSetting::val($v, 'auto_category') ?? 0;
  87. if (!empty($autoCategory)) {
  88. $categoryName = Category::query()->where('id', $product->category_id)->value('name');
  89. $category = Category::query()->firstOrCreate(['agent_id' => $v, 'name' => $categoryName]);
  90. $agentProduct->category_id = $category->id;
  91. }
  92. $agentProduct->save();
  93. //维护关联表
  94. $agentProductItem = AgentProductItem::query()->create([
  95. 'agent_id' => $v,
  96. 'supplier_id' => $product->supplier_id,
  97. 'agent_product_id' => $agentProduct->id,
  98. 'product_id' => $product->id,
  99. ]);
  100. //规格
  101. $productSpec = ProductSpec::query()->where('product_id',$product->id)->get();
  102. if (!empty($productSpec)) {
  103. $agentSpecArr = [];
  104. foreach ($productSpec as $spec) {
  105. $agentSpecArr[] = [
  106. 'agent_product_id' => $agentProduct->id,
  107. 'product_spec_id' => $spec->id,
  108. 'stock' => $spec->stock,
  109. 'original_price' => $spec->original_price,
  110. 'price' => $spec->price,
  111. ];
  112. }
  113. $agentProduct->spec()->createMany($agentSpecArr);
  114. }
  115. DB::commit();
  116. if(env('SMS_SWITCH','') == true) {
  117. $phone = Agent::query()->where('id',$v)->value('contact_mobile');
  118. (new SmsService)->send('auto_shelves',[$product->supplier->name,now(),SmsTraits::$systeaNameText['agent']],[$phone]);
  119. }
  120. } catch (\Exception $e) {
  121. Log::error('自动上架失败::' . $e->getTraceAsString());
  122. DB::rollBack();
  123. throw new \Exception('自动上架失败::' . $e->getTraceAsString());
  124. }
  125. }
  126. /** 自动上架 END */
  127. }
  128. });
  129. }
  130. }