UpdatePostStatus.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/8/13
  6. * Time: 11:50
  7. */
  8. namespace App\Console\Commands;
  9. use App\Models\Post;
  10. use App\Models\PostCollect;
  11. use App\Models\PostDislike;
  12. use App\Models\PostLike;
  13. use Illuminate\Console\Command;
  14. use Illuminate\Support\Facades\Redis;
  15. class UpdatePostStatus extends Command
  16. {
  17. /**
  18. * The name and signature of the console command.
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'post:update_status';
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = '更新内容用户状态';
  29. /**
  30. * Create a new command instance.
  31. *
  32. * @return void
  33. */
  34. public function __construct(Post $post,
  35. PostLike $postLike,
  36. PostDislike $postDislike,
  37. PostCollect $postCollect)
  38. {
  39. parent::__construct();
  40. $this->post = $post;
  41. $this->postLike = $postLike;
  42. $this->postDislike = $postDislike;
  43. $this->postCollect = $postCollect;
  44. }
  45. /**
  46. * Execute the console command.
  47. *
  48. * @return mixed
  49. */
  50. public function handle()
  51. {
  52. $this->line("开始更新内容用户状态");
  53. $bar = $this->output->createProgressBar($this->post->withTrashed()->count());
  54. $this->post->withTrashed()->chunk(100, function($posts) use ($bar){
  55. foreach($posts as $post) {
  56. //点赞
  57. $likeUid = $this->postLike->where('post_id', $post->id)->pluck('uid')->toArray();
  58. if($likeUid){
  59. $likeKey = 'post_like_'.$post->id;
  60. Redis::SADD($likeKey, $likeUid);
  61. }
  62. //不喜欢
  63. $dislikeUid = $this->postDislike->where('post_id', $post->id)->pluck('uid')->toArray();
  64. if($dislikeUid){
  65. $unlikeKey = 'post_unlike_'.$post->id;
  66. Redis::SADD($unlikeKey, $dislikeUid);
  67. }
  68. //收藏
  69. $collectUid = $this->postCollect->where('post_id', $post->id)->pluck('uid')->toArray();
  70. if($collectUid){
  71. $collectKey = 'post_collect_'.$post->id;
  72. Redis::SADD($collectKey, $collectUid);
  73. }
  74. $bar->advance();
  75. }
  76. usleep(100000);
  77. });
  78. $bar->finish();
  79. $this->line("\n更新内容用户状态结束");
  80. }
  81. }