RankCommand.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Console\Commands\Goodnight;
  3. use App\Models\Goodnight\TopicModel;
  4. use App\Models\Goodnight\VoiceModel;
  5. use Illuminate\Console\Command;
  6. class RankCommand extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'goodnight:rank {rank}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '晚安伴侣:榜单';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. //
  37. $rank = $this->argument('rank');
  38. switch ($rank) {
  39. case "total":
  40. $this->total();
  41. break;
  42. case "topic":
  43. $this->topic();
  44. break;
  45. case "daily":
  46. $this->daily();
  47. break;
  48. }
  49. }
  50. public function total()
  51. {
  52. $voices = VoiceModel::select('id', 'uid', 'voice', 'like', 'topic_id')
  53. ->where('check', 1)->where('like', '>', 0)
  54. ->orderBy('like', 'desc')
  55. ->limit(100)
  56. ->get();
  57. foreach ($voices as $key => $voice) {
  58. $voice->rank = $key + 1;
  59. $voice->user;
  60. $voice->topic = TopicModel::where('id', $voice->topic_id)->value('topic');
  61. }
  62. \Cache::forever("goodnight:rank:total", $voices);
  63. }
  64. public function daily()
  65. {
  66. $voices = VoiceModel::selectRaw("kdgx_goodnight_voice.`id`, kdgx_goodnight_voice.`uid`, kdgx_goodnight_voice.`voice`,kdgx_goodnight_voice.`topic_id`, COUNT(*) as `like`")
  67. ->join("kdgx_goodnight_voice_thumb", "kdgx_goodnight_voice.id", '=', "kdgx_goodnight_voice_thumb.voice_id")
  68. ->where("kdgx_goodnight_voice_thumb.created_at", ">", mktime(0, 0, 0))
  69. ->groupBy("voice_id")
  70. ->orderBy("like", "desc")
  71. ->limit(100)
  72. ->get();
  73. foreach ($voices as $key => $voice) {
  74. $voice->rank = $key + 1;
  75. $voice->user;
  76. $voice->topic = TopicModel::where('id', $voice->topic_id)->value('topic');
  77. }
  78. \Cache::forever("goodnight:rank:daily:" . date("Y-m-d"), $voices);
  79. }
  80. public function topic()
  81. {
  82. $voices = VoiceModel::select('id', 'uid', 'voice', 'like', 'topic_id')->where('topic_id', 1)->where(
  83. 'check',
  84. 1
  85. )->where('like', '>', 0)->orderBy('like', 'desc')->limit(100)->get();
  86. foreach ($voices as $key => $voice) {
  87. $voice->rank = $key + 1;
  88. $voice->user;
  89. $voice->topic = TopicModel::where('id', $voice->topic_id)->value('topic');
  90. }
  91. \Cache::forever("goodnight:rank:topic", $voices);
  92. }
  93. }