feat: 添加用户购买作品数量统计功能

This commit is contained in:
2025-12-20 23:23:04 +08:00
parent 65d40fa631
commit 257c9a286a
4 changed files with 165 additions and 56 deletions

View File

@@ -72,6 +72,37 @@ func (m *users) List(
}, nil
}
// BoughtStatistics 获取指定用户 ID 的购买作品数量(仅统计 user_posts 记录数)。
func (m *users) BoughtStatistics(ctx context.Context, userIDs []int64) (map[int64]int64, error) {
if len(userIDs) == 0 {
return map[int64]int64{}, nil
}
// 管理端用户列表需要展示购买数量;这里用 group by 聚合,避免 N+1。
tbl, query := models.UserPostQuery.QueryContext(ctx)
var items []struct {
Count int64
UserID int64
}
if err := query.
Select(
tbl.UserID.Count().As("count"),
tbl.UserID,
).
Where(tbl.UserID.In(userIDs...)).
Group(tbl.UserID).
Scan(&items); err != nil {
return nil, err
}
result := make(map[int64]int64, len(items))
for _, item := range items {
result[item.UserID] = item.Count
}
return result, nil
}
// PostList returns a paginated list of posts for a user
func (m *users) PostList(ctx context.Context, userId int64, filter *dto.PostListQuery) (*requests.Pager, error) {
filter.Format()