package repository import ( "app-cloudep-trade-service/internal/domain" "github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/shopspring/decimal" ) // WalletOperatorOption 選項模式 type WalletOperatorOption func(*WalletOptions) type WalletOptions struct { WithLock bool OrderID string Amount decimal.Decimal Kind domain.WalletType Business domain.BusinessName Tx *sqlx.SqlConn } // ApplyOptions 將多個 WalletOperatorOption 應用到一個 walletOptions 中 func ApplyOptions(opts ...WalletOperatorOption) WalletOptions { options := WalletOptions{} for _, opt := range opts { opt(&options) } return options } func WithLock() WalletOperatorOption { return func(opts *WalletOptions) { opts.WithLock = true } } func WithOrderID(orderID string) WalletOperatorOption { return func(opts *WalletOptions) { opts.OrderID = orderID } } func WithAmount(amount decimal.Decimal) WalletOperatorOption { return func(opts *WalletOptions) { opts.Amount = amount } } func WithKind(kind domain.WalletType) WalletOperatorOption { return func(opts *WalletOptions) { opts.Kind = kind } } func WithBusiness(business domain.BusinessName) WalletOperatorOption { return func(opts *WalletOptions) { opts.Business = business } } func WithSession(session *sqlx.SqlConn) WalletOperatorOption { return func(opts *WalletOptions) { opts.Tx = session } }