12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/8/13
- * Time: 11:50
- */
- namespace App\Console\Commands;
- use App\Models\MemberFollowTopic;
- use App\Models\Post;
- use App\Models\Topic;
- use Illuminate\Console\Command;
- class UpdateTopicData extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'post:update_topic_data';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '更新话题数据';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct(Post $post,
- MemberFollowTopic $memberFollowTopic,
- Topic $topic)
- {
- parent::__construct();
- $this->post = $post;
- $this->memberFollowTopic = $memberFollowTopic;
- $this->topic = $topic;
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $this->line("开始更新话题数据");
- $bar = $this->output->createProgressBar($this->topic->count());
- $this->topic->chunk(10, function($topics) use ($bar){
- foreach($topics as $topic) {
- $topic->follow_count = $this->memberFollowTopic->where('topic_id', $topic->id)->count();
- $topic->pv = $this->post->withTrashed()
- ->join('post_data', 'post_data.post_id', '=', 'post.id')
- ->select('post_data.pv')
- ->whereRaw('FIND_IN_SET('.$topic->id.', topic_ids)')
- ->sum('pv');
- $topic->save();
- $bar->advance();
- }
- usleep(100000);
- });
- $bar->finish();
- $this->line("\n更新话题数据结束");
- }
- }
|