Skip to content

Commit 2ab8cd5

Browse files
committed
update QQ chat monthly-stats functions
1 parent df260c3 commit 2ab8cd5

5 files changed

Lines changed: 194 additions & 119 deletions

File tree

.idea/workspace.xml

Lines changed: 77 additions & 73 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/com/phantoms/phantomsbackend/controller/OneBotController.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.springframework.util.StringUtils;
1616
import org.springframework.web.bind.annotation.*;
1717

18+
import java.time.LocalDate;
1819
import java.util.HashMap;
1920
import java.util.List;
2021
import java.util.Map;
@@ -154,10 +155,17 @@ public ResponseEntity<String> sendToGroup(@RequestBody Map<String, Object> reque
154155
}
155156

156157
@GetMapping("/onebot/monthly-stats")
157-
public ResponseEntity<Map<String, Object>> getMonthlyStats() {
158+
public ResponseEntity<Map<String, Object>> getMonthlyStats(
159+
@RequestParam(required = false) Integer year,
160+
@RequestParam(required = false) Integer month) {
158161
try {
162+
// 如果未提供年月,使用当前年月
163+
LocalDate now = LocalDate.now();
164+
int targetYear = (year != null) ? year : now.getYear();
165+
int targetMonth = (month != null) ? month : now.getMonthValue();
166+
159167
// 调用服务层获取月度统计
160-
Map<String, Object> monthlyStats = oneBotService.getMonthlyStats();
168+
Map<String, Object> monthlyStats = oneBotService.getMonthlyStats(targetYear, targetMonth);
161169

162170
// 返回 JSON 格式的响应
163171
return ResponseEntity.ok(monthlyStats);

src/main/java/com/phantoms/phantomsbackend/repository/primary/onebot/PrimaryChatRecordRepository.java

Lines changed: 82 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,41 +38,93 @@ public interface PrimaryChatRecordRepository extends JpaRepository<ChatRecord, L
3838
@Query(value = "SELECT COUNT(*) FROM onebot.chat_records cr WHERE cr.qq_user_id = ?1 AND cr.qq_group_id = ?2 AND cr.created_at > ?3", nativeQuery = true)
3939
long countByUserIdAndGroupIdAndTimestampAfter(Long userId, Long groupId, LocalDateTime timestamp);
4040

41-
// 本月度消息数量排名 - 修改为 PostgreSQL 语法
41+
// ========== 带年份和月份参数的查询方法 ==========
42+
43+
// 本月度消息数量排名 - 带参数
44+
@Query(value = "SELECT " +
45+
"cr.qq_user_id, " +
46+
"COUNT(*) AS total " +
47+
"FROM onebot.chat_records cr " +
48+
"WHERE EXTRACT(YEAR FROM cr.created_at) = ?1 " +
49+
"AND EXTRACT(MONTH FROM cr.created_at) = ?2 " +
50+
"GROUP BY cr.qq_user_id " +
51+
"ORDER BY total DESC",
52+
nativeQuery = true)
53+
List<Object[]> findMonthlyMessageRanking(int year, int month);
54+
55+
// 本月度图片数量排名 - 带参数
56+
@Query(value = "SELECT " +
57+
"cr.qq_user_id, " +
58+
"COUNT(*) AS total_images " +
59+
"FROM onebot.chat_records cr " +
60+
"WHERE cr.message LIKE '%type=image%' " +
61+
"AND EXTRACT(YEAR FROM cr.created_at) = ?1 " +
62+
"AND EXTRACT(MONTH FROM cr.created_at) = ?2 " +
63+
"GROUP BY cr.qq_user_id " +
64+
"ORDER BY total_images DESC",
65+
nativeQuery = true)
66+
List<Object[]> findMonthlyImageRanking(int year, int month);
67+
68+
// 本月度图片比例排名(消息总数大于50的用户)- 带参数
4269
@Query(value = "SELECT " +
43-
"cr.qq_user_id, " +
44-
"COUNT(*) AS total " +
45-
"FROM onebot.chat_records cr " +
46-
"WHERE cr.created_at >= DATE_TRUNC('month', CURRENT_DATE) " +
47-
"GROUP BY cr.qq_user_id " +
48-
"ORDER BY total DESC",
49-
nativeQuery = true)
70+
"cr.qq_user_id, " +
71+
"COUNT(*) AS total_messages, " +
72+
"SUM(CASE WHEN cr.message LIKE '%type=image%' THEN 1 ELSE 0 END) AS total_images, " +
73+
"(SUM(CASE WHEN cr.message LIKE '%type=image%' THEN 1 ELSE 0 END) * 1.0 / COUNT(*)) AS image_ratio " +
74+
"FROM onebot.chat_records cr " +
75+
"WHERE EXTRACT(YEAR FROM cr.created_at) = ?1 " +
76+
"AND EXTRACT(MONTH FROM cr.created_at) = ?2 " +
77+
"GROUP BY cr.qq_user_id " +
78+
"HAVING COUNT(*) > 50 " +
79+
"ORDER BY image_ratio DESC",
80+
nativeQuery = true)
81+
List<Object[]> findMonthlyImageRatioRanking(int year, int month);
82+
83+
// 查询指定年月消息总数
84+
@Query(value = "SELECT COUNT(*) FROM onebot.chat_records cr WHERE EXTRACT(YEAR FROM cr.created_at) = ?1 AND EXTRACT(MONTH FROM cr.created_at) = ?2", nativeQuery = true)
85+
long countMonthlyMessages(int year, int month);
86+
87+
// 查询指定年月图片总数
88+
@Query(value = "SELECT COUNT(*) FROM onebot.chat_records cr WHERE cr.message LIKE '%type=image%' AND EXTRACT(YEAR FROM cr.created_at) = ?1 AND EXTRACT(MONTH FROM cr.created_at) = ?2", nativeQuery = true)
89+
long countMonthlyImages(int year, int month);
90+
91+
// ========== 原有无参数查询方法(用于兼容性) ==========
92+
93+
// 本月度消息数量排名
94+
@Query(value = "SELECT " +
95+
"cr.qq_user_id, " +
96+
"COUNT(*) AS total " +
97+
"FROM onebot.chat_records cr " +
98+
"WHERE cr.created_at >= DATE_TRUNC('month', CURRENT_DATE) " +
99+
"GROUP BY cr.qq_user_id " +
100+
"ORDER BY total DESC",
101+
nativeQuery = true)
50102
List<Object[]> findMonthlyMessageRanking();
51103

52-
// 本月度图片数量排名 - 修改为 PostgreSQL 语法
104+
// 本月度图片数量排名
53105
@Query(value = "SELECT " +
54-
"cr.qq_user_id, " +
55-
"COUNT(*) AS total_images " +
56-
"FROM onebot.chat_records cr " +
57-
"WHERE cr.message LIKE '%type=image%' " +
58-
"AND cr.created_at >= DATE_TRUNC('month', CURRENT_DATE) " +
59-
"GROUP BY cr.qq_user_id " +
60-
"ORDER BY total_images DESC",
61-
nativeQuery = true)
106+
"cr.qq_user_id, " +
107+
"COUNT(*) AS total_images " +
108+
"FROM onebot.chat_records cr " +
109+
"WHERE cr.message LIKE '%type=image%' " +
110+
"AND cr.created_at >= DATE_TRUNC('month', CURRENT_DATE) " +
111+
"GROUP BY cr.qq_user_id " +
112+
"ORDER BY total_images DESC",
113+
nativeQuery = true)
62114
List<Object[]> findMonthlyImageRanking();
63115

64-
// 本月度图片比例排名(消息总数大于50的用户)- 修改为 PostgreSQL 语法
116+
// 本月度图片比例排名(消息总数大于50的用户)
65117
@Query(value = "SELECT " +
66-
"cr.qq_user_id, " +
67-
"COUNT(*) AS total_messages, " +
68-
"SUM(CASE WHEN cr.message LIKE '%type=image%' THEN 1 ELSE 0 END) AS total_images, " +
69-
"(SUM(CASE WHEN cr.message LIKE '%type=image%' THEN 1 ELSE 0 END) * 1.0 / COUNT(*)) AS image_ratio " +
70-
"FROM onebot.chat_records cr " +
71-
"WHERE cr.created_at >= DATE_TRUNC('month', CURRENT_DATE) " +
72-
"GROUP BY cr.qq_user_id " +
73-
"HAVING COUNT(*) > 50 " +
74-
"ORDER BY image_ratio DESC",
75-
nativeQuery = true)
118+
"cr.qq_user_id, " +
119+
"COUNT(*) AS total_messages, " +
120+
"SUM(CASE WHEN cr.message LIKE '%type=image%' THEN 1 ELSE 0 END) AS total_images, " +
121+
"(SUM(CASE WHEN cr.message LIKE '%type=image%' THEN 1 ELSE 0 END) * 1.0 / COUNT(*)) AS image_ratio " +
122+
"FROM onebot.chat_records cr " +
123+
"WHERE cr.created_at >= DATE_TRUNC('month', CURRENT_DATE) " +
124+
"GROUP BY cr.qq_user_id " +
125+
"HAVING COUNT(*) > 50 " +
126+
"ORDER BY image_ratio DESC",
127+
nativeQuery = true)
76128
List<Object[]> findMonthlyImageRatioRanking();
77129

78130
// 查询本月消息总数
@@ -83,6 +135,8 @@ public interface PrimaryChatRecordRepository extends JpaRepository<ChatRecord, L
83135
@Query(value = "SELECT COUNT(*) FROM onebot.chat_records cr WHERE cr.message LIKE '%type=image%' AND cr.created_at >= DATE_TRUNC('month', CURRENT_DATE)", nativeQuery = true)
84136
long countMonthlyImages();
85137

138+
// ========== 其他查询方法 ==========
139+
86140
// 查询所有图片消息总数
87141
@Query(value = "SELECT COUNT(*) FROM onebot.chat_records cr WHERE cr.message LIKE '%type=image%'", nativeQuery = true)
88142
long countByMessageContaining(String pattern);

0 commit comments

Comments
 (0)