|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Services; |
| 4 | + |
| 5 | +use App\News; |
| 6 | +use App\Post; |
| 7 | +use App\User; |
| 8 | +use Illuminate\Database\Eloquent\Collection; |
| 9 | +use Illuminate\Support\Facades\DB; |
| 10 | + |
| 11 | +class StatisticService |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Количество постов на сайте |
| 15 | + * @return int |
| 16 | + */ |
| 17 | + public function getPostsCount() |
| 18 | + { |
| 19 | + return Post::count(); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Общее количество новостей |
| 24 | + * @return int |
| 25 | + */ |
| 26 | + public function getNewsCount() |
| 27 | + { |
| 28 | + return News::count(); |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + /** |
| 33 | + * @return Collection |
| 34 | + */ |
| 35 | + |
| 36 | + public function getUserWithMaxPosts() |
| 37 | + { |
| 38 | +// $users = User::has('posts')->withCount('posts')->get(); |
| 39 | +// $usersWithMostPostsCount = $users->where('posts_count', $users->max('posts_count')); |
| 40 | + |
| 41 | + $users = DB::table('users') |
| 42 | + ->join('posts', 'users.id', '=', 'owner_id') |
| 43 | + ->select('users.name', DB::raw('Count(*) as posts_count')) |
| 44 | + ->groupBy('users.name') |
| 45 | + ->orderByDesc('posts_count') |
| 46 | + ->first(); |
| 47 | + |
| 48 | + $usersWithMostPostsCount = User::where('name', $users->name)->withCount('posts')->get(); |
| 49 | + |
| 50 | + return $usersWithMostPostsCount; |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + /** |
| 55 | + * @return Collection |
| 56 | + */ |
| 57 | + |
| 58 | + public function getTheLongestPosts() |
| 59 | + { |
| 60 | + $theLongestPosts = Post::where(DB::raw('Length(text)'), function($query){ |
| 61 | + $query->select(DB::raw('MAX(Length(text))')) |
| 62 | + ->from(DB::table('posts')); |
| 63 | + })->get(); |
| 64 | + |
| 65 | + return $theLongestPosts; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @return Collection |
| 70 | + */ |
| 71 | + |
| 72 | + public function getTheShortestPosts() |
| 73 | + { |
| 74 | + $theShortestPosts = Post::where(DB::raw('Length(text)'), function($query){ |
| 75 | + $query->select(DB::raw('MIN(Length(text))')) |
| 76 | + ->from(DB::table('posts')); |
| 77 | + })->get(); |
| 78 | + |
| 79 | + return $theShortestPosts; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @return int |
| 84 | + */ |
| 85 | + |
| 86 | + public function getAveragePosts() |
| 87 | + { |
| 88 | +// $posts = User::has('posts', '>', 1)->withCount('posts')->get(); |
| 89 | +// $averagePosts = intval(round($posts->avg('posts_count'))); |
| 90 | + |
| 91 | + $averagePosts = DB::select('select AVG(u.posts_count) as avg_posts_count from (select users.*, (select Count(*) from posts where users.id = posts.owner_id) as posts_count from users) as u')[0]; |
| 92 | + |
| 93 | + $averagePosts = intval(round($averagePosts->avg_posts_count)); |
| 94 | + |
| 95 | + return $averagePosts; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @return Collection |
| 100 | + */ |
| 101 | + public function getMostChangingPosts() |
| 102 | + { |
| 103 | +// $posts = Post::has('history', '>=', 1)->withCount('history')->get(); |
| 104 | +// $maxChangingPosts = $posts->where('history_count', $posts->max('history_count')); |
| 105 | + |
| 106 | + $maxHistoryCount = DB::table('posts') |
| 107 | + ->join('histories as h', 'posts.id', '=', 'h.post_id') |
| 108 | + ->select('posts.name', DB::raw('Count(*) as history_count')) |
| 109 | + ->groupBy('posts.name') |
| 110 | + ->orderByDesc('history_count') |
| 111 | + ->first() |
| 112 | + ; |
| 113 | + |
| 114 | + $maxChangingPosts = Post::where('name', $maxHistoryCount->name)->withCount('history')->get(); |
| 115 | + |
| 116 | + return $maxChangingPosts; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @return Collection |
| 121 | + */ |
| 122 | + |
| 123 | + public function getMostCommentPosts() |
| 124 | + { |
| 125 | +// $posts = Post::has('comments', '>=', 1)->withCount('comments')->get(); |
| 126 | +// $mostCommentPosts = $posts->where('comments_count', $posts->max('comments_count')); |
| 127 | + |
| 128 | + $maxComment = DB::table('posts') |
| 129 | + ->join('comments as c', function ($join) { |
| 130 | + $join->on('posts.id', '=', 'c.commentable_id') |
| 131 | + ->where('c.commentable_type', '=', 'App\Post'); |
| 132 | + }) |
| 133 | + ->select('posts.name', DB::raw('Count(*) as comments_count')) |
| 134 | + ->groupBy('posts.name') |
| 135 | + ->orderByDesc('comments_count') |
| 136 | + ->first() |
| 137 | + ; |
| 138 | + |
| 139 | + $mostCommentPosts = Post::where('name', $maxComment->name)->withCount('comments')->get(); |
| 140 | + |
| 141 | + return $mostCommentPosts; |
| 142 | + } |
| 143 | +} |
0 commit comments