tenant: extend admin order filters

This commit is contained in:
2025-12-18 23:22:37 +08:00
parent ec4506fd2d
commit 71bd15024e
9 changed files with 502 additions and 46 deletions

View File

@@ -10,6 +10,7 @@ import (
"quyun/v2/app/errorx"
"quyun/v2/app/http/tenant/dto"
"quyun/v2/app/requests"
"quyun/v2/database"
"quyun/v2/database/models"
"quyun/v2/pkg/consts"
@@ -350,10 +351,19 @@ func (s *order) AdminOrderPage(
}
logrus.WithFields(logrus.Fields{
"tenant_id": tenantID,
"user_id": lo.FromPtr(filter.UserID),
"status": lo.FromPtr(filter.Status),
"content_id": lo.FromPtr(filter.ContentID),
"tenant_id": tenantID,
"user_id": lo.FromPtr(filter.UserID),
"username": filter.UsernameTrimmed(),
"content_id": lo.FromPtr(filter.ContentID),
"content_title": filter.ContentTitleTrimmed(),
"type": lo.FromPtr(filter.Type),
"status": lo.FromPtr(filter.Status),
"created_at_from": filter.CreatedAtFrom,
"created_at_to": filter.CreatedAtTo,
"paid_at_from": filter.PaidAtFrom,
"paid_at_to": filter.PaidAtTo,
"amount_paid_min": filter.AmountPaidMin,
"amount_paid_max": filter.AmountPaidMax,
}).Info("services.order.admin.page")
filter.Pagination.Format()
@@ -365,9 +375,18 @@ func (s *order) AdminOrderPage(
if filter.UserID != nil {
conds = append(conds, tbl.UserID.Eq(*filter.UserID))
}
if filter.Type != nil {
conds = append(conds, tbl.Type.Eq(*filter.Type))
}
if filter.Status != nil {
conds = append(conds, tbl.Status.Eq(*filter.Status))
}
if filter.CreatedAtFrom != nil {
conds = append(conds, tbl.CreatedAt.Gte(*filter.CreatedAtFrom))
}
if filter.CreatedAtTo != nil {
conds = append(conds, tbl.CreatedAt.Lte(*filter.CreatedAtTo))
}
if filter.PaidAtFrom != nil {
conds = append(conds, tbl.PaidAt.Gte(*filter.PaidAtFrom))
}
@@ -380,10 +399,32 @@ func (s *order) AdminOrderPage(
if filter.AmountPaidMax != nil {
conds = append(conds, tbl.AmountPaid.Lte(*filter.AmountPaidMax))
}
if filter.ContentID != nil && *filter.ContentID > 0 {
// 用户关键字:按 users.username 模糊匹配。
// 关键点orders.user_id 与 users.id 一对一,不会导致重复行,无需 group by。
if username := filter.UsernameTrimmed(); username != "" {
uTbl, _ := models.UserQuery.QueryContext(ctx)
query = query.LeftJoin(uTbl, uTbl.ID.EqCol(tbl.UserID))
conds = append(conds, uTbl.Username.Like(database.WrapLike(username)))
}
// 内容过滤:通过 order_items以及 contents关联查询。
// 关键点orders 与 order_items 一对多join 后必须 group by orders.id 以避免同一订单重复返回。
needItemJoin := (filter.ContentID != nil && *filter.ContentID > 0) || filter.ContentTitleTrimmed() != ""
if needItemJoin {
oiTbl, _ := models.OrderItemQuery.QueryContext(ctx)
query = query.LeftJoin(oiTbl, oiTbl.OrderID.EqCol(tbl.ID))
conds = append(conds, oiTbl.ContentID.Eq(*filter.ContentID))
if filter.ContentID != nil && *filter.ContentID > 0 {
conds = append(conds, oiTbl.ContentID.Eq(*filter.ContentID))
}
if title := filter.ContentTitleTrimmed(); title != "" {
cTbl, _ := models.ContentQuery.QueryContext(ctx)
query = query.LeftJoin(cTbl, cTbl.ID.EqCol(oiTbl.ContentID))
conds = append(conds, cTbl.Title.Like(database.WrapLike(title)))
}
query = query.Group(tbl.ID)
}
@@ -650,7 +691,9 @@ func (s *order) PurchaseContent(ctx context.Context, params *PurchaseContentPara
return err
}
var access models.ContentAccess
if err := tx.Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).First(&access).Error; err != nil {
if err := tx.Table(models.TableNameContentAccess).
Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).
First(&access).Error; err != nil {
return err
}
out.AmountPaid = 0
@@ -762,7 +805,9 @@ func (s *order) PurchaseContent(ctx context.Context, params *PurchaseContentPara
return err
}
var access models.ContentAccess
if err := tx.Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).First(&access).Error; err != nil {
if err := tx.Table(models.TableNameContentAccess).
Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).
First(&access).Error; err != nil {
return err
}
out.Order = orderModel
@@ -829,12 +874,18 @@ func (s *order) PurchaseContent(ctx context.Context, params *PurchaseContentPara
}).Error; err != nil {
return err
}
// 关键点:上面是 DB 更新;这里同步更新内存对象,避免返回给调用方的状态仍为 created。
orderModel.Status = consts.OrderStatusPaid
orderModel.PaidAt = now
orderModel.UpdatedAt = now
if err := s.grantAccess(ctx, tx, params.TenantID, params.UserID, params.ContentID, orderModel.ID, now); err != nil {
return err
}
var access models.ContentAccess
if err := tx.Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).First(&access).Error; err != nil {
if err := tx.Table(models.TableNameContentAccess).
Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).
First(&access).Error; err != nil {
return err
}
@@ -993,7 +1044,9 @@ func (s *order) PurchaseContent(ctx context.Context, params *PurchaseContentPara
return err
}
var access models.ContentAccess
if err := tx.Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).First(&access).Error; err != nil {
if err := tx.Table(models.TableNameContentAccess).
Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).
First(&access).Error; err != nil {
return err
}
out.Order = orderModel
@@ -1053,13 +1106,19 @@ func (s *order) PurchaseContent(ctx context.Context, params *PurchaseContentPara
}).Error; err != nil {
return err
}
// 关键点:上面是 DB 更新;这里同步更新内存对象,避免返回给调用方的状态仍为 created。
orderModel.Status = consts.OrderStatusPaid
orderModel.PaidAt = now
orderModel.UpdatedAt = now
if err := s.grantAccess(ctx, tx, params.TenantID, params.UserID, params.ContentID, orderModel.ID, now); err != nil {
return err
}
var access models.ContentAccess
if err := tx.Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).First(&access).Error; err != nil {
if err := tx.Table(models.TableNameContentAccess).
Where("tenant_id = ? AND user_id = ? AND content_id = ?", params.TenantID, params.UserID, params.ContentID).
First(&access).Error; err != nil {
return err
}

View File

@@ -397,6 +397,238 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
So(pager.Total, ShouldEqual, 1)
})
Convey("按 username 关键字过滤", func() {
s.truncate(ctx, models.TableNameOrderItem, models.TableNameOrder, models.TableNameUser)
u1 := &models.User{
Username: "alice",
Password: "x",
Roles: types.NewArray([]consts.Role{consts.RoleUser}),
Status: consts.UserStatusVerified,
Metas: types.JSON([]byte("{}")),
CreatedAt: now,
UpdatedAt: now,
}
So(u1.Create(ctx), ShouldBeNil)
u2 := &models.User{
Username: "bob",
Password: "x",
Roles: types.NewArray([]consts.Role{consts.RoleUser}),
Status: consts.UserStatusVerified,
Metas: types.JSON([]byte("{}")),
CreatedAt: now,
UpdatedAt: now,
}
So(u2.Create(ctx), ShouldBeNil)
o1 := &models.Order{
TenantID: tenantID,
UserID: u1.ID,
Type: consts.OrderTypeContentPurchase,
Status: consts.OrderStatusPaid,
Currency: consts.CurrencyCNY,
AmountPaid: 100,
Snapshot: types.JSON([]byte("{}")),
PaidAt: now,
CreatedAt: now,
UpdatedAt: now,
}
So(o1.Create(ctx), ShouldBeNil)
o2 := &models.Order{
TenantID: tenantID,
UserID: u2.ID,
Type: consts.OrderTypeContentPurchase,
Status: consts.OrderStatusPaid,
Currency: consts.CurrencyCNY,
AmountPaid: 100,
Snapshot: types.JSON([]byte("{}")),
PaidAt: now,
CreatedAt: now,
UpdatedAt: now,
}
So(o2.Create(ctx), ShouldBeNil)
username := "ali"
pager, err := Order.AdminOrderPage(ctx, tenantID, &dto.AdminOrderListFilter{
Username: &username,
})
So(err, ShouldBeNil)
So(pager.Total, ShouldEqual, 1)
items, ok := pager.Items.([]*models.Order)
So(ok, ShouldBeTrue)
So(items[0].UserID, ShouldEqual, u1.ID)
})
Convey("按 content_title 关键字过滤", func() {
s.truncate(ctx, models.TableNameOrderItem, models.TableNameOrder, models.TableNameContent)
c1 := &models.Content{
TenantID: tenantID,
UserID: 1,
Title: "Go 教程",
Description: "desc",
Status: consts.ContentStatusPublished,
Visibility: consts.ContentVisibilityTenantOnly,
PreviewSeconds: consts.DefaultContentPreviewSeconds,
PreviewDownloadable: false,
PublishedAt: now,
CreatedAt: now,
UpdatedAt: now,
}
So(c1.Create(ctx), ShouldBeNil)
c2 := &models.Content{
TenantID: tenantID,
UserID: 1,
Title: "Rust 教程",
Description: "desc",
Status: consts.ContentStatusPublished,
Visibility: consts.ContentVisibilityTenantOnly,
PreviewSeconds: consts.DefaultContentPreviewSeconds,
PreviewDownloadable: false,
PublishedAt: now,
CreatedAt: now,
UpdatedAt: now,
}
So(c2.Create(ctx), ShouldBeNil)
o1 := &models.Order{
TenantID: tenantID,
UserID: 2,
Type: consts.OrderTypeContentPurchase,
Status: consts.OrderStatusPaid,
Currency: consts.CurrencyCNY,
AmountPaid: 100,
Snapshot: types.JSON([]byte("{}")),
PaidAt: now,
CreatedAt: now,
UpdatedAt: now,
}
So(o1.Create(ctx), ShouldBeNil)
So((&models.OrderItem{
TenantID: tenantID,
UserID: 2,
OrderID: o1.ID,
ContentID: c1.ID,
ContentUserID: 1,
AmountPaid: 100,
Snapshot: types.JSON([]byte("{}")),
CreatedAt: now,
UpdatedAt: now,
}).Create(ctx), ShouldBeNil)
o2 := &models.Order{
TenantID: tenantID,
UserID: 3,
Type: consts.OrderTypeContentPurchase,
Status: consts.OrderStatusPaid,
Currency: consts.CurrencyCNY,
AmountPaid: 100,
Snapshot: types.JSON([]byte("{}")),
PaidAt: now,
CreatedAt: now,
UpdatedAt: now,
}
So(o2.Create(ctx), ShouldBeNil)
So((&models.OrderItem{
TenantID: tenantID,
UserID: 3,
OrderID: o2.ID,
ContentID: c2.ID,
ContentUserID: 1,
AmountPaid: 100,
Snapshot: types.JSON([]byte("{}")),
CreatedAt: now,
UpdatedAt: now,
}).Create(ctx), ShouldBeNil)
title := "Go"
pager, err := Order.AdminOrderPage(ctx, tenantID, &dto.AdminOrderListFilter{
ContentTitle: &title,
})
So(err, ShouldBeNil)
So(pager.Total, ShouldEqual, 1)
})
Convey("按 created_at 时间窗过滤", func() {
s.truncate(ctx, models.TableNameOrderItem, models.TableNameOrder)
o1 := &models.Order{
TenantID: tenantID,
UserID: 2,
Type: consts.OrderTypeContentPurchase,
Status: consts.OrderStatusPaid,
Currency: consts.CurrencyCNY,
AmountPaid: 100,
Snapshot: types.JSON([]byte("{}")),
PaidAt: now,
CreatedAt: now.Add(-time.Hour),
UpdatedAt: now.Add(-time.Hour),
}
So(o1.Create(ctx), ShouldBeNil)
o2 := &models.Order{
TenantID: tenantID,
UserID: 3,
Type: consts.OrderTypeContentPurchase,
Status: consts.OrderStatusPaid,
Currency: consts.CurrencyCNY,
AmountPaid: 200,
Snapshot: types.JSON([]byte("{}")),
PaidAt: now,
CreatedAt: now,
UpdatedAt: now,
}
So(o2.Create(ctx), ShouldBeNil)
from := now.Add(-10 * time.Minute)
to := now.Add(10 * time.Minute)
pager, err := Order.AdminOrderPage(ctx, tenantID, &dto.AdminOrderListFilter{
CreatedAtFrom: &from,
CreatedAtTo: &to,
})
So(err, ShouldBeNil)
So(pager.Total, ShouldEqual, 1)
})
Convey("按 type 过滤", func() {
s.truncate(ctx, models.TableNameOrderItem, models.TableNameOrder)
o1 := &models.Order{
TenantID: tenantID,
UserID: 2,
Type: consts.OrderTypeTopup,
Status: consts.OrderStatusPaid,
Currency: consts.CurrencyCNY,
AmountPaid: 100,
Snapshot: types.JSON([]byte("{}")),
PaidAt: now,
CreatedAt: now,
UpdatedAt: now,
}
So(o1.Create(ctx), ShouldBeNil)
o2 := &models.Order{
TenantID: tenantID,
UserID: 3,
Type: consts.OrderTypeContentPurchase,
Status: consts.OrderStatusPaid,
Currency: consts.CurrencyCNY,
AmountPaid: 200,
Snapshot: types.JSON([]byte("{}")),
PaidAt: now,
CreatedAt: now,
UpdatedAt: now,
}
So(o2.Create(ctx), ShouldBeNil)
typ := consts.OrderTypeTopup
pager, err := Order.AdminOrderPage(ctx, tenantID, &dto.AdminOrderListFilter{
Type: &typ,
})
So(err, ShouldBeNil)
So(pager.Total, ShouldEqual, 1)
})
Convey("组合筛选user_id + status + amount_paid 区间 + content_id", func() {
s.truncate(ctx, models.TableNameOrderItem, models.TableNameOrder)