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

104 lines
3.0 KiB

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