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

132 lines
3.9 KiB

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. //自动上架
  40. $agentIds = AgentProductItem::query()
  41. ->withoutGlobalScope('orderById')
  42. ->where('supplier_id', $product->supplier_id)
  43. ->whereHas('agent',function($query) {
  44. $query->where('type','!=',AgentType::CLUSTER);
  45. })
  46. ->distinct()
  47. ->pluck('agent_id');
  48. foreach ($agentIds as $v) {
  49. //如果没开启自动上架 滚蛋
  50. if (empty(AgentSetting::val($v, 'auto_shelves'))) {
  51. continue;
  52. }
  53. //如果已经有这个产品 滚蛋
  54. if(AgentProductItem::query()->where(['product_id' => $product->id,'agent_id' => $v])->exists()){
  55. continue;
  56. }
  57. //啥有没有 自动添加商品
  58. DB::beginTransaction();
  59. try {
  60. $agentProduct = new AgentProduct();
  61. $agentProduct->title = $product->title;
  62. $agentProduct->agent_id = $v;
  63. $agentProduct->product_id = $product->id;
  64. $agentProduct->product_ids = $product->id;
  65. $agentProduct->stock = $product->stock;
  66. $agentProduct->status = 1;
  67. $agentProduct->pictures = $product->pictures;
  68. $agentProduct->content = $product->content;
  69. $agentProduct->know = $product->know;
  70. //计算价格
  71. $profit = AgentSetting::val($v, 'profit') ?? 0;
  72. $price = bcmul($product->price, bcdiv($profit + 100, 100, 6), 6);
  73. $agentProduct->price = $price;
  74. $agentProduct->original_price = $price;
  75. //自动添加分类
  76. $autoCategory = AgentSetting::val($v, 'auto_category') ?? 0;
  77. if (!empty($autoCategory)) {
  78. $categoryName = Category::query()->where('id', $product->category_id)->value('name');
  79. $category = Category::query()->firstOrCreate(['agent_id' => $v, 'name' => $categoryName]);
  80. $agentProduct->category_id = $category->id;
  81. }
  82. $agentProduct->save();
  83. //维护关联表
  84. $agentProductItem = AgentProductItem::query()->create([
  85. 'agent_id' => $v,
  86. 'supplier_id' => $product->supplier_id,
  87. 'agent_product_id' => $agentProduct->id,
  88. 'product_id' => $product->id,
  89. ]);
  90. //规格
  91. $productSpec = ProductSpec::query()->where('product_id',$product->id)->get();
  92. if (!empty($productSpec)) {
  93. $agentSpecArr = [];
  94. foreach ($productSpec as $spec) {
  95. $agentSpecArr[] = [
  96. 'agent_product_id' => $agentProduct->id,
  97. 'product_spec_id' => $spec->id,
  98. 'stock' => $spec->stock,
  99. 'original_price' => bcmul($spec->price, bcdiv($profit + 100, 100, 6), 6),
  100. 'price' => bcmul($spec->price, bcdiv($profit + 100, 100, 6), 6),
  101. ];
  102. }
  103. $agentProduct->spec()->createMany($agentSpecArr);
  104. }
  105. DB::commit();
  106. if(env('SMS_SWITCH','') == true) {
  107. $phone = Agent::query()->where('id',$v)->value('contact_phone');
  108. (new SmsService)->send('auto_shelves',[$product->supplier->name,now(),SmsTraits::$systeaNameText['agent']],[$phone]);
  109. }
  110. } catch (\Exception $e) {
  111. Log::error('自动上架失败::' . $e->getTraceAsString());
  112. DB::rollBack();
  113. throw new \Exception('自动上架失败::' . $e->getTraceAsString());
  114. }
  115. }
  116. }
  117. });
  118. }
  119. }