fix: 实现空间 TotalReads 统计,修复 selectMention 闭包

- P0-2: posts/show.html selectMention 闭包 var→let 修复
- P0-3: GetUserStats 增加 SUM(views_count) 统计,替换硬编码 int64(0)
- 更新 Fix_List.md 标注 P0-1/2/3 已修复
This commit is contained in:
2026-06-02 15:21:37 +08:00
parent c1ad8a0121
commit 166e01c7c3
6 changed files with 14 additions and 13 deletions

View File

@ -93,18 +93,19 @@ func (r *PostRepo) CountUserPosts(userID uint) (int64, error) {
return count, err
}
// GetUserStats 获取用户的文章统计数据(总获赞/总收藏)
func (r *PostRepo) GetUserStats(userID uint) (likes, favorites int64, err error) {
// GetUserStats 获取用户的文章统计数据(总获赞/总收藏/总阅读
func (r *PostRepo) GetUserStats(userID uint) (likes, favorites, reads int64, err error) {
type stats struct {
TotalLikes int64
TotalLikes int64
TotalFavorites int64
TotalReads int64
}
var s stats
err = r.db.Model(&model.Post{}).
Select("COALESCE(SUM(likes_count), 0) AS total_likes, COALESCE(SUM(favorites_count), 0) AS total_favorites").
Select("COALESCE(SUM(likes_count), 0) AS total_likes, COALESCE(SUM(favorites_count), 0) AS total_favorites, COALESCE(SUM(views_count), 0) AS total_reads").
Where("user_id = ? AND status = ? AND deleted_at IS NULL", userID, model.PostStatusApproved).
Scan(&s).Error
return s.TotalLikes, s.TotalFavorites, err
return s.TotalLikes, s.TotalFavorites, s.TotalReads, err
}
// FindByUserIDAndStatus 分页查询某用户指定状态的帖子(空 status=全状态)