5 changed files with 93 additions and 65 deletions
			
			
		- 
					5app/Jobs/AgentProductShelve.php
 - 
					74app/Jobs/ProductSaved.php
 - 
					18app/Models/Product.php
 - 
					60app/Providers/ProductServiceProvider.php
 - 
					1config/app.php
 
@ -0,0 +1,74 @@ | 
			
		|||||
 | 
				<?php | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				namespace App\Jobs; | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				use App\Common\AgentType; | 
			
		||||
 | 
				use App\Common\UserStatus; | 
			
		||||
 | 
				use App\Models\Agent; | 
			
		||||
 | 
				use App\Models\AgentProduct; | 
			
		||||
 | 
				use App\Models\Product; | 
			
		||||
 | 
				use Illuminate\Bus\Queueable; | 
			
		||||
 | 
				use Illuminate\Contracts\Queue\ShouldBeUnique; | 
			
		||||
 | 
				use Illuminate\Contracts\Queue\ShouldQueue; | 
			
		||||
 | 
				use Illuminate\Foundation\Bus\Dispatchable; | 
			
		||||
 | 
				use Illuminate\Queue\InteractsWithQueue; | 
			
		||||
 | 
				use Illuminate\Queue\SerializesModels; | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				class ProductSaved implements ShouldQueue | 
			
		||||
 | 
				{ | 
			
		||||
 | 
				    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | 
			
		||||
 | 
				
 | 
			
		||||
 | 
					/** | 
			
		||||
 | 
					 * 任务可尝试的次数 | 
			
		||||
 | 
					 * @var int | 
			
		||||
 | 
					 */ | 
			
		||||
 | 
					public $tries = 3; | 
			
		||||
 | 
				
 | 
			
		||||
 | 
					/** | 
			
		||||
 | 
					 * 任务失败前允许的最大异常数 | 
			
		||||
 | 
					 * @var int | 
			
		||||
 | 
					 */ | 
			
		||||
 | 
					public $maxExceptions = 3; | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				    /** | 
			
		||||
 | 
				     * Create a new job instance. | 
			
		||||
 | 
				     * | 
			
		||||
 | 
				     * @return void | 
			
		||||
 | 
				     */ | 
			
		||||
 | 
				    public function __construct(protected Product $product) | 
			
		||||
 | 
				    { | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				    } | 
			
		||||
 | 
				
 | 
			
		||||
 | 
				    /** | 
			
		||||
 | 
				     * Execute the job. | 
			
		||||
 | 
				     * | 
			
		||||
 | 
				     * @return void | 
			
		||||
 | 
				     */ | 
			
		||||
 | 
				    public function handle() | 
			
		||||
 | 
				    { | 
			
		||||
 | 
						$product = $this->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 */ | 
			
		||||
 | 
				    } | 
			
		||||
 | 
				} | 
			
		||||
@ -1,60 +0,0 @@ | 
			
		|||||
<?php | 
				 | 
			
		||||
 | 
				 | 
			
		||||
namespace App\Providers; | 
				 | 
			
		||||
 | 
				 | 
			
		||||
use App\Common\AgentType; | 
				 | 
			
		||||
use App\Common\ProductStatus; | 
				 | 
			
		||||
use App\Common\UserStatus; | 
				 | 
			
		||||
use App\Jobs\AgentProductShelve; | 
				 | 
			
		||||
use App\Models\AgentProduct; | 
				 | 
			
		||||
use App\Models\Agent; | 
				 | 
			
		||||
use App\Models\Product; | 
				 | 
			
		||||
use Illuminate\Support\ServiceProvider; | 
				 | 
			
		||||
 | 
				 | 
			
		||||
class ProductServiceProvider extends ServiceProvider | 
				 | 
			
		||||
{ | 
				 | 
			
		||||
	/** | 
				 | 
			
		||||
	 * Register services. | 
				 | 
			
		||||
	 * | 
				 | 
			
		||||
	 * @return void | 
				 | 
			
		||||
	 */ | 
				 | 
			
		||||
	public function register() | 
				 | 
			
		||||
	{ | 
				 | 
			
		||||
		//
 | 
				 | 
			
		||||
	} | 
				 | 
			
		||||
 | 
				 | 
			
		||||
	/** | 
				 | 
			
		||||
	 * Bootstrap services. | 
				 | 
			
		||||
	 * | 
				 | 
			
		||||
	 * @return void | 
				 | 
			
		||||
	 */ | 
				 | 
			
		||||
	public function boot() | 
				 | 
			
		||||
	{ | 
				 | 
			
		||||
		Product::saved(function ($product) { | 
				 | 
			
		||||
			if ($product->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 */ | 
				 | 
			
		||||
			} | 
				 | 
			
		||||
		}); | 
				 | 
			
		||||
	} | 
				 | 
			
		||||
} | 
				 | 
			
		||||
						Write
						Preview
					
					
					Loading…
					
					Cancel
						Save
					
		Reference in new issue