1+ package com .codecampus .post .controller ;
2+
3+ import com .codecampus .post .dto .common .ApiResponse ;
4+ import com .codecampus .post .dto .common .PageResponse ;
5+ import com .codecampus .post .dto .response .stats .AdminPostStatDto ;
6+ import com .codecampus .post .dto .response .stats .HashtagStatDto ;
7+ import com .codecampus .post .dto .response .stats .UserPostLeaderboardDto ;
8+ import com .codecampus .post .service .AdminStatisticsService ;
9+ import java .time .Instant ;
10+ import lombok .AccessLevel ;
11+ import lombok .RequiredArgsConstructor ;
12+ import lombok .experimental .FieldDefaults ;
13+ import org .springframework .data .domain .Sort ;
14+ import org .springframework .format .annotation .DateTimeFormat ;
15+ import org .springframework .web .bind .annotation .GetMapping ;
16+ import org .springframework .web .bind .annotation .RequestMapping ;
17+ import org .springframework .web .bind .annotation .RequestParam ;
18+ import org .springframework .web .bind .annotation .RestController ;
19+
20+ @ RestController
21+ @ RequiredArgsConstructor
22+ @ RequestMapping ("/stats" )
23+ @ FieldDefaults (level = AccessLevel .PRIVATE , makeFinal = true )
24+ public class AdminStatisticsController {
25+
26+ AdminStatisticsService service ;
27+
28+ // Tổng quan bài viết (lọc + sắp xếp)
29+ @ GetMapping ("/posts/overview" )
30+ ApiResponse <PageResponse <AdminPostStatDto >> overview (
31+ @ RequestParam (required = false ) String orgId ,
32+ @ RequestParam (required = false ) String userId ,
33+ @ RequestParam (required = false ) String postType ,
34+ @ RequestParam (required = false ) Boolean is ,
35+ @ RequestParam (required = false )
36+ @ DateTimeFormat (iso = DateTimeFormat .ISO .DATE_TIME ) Instant start ,
37+ @ RequestParam (required = false )
38+ @ DateTimeFormat (iso = DateTimeFormat .ISO .DATE_TIME ) Instant end ,
39+ @ RequestParam (required = false ) String q ,
40+ @ RequestParam (defaultValue = "1" ) int page ,
41+ @ RequestParam (defaultValue = "10" ) int size ,
42+ // sortField: createdAt | lastActivityAt | commentCount | upvoteCount | downvoteCount
43+ @ RequestParam (defaultValue = "lastActivityAt" ) String sortField ,
44+ @ RequestParam (defaultValue = "DESC" ) String sortDir
45+ ) {
46+ Sort sort = Sort .by (sortField );
47+ sort =
48+ "DESC" .equalsIgnoreCase (sortDir ) ? sort .descending () : sort .ascending ();
49+
50+ return ApiResponse .<PageResponse <AdminPostStatDto >>builder ()
51+ .message ("Thống kê tổng quan bài viết thành công!" )
52+ .result (
53+ service .overview (orgId , userId , postType , is , start , end , q ,
54+ page , size , sort ))
55+ .build ();
56+ }
57+
58+ // Top bài viết theo số bình luận
59+ @ GetMapping ("/posts/top-commented" )
60+ ApiResponse <PageResponse <AdminPostStatDto >> topCommented (
61+ @ RequestParam (required = false ) String orgId ,
62+ @ RequestParam (required = false )
63+ @ DateTimeFormat (iso = DateTimeFormat .ISO .DATE_TIME ) Instant start ,
64+ @ RequestParam (required = false )
65+ @ DateTimeFormat (iso = DateTimeFormat .ISO .DATE_TIME ) Instant end ,
66+ @ RequestParam (defaultValue = "1" ) int page ,
67+ @ RequestParam (defaultValue = "10" ) int size
68+ ) {
69+ return ApiResponse .<PageResponse <AdminPostStatDto >>builder ()
70+ .message ("Thống kê top bài viết theo bình luận thành công!" )
71+ .result (service .topCommented (orgId , start , end , page , size ))
72+ .build ();
73+ }
74+
75+ // Top bài viết theo reaction (score = up - down)
76+ @ GetMapping ("/posts/top-reacted" )
77+ ApiResponse <PageResponse <AdminPostStatDto >> topReacted (
78+ @ RequestParam (required = false ) String orgId ,
79+ @ RequestParam (required = false )
80+ @ DateTimeFormat (iso = DateTimeFormat .ISO .DATE_TIME ) Instant start ,
81+ @ RequestParam (required = false )
82+ @ DateTimeFormat (iso = DateTimeFormat .ISO .DATE_TIME ) Instant end ,
83+ @ RequestParam (defaultValue = "1" ) int page ,
84+ @ RequestParam (defaultValue = "10" ) int size
85+ ) {
86+ return ApiResponse .<PageResponse <AdminPostStatDto >>builder ()
87+ .message ("Thống kê top bài viết theo reaction thành công!" )
88+ .result (service .topReacted (orgId , start , end , page , size ))
89+ .build ();
90+ }
91+
92+ // BXH người dùng theo số bài viết (kèm comment/reaction tạo)
93+ @ GetMapping ("/users/top-posters" )
94+ ApiResponse <PageResponse <UserPostLeaderboardDto >> topPosters (
95+ @ RequestParam (required = false )
96+ @ DateTimeFormat (iso = DateTimeFormat .ISO .DATE_TIME ) Instant start ,
97+ @ RequestParam (required = false )
98+ @ DateTimeFormat (iso = DateTimeFormat .ISO .DATE_TIME ) Instant end ,
99+ @ RequestParam (defaultValue = "1" ) int page ,
100+ @ RequestParam (defaultValue = "10" ) int size
101+ ) {
102+ return ApiResponse .<PageResponse <UserPostLeaderboardDto >>builder ()
103+ .message ("Bảng xếp hạng người dùng theo bài viết thành công!" )
104+ .result (service .topPosters (start , end , page , size ))
105+ .build ();
106+ }
107+
108+ // Thống kê hashtag
109+ @ GetMapping ("/hashtags" )
110+ ApiResponse <PageResponse <HashtagStatDto >> hashtagStats (
111+ @ RequestParam (required = false ) String orgId ,
112+ @ RequestParam (required = false )
113+ @ DateTimeFormat (iso = DateTimeFormat .ISO .DATE_TIME ) Instant start ,
114+ @ RequestParam (required = false )
115+ @ DateTimeFormat (iso = DateTimeFormat .ISO .DATE_TIME ) Instant end ,
116+ @ RequestParam (defaultValue = "1" ) int page ,
117+ @ RequestParam (defaultValue = "10" ) int size
118+ ) {
119+ return ApiResponse .<PageResponse <HashtagStatDto >>builder ()
120+ .message ("Thống kê hashtag thành công!" )
121+ .result (service .hashtagStats (orgId , start , end , page , size ))
122+ .build ();
123+ }
124+ }
0 commit comments