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

102 lines
2.9 KiB

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