123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- /**
- * Created by PhpStorm.
- * User: durong
- * Date: 2019/6/19
- * Time: 下午3:45
- */
- 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 ExcellentResidents extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'excellent:residents';
- /**
- * 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, $virus_behavior_id)
- {
- $yesterday_start = Carbon::now()->addDays(-1)->startOfDay()->toDateTimeString();
- $yesterday_end = Carbon::now()->addDays(-1)->endOfDay()->toDateTimeString();
- return $model
- ->where('virus_behavior_id', $virus_behavior_id)
- ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
- ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
- ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->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();
- //昨日发放总豆
- $comment_quantity_issued = CommentRecord::select('quantity_issued')
- ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
- ->sum('quantity_issued');
- $general_quantity_issued = GeneralRecord::select('quantity_issued')
- ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
- ->sum('quantity_issued');
- $registered_quantity_issued = RegisteredRecord::select('quantity_issued')
- ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
- ->sum('quantity_issued');
- $release_quantity_issued = ReleaseRecord::select('quantity_issued')
- ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
- ->sum('quantity_issued');
- $all_quantity_issued = $comment_quantity_issued + $general_quantity_issued + $registered_quantity_issued + $release_quantity_issued;
- //文章被评论最多用户
- $comment = $this->commentRecord
- ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
- ->select(DB::raw('count(*) as count'), 'content_author_id', 'related_content_id')
- ->groupBy('related_content_id', 'content_author_id')->orderBy('count', 'desc')->limit(1)->get();
- $comment = $comment->toArray();
- foreach ($comment as $k => $v) {
- $comment[$k]['type'] = 'comment';//类型
- }
- //昨日拉新最多用户
- $registered = $this->registeredRecord
- ->where('superior_uid', '!=', 0)
- ->whereBetween('created_at', [$yesterday_start, $yesterday_end])
- ->select(DB::raw('count(*) as count'), 'superior_uid as content_author_id')//作为用户ID
- ->groupBy('superior_uid')->orderBy('count', 'desc')->limit(1)->get();
- $registered = $registered->toArray();
- foreach ($registered as $k => $v) {
- $registered[$k]['type'] = 'registered';
- }
- //文章被收藏最多用户
- $virus_id = $this->behavior
- ->select('virus_behavior_id')
- ->where('behavior_identification', 'collect')
- ->first();
- $collent = $this->_condition($this->generalRecord, $virus_id->virus_behavior_id);
- $collent = $collent->toArray();
- foreach ($collent as $k => $v) {
- $collent[$k]['type'] = 'collect';
- }
- //文章被喜欢最多用户
- $virus_id = $this->behavior
- ->select('virus_behavior_id')
- ->where('behavior_identification', 'like')
- ->first();
- $like = $this->_condition($this->generalRecord, $virus_id->virus_behavior_id);
- $like = $like->toArray();
- foreach ($like as $k => $v) {
- $like[$k]['type'] = 'like';
- }
- //文章被转发最多用户
- $virus_id = $this->behavior
- ->select('virus_behavior_id')
- ->where('behavior_identification', 'forward')
- ->first();
- $forward = $this->_condition($this->generalRecord, $virus_id->virus_behavior_id);
- $forward = $forward->toArray();
- foreach ($forward as $k => $v) {
- $forward[$k]['type'] = 'forward';
- }
- //文章被阅读最多用户
- $virus_id = $this->behavior
- ->select('virus_behavior_id')
- ->where('behavior_identification', 'read')
- ->first();
- $read = $this->_condition($this->generalRecord, $virus_id->virus_behavior_id);
- $read = $read->toArray();
- foreach ($read as $k => $v) {
- $read[$k]['type'] = 'read';
- }
- $all_merge = array_merge($comment, $registered, $collent, $like, $forward, $read);
- $all_excellent_residents = json_encode($all_merge);
- Log::info('统计昨日优秀居民内容' . json_encode($all_excellent_residents));
- Log::info('昨日发放总U米' . json_encode($all_quantity_issued));
- Redis::set('yesterday_excellent_residents', $all_excellent_residents);
- Redis::set('yesterday_quantity_issued', $all_quantity_issued);
- $this->line("统计昨日内容结束");
- }
- }
|