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