@@ -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