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', 'forward', '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_share_count']) && $logContent['add_share_count']){ $shareCount -= $logContent['add_share_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; $data['created_at'] = Carbon::yesterday()->toDateTimeString(); $this->postStatistics->create($data); Log::info(date('Y-m-d H:i:s').'统计'.$yesterdayStart.'最终记录数据:'.json_encode($data)); } }