// Code generated by goctl. DO NOT EDIT. package model import ( "app-cloudep-trade-service/internal/domain/wallet" "context" "database/sql" "fmt" "strings" "github.com/shopspring/decimal" "github.com/zeromicro/go-zero/core/stores/builder" "github.com/zeromicro/go-zero/core/stores/sqlc" "github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stringx" ) var ( walletFieldNames = builder.RawFieldNames(&Wallet{}) walletRows = strings.Join(walletFieldNames, ",") walletRowsExpectAutoSet = strings.Join(stringx.Remove(walletFieldNames, "`id`"), ",") walletRowsWithPlaceHolder = strings.Join(stringx.Remove(walletFieldNames, "`id`"), "=?,") + "=?" ) type ( walletModel interface { Insert(ctx context.Context, data *Wallet) (sql.Result, error) FindOne(ctx context.Context, id int64) (*Wallet, error) FindOneByUidCurrencyWalletType(ctx context.Context, uid string, currency string, walletType int64) (*Wallet, error) Update(ctx context.Context, data *Wallet) error Delete(ctx context.Context, id int64) error } defaultWalletModel struct { conn sqlx.SqlConn table string } Wallet struct { Id int64 `db:"id"` // 錢包流水號 Uid string `db:"uid"` // 用戶ID Brand string `db:"brand"` // 品牌名稱 Currency string `db:"currency"` // 幣別(或平台點數) Balance decimal.Decimal `db:"balance"` // 錢包餘額 WalletType wallet.BalanceType `db:"wallet_type"` // 錢包種類: 1=可用, 2=凍結, 3=限制(僅出金) CreatedAt int64 `db:"created_at"` // 創建時間 UpdatedAt int64 `db:"updated_at"` // 更新時間 } ) func newWalletModel(conn sqlx.SqlConn) *defaultWalletModel { return &defaultWalletModel{ conn: conn, table: "`wallet`", } } func (m *defaultWalletModel) withSession(session sqlx.Session) *defaultWalletModel { return &defaultWalletModel{ conn: sqlx.NewSqlConnFromSession(session), table: "`wallet`", } } func (m *defaultWalletModel) Delete(ctx context.Context, id int64) error { query := fmt.Sprintf("delete from %s where `id` = ?", m.table) _, err := m.conn.ExecCtx(ctx, query, id) return err } func (m *defaultWalletModel) FindOne(ctx context.Context, id int64) (*Wallet, error) { query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", walletRows, m.table) var resp Wallet err := m.conn.QueryRowCtx(ctx, &resp, query, id) switch err { case nil: return &resp, nil case sqlc.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *defaultWalletModel) FindOneByUidCurrencyWalletType(ctx context.Context, uid string, currency string, walletType int64) (*Wallet, error) { var resp Wallet query := fmt.Sprintf("select %s from %s where `uid` = ? and `currency` = ? and `wallet_type` = ? limit 1", walletRows, m.table) err := m.conn.QueryRowCtx(ctx, &resp, query, uid, currency, walletType) switch err { case nil: return &resp, nil case sqlc.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *defaultWalletModel) Insert(ctx context.Context, data *Wallet) (sql.Result, error) { query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?)", m.table, walletRowsExpectAutoSet) ret, err := m.conn.ExecCtx(ctx, query, data.Uid, data.Brand, data.Currency, data.Balance, data.WalletType, data.CreatedAt, data.UpdatedAt) return ret, err } func (m *defaultWalletModel) Update(ctx context.Context, newData *Wallet) error { query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, walletRowsWithPlaceHolder) _, err := m.conn.ExecCtx(ctx, query, newData.Uid, newData.Brand, newData.Currency, newData.Balance, newData.WalletType, newData.CreatedAt, newData.UpdatedAt, newData.Id) return err } func (m *defaultWalletModel) tableName() string { return m.table }