123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- /**
- * Created by PhpStorm.
- * User: durong
- * Date: 2019/6/20
- * Time: 下午2:27
- */
- namespace App\Console\Commands;
- use App\Models\Behavior;
- use App\Models\CommentRecord;
- use App\Models\GeneralRecord;
- use App\Models\RegisteredRecord;
- use App\Models\ReleaseRecord;
- use Illuminate\Console\Command;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- class RankingList extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'ranking:list';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '昨日排行榜统计';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct(Behavior $behavior, RegisteredRecord $registeredRecord,
- CommentRecord $commentRecord,
- GeneralRecord $generalRecord,
- ReleaseRecord $releaseRecord)
- {
- parent::__construct();
- $this->behavior = $behavior;
- $this->commentRecord = $commentRecord;
- $this->generalRecord = $generalRecord;
- $this->registeredRecord = $registeredRecord;
- $this->releaseRecord = $releaseRecord;
- }
- private function _condition($model)
- {
- $yesterday_start = Carbon::now()->addDays(-1)->startOfDay()->toDateTimeString();
- $yesterday_end = Carbon::now()->addDays(-1)->endOfDay()->toDateTimeString();
- return $model->select(DB::raw('count(*) as num'),'content_author_id', DB::raw('sum(generation_quantity) as count'))
- ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
- ->groupBy('content_author_id')
- ->orderBy('count', 'desc')
- ->limit(10)
- ->get();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $this->line("开始统计排行榜相关内容");
- $yesterday_start = Carbon::now()->addDays(-1)->startOfDay()->toDateTimeString();
- $yesterday_end = Carbon::now()->addDays(-1)->endOfDay()->toDateTimeString();
- //昨日拉新最多前十人
- $registered_most = $this->registeredRecord
- ->select(DB::raw('count(*) as count'), 'superior_uid')
- ->where('superior_uid', '!=', 0)
- ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
- ->groupBy('superior_uid')->orderBy('count', 'desc')->limit(10)->get();
- $registered_most = $registered_most->toArray();
- //昨日文章产生U米最多前十人
- $comment_author = $this->_condition($this->commentRecord);
- $comment_best_author = $comment_author->toArray();
- foreach ($comment_best_author as $k => $v) {
- $comment_best_author[$k]['type'] = 'comment';
- }
- $general_author = $this->_condition($this->generalRecord);
- $general_best_author = $general_author->toArray();
- foreach ($general_best_author as $k => $v) {
- $general_best_author[$k]['type'] = 'general';
- }
- $release_author = $this->releaseRecord
- ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
- ->select(DB::raw('count(*) as num'), 'uid as content_author_id', DB::raw('sum(generation_quantity) as count'))
- ->groupBy('content_author_id')->orderBy('count', 'desc')->limit(10)->get();
- $release_best_author = $release_author->toArray();
- foreach ($release_best_author as $k => $v) {
- $release_best_author[$k]['type'] = 'release';
- }
- $best_author = array_merge($comment_best_author, $general_best_author, $release_best_author);
- $result = [];
- foreach ($best_author as $key => $val) {
- if (isset($result[$val['content_author_id']]) && isset($result[$val['content_author_id']]['num'])) {
- $result[$val['content_author_id']]['num'] += $val['num'];
- } else {
- $result[$val['content_author_id']]['num'] = $val['num'];
- }
- if (isset($result[$val['content_author_id']]) && isset($result[$val['content_author_id']]['count'])) {
- $result[$val['content_author_id']]['count'] += $val['count'];
- } else {
- $result[$val['content_author_id']]['count'] = $val['count'];
- }
- $result[$val['content_author_id']]['content_author_id'] = $val['content_author_id'];
- }
- $all_best_author = array_merge($result);
- $column = array_column($all_best_author, 'count');
- array_multisort($column, SORT_DESC, $all_best_author);
- $all_best_author = array_slice($all_best_author, 0, 10);
- Log::debug('昨日拉新最多前十人' . json_encode($registered_most));
- Log::debug('昨日文章产生U米最多前十人' . json_encode($all_best_author));
- Redis::set('yesterday_registered_most', json_encode($registered_most));
- Redis::set('yesterday_best_author', json_encode($all_best_author));
- $this->line("统计昨日排行榜内容结束");
- }
- }
|