UpdateTopicData.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\MemberFollowTopic;
  10. use App\Models\Post;
  11. use App\Models\Topic;
  12. use Illuminate\Console\Command;
  13. class UpdateTopicData extends Command
  14. {
  15. /**
  16. * The name and signature of the console command.
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'post:update_topic_data';
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = '更新话题数据';
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct(Post $post,
  33. MemberFollowTopic $memberFollowTopic,
  34. Topic $topic)
  35. {
  36. parent::__construct();
  37. $this->post = $post;
  38. $this->memberFollowTopic = $memberFollowTopic;
  39. $this->topic = $topic;
  40. }
  41. /**
  42. * Execute the console command.
  43. *
  44. * @return mixed
  45. */
  46. public function handle()
  47. {
  48. $this->line("开始更新话题数据");
  49. $bar = $this->output->createProgressBar($this->topic->count());
  50. $this->topic->chunk(10, function($topics) use ($bar){
  51. foreach($topics as $topic) {
  52. $topic->follow_count = $this->memberFollowTopic->where('topic_id', $topic->id)->count();
  53. $topic->pv = $this->post->withTrashed()
  54. ->join('post_data', 'post_data.post_id', '=', 'post.id')
  55. ->select('post_data.pv')
  56. ->whereRaw('FIND_IN_SET('.$topic->id.', topic_ids)')
  57. ->sum('pv');
  58. $topic->save();
  59. $bar->advance();
  60. }
  61. usleep(100000);
  62. });
  63. $bar->finish();
  64. $this->line("\n更新话题数据结束");
  65. }
  66. }