This commit is contained in:
@@ -127,6 +127,62 @@ func (m *posts) Bought(ctx context.Context, userId int64, pagination *requests.P
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Buyers 获取某个作品的购买人列表(管理端使用)
|
||||
func (m *posts) Buyers(ctx context.Context, postID int64, pagination *requests.Pagination) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
|
||||
// 先分页查询购买记录,避免一次性拉取全量 user_posts 造成内存/延迟抖动
|
||||
tblUserPost, queryUserPost := models.UserPostQuery.QueryContext(ctx)
|
||||
queryUserPost = queryUserPost.
|
||||
Where(tblUserPost.PostID.Eq(postID)).
|
||||
Order(tblUserPost.CreatedAt.Desc())
|
||||
|
||||
userPosts, cnt, err := queryUserPost.FindByPage(int(pagination.Offset()), int(pagination.Limit))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(userPosts) == 0 {
|
||||
return &requests.Pager{
|
||||
Items: []dto.PostBuyerItem{},
|
||||
Total: cnt,
|
||||
Pagination: *pagination,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 批量回表查询用户信息,避免 N+1
|
||||
userIDs := lo.Uniq(lo.Map(userPosts, func(item *models.UserPost, _ int) int64 {
|
||||
return item.UserID
|
||||
}))
|
||||
tblUser, queryUser := models.UserQuery.QueryContext(ctx)
|
||||
users, err := queryUser.Where(tblUser.ID.In(userIDs...)).Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
userMap := lo.KeyBy(users, func(item *models.User) int64 { return item.ID })
|
||||
|
||||
items := make([]dto.PostBuyerItem, 0, len(userPosts))
|
||||
for _, item := range userPosts {
|
||||
user, ok := userMap[item.UserID]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
items = append(items, dto.PostBuyerItem{
|
||||
UserID: user.ID,
|
||||
Username: user.Username,
|
||||
Avatar: user.Avatar,
|
||||
Phone: user.Phone,
|
||||
BoughtAt: item.CreatedAt,
|
||||
Price: item.Price,
|
||||
})
|
||||
}
|
||||
|
||||
return &requests.Pager{
|
||||
Items: items,
|
||||
Total: cnt,
|
||||
Pagination: *pagination,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetPostsMapByIDs
|
||||
func (m *posts) GetPostsMapByIDs(ctx context.Context, ids []int64) (map[int64]*models.Post, error) {
|
||||
tbl, query := models.PostQuery.QueryContext(ctx)
|
||||
|
||||
Reference in New Issue
Block a user