Pārlūkot izejas kodu

更新内容用户状态

wzq 5 gadi atpakaļ
vecāks
revīzija
a9fce88381

+ 5 - 6
app/Console/Commands/UpdatePostInfo.php

@@ -10,9 +10,6 @@ namespace App\Console\Commands;
 
 use App\Models\Post;
 use Illuminate\Console\Command;
-use Illuminate\Support\Carbon;
-use Illuminate\Support\Facades\DB;
-use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\Redis;
 
 class UpdatePostInfo extends Command
@@ -51,10 +48,11 @@ class UpdatePostInfo extends Command
     {
         $this->line("开始更新内容信息");
 
-        $this->post->chunk(100, function($posts){
+        $bar = $this->output->createProgressBar($this->post->count());
+        $this->post->chunk(100, function($posts) use ($bar){
             foreach($posts as $post){
+                $bar->advance();
                 if(!$post->data) continue;
-//                echo $post->data->pv;exit;
                 Redis::HSET('post_info_'.$post->id,
                     'id', $post->id,
                     'uid', $post->uid,
@@ -80,7 +78,8 @@ class UpdatePostInfo extends Command
             }
             usleep(100000);
         });
-        $this->line("更新内容信息结束");
+        $bar->finish();
+        $this->line("\n更新内容信息结束");
 
     }
 }

+ 89 - 0
app/Console/Commands/UpdatePostStatus.php

@@ -0,0 +1,89 @@
+<?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 Illuminate\Console\Command;
+use Illuminate\Support\Facades\Redis;
+
+class UpdatePostStatus extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'post:update_status';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '更新内容用户状态';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct(Post $post,
+                                PostLike $postLike,
+                                PostDislike $postDislike,
+                                PostCollect $postCollect)
+    {
+        parent::__construct();
+        $this->post = $post;
+        $this->postLike = $postLike;
+        $this->postDislike = $postDislike;
+        $this->postCollect = $postCollect;
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->line("开始更新内容用户状态");
+
+        $bar = $this->output->createProgressBar($this->post->count());
+        $this->post->chunk(100, function($posts) use ($bar){
+            foreach($posts as $post) {
+                //点赞
+                $likeUid = $this->postLike->where('post_id', $post->id)->pluck('uid')->toArray();
+                if($likeUid){
+                    $likeKey = 'post_like_'.$post->id;
+                    Redis::SADD($likeKey, $likeUid);
+                }
+                //不喜欢
+                $dislikeUid = $this->postDislike->where('post_id', $post->id)->pluck('uid')->toArray();
+                if($dislikeUid){
+                    $unlikeKey = 'post_unlike_'.$post->id;
+                    Redis::SADD($unlikeKey, $dislikeUid);
+                }
+                //收藏
+                $collectUid = $this->postCollect->where('post_id', $post->id)->pluck('uid')->toArray();
+                if($collectUid){
+                    $collectKey = 'post_collect_'.$post->id;
+                    Redis::SADD($collectKey, $collectUid);
+                }
+                $bar->advance();
+            }
+            usleep(100000);
+        });
+        $bar->finish();
+        $this->line("\n更新内容用户状态结束");
+
+    }
+}

+ 2 - 0
app/Console/Kernel.php

@@ -11,6 +11,7 @@ use App\Console\Commands\PostCreateBean;
 use App\Console\Commands\PostStatistics;
 use App\Console\Commands\PostYesterday;
 use App\Console\Commands\UpdatePostInfo;
+use App\Console\Commands\UpdatePostStatus;
 use App\Console\Commands\VirusAdd;
 use Illuminate\Console\Scheduling\Schedule;
 use Laravel\Lumen\Console\Kernel as ConsoleKernel;
@@ -31,6 +32,7 @@ class Kernel extends ConsoleKernel
         PostCreateBean::class,
         PostCollectBean::class,
         UpdatePostInfo::class,
+        UpdatePostStatus::class,
         CommunityMemberStatistics::class,
         MusicImport::class
     ];

+ 16 - 0
app/Models/PostCollect.php

@@ -0,0 +1,16 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/8/13
+ * Time: 11:52
+ */
+namespace App\Models;
+use Illuminate\Database\Eloquent\Model;
+
+class PostCollect extends Model
+{
+//
+    protected $table = 'post_collect';
+    protected $guarded = [];
+}

+ 16 - 0
app/Models/PostDislike.php

@@ -0,0 +1,16 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/8/13
+ * Time: 11:52
+ */
+namespace App\Models;
+use Illuminate\Database\Eloquent\Model;
+
+class PostDislike extends Model
+{
+//
+    protected $table = 'post_dislike';
+    protected $guarded = [];
+}

+ 16 - 0
app/Models/PostLike.php

@@ -0,0 +1,16 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/8/13
+ * Time: 11:52
+ */
+namespace App\Models;
+use Illuminate\Database\Eloquent\Model;
+
+class PostLike extends Model
+{
+//
+    protected $table = 'post_like';
+    protected $guarded = [];
+}