123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/8/13
- * Time: 11:50
- */
- namespace App\Console\Commands;
- use App\Models\Post;
- use App\Models\Topic;
- use Illuminate\Console\Command;
- class UpdateTopicUseCount extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'post:update_topic_use_count';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '更新话题使用数';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct(Post $post,
- Topic $topic)
- {
- parent::__construct();
- $this->post = $post;
- $this->topic = $topic;
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $this->line("开始更新话题使用数");
- $topic = [];
- $bar = $this->output->createProgressBar($this->post->withTrashed()->count());
- $this->post->withTrashed()->chunk(100, function($posts) use ($bar, &$topic){
- foreach($posts as $post) {
- $topicIds = explode(',', $post->topic_ids);
- foreach($topicIds as $id){
- if(isset($topic[$id])){
- $topic[$id] += 1;
- }else{
- $topic[$id] = 1;
- }
- }
- $bar->advance();
- }
- usleep(100000);
- });
- foreach($topic as $id => $useCount){
- $this->topic->where('id', $id)->update(['use_count' => $useCount]);
- }
- $bar->finish();
- $this->line("\n更新话题使用数结束");
- }
- }
|