1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/8/13
- * Time: 11:50
- */
- namespace App\Console\Commands;
- use App\Models\Post;
- use App\Models\PostCollect;
- use App\Models\PostDislike;
- use App\Models\PostLike;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Redis;
- class UpdatePostStatus extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'post:update_status';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '更新内容用户状态';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct(Post $post,
- PostLike $postLike,
- PostDislike $postDislike,
- PostCollect $postCollect)
- {
- parent::__construct();
- $this->post = $post;
- $this->postLike = $postLike;
- $this->postDislike = $postDislike;
- $this->postCollect = $postCollect;
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $this->line("开始更新内容用户状态");
- $bar = $this->output->createProgressBar($this->post->withTrashed()->count());
- $this->post->withTrashed()->chunk(100, function($posts) use ($bar){
- foreach($posts as $post) {
- //点赞
- $likeUid = $this->postLike->where('post_id', $post->id)->pluck('uid')->toArray();
- if($likeUid){
- $likeKey = 'post_like_'.$post->id;
- Redis::SADD($likeKey, $likeUid);
- }
- //不喜欢
- $dislikeUid = $this->postDislike->where('post_id', $post->id)->pluck('uid')->toArray();
- if($dislikeUid){
- $unlikeKey = 'post_unlike_'.$post->id;
- Redis::SADD($unlikeKey, $dislikeUid);
- }
- //收藏
- $collectUid = $this->postCollect->where('post_id', $post->id)->pluck('uid')->toArray();
- if($collectUid){
- $collectKey = 'post_collect_'.$post->id;
- Redis::SADD($collectKey, $collectUid);
- }
- $bar->advance();
- }
- usleep(100000);
- });
- $bar->finish();
- $this->line("\n更新内容用户状态结束");
- }
- }
|