123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace App\Console\Commands;
- 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();
- if(empty($postInfo)){
- continue;
- }
- Log::debug('帖子:' . json_encode($postInfo));
- $temp = ($postInfo['pv'] +
- (5 * $postInfo->share_count) +
- (2 * $postInfo->praise_count) +
- (10 * $postInfo->collect_count) +
- (3 * $postInfo->comment_count) -
- (10 * $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 / 86400;
- }else{
- $score = $fresh / 86400;
- }
- $postInfo->weight = $score;
- $postInfo->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').' 计算权重结束');
- }
- }
|