RankingList.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: durong
  5. * Date: 2019/6/20
  6. * Time: 下午2:27
  7. */
  8. namespace App\Console\Commands;
  9. use App\Models\Behavior;
  10. use App\Models\CommentRecord;
  11. use App\Models\GeneralRecord;
  12. use App\Models\RegisteredRecord;
  13. use App\Models\ReleaseRecord;
  14. use Illuminate\Console\Command;
  15. use Illuminate\Support\Carbon;
  16. use Illuminate\Support\Facades\DB;
  17. use Illuminate\Support\Facades\Log;
  18. use Illuminate\Support\Facades\Redis;
  19. class RankingList extends Command
  20. {
  21. /**
  22. * The name and signature of the console command.
  23. *
  24. * @var string
  25. */
  26. protected $signature = 'ranking:list';
  27. /**
  28. * The console command description.
  29. *
  30. * @var string
  31. */
  32. protected $description = '昨日排行榜统计';
  33. /**
  34. * Create a new command instance.
  35. *
  36. * @return void
  37. */
  38. public function __construct(Behavior $behavior, RegisteredRecord $registeredRecord,
  39. CommentRecord $commentRecord,
  40. GeneralRecord $generalRecord,
  41. ReleaseRecord $releaseRecord)
  42. {
  43. parent::__construct();
  44. $this->behavior = $behavior;
  45. $this->commentRecord = $commentRecord;
  46. $this->generalRecord = $generalRecord;
  47. $this->registeredRecord = $registeredRecord;
  48. $this->releaseRecord = $releaseRecord;
  49. }
  50. private function _condition($model)
  51. {
  52. $yesterday_start = Carbon::now()->addDays(-1)->startOfDay()->toDateTimeString();
  53. $yesterday_end = Carbon::now()->addDays(-1)->endOfDay()->toDateTimeString();
  54. return $model->select(DB::raw('count(*) as count'),'generation_quantity','content_author_id','related_content_id')
  55. ->whereBetween('created_at', [$yesterday_start,$yesterday_end])
  56. ->groupBy('generation_quantity','related_content_id','content_author_id')
  57. ->orderBy('generation_quantity','desc')
  58. ->limit(10)
  59. ->get();
  60. }
  61. /**
  62. * Execute the console command.
  63. *
  64. * @return mixed
  65. */
  66. public function handle()
  67. {
  68. $this->line("开始统计排行榜相关内容");
  69. $yesterday_start = Carbon::now()->addDays(-1)->startOfDay()->toDateTimeString();
  70. $yesterday_end = Carbon::now()->addDays(-1)->endOfDay()->toDateTimeString();
  71. //昨日拉新最多前十人
  72. $registered_most = $this->registeredRecord
  73. ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
  74. ->select(DB::raw('count(*) as count'), 'superior_uid')
  75. ->groupBy('superior_uid')->orderBy('count', 'desc')->limit(10)->get();
  76. $registered_most = $registered_most->toArray();
  77. //昨日文章产生彩虹豆最多前十人
  78. $comment_author = $this->_condition($this->commentRecord);
  79. $comment_best_author = $comment_author->toArray();
  80. foreach ($comment_best_author as $k => $v) {
  81. $comment_best_author[$k]['type'] = 'comment';
  82. }
  83. $general_author = $this->_condition($this->commentRecord);
  84. $general_best_author = $general_author->toArray();
  85. foreach ($general_best_author as $k => $v) {
  86. $general_best_author[$k]['type'] = 'general';
  87. }
  88. $release_author = $this->releaseRecord
  89. ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
  90. ->select(DB::raw('count(*) as count'), 'generation_quantity', 'uid as content_author_id', 'related_content_id')
  91. ->groupBy('generation_quantity', 'content_author_id', 'related_content_id')->orderBy('generation_quantity', 'desc')->limit(10)->get();
  92. $release_best_author = $release_author->toArray();
  93. foreach ($release_best_author as $k => $v) {
  94. $release_best_author[$k]['type'] = 'release';
  95. }
  96. $all_best_author = array_merge($comment_best_author, $general_best_author, $release_best_author);
  97. $column = array_column($all_best_author, 'generation_quantity');
  98. array_multisort($column, SORT_DESC, $all_best_author);
  99. $all_best_author = array_slice($all_best_author, 0, 10);
  100. Log::debug('昨日拉新最多前十人' . json_encode($registered_most));
  101. Log::debug('昨日文章产生彩虹豆最多前十人' . json_encode($all_best_author));
  102. Redis::set('yesterday_registered_most', json_encode($registered_most));
  103. Redis::set('yesterday_best_author', json_encode($all_best_author));
  104. $this->line("统计昨日排行榜内容结束");
  105. }
  106. }