From 5029234e473e8e50ff4e7d094693a0d614eba068 Mon Sep 17 00:00:00 2001 From: Rogee Date: Thu, 18 Dec 2025 16:50:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E7=AD=9B=E9=80=89=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=8C=89=E9=87=91=E9=A2=9D=E5=8C=BA=E9=97=B4=E7=AD=9B=E9=80=89?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E6=8D=AE=E5=BA=93=E7=B4=A2=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/services/order_test.go | 124 ++++++++++++++++++ ...0251218164000_orders_amount_paid_index.sql | 11 ++ 2 files changed, 135 insertions(+) create mode 100644 backend/database/migrations/20251218164000_orders_amount_paid_index.sql diff --git a/backend/app/services/order_test.go b/backend/app/services/order_test.go index 7d8ba6a..b6cb749 100644 --- a/backend/app/services/order_test.go +++ b/backend/app/services/order_test.go @@ -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) + }) }) } diff --git a/backend/database/migrations/20251218164000_orders_amount_paid_index.sql b/backend/database/migrations/20251218164000_orders_amount_paid_index.sql new file mode 100644 index 0000000..9fcc843 --- /dev/null +++ b/backend/database/migrations/20251218164000_orders_amount_paid_index.sql @@ -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 +