2026-04-25 13:21:39 +08:00

46 lines
1.4 KiB
Go

package wallet
import (
"context"
"hyapp/pkg/xerr"
"hyapp/services/wallet-service/internal/domain/ledger"
)
// Repository 隔离账务存储,扣费必须由 repository 保证余额和幂等。
type Repository interface {
DebitGift(ctx context.Context, command ledger.DebitGiftCommand) (ledger.Receipt, error)
}
// Config 保存钱包底座配置。
type Config struct {
Currency string
IdempotencyTTLSec int64
}
// Service 承载钱包账务用例。
type Service struct {
cfg Config
repository Repository
}
// New 创建钱包服务。
func New(cfg Config, repository Repository) *Service {
return &Service{cfg: cfg, repository: repository}
}
// DebitGift 校验送礼扣费命令,并交给 repository 做原子落账和幂等。
func (s *Service) DebitGift(ctx context.Context, command ledger.DebitGiftCommand) (ledger.Receipt, error) {
if command.CommandID == "" || command.RoomID == "" || command.SenderUserID <= 0 || command.TargetUserID <= 0 || command.GiftID == "" {
return ledger.Receipt{}, xerr.New(xerr.InvalidArgument, "billing command is incomplete")
}
if command.GiftCount <= 0 || command.GiftUnitValue <= 0 {
return ledger.Receipt{}, xerr.New(xerr.InvalidArgument, "gift_count and gift_unit_value must be positive")
}
if s.repository == nil {
return ledger.Receipt{}, xerr.New(xerr.Unavailable, "wallet repository is not configured")
}
return s.repository.DebitGift(ctx, command)
}