RankingList.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 num'),'content_author_id', DB::raw('sum(generation_quantity) as count'))
  55. ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
  56. ->groupBy('content_author_id')
  57. ->orderBy('count', '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. ->select(DB::raw('count(*) as count'), 'superior_uid')
  74. ->where('superior_uid', '!=', 0)
  75. ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
  76. ->groupBy('superior_uid')->orderBy('count', 'desc')->limit(10)->get();
  77. $registered_most = $registered_most->toArray();
  78. //昨日文章产生U米最多前十人
  79. $comment_author = $this->_condition($this->commentRecord);
  80. $comment_best_author = $comment_author->toArray();
  81. foreach ($comment_best_author as $k => $v) {
  82. $comment_best_author[$k]['type'] = 'comment';
  83. }
  84. $general_author = $this->_condition($this->generalRecord);
  85. $general_best_author = $general_author->toArray();
  86. foreach ($general_best_author as $k => $v) {
  87. $general_best_author[$k]['type'] = 'general';
  88. }
  89. $release_author = $this->releaseRecord
  90. ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
  91. ->select(DB::raw('count(*) as num'), 'uid as content_author_id', DB::raw('sum(generation_quantity) as count'))
  92. ->groupBy('content_author_id')->orderBy('count', 'desc')->limit(10)->get();
  93. $release_best_author = $release_author->toArray();
  94. foreach ($release_best_author as $k => $v) {
  95. $release_best_author[$k]['type'] = 'release';
  96. }
  97. $best_author = array_merge($comment_best_author, $general_best_author, $release_best_author);
  98. $result = [];
  99. foreach ($best_author as $key => $val) {
  100. if (isset($result[$val['content_author_id']]) && isset($result[$val['content_author_id']]['num'])) {
  101. $result[$val['content_author_id']]['num'] += $val['num'];
  102. } else {
  103. $result[$val['content_author_id']]['num'] = $val['num'];
  104. }
  105. if (isset($result[$val['content_author_id']]) && isset($result[$val['content_author_id']]['count'])) {
  106. $result[$val['content_author_id']]['count'] += $val['count'];
  107. } else {
  108. $result[$val['content_author_id']]['count'] = $val['count'];
  109. }
  110. $result[$val['content_author_id']]['content_author_id'] = $val['content_author_id'];
  111. }
  112. $all_best_author = array_merge($result);
  113. $column = array_column($all_best_author, 'count');
  114. array_multisort($column, SORT_DESC, $all_best_author);
  115. $all_best_author = array_slice($all_best_author, 0, 10);
  116. Log::debug('昨日拉新最多前十人' . json_encode($registered_most));
  117. Log::debug('昨日文章产生U米最多前十人' . json_encode($all_best_author));
  118. Redis::set('yesterday_registered_most', json_encode($registered_most));
  119. Redis::set('yesterday_best_author', json_encode($all_best_author));
  120. $this->line("统计昨日排行榜内容结束");
  121. }
  122. }