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

68 lines
1.4 KiB
Go
Raw Permalink Normal View History

2024-10-30 11:13:56 +00:00
package repository
import (
2024-10-31 04:05:22 +00:00
"app-cloudep-trade-service/internal/domain/wallet"
2024-10-30 11:13:56 +00:00
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/shopspring/decimal"
)
// WalletOperatorOption 選項模式
type WalletOperatorOption func(*WalletOptions)
type WalletOptions struct {
2024-10-31 04:05:22 +00:00
WithLock bool
OrderID string
Amount decimal.Decimal
BalanceType wallet.BalanceType
Business wallet.BusinessLogic
Tx *sqlx.SqlConn
2024-10-30 11:13:56 +00:00
}
// 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
}
}
2024-10-31 04:05:22 +00:00
func WithKind(balanceType wallet.BalanceType) WalletOperatorOption {
2024-10-30 11:13:56 +00:00
return func(opts *WalletOptions) {
2024-10-31 04:05:22 +00:00
opts.BalanceType = balanceType
2024-10-30 11:13:56 +00:00
}
}
2024-10-31 04:05:22 +00:00
func WithBusiness(business wallet.BusinessLogic) WalletOperatorOption {
2024-10-30 11:13:56 +00:00
return func(opts *WalletOptions) {
opts.Business = business
}
}
func WithSession(session *sqlx.SqlConn) WalletOperatorOption {
return func(opts *WalletOptions) {
opts.Tx = session
}
}