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

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