瀏覽代碼

评论回复数量

wzq 5 年之前
父節點
當前提交
dd805782fb
共有 3 個文件被更改,包括 74 次插入0 次删除
  1. 68 0
      app/Console/Commands/UpdateReplyCount.php
  2. 2 0
      app/Console/Kernel.php
  3. 4 0
      app/Repositories/Post/PostRepository.php

+ 68 - 0
app/Console/Commands/UpdateReplyCount.php

@@ -0,0 +1,68 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/8/13
+ * Time: 10:59
+ */
+namespace App\Console\Commands;
+
+
+use App\Models\PostComment;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Redis;
+
+class UpdateReplyCount extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'post:update_reply_count';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '更新评论回复数量';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct(PostComment $postComment)
+    {
+        parent::__construct();
+        $this->postComment = $postComment;
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->line("开始更新评论回复数量");
+
+        $bar = $this->output->createProgressBar($this->postComment->where('parent_id', 0)->count());
+        $this->postComment->where('parent_id', 0)->chunk(100, function($comments) use ($bar){
+            foreach($comments as $comment){
+                $replyCount = $this->postComment->where('parent_id', $comment->id)->count();
+                if($replyCount){
+                    $comment->reply_count = $replyCount;
+                    $comment->save();
+                }
+
+                $bar->advance();
+            }
+            usleep(100000);
+        });
+        $bar->finish();
+        $this->line("\n更新评论回复数量结束");
+
+    }
+}

+ 2 - 0
app/Console/Kernel.php

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

+ 4 - 0
app/Repositories/Post/PostRepository.php

@@ -431,6 +431,10 @@ class PostRepository
         try {
         try {
             $comment = $this->postComment->create($data);
             $comment = $this->postComment->create($data);
 
 
+            if($comment->parent_id){
+                $this->postComment->where('parent_id', $comment->parent_id)->increment('reply_count');
+            }
+
             DB::commit();
             DB::commit();
 
 
             if(!$comment->parent_id){
             if(!$comment->parent_id){