UpdateReplyCount.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. for($i=1;$i<1000;$i++){
  45. Redis::del('post_new_comment_'.$i);
  46. }
  47. $bar = $this->output->createProgressBar($this->postComment->where('parent_id', 0)->count());
  48. $this->postComment->where('parent_id', 0)->chunk(100, function($comments) use ($bar){
  49. foreach($comments as $comment){
  50. $replyCount = $this->postComment->where('parent_id', $comment->id)->count();
  51. if($replyCount){
  52. $comment->reply_count = $replyCount;
  53. $comment->save();
  54. }
  55. $bar->advance();
  56. }
  57. usleep(100000);
  58. });
  59. $bar->finish();
  60. $this->line("\n更新评论回复数量结束");
  61. }
  62. }