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

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