// Code generated by goctl. DO NOT EDIT. // versions: // goctl version: 1.7.2 package model import ( "context" "database/sql" "fmt" "strings" "time" "github.com/zeromicro/go-zero/core/stores/builder" "github.com/zeromicro/go-zero/core/stores/cache" "github.com/zeromicro/go-zero/core/stores/sqlc" "github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stringx" ) var ( walletsFieldNames = builder.RawFieldNames(&Wallets{}) walletsRows = strings.Join(walletsFieldNames, ",") walletsRowsExpectAutoSet = strings.Join(stringx.Remove(walletsFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") walletsRowsWithPlaceHolder = strings.Join(stringx.Remove(walletsFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" cacheWalletsIdPrefix = "cache:wallets:id:" cacheWalletsUserIdPrefix = "cache:wallets:userId:" ) type ( walletsModel interface { Insert(ctx context.Context, data *Wallets) (sql.Result, error) FindOne(ctx context.Context, id int64) (*Wallets, error) FindOneByUserId(ctx context.Context, userId int64) (*Wallets, error) Update(ctx context.Context, data *Wallets) error Delete(ctx context.Context, id int64) error } defaultWalletsModel struct { sqlc.CachedConn table string } Wallets struct { Id int64 `db:"id"` // 钱包ID UserId int64 `db:"user_id"` // 用户ID Balance float64 `db:"balance"` // 钱包余额 Version int64 `db:"version"` // 乐观锁版本号 CreatedAt time.Time `db:"created_at"` // 创建时间 UpdatedAt time.Time `db:"updated_at"` // 更新时间 } ) func newWalletsModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultWalletsModel { return &defaultWalletsModel{ CachedConn: sqlc.NewConn(conn, c, opts...), table: "`wallets`", } } func (m *defaultWalletsModel) Delete(ctx context.Context, id int64) error { data, err := m.FindOne(ctx, id) if err != nil { return err } walletsIdKey := fmt.Sprintf("%s%v", cacheWalletsIdPrefix, id) walletsUserIdKey := fmt.Sprintf("%s%v", cacheWalletsUserIdPrefix, data.UserId) _, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("delete from %s where `id` = ?", m.table) return conn.ExecCtx(ctx, query, id) }, walletsIdKey, walletsUserIdKey) return err } func (m *defaultWalletsModel) FindOne(ctx context.Context, id int64) (*Wallets, error) { walletsIdKey := fmt.Sprintf("%s%v", cacheWalletsIdPrefix, id) var resp Wallets err := m.QueryRowCtx(ctx, &resp, walletsIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", walletsRows, m.table) return conn.QueryRowCtx(ctx, v, query, id) }) switch err { case nil: return &resp, nil case sqlc.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *defaultWalletsModel) FindOneByUserId(ctx context.Context, userId int64) (*Wallets, error) { walletsUserIdKey := fmt.Sprintf("%s%v", cacheWalletsUserIdPrefix, userId) var resp Wallets err := m.QueryRowIndexCtx(ctx, &resp, walletsUserIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) { query := fmt.Sprintf("select %s from %s where `user_id` = ? limit 1", walletsRows, m.table) if err := conn.QueryRowCtx(ctx, &resp, query, userId); err != nil { return nil, err } return resp.Id, nil }, m.queryPrimary) switch err { case nil: return &resp, nil case sqlc.ErrNotFound: return nil, ErrNotFound default: return nil, err } } func (m *defaultWalletsModel) Insert(ctx context.Context, data *Wallets) (sql.Result, error) { walletsIdKey := fmt.Sprintf("%s%v", cacheWalletsIdPrefix, data.Id) walletsUserIdKey := fmt.Sprintf("%s%v", cacheWalletsUserIdPrefix, data.UserId) ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?)", m.table, walletsRowsExpectAutoSet) return conn.ExecCtx(ctx, query, data.UserId, data.Balance, data.Version) }, walletsIdKey, walletsUserIdKey) return ret, err } func (m *defaultWalletsModel) Update(ctx context.Context, newData *Wallets) error { data, err := m.FindOne(ctx, newData.Id) if err != nil { return err } walletsIdKey := fmt.Sprintf("%s%v", cacheWalletsIdPrefix, data.Id) walletsUserIdKey := fmt.Sprintf("%s%v", cacheWalletsUserIdPrefix, data.UserId) _, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, walletsRowsWithPlaceHolder) return conn.ExecCtx(ctx, query, newData.UserId, newData.Balance, newData.Version, newData.Id) }, walletsIdKey, walletsUserIdKey) return err } func (m *defaultWalletsModel) formatPrimary(primary any) string { return fmt.Sprintf("%s%v", cacheWalletsIdPrefix, primary) } func (m *defaultWalletsModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error { query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", walletsRows, m.table) return conn.QueryRowCtx(ctx, v, query, primary) } func (m *defaultWalletsModel) tableName() string { return m.table }