CalcPostWeight.php 2.1 KB

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