feat: 增加订单筛选功能,添加按金额区间筛选的数据库索引

This commit is contained in:
2025-12-18 16:50:24 +08:00
parent e268176af5
commit 5029234e47
2 changed files with 135 additions and 0 deletions

View File

@@ -396,6 +396,130 @@ func (s *OrderTestSuite) Test_AdminOrderPage() {
So(err, ShouldBeNil)
So(pager.Total, ShouldEqual, 1)
})
Convey("组合筛选user_id + status + amount_paid 区间 + content_id", func() {
s.truncate(ctx, models.TableNameOrderItem, models.TableNameOrder)
statusPaid := consts.OrderStatusPaid
userID := int64(7)
contentID := int64(777)
// 命中user_id=7, status=paid, amount_paid=500, content_id=777
oHit := &models.Order{
TenantID: tenantID,
UserID: userID,
Type: consts.OrderTypeContentPurchase,
Status: consts.OrderStatusPaid,
Currency: consts.CurrencyCNY,
AmountPaid: 500,
Snapshot: types.JSON([]byte("{}")),
PaidAt: now,
CreatedAt: now,
UpdatedAt: now,
}
So(oHit.Create(ctx), ShouldBeNil)
So((&models.OrderItem{
TenantID: tenantID,
UserID: userID,
OrderID: oHit.ID,
ContentID: contentID,
ContentUserID: 1,
AmountPaid: 500,
Snapshot: types.JSON([]byte("{}")),
CreatedAt: now,
UpdatedAt: now,
}).Create(ctx), ShouldBeNil)
// 不命中amount_paid 不在区间
oNoAmount := &models.Order{
TenantID: tenantID,
UserID: userID,
Type: consts.OrderTypeContentPurchase,
Status: consts.OrderStatusPaid,
Currency: consts.CurrencyCNY,
AmountPaid: 50,
Snapshot: types.JSON([]byte("{}")),
PaidAt: now,
CreatedAt: now,
UpdatedAt: now,
}
So(oNoAmount.Create(ctx), ShouldBeNil)
So((&models.OrderItem{
TenantID: tenantID,
UserID: userID,
OrderID: oNoAmount.ID,
ContentID: contentID,
ContentUserID: 1,
AmountPaid: 50,
Snapshot: types.JSON([]byte("{}")),
CreatedAt: now,
UpdatedAt: now,
}).Create(ctx), ShouldBeNil)
// 不命中status 不同
oNoStatus := &models.Order{
TenantID: tenantID,
UserID: userID,
Type: consts.OrderTypeContentPurchase,
Status: consts.OrderStatusCreated,
Currency: consts.CurrencyCNY,
AmountPaid: 500,
Snapshot: types.JSON([]byte("{}")),
PaidAt: now,
CreatedAt: now,
UpdatedAt: now,
}
So(oNoStatus.Create(ctx), ShouldBeNil)
So((&models.OrderItem{
TenantID: tenantID,
UserID: userID,
OrderID: oNoStatus.ID,
ContentID: contentID,
ContentUserID: 1,
AmountPaid: 500,
Snapshot: types.JSON([]byte("{}")),
CreatedAt: now,
UpdatedAt: now,
}).Create(ctx), ShouldBeNil)
// 不命中user_id 不同
oNoUser := &models.Order{
TenantID: tenantID,
UserID: 8,
Type: consts.OrderTypeContentPurchase,
Status: consts.OrderStatusPaid,
Currency: consts.CurrencyCNY,
AmountPaid: 500,
Snapshot: types.JSON([]byte("{}")),
PaidAt: now,
CreatedAt: now,
UpdatedAt: now,
}
So(oNoUser.Create(ctx), ShouldBeNil)
So((&models.OrderItem{
TenantID: tenantID,
UserID: 8,
OrderID: oNoUser.ID,
ContentID: contentID,
ContentUserID: 1,
AmountPaid: 500,
Snapshot: types.JSON([]byte("{}")),
CreatedAt: now,
UpdatedAt: now,
}).Create(ctx), ShouldBeNil)
min := int64(100)
max := int64(900)
pager, err := Order.AdminOrderPage(ctx, tenantID, &dto.AdminOrderListFilter{
UserID: &userID,
Status: &statusPaid,
ContentID: &contentID,
AmountPaidMin: &min,
AmountPaidMax: &max,
})
So(err, ShouldBeNil)
So(pager.Total, ShouldEqual, 1)
})
})
}

View File

@@ -0,0 +1,11 @@
-- +goose Up
-- +goose StatementBegin
-- orders.amount_paid为“按金额区间筛选订单”提供索引租户内隔离维度
CREATE INDEX IF NOT EXISTS ix_orders_tenant_amount_paid ON orders(tenant_id, amount_paid);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP INDEX IF EXISTS ix_orders_tenant_amount_paid;
-- +goose StatementEnd