app-cloudep-trade-service/internal/model/wallet_model.go

126 lines
3.6 KiB
Go
Raw Permalink Normal View History

2024-10-29 08:17:31 +00:00
package model
2024-10-29 14:49:47 +00:00
import (
2024-10-31 04:05:22 +00:00
"app-cloudep-trade-service/internal/domain/wallet"
2024-10-29 14:49:47 +00:00
"context"
"database/sql"
"errors"
"fmt"
"strings"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
2024-10-29 08:17:31 +00:00
var _ WalletModel = (*customWalletModel)(nil)
type (
// WalletModel is an interface to be customized, add more methods here,
// and implement the added methods in customWalletModel.
WalletModel interface {
walletModel
2024-10-29 14:49:47 +00:00
InsertMany(ctx context.Context, wallets []*Wallet) (sql.Result, error)
2024-10-30 11:13:56 +00:00
// Balances 給他 select for update 的鎖先加上去
Balances(ctx context.Context, req BalanceReq, withLock bool, tx sqlx.Session) ([]Wallet, error)
// BalancesByIDs 給他 select for update 的鎖先加上去
BalancesByIDs(ctx context.Context, ids []int64, withLock bool, tx sqlx.Session) ([]Wallet, error)
2024-10-29 08:17:31 +00:00
}
customWalletModel struct {
*defaultWalletModel
}
2024-10-29 14:49:47 +00:00
BalanceReq struct {
UID []string
Currency []string
2024-10-31 04:05:22 +00:00
Kind []wallet.BalanceType
2024-10-29 14:49:47 +00:00
}
2024-10-29 08:17:31 +00:00
)
// NewWalletModel returns a model for the database table.
func NewWalletModel(conn sqlx.SqlConn) WalletModel {
return &customWalletModel{
defaultWalletModel: newWalletModel(conn),
}
}
2024-10-29 14:49:47 +00:00
func (m *customWalletModel) InsertMany(ctx context.Context, wallets []*Wallet) (sql.Result, error) {
if len(wallets) == 0 {
return nil, fmt.Errorf("no data to insert")
}
// 構建多條記錄的佔位符,例如: (?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?), ...
valueStrings := make([]string, 0, len(wallets))
valueArgs := make([]interface{}, 0, len(wallets)*7) // 每條記錄有7個值
for _, wallet := range wallets {
valueStrings = append(valueStrings, "(?, ?, ?, ?, ?, ?, ?)")
valueArgs = append(valueArgs, wallet.Uid, wallet.Brand, wallet.Currency, wallet.Balance, wallet.WalletType, wallet.CreatedAt, wallet.UpdatedAt)
}
// 構建批量插入的 SQL 語句
query := fmt.Sprintf("insert into %s (%s) values %s", m.table, walletRowsExpectAutoSet, strings.Join(valueStrings, ","))
// 使用單一連線執行批量插入
return m.conn.ExecCtx(ctx, query, valueArgs...)
}
2024-10-30 11:13:56 +00:00
func (m *customWalletModel) Balances(ctx context.Context, req BalanceReq, isLock bool, tx sqlx.Session) ([]Wallet, error) {
baseQuery := fmt.Sprintf("SELECT `id`, `currency`, `balance`, `wallet_type` FROM %s", m.table)
// 構建條件字典
conditions := map[string][]any{
"`uid`": convertSliceToInterface(req.UID),
"`currency`": convertSliceToInterface(req.Currency),
"`wallet_type`": convertSliceToInterface(req.Kind),
2024-10-29 14:49:47 +00:00
}
2024-10-30 11:13:56 +00:00
// 使用 queryBuilder 構建完整查詢
query, args := queryBindINBuilder(baseQuery, conditions)
2024-10-29 14:49:47 +00:00
2024-10-30 11:13:56 +00:00
if isLock {
// 加上排他鎖
query += " FOR UPDATE"
2024-10-29 14:49:47 +00:00
}
// 執行查詢
2024-10-30 11:13:56 +00:00
var wallets []Wallet
err := tx.QueryRowsPartialCtx(ctx, &wallets, query, args...)
2024-10-29 14:49:47 +00:00
switch {
case err == nil:
2024-10-30 11:13:56 +00:00
return wallets, nil
2024-10-31 04:05:22 +00:00
case errors.Is(err, sqlc.ErrNotFound):
2024-10-29 14:49:47 +00:00
return nil, ErrNotFound
default:
return nil, err
}
}
2024-10-30 11:13:56 +00:00
func (m *customWalletModel) BalancesByIDs(ctx context.Context, ids []int64, withLock bool, tx sqlx.Session) ([]Wallet, error) {
baseQuery := fmt.Sprintf("SELECT `id`, `currency`, `balance`, `wallet_type` FROM %s", m.table)
// 構建條件字典
conditions := map[string][]any{
"`id`": convertSliceToInterface(ids),
}
// 使用 queryBuilder 構建完整查詢
query, args := queryBindINBuilder(baseQuery, conditions)
if withLock {
// 加上排他鎖
query += " FOR UPDATE"
}
// 執行查詢
var wallets []Wallet
err := tx.QueryRowsPartialCtx(ctx, &wallets, query, args...)
switch {
case err == nil:
return wallets, nil
2024-10-31 04:05:22 +00:00
case errors.Is(err, sqlc.ErrNotFound):
2024-10-30 11:13:56 +00:00
return nil, ErrNotFound
default:
return nil, err
2024-10-29 14:49:47 +00:00
}
}