12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/8/13
- * Time: 11:50
- */
- namespace App\Console\Commands;
- use App\Models\Post;
- use App\Models\PostCollect;
- use App\Models\PostDislike;
- use App\Models\PostLike;
- use App\Models\Topic;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Redis;
- class UpdateTopicUseCount extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'post: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更新话题使用数结束");
- }
- }
|