|
@@ -19,37 +19,32 @@ public interface ArticleReadingRecordMapper extends BaseMapper<ArticleReadingRec
|
|
|
@Param("since") java.util.Date since);
|
|
@Param("since") java.util.Date since);
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 按文章 ID 分页查询阅读记录,JOIN family_members 获取昵称,按最后阅读时间倒序
|
|
|
|
|
|
|
+ * 按文章 ID 分页查询阅读记录(每人聚合一行),JOIN family_members 获取昵称
|
|
|
*/
|
|
*/
|
|
|
@Select("SELECT " +
|
|
@Select("SELECT " +
|
|
|
- " r.id, " +
|
|
|
|
|
- " r.user_id, " +
|
|
|
|
|
- " r.child_id, " +
|
|
|
|
|
- " r.duration_seconds, " +
|
|
|
|
|
- " r.read_at, " +
|
|
|
|
|
- " r.updated_at, " +
|
|
|
|
|
" m.nickname, " +
|
|
" m.nickname, " +
|
|
|
- " DATE(r.read_at) AS read_date " +
|
|
|
|
|
|
|
+ " SUM(r.duration_seconds) AS total_duration_seconds, " +
|
|
|
|
|
+ " COUNT(*) AS read_count, " +
|
|
|
|
|
+ " MAX(r.updated_at) AS last_read_at, " +
|
|
|
|
|
+ " MAX(DATE(r.read_at)) AS last_read_date " +
|
|
|
"FROM article_reading_records r " +
|
|
"FROM article_reading_records r " +
|
|
|
"LEFT JOIN family_members m ON r.child_id = m.id " +
|
|
"LEFT JOIN family_members m ON r.child_id = m.id " +
|
|
|
"WHERE r.article_id = #{articleId} " +
|
|
"WHERE r.article_id = #{articleId} " +
|
|
|
- "ORDER BY r.updated_at DESC, r.read_at DESC " +
|
|
|
|
|
|
|
+ "GROUP BY r.child_id, m.nickname " +
|
|
|
|
|
+ "ORDER BY total_duration_seconds DESC " +
|
|
|
"LIMIT #{limit} OFFSET #{offset}")
|
|
"LIMIT #{limit} OFFSET #{offset}")
|
|
|
- @Results(id = "readRecordResultMap", value = {
|
|
|
|
|
- @Result(column = "id", property = "id"),
|
|
|
|
|
- @Result(column = "user_id", property = "user_id"),
|
|
|
|
|
- @Result(column = "child_id", property = "child_id"),
|
|
|
|
|
- @Result(column = "duration_seconds", property = "duration_seconds"),
|
|
|
|
|
- @Result(column = "read_at", property = "read_at"),
|
|
|
|
|
- @Result(column = "updated_at", property = "updated_at"),
|
|
|
|
|
|
|
+ @Results(id = "readRecordAggregatedMap", value = {
|
|
|
@Result(column = "nickname", property = "nickname"),
|
|
@Result(column = "nickname", property = "nickname"),
|
|
|
- @Result(column = "read_date", property = "read_date")
|
|
|
|
|
|
|
+ @Result(column = "total_duration_seconds", property = "total_duration_seconds"),
|
|
|
|
|
+ @Result(column = "read_count", property = "read_count"),
|
|
|
|
|
+ @Result(column = "last_read_at", property = "last_read_at"),
|
|
|
|
|
+ @Result(column = "last_read_date", property = "last_read_date")
|
|
|
})
|
|
})
|
|
|
- List<Map<String, Object>> selectByArticle(
|
|
|
|
|
|
|
+ List<Map<String, Object>> selectByArticleAggregated(
|
|
|
@Param("articleId") Long articleId,
|
|
@Param("articleId") Long articleId,
|
|
|
@Param("offset") int offset,
|
|
@Param("offset") int offset,
|
|
|
@Param("limit") int limit);
|
|
@Param("limit") int limit);
|
|
|
|
|
|
|
|
- @Select("SELECT COUNT(*) FROM article_reading_records WHERE article_id = #{articleId}")
|
|
|
|
|
- long countByArticle(@Param("articleId") Long articleId);
|
|
|
|
|
|
|
+ @Select("SELECT COUNT(DISTINCT child_id) FROM article_reading_records WHERE article_id = #{articleId}")
|
|
|
|
|
+ long countDistinctByArticle(@Param("articleId") Long articleId);
|
|
|
}
|
|
}
|