CalcPostWeight.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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('开始计算权重');
  39. $key = "community_calc_post_score";
  40. $postIds = Redis::smembers($key);
  41. foreach ($postIds as $postId) {
  42. $postInfo = PostData::where("post_id", $postId)->first();
  43. $temp = $postInfo['pv'] +
  44. (5 * $postInfo->share_cout) +
  45. (2 * $postInfo->praise_count) +
  46. (10 * $postInfo->collect_count) +
  47. (3 * $postInfo->comment_count) -
  48. (10 * $postInfo->dislike_count);
  49. $fresh = (Carbon::parse($postInfo['created_at'])->timestamp) - (Carbon::parse("2019-05-01 00:00:00")->timestamp);
  50. $score = log10($temp) + $fresh / 86400;
  51. $postInfo->weight = $score;
  52. $postInfo->save();
  53. $this->line(date("Y-m-d H:i:s")."设置帖子".$postInfo->post_id."的权重分为:".$score);
  54. }
  55. }
  56. }