From f90cacdd017243c868333d2ac8019d7e2c59bec8 Mon Sep 17 00:00:00 2001 From: liapples Date: Wed, 10 Nov 2021 22:53:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E4=B8=8A=E6=9E=B6=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E9=98=9F=E5=88=97=EF=BC=8C=E5=B9=B6=E7=A7=BB=E5=88=B0?= =?UTF-8?q?Product=20model=E9=87=8C=E9=9D=A2=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Jobs/AgentProductShelve.php | 5 +- app/Jobs/ProductSaved.php | 74 ++++++++++++++++++++++++ app/Models/Product.php | 18 +++++- app/Providers/ProductServiceProvider.php | 60 ------------------- config/app.php | 1 - 5 files changed, 93 insertions(+), 65 deletions(-) create mode 100644 app/Jobs/ProductSaved.php delete mode 100644 app/Providers/ProductServiceProvider.php diff --git a/app/Jobs/AgentProductShelve.php b/app/Jobs/AgentProductShelve.php index 7958f26..747226f 100644 --- a/app/Jobs/AgentProductShelve.php +++ b/app/Jobs/AgentProductShelve.php @@ -120,15 +120,14 @@ class AgentProductShelve implements ShouldQueue $agentProduct->spec()->createMany($agentSpecArr); } DB::commit(); - Log::info($agent_id . "自动上架{$product->id}成功!"); if(env('SMS_SWITCH','') == true) { $phone = Agent::query()->where('id',$agent_id)->value('contact_mobile'); (new SmsService)->send('auto_shelves',[$product->supplier->name,now(),SmsTraits::$systeaNameText['agent']],[$phone]); } } catch (\Exception $e) { DB::rollBack(); - Log::error('自动上架失败::' . $e->getTraceAsString()); - throw new \Exception('自动上架失败::' . $e->getTraceAsString()); + Log::error('自动上架失败::' . $e->getMessage()); + throw new \Exception('自动上架失败::' . $e->getMessage()); } } } diff --git a/app/Jobs/ProductSaved.php b/app/Jobs/ProductSaved.php new file mode 100644 index 0000000..30614ef --- /dev/null +++ b/app/Jobs/ProductSaved.php @@ -0,0 +1,74 @@ +product; + /** 同步信息到代理商产品 START */ + AgentProduct::query() + ->where(['type' => 0, 'product_id' => $product->id]) + ->update([ + 'title' => $product->title, + 'know' => $product->know, + 'content' => $product->content, + 'pictures' => $product->pictures, + ]); + /** 同步信息到代理商产品 END */ + + /** 自动上架 START */ + Agent::query()->where([ + ['id', '>', 1], //1是总管理员,不处理 + ['type', '<>', AgentType::OPERATOR], + ['status', '=', UserStatus::NORMAL] + ])->select(['id'])->chunk(100, function ($agents) use ($product) { + foreach ($agents as $agent) { + AgentProductShelve::dispatch($agent['id'], $product->id); + } + }); + /** 自动上架 END */ + } +} diff --git a/app/Models/Product.php b/app/Models/Product.php index e9ca82e..eccca4c 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -2,6 +2,8 @@ namespace App\Models; +use App\Common\ProductStatus; +use App\Jobs\ProductSaved; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\SoftDeletes; @@ -17,7 +19,21 @@ class Product extends BaseModel protected $guarded = ['id']; //不允许编辑的字段 protected $casts = ['extends' => 'json']; - // 获取所有产品图片 + /** + * 模型的 "booted" 方法 + * @return void + */ + protected static function booted() + { + parent::booted(); + static::saved(function ($product) { + if ($product->isDirty('status') && $product->status == ProductStatus::ON_SALE) { + ProductSaved::dispatch($product); + } + }); + } + + // 获取所有产品图片 public function getPicturesAttribute($value): array { if (is_string($value)) { diff --git a/app/Providers/ProductServiceProvider.php b/app/Providers/ProductServiceProvider.php deleted file mode 100644 index d346707..0000000 --- a/app/Providers/ProductServiceProvider.php +++ /dev/null @@ -1,60 +0,0 @@ -isDirty('status') && $product->status == ProductStatus::ON_SALE) { - /** 同步信息到代理商产品 START */ - AgentProduct::query() - ->where(['type' => 0, 'product_id' => $product->id]) - ->update([ - 'title' => $product->title, - 'know' => $product->know, - 'content' => $product->content, - 'pictures' => $product->pictures, - ]); - /** 同步信息到代理商产品 END */ - - /** 自动上架 START */ - Agent::query()->where([ - ['id', '>', 1], //1是总管理员,不处理 - ['type', '<>', AgentType::OPERATOR], - ['status', '=', UserStatus::NORMAL] - ])->select(['id'])->chunk(100, function ($agents) use ($product) { - foreach ($agents as $agent) { - AgentProductShelve::dispatch($agent['id'], $product->id); - } - }); - /** 自动上架 END */ - } - }); - } -} diff --git a/config/app.php b/config/app.php index d479b6b..09aecc1 100644 --- a/config/app.php +++ b/config/app.php @@ -175,7 +175,6 @@ return [ App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, App\Providers\DemandBiddingServiceProvider::class, - App\Providers\ProductServiceProvider::class, ], /*