2024-10-29 14:49:47 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"app-cloudep-trade-service/internal/domain"
|
|
|
|
"app-cloudep-trade-service/internal/domain/repository"
|
|
|
|
"app-cloudep-trade-service/internal/model"
|
2024-10-30 11:13:56 +00:00
|
|
|
"context"
|
2024-10-29 14:49:47 +00:00
|
|
|
|
|
|
|
"github.com/shopspring/decimal"
|
2024-10-30 11:13:56 +00:00
|
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
2024-10-29 14:49:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 用戶某個幣種餘額
|
|
|
|
type userLocalWallet struct {
|
|
|
|
wm model.WalletModel
|
|
|
|
txConn sqlx.SqlConn
|
|
|
|
|
2024-10-30 11:13:56 +00:00
|
|
|
uid string
|
|
|
|
currency string
|
2024-10-29 14:49:47 +00:00
|
|
|
|
|
|
|
// local wallet 相關計算的餘額存在這裡
|
|
|
|
walletBalance map[domain.WalletType]model.Wallet
|
|
|
|
|
|
|
|
// local order wallet 相關計算的餘額存在這裡
|
|
|
|
localOrderBalance map[int64]decimal.Decimal
|
|
|
|
|
|
|
|
// local wallet 內所有餘額變化紀錄
|
|
|
|
transactions []model.WalletJournal
|
|
|
|
}
|
|
|
|
|
2024-10-30 11:13:56 +00:00
|
|
|
func (use *userLocalWallet) GetBalancesByID(ctx context.Context, ids []int64, opts ...repository.WalletOperatorOption) ([]model.Wallet, error) {
|
|
|
|
o := repository.ApplyOptions(opts...)
|
|
|
|
tx := use.txConn
|
|
|
|
if o.Tx != nil {
|
|
|
|
tx = *o.Tx
|
|
|
|
}
|
|
|
|
|
|
|
|
wallets, err := use.wm.BalancesByIDs(ctx, ids, o.WithLock, tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, wallet := range wallets {
|
|
|
|
use.walletBalance[wallet.WalletType] = wallet
|
|
|
|
}
|
|
|
|
|
|
|
|
return wallets, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalBalance 內存餘額
|
|
|
|
func (use *userLocalWallet) LocalBalance(kind domain.WalletType) decimal.Decimal {
|
|
|
|
wallet, ok := use.walletBalance[kind]
|
|
|
|
if !ok {
|
|
|
|
return decimal.Zero
|
|
|
|
}
|
|
|
|
|
|
|
|
return wallet.Balance
|
|
|
|
}
|
|
|
|
|
|
|
|
func (use *userLocalWallet) Balances(ctx context.Context, kind []domain.WalletType, opts ...repository.WalletOperatorOption) ([]model.Wallet, error) {
|
|
|
|
o := repository.ApplyOptions(opts...)
|
|
|
|
tx := use.txConn
|
|
|
|
if o.Tx != nil {
|
|
|
|
tx = *o.Tx
|
|
|
|
}
|
|
|
|
|
|
|
|
wallets, err := use.wm.Balances(ctx, model.BalanceReq{
|
|
|
|
UID: []string{use.uid},
|
|
|
|
Currency: []string{use.currency},
|
|
|
|
Kind: kind,
|
|
|
|
}, o.WithLock, tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, wallet := range wallets {
|
|
|
|
use.walletBalance[wallet.WalletType] = wallet
|
|
|
|
}
|
|
|
|
|
|
|
|
return wallets, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserWalletOperator(uid, currency string, wm model.WalletModel, txConn sqlx.SqlConn) repository.UserWalletOperator {
|
2024-10-29 14:49:47 +00:00
|
|
|
return &userLocalWallet{
|
2024-10-30 11:13:56 +00:00
|
|
|
wm: wm,
|
|
|
|
txConn: txConn,
|
|
|
|
uid: uid,
|
|
|
|
currency: currency,
|
2024-10-29 14:49:47 +00:00
|
|
|
|
|
|
|
walletBalance: make(map[domain.WalletType]model.Wallet, len(domain.AllWalletType)),
|
|
|
|
localOrderBalance: make(map[int64]decimal.Decimal, len(domain.AllWalletType)),
|
|
|
|
}
|
|
|
|
}
|