CalcPostWeight.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\PostData;
  4. use Carbon\Carbon;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Log;
  7. use Illuminate\Support\Facades\Redis;
  8. class CalcPostWeight extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'post:calc_weight';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '计算内容权重';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $this->line(date('Y-m-d H:i:s').'开始计算权重');
  39. $key = "community_calc_post_score";
  40. $postIds = Redis::smembers($key);
  41. Log::debug('权重帖子ID:' . json_encode($postIds));
  42. foreach ($postIds as $postId) {
  43. $postInfo = PostData::where("post_id", $postId)->first();
  44. Log::debug('帖子:' . json_encode($postInfo));
  45. $temp = ($postInfo['pv'] +
  46. (5 * $postInfo->share_cout) +
  47. (2 * $postInfo->praise_count) +
  48. (10 * $postInfo->collect_count) +
  49. (3 * $postInfo->comment_count) -
  50. (10 * $postInfo->dislike_count));
  51. $fresh = (Carbon::parse($postInfo['created_at'])->timestamp) - (Carbon::parse("2019-05-01 00:00:00")->timestamp);
  52. $score = log10($temp) + $fresh / 86400;
  53. $postInfo->weight = $score;
  54. $postInfo->save();
  55. Redis::srem($key,$postId);
  56. Log::debug(date("Y-m-d H:i:s") . "设置帖子" . $postInfo->post_id . "的权重分为:" . $score);
  57. }
  58. $this->line(date('Y-m-d H:i:s').' 计算权重结束');
  59. }
  60. }