app-cloudep-trade-service/internal/domain/repository/wallet_option.go

68 lines
1.4 KiB
Go

package repository
import (
"app-cloudep-trade-service/internal/domain/wallet"
"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
BalanceType wallet.BalanceType
Business wallet.BusinessLogic
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(balanceType wallet.BalanceType) WalletOperatorOption {
return func(opts *WalletOptions) {
opts.BalanceType = balanceType
}
}
func WithBusiness(business wallet.BusinessLogic) WalletOperatorOption {
return func(opts *WalletOptions) {
opts.Business = business
}
}
func WithSession(session *sqlx.SqlConn) WalletOperatorOption {
return func(opts *WalletOptions) {
opts.Tx = session
}
}