From 1cffc335cebabff67266d9b8389cbe5a6bf47405 Mon Sep 17 00:00:00 2001 From: zhx Date: Fri, 5 Jun 2026 15:23:51 +0800 Subject: [PATCH] Fix gift list type filtering --- .../internal/service/wallet/service_test.go | 120 ++++++++++++++++++ .../storage/mysql/resource_repository.go | 5 +- 2 files changed, 124 insertions(+), 1 deletion(-) diff --git a/services/wallet-service/internal/service/wallet/service_test.go b/services/wallet-service/internal/service/wallet/service_test.go index 7f7864d4..994eaf00 100644 --- a/services/wallet-service/internal/service/wallet/service_test.go +++ b/services/wallet-service/internal/service/wallet/service_test.go @@ -2506,6 +2506,126 @@ func TestGiftConfigRegionFilter(t *testing.T) { } } +// TestListGiftConfigsWithoutTypeFilterIncludesLuckyTypes 验证礼物面板预加载全部礼物时不会被空类型过滤误收窄到普通礼物。 +func TestListGiftConfigsWithoutTypeFilterIncludesLuckyTypes(t *testing.T) { + repository := mysqltest.NewRepository(t) + svc := walletservice.New(repository) + ctx := context.Background() + + normalResource, err := svc.CreateResource(ctx, resourcedomain.ResourceCommand{ + ResourceCode: "gift_all_normal", + ResourceType: resourcedomain.TypeGift, + Name: "All Normal", + Status: resourcedomain.StatusActive, + Grantable: true, + GrantStrategy: resourcedomain.GrantStrategyIncreaseQuantity, + UsageScopes: []string{"gift"}, + OperatorUserID: 90001, + }) + if err != nil { + t.Fatalf("create normal gift resource failed: %v", err) + } + luckyResource, err := svc.CreateResource(ctx, resourcedomain.ResourceCommand{ + ResourceCode: "gift_all_lucky", + ResourceType: resourcedomain.TypeGift, + Name: "All Lucky", + Status: resourcedomain.StatusActive, + Grantable: true, + GrantStrategy: resourcedomain.GrantStrategyIncreaseQuantity, + UsageScopes: []string{"gift"}, + OperatorUserID: 90001, + }) + if err != nil { + t.Fatalf("create lucky gift resource failed: %v", err) + } + superLuckyResource, err := svc.CreateResource(ctx, resourcedomain.ResourceCommand{ + ResourceCode: "gift_all_super_lucky", + ResourceType: resourcedomain.TypeGift, + Name: "All Super Lucky", + Status: resourcedomain.StatusActive, + Grantable: true, + GrantStrategy: resourcedomain.GrantStrategyIncreaseQuantity, + UsageScopes: []string{"gift"}, + OperatorUserID: 90001, + }) + if err != nil { + t.Fatalf("create super lucky gift resource failed: %v", err) + } + commands := []resourcedomain.GiftConfigCommand{ + { + GiftID: "all-normal", + ResourceID: normalResource.ResourceID, + Status: resourcedomain.StatusActive, + Name: "All Normal", + PriceVersion: "v1", + CoinPrice: 100, + HeatValue: 100, + OperatorUserID: 90001, + RegionIDs: []int64{0}, + }, + { + GiftID: "all-lucky", + ResourceID: luckyResource.ResourceID, + Status: resourcedomain.StatusActive, + Name: "All Lucky", + GiftTypeCode: resourcedomain.GiftTypeLucky, + PriceVersion: "v1", + CoinPrice: 200, + HeatValue: 200, + OperatorUserID: 90001, + RegionIDs: []int64{0}, + }, + { + GiftID: "all-super-lucky", + ResourceID: superLuckyResource.ResourceID, + Status: resourcedomain.StatusActive, + Name: "All Super Lucky", + GiftTypeCode: resourcedomain.GiftTypeSuperLucky, + PriceVersion: "v1", + CoinPrice: 300, + HeatValue: 300, + OperatorUserID: 90001, + RegionIDs: []int64{0}, + }, + } + for _, command := range commands { + if _, err := svc.CreateGiftConfig(ctx, command); err != nil { + t.Fatalf("create gift config %s failed: %v", command.GiftID, err) + } + } + + // 空 gift_type_code 表示“不按类型过滤”,gateway 的 gift-tabs 会依赖这次查询一次性拿到所有礼物类型。 + items, total, err := svc.ListGiftConfigs(ctx, resourcedomain.ListGiftConfigsQuery{ + ActiveOnly: true, + FilterRegion: true, + RegionID: 0, + Page: 1, + PageSize: 10, + }) + if err != nil { + t.Fatalf("list all gift configs failed: %v", err) + } + if total != 3 || !giftIDsContain(items, "all-normal") || !giftIDsContain(items, "all-lucky") || !giftIDsContain(items, "all-super-lucky") { + t.Fatalf("all gift type list mismatch total=%d items=%+v", total, items) + } + + // 显式传入 lucky 时仍保留类型过滤能力,避免修复全部礼物预加载时破坏后台按类型筛选。 + luckyItems, luckyTotal, err := svc.ListGiftConfigs(ctx, resourcedomain.ListGiftConfigsQuery{ + GiftTypeCode: resourcedomain.GiftTypeLucky, + ActiveOnly: true, + FilterRegion: true, + RegionID: 0, + Page: 1, + PageSize: 10, + }) + if err != nil { + t.Fatalf("list lucky gift configs failed: %v", err) + } + if luckyTotal != 1 || !giftIDsContain(luckyItems, "all-lucky") || giftIDsContain(luckyItems, "all-normal") || giftIDsContain(luckyItems, "all-super-lucky") { + t.Fatalf("lucky gift type filter mismatch total=%d items=%+v", luckyTotal, luckyItems) + } +} + // TestGrantResourceGroupExpandsWalletAssetAndEntitlement 验证资源组赠送会原子展开钱包资产入账和非钱包权益。 func TestGrantResourceGroupExpandsWalletAssetAndEntitlement(t *testing.T) { repository := mysqltest.NewRepository(t) diff --git a/services/wallet-service/internal/storage/mysql/resource_repository.go b/services/wallet-service/internal/storage/mysql/resource_repository.go index 521972b1..1b2d18b5 100644 --- a/services/wallet-service/internal/storage/mysql/resource_repository.go +++ b/services/wallet-service/internal/storage/mysql/resource_repository.go @@ -2913,7 +2913,10 @@ func normalizeResourceGroupListQuery(query resourcedomain.ListResourceGroupsQuer func normalizeGiftConfigListQuery(query resourcedomain.ListGiftConfigsQuery) resourcedomain.ListGiftConfigsQuery { query.Status = strings.ToLower(strings.TrimSpace(query.Status)) query.Keyword = strings.TrimSpace(query.Keyword) - query.GiftTypeCode = resourcedomain.NormalizeGiftTypeCode(query.GiftTypeCode) + // 列表查询的 gift_type_code 是可选过滤条件;空值必须保持为空,避免礼物面板加载全部礼物时被默认收窄到 normal。 + if strings.TrimSpace(query.GiftTypeCode) != "" { + query.GiftTypeCode = resourcedomain.NormalizeGiftTypeCode(query.GiftTypeCode) + } query.Page, query.PageSize = normalizePage(query.Page, query.PageSize) return query }