diff --git a/backend_v1/app/services/posts.go b/backend_v1/app/services/posts.go index be97e4e..b188409 100644 --- a/backend_v1/app/services/posts.go +++ b/backend_v1/app/services/posts.go @@ -88,45 +88,36 @@ func (m *posts) BoughtStatistics(ctx context.Context, postIds []int64) (map[int6 return result, nil } -// Bought 获取用户购买记录 func (m *posts) Bought(ctx context.Context, userId int64, pagination *requests.Pagination) (*requests.Pager, error) { pagination.Format() - tbl, query := models.UserPostQuery.QueryContext(ctx) - items, cnt, err := query. - Where(tbl.UserID.Eq(userId)). - FindByPage(int(pagination.Offset()), int(pagination.Limit)) - if err != nil { - return nil, err - } - postIds := lo.Map(items, func(item *models.UserPost, _ int) int64 { return item.PostID }) - postInfoMap := lo.KeyBy(items, func(item *models.UserPost) int64 { return item.PostID }) + tblUserPost, queryUserPost := models.UserPostQuery.QueryContext(ctx) - postItemMap, err := m.GetPostsMapByIDs(ctx, postIds) + cnt, err := queryUserPost.Where(tblUserPost.UserID.Eq(userId)).Count() if err != nil { return nil, err } - type retItem struct { - Title string `json:"title"` + tbl, query := models.PostQuery.QueryContext(ctx) + + var retItems []struct { + models.Post Price int64 `json:"price"` BoughtAt time.Time `json:"bought_at"` } - var retItems []retItem - for _, postID := range postIds { - post, ok := postItemMap[postID] - if !ok { - continue - } - - postInfo := postInfoMap[postID] - - retItems = append(retItems, retItem{ - Title: post.Title, - Price: postInfo.Price, - BoughtAt: postInfo.CreatedAt, - }) + err = query. + Select( + tbl.ALL, + tblUserPost.Price.As("price"), + tblUserPost.CreatedAt.As("bought_at"), + ). + RightJoin(tblUserPost, tbl.ID.EqCol(tblUserPost.PostID)). + Where(tblUserPost.UserID.Eq(userId)). + Order(tblUserPost.CreatedAt.Desc()). + Scan(&retItems) + if err != nil { + return nil, err } return &requests.Pager{