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, ], /*