From ffd3eeddfda9b224809af0ca7a9089a476e0ab7f Mon Sep 17 00:00:00 2001 From: hy001 Date: Fri, 15 May 2026 23:03:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8F=91=E5=A5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/integration/gateways.go | 10 +++ internal/integration/java.go | 8 +++ internal/service/smashegg/draw.go | 14 ++-- internal/service/smashegg/draw_test.go | 88 ++++++++++++++++++++++++++ internal/service/smashegg/types.go | 1 + internal/service/wheel/draw.go | 14 ++-- internal/service/wheel/draw_test.go | 88 ++++++++++++++++++++++++++ internal/service/wheel/types.go | 1 + 8 files changed, 210 insertions(+), 14 deletions(-) create mode 100644 internal/service/smashegg/draw_test.go create mode 100644 internal/service/wheel/draw_test.go diff --git a/internal/integration/gateways.go b/internal/integration/gateways.go index f8d866e..54906e6 100644 --- a/internal/integration/gateways.go +++ b/internal/integration/gateways.go @@ -96,6 +96,11 @@ func (g *Gateways) GivePropsBackpack(ctx context.Context, req GivePropsBackpackR return g.RewardDispatch.GivePropsBackpack(ctx, req) } +// IncrGiftBackpack 透传到礼物背包发放接口。 +func (g *Gateways) IncrGiftBackpack(ctx context.Context, userID int64, giftID int64, quantity int) error { + return g.RewardDispatch.IncrGiftBackpack(ctx, userID, giftID, quantity) +} + // GetNobleVIPAbility 透传到贵族 VIP 能力配置接口。 func (g *Gateways) GetNobleVIPAbility(ctx context.Context, sourceID int64) (PropsNobleVIPAbility, error) { return g.RewardQuery.GetNobleVIPAbility(ctx, sourceID) @@ -325,6 +330,11 @@ func (g *RewardDispatchGateway) GivePropsBackpack(ctx context.Context, req GiveP return g.client.GivePropsBackpack(ctx, req) } +// IncrGiftBackpack 发放礼物到用户礼物背包。 +func (g *RewardDispatchGateway) IncrGiftBackpack(ctx context.Context, userID int64, giftID int64, quantity int) error { + return g.client.IncrGiftBackpack(ctx, userID, giftID, quantity) +} + // SwitchUseProps 切换用户当前使用道具。 func (g *RewardDispatchGateway) SwitchUseProps(ctx context.Context, userID int64, propsID int64) error { return g.client.SwitchUseProps(ctx, userID, propsID) diff --git a/internal/integration/java.go b/internal/integration/java.go index 0c01a8c..41d9af5 100644 --- a/internal/integration/java.go +++ b/internal/integration/java.go @@ -640,6 +640,14 @@ func (c *Client) GivePropsBackpack(ctx context.Context, req GivePropsBackpackReq return c.postJSON(ctx, c.cfg.Java.OtherBaseURL+"/props/client/giveProps", req, nil, nil) } +func (c *Client) IncrGiftBackpack(ctx context.Context, userID int64, giftID int64, quantity int) error { + endpoint := c.cfg.Java.OtherBaseURL + "/gift-backpack/client/incrGift?userId=" + + url.QueryEscape(strconv.FormatInt(userID, 10)) + + "&giftId=" + url.QueryEscape(strconv.FormatInt(giftID, 10)) + + "&quantity=" + url.QueryEscape(strconv.Itoa(quantity)) + return c.getJSON(ctx, endpoint, nil, nil) +} + func (c *Client) GetNobleVIPAbility(ctx context.Context, sourceID int64) (PropsNobleVIPAbility, error) { endpoint := c.cfg.Java.OtherBaseURL + "/props/noble-vip/client/getAbilityDTO?sourceId=" + url.QueryEscape(strconv.FormatInt(sourceID, 10)) diff --git a/internal/service/smashegg/draw.go b/internal/service/smashegg/draw.go index 4bd9100..a4f9648 100644 --- a/internal/service/smashegg/draw.go +++ b/internal/service/smashegg/draw.go @@ -254,8 +254,10 @@ func (s *Service) grantResourceReward(ctx context.Context, record *model.SmashEg _ = s.java.RemoveUserProfileCacheAll(ctx, record.UserID) return nil } + if resourceType == resourceTypeGift { + return s.java.IncrGiftBackpack(ctx, record.UserID, resourceID, 1) + } - useProps := resourceType != resourceTypeGift if err := s.java.GivePropsBackpack(ctx, integration.GivePropsBackpackRequest{ AcceptUserID: record.UserID, PropsID: resourceID, @@ -263,17 +265,15 @@ func (s *Service) grantResourceReward(ctx context.Context, record *model.SmashEg Origin: walletOrigin, OriginDesc: walletOriginDesc, Days: days, - UseProps: useProps, + UseProps: true, AllowGive: false, }); err != nil { return err } - if useProps { - if err := s.java.SwitchUseProps(ctx, record.UserID, resourceID); err != nil { - return err - } - _ = s.java.RemoveUserProfileCacheAll(ctx, record.UserID) + if err := s.java.SwitchUseProps(ctx, record.UserID, resourceID); err != nil { + return err } + _ = s.java.RemoveUserProfileCacheAll(ctx, record.UserID) return nil } diff --git a/internal/service/smashegg/draw_test.go b/internal/service/smashegg/draw_test.go new file mode 100644 index 0000000..b64907d --- /dev/null +++ b/internal/service/smashegg/draw_test.go @@ -0,0 +1,88 @@ +package smashegg + +import ( + "context" + "testing" + + "chatapp3-golang/internal/integration" + "chatapp3-golang/internal/model" +) + +type fakeSmashEggGateway struct { + giftCalls []fakeGiftBackpackCall + propsCalls []integration.GivePropsBackpackRequest + switches int +} + +type fakeGiftBackpackCall struct { + userID int64 + giftID int64 + quantity int +} + +func (g *fakeSmashEggGateway) ChangeGoldBalance(context.Context, integration.GoldReceiptCommand) error { + return nil +} + +func (g *fakeSmashEggGateway) GivePropsBackpack(_ context.Context, req integration.GivePropsBackpackRequest) error { + g.propsCalls = append(g.propsCalls, req) + return nil +} + +func (g *fakeSmashEggGateway) IncrGiftBackpack(_ context.Context, userID int64, giftID int64, quantity int) error { + g.giftCalls = append(g.giftCalls, fakeGiftBackpackCall{ + userID: userID, + giftID: giftID, + quantity: quantity, + }) + return nil +} + +func (g *fakeSmashEggGateway) SwitchUseProps(context.Context, int64, int64) error { + g.switches++ + return nil +} + +func (g *fakeSmashEggGateway) ActivateTemporaryBadge(context.Context, int64, int64, int) error { + return nil +} + +func (g *fakeSmashEggGateway) RemoveUserProfileCacheAll(context.Context, int64) error { + return nil +} + +func (g *fakeSmashEggGateway) MapGoldBalance(context.Context, []int64) (map[int64]int64, error) { + return map[int64]int64{}, nil +} + +func (g *fakeSmashEggGateway) MapUserTotalRecharge(context.Context, []int64) (map[int64][]integration.UserTotalRecharge, error) { + return map[int64][]integration.UserTotalRecharge{}, nil +} + +func TestGrantResourceRewardRoutesGiftToGiftBackpack(t *testing.T) { + gateway := &fakeSmashEggGateway{} + service := &Service{java: gateway} + giftID := int64(2044086730826510337) + + if err := service.grantResourceReward(context.Background(), &model.SmashEggDrawRecord{ + UserID: 2042274349343506434, + RewardGroupID: &giftID, + ResourceType: resourceTypeGift, + DurationDays: 7, + }); err != nil { + t.Fatalf("grantResourceReward() error = %v", err) + } + + if len(gateway.giftCalls) != 1 { + t.Fatalf("gift backpack calls = %d, want 1", len(gateway.giftCalls)) + } + if got := gateway.giftCalls[0]; got.userID != 2042274349343506434 || got.giftID != giftID || got.quantity != 1 { + t.Fatalf("gift backpack call = %+v", got) + } + if len(gateway.propsCalls) != 0 { + t.Fatalf("props backpack calls = %d, want 0", len(gateway.propsCalls)) + } + if gateway.switches != 0 { + t.Fatalf("switch use props calls = %d, want 0", gateway.switches) + } +} diff --git a/internal/service/smashegg/types.go b/internal/service/smashegg/types.go index edc435d..ab4c6b8 100644 --- a/internal/service/smashegg/types.go +++ b/internal/service/smashegg/types.go @@ -63,6 +63,7 @@ type smashEggDB interface { type gateway interface { ChangeGoldBalance(ctx context.Context, cmd integration.GoldReceiptCommand) error GivePropsBackpack(ctx context.Context, req integration.GivePropsBackpackRequest) error + IncrGiftBackpack(ctx context.Context, userID int64, giftID int64, quantity int) error SwitchUseProps(ctx context.Context, userID int64, propsID int64) error ActivateTemporaryBadge(ctx context.Context, userID int64, badgeID int64, days int) error RemoveUserProfileCacheAll(ctx context.Context, userID int64) error diff --git a/internal/service/wheel/draw.go b/internal/service/wheel/draw.go index 97d94df..f7fc0e4 100644 --- a/internal/service/wheel/draw.go +++ b/internal/service/wheel/draw.go @@ -324,8 +324,10 @@ func (s *Service) grantResourceReward(ctx context.Context, record *model.WheelDr _ = s.java.RemoveUserProfileCacheAll(ctx, record.UserID) return nil } + if resourceType == resourceTypeGift { + return s.java.IncrGiftBackpack(ctx, record.UserID, resourceID, 1) + } - useProps := resourceType != resourceTypeGift if err := s.java.GivePropsBackpack(ctx, integration.GivePropsBackpackRequest{ AcceptUserID: record.UserID, PropsID: resourceID, @@ -333,17 +335,15 @@ func (s *Service) grantResourceReward(ctx context.Context, record *model.WheelDr Origin: walletOrigin, OriginDesc: walletOriginDesc, Days: days, - UseProps: useProps, + UseProps: true, AllowGive: false, }); err != nil { return err } - if useProps { - if err := s.java.SwitchUseProps(ctx, record.UserID, resourceID); err != nil { - return err - } - _ = s.java.RemoveUserProfileCacheAll(ctx, record.UserID) + if err := s.java.SwitchUseProps(ctx, record.UserID, resourceID); err != nil { + return err } + _ = s.java.RemoveUserProfileCacheAll(ctx, record.UserID) return nil } diff --git a/internal/service/wheel/draw_test.go b/internal/service/wheel/draw_test.go new file mode 100644 index 0000000..d7e6bc6 --- /dev/null +++ b/internal/service/wheel/draw_test.go @@ -0,0 +1,88 @@ +package wheel + +import ( + "context" + "testing" + + "chatapp3-golang/internal/integration" + "chatapp3-golang/internal/model" +) + +type fakeWheelGateway struct { + giftCalls []fakeGiftBackpackCall + propsCalls []integration.GivePropsBackpackRequest + switches int +} + +type fakeGiftBackpackCall struct { + userID int64 + giftID int64 + quantity int +} + +func (g *fakeWheelGateway) ChangeGoldBalance(context.Context, integration.GoldReceiptCommand) error { + return nil +} + +func (g *fakeWheelGateway) GivePropsBackpack(_ context.Context, req integration.GivePropsBackpackRequest) error { + g.propsCalls = append(g.propsCalls, req) + return nil +} + +func (g *fakeWheelGateway) IncrGiftBackpack(_ context.Context, userID int64, giftID int64, quantity int) error { + g.giftCalls = append(g.giftCalls, fakeGiftBackpackCall{ + userID: userID, + giftID: giftID, + quantity: quantity, + }) + return nil +} + +func (g *fakeWheelGateway) SwitchUseProps(context.Context, int64, int64) error { + g.switches++ + return nil +} + +func (g *fakeWheelGateway) ActivateTemporaryBadge(context.Context, int64, int64, int) error { + return nil +} + +func (g *fakeWheelGateway) RemoveUserProfileCacheAll(context.Context, int64) error { + return nil +} + +func (g *fakeWheelGateway) MapGoldBalance(context.Context, []int64) (map[int64]int64, error) { + return map[int64]int64{}, nil +} + +func (g *fakeWheelGateway) GetRoomProfileByUserID(context.Context, int64) (integration.RoomProfile, error) { + return integration.RoomProfile{}, nil +} + +func TestGrantResourceRewardRoutesGiftToGiftBackpack(t *testing.T) { + gateway := &fakeWheelGateway{} + service := &Service{java: gateway} + giftID := int64(2044086730826510337) + + if err := service.grantResourceReward(context.Background(), &model.WheelDrawRecord{ + UserID: 2042274349343506434, + ResourceID: &giftID, + ResourceType: resourceTypeGift, + DurationDays: 7, + }); err != nil { + t.Fatalf("grantResourceReward() error = %v", err) + } + + if len(gateway.giftCalls) != 1 { + t.Fatalf("gift backpack calls = %d, want 1", len(gateway.giftCalls)) + } + if got := gateway.giftCalls[0]; got.userID != 2042274349343506434 || got.giftID != giftID || got.quantity != 1 { + t.Fatalf("gift backpack call = %+v", got) + } + if len(gateway.propsCalls) != 0 { + t.Fatalf("props backpack calls = %d, want 0", len(gateway.propsCalls)) + } + if gateway.switches != 0 { + t.Fatalf("switch use props calls = %d, want 0", gateway.switches) + } +} diff --git a/internal/service/wheel/types.go b/internal/service/wheel/types.go index df14a66..c025dd6 100644 --- a/internal/service/wheel/types.go +++ b/internal/service/wheel/types.go @@ -100,6 +100,7 @@ type wheelDB interface { type wheelGateway interface { ChangeGoldBalance(ctx context.Context, cmd integration.GoldReceiptCommand) error GivePropsBackpack(ctx context.Context, req integration.GivePropsBackpackRequest) error + IncrGiftBackpack(ctx context.Context, userID int64, giftID int64, quantity int) error SwitchUseProps(ctx context.Context, userID int64, propsID int64) error ActivateTemporaryBadge(ctx context.Context, userID int64, badgeID int64, days int) error RemoveUserProfileCacheAll(ctx context.Context, userID int64) error