1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace App\Console\Commands;
- 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 CalcPostWeight extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'post: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_post_score";
- $postIds = Redis::smembers($key);
- Log::debug('权重帖子ID:' . json_encode($postIds));
- foreach ($postIds as $postId) {
- $postInfo = PostData::where("post_id", $postId)->first();
- $post = Post::find($postId);
- if(empty($postInfo) || empty($post)){
- continue;
- }
- Log::debug('帖子:' . json_encode($postInfo));
- $temp = (0.2 * $postInfo['pv'] +
- (5 * $postInfo->share_count) +
- (3 * $postInfo->praise_count) +
- (10 * $postInfo->collect_count) +
- (5 * $postInfo->comment_count) -
- (23 * $postInfo->dislike_count));
- $fresh = (Carbon::parse($postInfo['created_at'])->timestamp) - (Carbon::parse("2019-05-01 00:00:00")->timestamp);
- if($temp){
- $score = log10($temp) + $fresh / 43200;
- }else{
- $score = $fresh / 43200;
- }
- $post->weight = $score;
- $post->save();
- Redis::srem($key,$postId);
- Log::debug(date("Y-m-d H:i:s") . "设置帖子" . $postInfo->post_id . "的权重分为:" . $score);
- }
- $this->line(date('Y-m-d H:i:s').' 计算权重结束');
- }
- }
|