CalcCircleMessageWeight.php 2.0 KB

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