RankingList.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. return $model->select(DB::raw('count(*) as count'),'generation_quantity','content_author_id','related_content_id')
  53. ->where('created_at', '>=', Carbon::now()->startOfDay()->toDateTimeString())
  54. ->groupBy('generation_quantity','related_content_id','content_author_id')
  55. ->orderBy('generation_quantity','desc')
  56. ->limit(10)
  57. ->get();
  58. }
  59. /**
  60. * Execute the console command.
  61. *
  62. * @return mixed
  63. */
  64. public function handle()
  65. {
  66. $this->line("开始统计排行榜相关内容");
  67. $time = Carbon::now()->startOfDay()->toDateTimeString();
  68. //昨日拉新最多前十人
  69. $registered_most = $this->registeredRecord
  70. ->where('created_at', '>=',$time)
  71. ->select(DB::raw('count(*) as count'),'superior_uid')
  72. ->groupBy('superior_uid')->orderBy('count','desc')->limit(10)->get();
  73. $registered_most = $registered_most->toArray();
  74. //昨日文章产生彩虹豆最多前十人
  75. $comment_author = $this->_condition($this->commentRecord);
  76. $comment_best_author = $comment_author->toArray();
  77. foreach ($comment_best_author as $k=>$v){
  78. $comment_best_author[$k]['type'] = 'comment';
  79. }
  80. $general_author = $this->_condition($this->commentRecord);
  81. $general_best_author = $general_author->toArray();
  82. foreach ($general_best_author as $k=>$v){
  83. $general_best_author[$k]['type'] = 'general';
  84. }
  85. $release_author = $this->releaseRecord
  86. ->where('created_at', '>=', $time)
  87. ->select(DB::raw('count(*) as count'),'generation_quantity','uid as content_author_id','related_content_id')
  88. ->groupBy('generation_quantity','content_author_id','related_content_id')->orderBy('generation_quantity','desc')->limit(10)->get();
  89. $release_best_author = $release_author->toArray();
  90. foreach ($release_best_author as $k=>$v){
  91. $release_best_author[$k]['type'] = 'release';
  92. }
  93. $all_best_author = array_merge($comment_best_author,$general_best_author,$release_best_author);
  94. $column = array_column($all_best_author,'generation_quantity');
  95. array_multisort($column,SORT_DESC,$all_best_author);
  96. $all_best_author = array_slice($all_best_author,0,10);
  97. Log::debug('昨日拉新最多前十人'.json_encode($registered_most));
  98. Log::debug('昨日文章产生彩虹豆最多前十人'.json_encode($all_best_author));
  99. Redis::set('yesterday_registered_most', json_encode($registered_most));
  100. Redis::set('yesterday_best_author', json_encode($all_best_author));
  101. $this->line("统计昨日排行榜内容结束");
  102. }
  103. }