UpdateReplyCount.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/8/13
  6. * Time: 10:59
  7. */
  8. namespace App\Console\Commands;
  9. use App\Models\PostComment;
  10. use Illuminate\Console\Command;
  11. use Illuminate\Support\Facades\Redis;
  12. class UpdateReplyCount extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'post:update_reply_count';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = '更新评论回复数量';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct(PostComment $postComment)
  32. {
  33. parent::__construct();
  34. $this->postComment = $postComment;
  35. }
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return mixed
  40. */
  41. public function handle()
  42. {
  43. $this->line("开始更新评论回复数量");
  44. $bar = $this->output->createProgressBar($this->postComment->where('parent_id', 0)->count());
  45. $this->postComment->where('parent_id', 0)->chunk(100, function($comments) use ($bar){
  46. foreach($comments as $comment){
  47. $replyCount = $this->postComment->where('parent_id', $comment->id)->count();
  48. if($replyCount){
  49. $comment->reply_count = $replyCount;
  50. $comment->save();
  51. }
  52. $bar->advance();
  53. }
  54. usleep(100000);
  55. });
  56. $bar->finish();
  57. $this->line("\n更新评论回复数量结束");
  58. }
  59. }