This commit is contained in:
133
backend_v1/app/services/orders.go
Normal file
133
backend_v1/app/services/orders.go
Normal file
@@ -0,0 +1,133 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/fields"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
"go.ipao.vip/gen"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type orders struct{}
|
||||
|
||||
// List 订单列表(支持按订单号模糊查询、按用户过滤)。
|
||||
func (m *orders) List(
|
||||
ctx context.Context,
|
||||
pagination *requests.Pagination,
|
||||
orderNumber *string,
|
||||
userID *int64,
|
||||
) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
|
||||
tbl, query := models.OrderQuery.QueryContext(ctx)
|
||||
|
||||
conds := make([]gen.Condition, 0, 2)
|
||||
if orderNumber != nil && *orderNumber != "" {
|
||||
conds = append(conds, tbl.OrderNo.Like("%"+*orderNumber+"%"))
|
||||
}
|
||||
if userID != nil {
|
||||
conds = append(conds, tbl.UserID.Eq(*userID))
|
||||
}
|
||||
|
||||
orders, cnt, err := query.
|
||||
Where(conds...).
|
||||
Order(tbl.ID.Desc()).
|
||||
FindByPage(int(pagination.Offset()), int(pagination.Limit))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to list orders")
|
||||
}
|
||||
|
||||
// 这里刻意使用“先查订单,再批量查关联”的方式,避免在分页时 JOIN 造成重复行/分页不稳定。
|
||||
postIDs := lo.Uniq(lo.Map(orders, func(o *models.Order, _ int) int64 { return o.PostID }))
|
||||
userIDs := lo.Uniq(lo.Map(orders, func(o *models.Order, _ int) int64 { return o.UserID }))
|
||||
|
||||
posts, err := models.PostQuery.WithContext(ctx).GetByIDs(postIDs...)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get posts by ids")
|
||||
}
|
||||
users, err := models.UserQuery.WithContext(ctx).GetByIDs(userIDs...)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get users by ids")
|
||||
}
|
||||
|
||||
postMap := lo.SliceToMap(posts, func(p *models.Post) (int64, *models.Post) { return p.ID, p })
|
||||
userMap := lo.SliceToMap(users, func(u *models.User) (int64, *models.User) { return u.ID, u })
|
||||
|
||||
// OrderListItem 用于订单列表展示,补充作品标题与用户名等冗余信息,避免前端二次查询。
|
||||
type orderListItem struct {
|
||||
*models.Order
|
||||
PostTitle string `json:"post_title"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
items := lo.Map(orders, func(o *models.Order, _ int) *orderListItem {
|
||||
item := &orderListItem{Order: o}
|
||||
if post, ok := postMap[o.PostID]; ok {
|
||||
item.PostTitle = post.Title
|
||||
}
|
||||
if user, ok := userMap[o.UserID]; ok {
|
||||
item.Username = user.Username
|
||||
}
|
||||
return item
|
||||
})
|
||||
|
||||
return &requests.Pager{
|
||||
Items: items,
|
||||
Total: cnt,
|
||||
Pagination: *pagination,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Refund 订单退款(余额支付走本地退款;微信支付走微信退款并标记为退款处理中)。
|
||||
func (m *orders) Refund(ctx context.Context, id int64) error {
|
||||
// 余额支付:这里强调“状态一致性”,必须在一个事务中完成:余额退回 + 撤销购买权益 + 更新订单状态。
|
||||
return models.Q.Transaction(func(tx *models.Query) error {
|
||||
order, err := tx.Order.WithContext(ctx).GetByID(id)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get order in tx")
|
||||
}
|
||||
|
||||
// 退回余额(使用原子自增,避免并发覆盖)。
|
||||
costBalance := order.Meta.Data().CostBalance
|
||||
if costBalance > 0 {
|
||||
if _, err := tx.User.
|
||||
WithContext(ctx).
|
||||
Where(tx.User.ID.Eq(order.UserID)).
|
||||
Inc(tx.User.Balance, costBalance); err != nil {
|
||||
return errors.Wrap(err, "failed to refund balance")
|
||||
}
|
||||
}
|
||||
|
||||
// 撤销已购买的作品权限(删除 user_posts 记录)。
|
||||
if _, err := tx.UserPost.
|
||||
WithContext(ctx).
|
||||
Where(
|
||||
tx.UserPost.UserID.Eq(order.UserID),
|
||||
tx.UserPost.PostID.Eq(order.PostID),
|
||||
).
|
||||
Delete(); err != nil {
|
||||
return errors.Wrap(err, "failed to revoke user post")
|
||||
}
|
||||
|
||||
// 更新订单状态为“退款成功”。
|
||||
if _, err := tx.Order.
|
||||
WithContext(ctx).
|
||||
Where(tx.Order.ID.Eq(order.ID)).
|
||||
Update(tx.Order.Status, fields.OrderStatusRefundSuccess); err != nil {
|
||||
return errors.Wrap(err, "failed to update order status")
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// GetByOrderNO
|
||||
func (m *orders) GetByOrderNO(ctx context.Context, orderNo string) (*models.Order, error) {
|
||||
return models.OrderQuery.WithContext(ctx).Where(models.OrderQuery.OrderNo.Eq(orderNo)).First()
|
||||
}
|
||||
Reference in New Issue
Block a user