This commit is contained in:
@@ -88,45 +88,36 @@ func (m *posts) BoughtStatistics(ctx context.Context, postIds []int64) (map[int6
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bought 获取用户购买记录
|
|
||||||
func (m *posts) Bought(ctx context.Context, userId int64, pagination *requests.Pagination) (*requests.Pager, error) {
|
func (m *posts) Bought(ctx context.Context, userId int64, pagination *requests.Pagination) (*requests.Pager, error) {
|
||||||
pagination.Format()
|
pagination.Format()
|
||||||
tbl, query := models.UserPostQuery.QueryContext(ctx)
|
|
||||||
|
|
||||||
items, cnt, err := query.
|
tblUserPost, queryUserPost := models.UserPostQuery.QueryContext(ctx)
|
||||||
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 })
|
|
||||||
|
|
||||||
postItemMap, err := m.GetPostsMapByIDs(ctx, postIds)
|
cnt, err := queryUserPost.Where(tblUserPost.UserID.Eq(userId)).Count()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
type retItem struct {
|
tbl, query := models.PostQuery.QueryContext(ctx)
|
||||||
Title string `json:"title"`
|
|
||||||
|
var retItems []struct {
|
||||||
|
models.Post
|
||||||
Price int64 `json:"price"`
|
Price int64 `json:"price"`
|
||||||
BoughtAt time.Time `json:"bought_at"`
|
BoughtAt time.Time `json:"bought_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var retItems []retItem
|
err = query.
|
||||||
for _, postID := range postIds {
|
Select(
|
||||||
post, ok := postItemMap[postID]
|
tbl.ALL,
|
||||||
if !ok {
|
tblUserPost.Price.As("price"),
|
||||||
continue
|
tblUserPost.CreatedAt.As("bought_at"),
|
||||||
}
|
).
|
||||||
|
RightJoin(tblUserPost, tbl.ID.EqCol(tblUserPost.PostID)).
|
||||||
postInfo := postInfoMap[postID]
|
Where(tblUserPost.UserID.Eq(userId)).
|
||||||
|
Order(tblUserPost.CreatedAt.Desc()).
|
||||||
retItems = append(retItems, retItem{
|
Scan(&retItems)
|
||||||
Title: post.Title,
|
if err != nil {
|
||||||
Price: postInfo.Price,
|
return nil, err
|
||||||
BoughtAt: postInfo.CreatedAt,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &requests.Pager{
|
return &requests.Pager{
|
||||||
|
|||||||
Reference in New Issue
Block a user