Ver código fonte

更新话题数据

wzq 5 anos atrás
pai
commit
16e162bbc8
2 arquivos alterados com 78 adições e 0 exclusões
  1. 76 0
      app/Console/Commands/UpdateTopicData.php
  2. 2 0
      app/Console/Kernel.php

+ 76 - 0
app/Console/Commands/UpdateTopicData.php

@@ -0,0 +1,76 @@
+<?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更新话题数据结束");
+
+
+    }
+}

+ 2 - 0
app/Console/Kernel.php

@@ -14,6 +14,7 @@ use App\Console\Commands\TopicUseCount;
 use App\Console\Commands\UpdatePostInfo;
 use App\Console\Commands\UpdatePostStatus;
 use App\Console\Commands\UpdateReplyCount;
+use App\Console\Commands\UpdateTopicData;
 use App\Console\Commands\UpdateTopicUseCount;
 use App\Console\Commands\VirusAdd;
 use Illuminate\Console\Scheduling\Schedule;
@@ -38,6 +39,7 @@ class Kernel extends ConsoleKernel
         UpdatePostStatus::class,
         UpdateReplyCount::class,
         UpdateTopicUseCount::class,
+        UpdateTopicData::class,
         TopicUseCount::class,
         CommunityMemberStatistics::class,
         MusicImport::class