123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/12
- * Time: 16:32
- */
- namespace App\Console\Commands;
- use App\Models\Behavior;
- use App\Models\CommentRecord;
- use App\Models\GeneralRecord;
- use App\Models\PostLog;
- 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 PostStatistics extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'post:statistics';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '全部内容统计';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct(\App\Models\PostStatistics $postStatistics,
- GeneralRecord $generalRecord,
- ReleaseRecord $releaseRecord,
- CommentRecord $commentRecord,
- PostLog $postLog,
- Behavior $behavior)
- {
- parent::__construct();
- $this->postStatistics = $postStatistics;
- $this->generalRecord = $generalRecord;
- $this->releaseRecord = $releaseRecord;
- $this->commentRecord = $commentRecord;
- $this->postLog = $postLog;
- $this->behavior = $behavior;
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $this->line("开始统计所有内容");
- $yesterdayStart = Carbon::yesterday()->startOfDay()->toDateTimeString();
- $yesterdayEnd = Carbon::yesterday()->endOfDay()->toDateTimeString();
- $post = $this->releaseRecord
- ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
- ->select(DB::raw('count(*) as post_count'))
- ->first();
- $comment = $this->commentRecord
- ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
- ->select(DB::raw('count(*) as comment_count'))
- ->first();
- $behaviors = $this->behavior->whereIn('behavior_identification', ['read', 'like', 'share', 'collect'])->get()->toArray();
- $postCount = $post->post_count;
- $commentCount = $comment->comment_count;
- $readCount = 0;
- $likeCount = 0;
- $shareCount = 0;
- $collectCount = 0;
- foreach ($behaviors as $behavior) {
- $behaviorId = $behavior['virus_behavior_id'];
- if ($behavior['behavior_identification'] == 'read') {
- $read = $this->generalRecord
- ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd], ['virus_behavior_id', $behaviorId]])
- ->select(DB::raw('count(*) as read_count'))
- ->first();
- $readCount = $read->read_count;
- } elseif ($behavior['behavior_identification'] == 'like') {
- $like = $this->generalRecord
- ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd], ['virus_behavior_id', $behaviorId]])
- ->select(DB::raw('count(*) as like_count'))
- ->first();
- $likeCount = $like->like_count;
- } elseif ($behavior['behavior_identification'] == 'share') {
- $share = $this->generalRecord
- ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd], ['virus_behavior_id', $behaviorId]])
- ->select(DB::raw('count(*) as share_count'))
- ->first();
- $shareCount = $share->share_count;
- } elseif ($behavior['behavior_identification'] == 'collect') {
- $collect = $this->generalRecord
- ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd], ['virus_behavior_id', $behaviorId]])
- ->select(DB::raw('count(*) as collect_count'))
- ->first();
- $collectCount = $collect->collect_count;
- }
- }
- Log::info(date('Y-m-d H:i:s').'统计'.$yesterdayStart.',帖子数:'.$postCount.'评论数:'.$commentCount.'阅读数:'.$readCount.'点赞数:'.$likeCount.'收藏数:'.$collectCount.'分享数:'.$shareCount);
- $postLog = $this->postLog
- ->where([['created_at', '>=', $yesterdayStart], ['created_at', '<=', $yesterdayEnd]])
- ->select('content')
- ->get()->toArray();
- foreach ($postLog as $log){
- $logContent = json_decode($log['content'],true);
- if(isset($logContent['add_pv']) && $logContent['add_pv']){
- $readCount -= $logContent['add_pv'];
- }
- if(isset($logContent['add_praise_count']) && $logContent['add_praise_count']){
- $likeCount -= $logContent['add_praise_count'];
- }
- if(isset($logContent['add_collect_count']) && $logContent['add_collect_count']){
- $collectCount -= $logContent['add_collect_count'];
- }
- if(isset($logContent['add_collect_count']) && $logContent['add_collect_count']){
- $shareCount -= $logContent['add_collect_count'];
- }
- Log::debug(date('Y-m-d H:i:s').'统计虚拟数'.$yesterdayStart.',阅读数:'.$logContent['add_pv'].'点赞数:'.$logContent['add_praise_count'].'收藏数:'.$logContent['add_collect_count'].'分享数:'.$logContent['add_collect_count']);
- }
- $data['post_count'] = $postCount;
- $data['read_count'] = $readCount;
- $data['share_count'] = $shareCount;
- $data['like_count'] = $likeCount;
- $data['collect_count'] = $collectCount;
- $data['comment_count'] = $commentCount;
- $this->postStatistics->insert($data);
- Log::info(date('Y-m-d H:i:s').'统计'.$yesterdayStart.'最终记录数据:'.json_encode($data));
- }
- }
|