xielin 5 yıl önce
ebeveyn
işleme
0fbd7e8d16

+ 74 - 0
app/Console/Commands/CalcCircleMessageWeight.php

@@ -0,0 +1,74 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Models\InterestCircleMessage;
+use App\Models\Post;
+use App\Models\PostData;
+use Carbon\Carbon;
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Redis;
+
+class CalcCircleMessageWeight extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'circle:calc_weight';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '计算提问权重';
+
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $this->line(date('Y-m-d H:i:s') . '开始计算权重');
+        $key = "community_calc_circle_score";
+        $postIds = Redis::smembers($key);
+        Log::debug('提问ID:' . json_encode($postIds));
+        foreach ($postIds as $postId) {
+            $info = InterestCircleMessage::where("id", $postId)->first();
+            if (empty($info)) {
+                continue;
+            }
+            Log::debug('帖子:' . json_encode($info));
+            $temp = (
+                (3 * $info->good) +
+                (10 * $info->comment_count) +
+                (2 * $info->bad));
+            $fresh = (Carbon::parse($info['created_at'])->timestamp) - (Carbon::parse("2019-10-08 00:00:00")->timestamp);
+            if ($temp) {
+                $score = log10($temp) + $fresh / 43200 * 14;
+            } else {
+                $score = $fresh / 43200;
+            }
+            $info->weight = $score;
+            $info->save();
+            Redis::srem($key, $postId);
+            Log::debug(date("Y-m-d H:i:s") . "设置提问" . $info->id . "的权重分为:" . $score);
+        }
+        $this->line(date('Y-m-d H:i:s') . ' 计算权重结束');
+    }
+}

+ 5 - 1
app/Console/Kernel.php

@@ -4,6 +4,7 @@ namespace App\Console;
 
 use App\Console\Commands\Apollo;
 use App\Console\Commands\BehaviorRecord;
+use App\Console\Commands\CalcCircleMessageWeight;
 use App\Console\Commands\CalcPostWeight;
 use App\Console\Commands\ContentFeedCreate;
 use App\Console\Commands\ContentFeedDelete;
@@ -32,7 +33,8 @@ class Kernel extends ConsoleKernel
         DelPostNewReply::class,
         DelPostNewComment::class,
         DelMemberRepeatTopic::class,
-        RankingList::class
+        RankingList::class,
+        CalcCircleMessageWeight::class
     ];
 
     /**
@@ -52,5 +54,7 @@ class Kernel extends ConsoleKernel
         $schedule->command('ranking:list')
             ->dailyAt('00:05')
             ->withoutOverlapping()->appendOutputTo($path);
+        $schedule->command('circle:calc_weight')
+            ->everyFiveMinutes()->withoutOverlapping()->appendOutputTo($path);
     }
 }

+ 4 - 0
app/Repositories/Circle/CircleMessageRepository.php

@@ -272,6 +272,8 @@ class CircleMessageRepository
             } else {
                 Redis::DEL('circle_message_new_comment_' . $newComment->post_id);
             }
+            $key = "community_calc_circle_score";
+            Redis::sadd($key,$request['msg_id']);
             return jsonSuccess(['id' => $newComment->id], '评论成功');
 
         } catch (QueryException $exception) {
@@ -369,6 +371,8 @@ class CircleMessageRepository
             }
 
             DB::commit();
+            $key = "community_calc_circle_score";
+            Redis::sadd($key,$request['msg_id']);
             return jsonSuccess();
         } catch (QueryException $exception) {
             DB::rollBack();