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

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