feat: update order s

This commit is contained in:
yanghao05
2025-04-18 22:55:37 +08:00
parent 6ca359ec1e
commit 75bbca00cf
4 changed files with 111 additions and 16 deletions

View File

@@ -12,6 +12,7 @@ import (
. "github.com/go-jet/jet/v2/postgres"
"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
)
@@ -100,20 +101,56 @@ func (m *ordersModel) List(ctx context.Context, pagination *requests.Pagination,
m.log.Infof("sql: %s", stmt.DebugSql())
var orders []model.Orders = make([]model.Orders, 0)
err := stmt.QueryContext(ctx, db, &orders)
if err != nil {
if err := stmt.QueryContext(ctx, db, &orders); err != nil {
m.log.Errorf("error querying orders: %v", err)
return nil, err
}
postsMap, err := Posts.GetPostsMapByIDs(ctx, lo.Map(orders, func(order model.Orders, _ int) int64 {
return order.PostID
}))
if err != nil {
m.log.Errorf("error getting posts map: %v", err)
return nil, err
}
userMap, err := Users.GetUsersMapByIDs(ctx, lo.Map(orders, func(order model.Orders, _ int) int64 {
return order.UserID
}))
if err != nil {
m.log.Errorf("error getting users map: %v", err)
return nil, err
}
count, err := m.countByCondition(ctx, cond)
if err != nil {
m.log.Errorf("error getting order count: %v", err)
return nil, err
}
type orderItem struct {
model.Orders
PostTitle string `json:"post_title"`
Username string `json:"username"`
}
return &requests.Pager{
Items: orders,
Items: lo.Map(orders, func(order model.Orders, _ int) *orderItem {
item := &orderItem{
Orders: order,
}
if post, ok := postsMap[order.PostID]; ok {
item.PostTitle = post.Title
}
if user, ok := userMap[order.UserID]; ok {
item.Username = user.Username
}
return item
}),
Total: count,
Pagination: *pagination,
}, nil

View File

@@ -343,3 +343,30 @@ func (m *postsModel) Bought(ctx context.Context, userId int64, pagination *reque
Pagination: *pagination,
}, nil
}
// GetPostsMapByIDs
func (m *postsModel) GetPostsMapByIDs(ctx context.Context, ids []int64) (map[int64]model.Posts, error) {
if len(ids) == 0 {
return nil, nil
}
tbl := table.Posts
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(
tbl.ID.IN(lo.Map(ids, func(id int64, _ int) Expression { return Int64(id) })...),
)
m.log.Infof("sql: %s", stmt.DebugSql())
var posts []model.Posts = make([]model.Posts, 0)
err := stmt.QueryContext(ctx, db, &posts)
if err != nil {
m.log.Errorf("error querying posts: %v", err)
return nil, err
}
return lo.SliceToMap(posts, func(item model.Posts) (int64, model.Posts) {
return item.ID, item
}), nil
}

View File

@@ -256,3 +256,30 @@ func (m *usersModel) GetUserByOpenID(ctx context.Context, openID string) (*model
return &user, nil
}
// GetUsersMapByIDs
func (m *usersModel) GetUsersMapByIDs(ctx context.Context, ids []int64) (map[int64]model.Users, error) {
if len(ids) == 0 {
return nil, nil
}
tbl := table.Users
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(
tbl.ID.IN(lo.Map(ids, func(id int64, _ int) Expression { return Int64(id) })...),
)
m.log.Infof("sql: %s", stmt.DebugSql())
var users []model.Users = make([]model.Users, 0)
err := stmt.QueryContext(ctx, db, &users)
if err != nil {
m.log.Errorf("error querying users: %v", err)
return nil, err
}
return lo.SliceToMap(users, func(item model.Users) (int64, model.Users) {
return item.ID, item
}), nil
}