114 lines
4.1 KiB
Go
114 lines
4.1 KiB
Go
|
// 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 (
|
||
|
userConfigFieldNames = builder.RawFieldNames(&UserConfig{})
|
||
|
userConfigRows = strings.Join(userConfigFieldNames, ",")
|
||
|
userConfigRowsExpectAutoSet = strings.Join(stringx.Remove(userConfigFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||
|
userConfigRowsWithPlaceHolder = strings.Join(stringx.Remove(userConfigFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||
|
|
||
|
cacheUserConfigIdPrefix = "cache:userConfig:id:"
|
||
|
)
|
||
|
|
||
|
type (
|
||
|
userConfigModel interface {
|
||
|
Insert(ctx context.Context, data *UserConfig) (sql.Result, error)
|
||
|
FindOne(ctx context.Context, id int64) (*UserConfig, error)
|
||
|
Update(ctx context.Context, data *UserConfig) error
|
||
|
Delete(ctx context.Context, id int64) error
|
||
|
}
|
||
|
|
||
|
defaultUserConfigModel struct {
|
||
|
sqlc.CachedConn
|
||
|
table string
|
||
|
}
|
||
|
|
||
|
UserConfig struct {
|
||
|
Id int64 `db:"id"` // 主键
|
||
|
GiftAmount float64 `db:"gift_amount"` // 赠送额度
|
||
|
CreatedAt time.Time `db:"created_at"` // 创建时间
|
||
|
UpdatedAt time.Time `db:"updated_at"` // 更新时间
|
||
|
}
|
||
|
)
|
||
|
|
||
|
func newUserConfigModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultUserConfigModel {
|
||
|
return &defaultUserConfigModel{
|
||
|
CachedConn: sqlc.NewConn(conn, c, opts...),
|
||
|
table: "`user_config`",
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (m *defaultUserConfigModel) Delete(ctx context.Context, id int64) error {
|
||
|
userConfigIdKey := fmt.Sprintf("%s%v", cacheUserConfigIdPrefix, id)
|
||
|
_, 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)
|
||
|
}, userConfigIdKey)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (m *defaultUserConfigModel) FindOne(ctx context.Context, id int64) (*UserConfig, error) {
|
||
|
userConfigIdKey := fmt.Sprintf("%s%v", cacheUserConfigIdPrefix, id)
|
||
|
var resp UserConfig
|
||
|
err := m.QueryRowCtx(ctx, &resp, userConfigIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
||
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", userConfigRows, 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 *defaultUserConfigModel) Insert(ctx context.Context, data *UserConfig) (sql.Result, error) {
|
||
|
userConfigIdKey := fmt.Sprintf("%s%v", cacheUserConfigIdPrefix, data.Id)
|
||
|
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, userConfigRowsExpectAutoSet)
|
||
|
return conn.ExecCtx(ctx, query, data.GiftAmount)
|
||
|
}, userConfigIdKey)
|
||
|
return ret, err
|
||
|
}
|
||
|
|
||
|
func (m *defaultUserConfigModel) Update(ctx context.Context, data *UserConfig) error {
|
||
|
userConfigIdKey := fmt.Sprintf("%s%v", cacheUserConfigIdPrefix, data.Id)
|
||
|
_, 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, userConfigRowsWithPlaceHolder)
|
||
|
return conn.ExecCtx(ctx, query, data.GiftAmount, data.Id)
|
||
|
}, userConfigIdKey)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (m *defaultUserConfigModel) formatPrimary(primary any) string {
|
||
|
return fmt.Sprintf("%s%v", cacheUserConfigIdPrefix, primary)
|
||
|
}
|
||
|
|
||
|
func (m *defaultUserConfigModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
||
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", userConfigRows, m.table)
|
||
|
return conn.QueryRowCtx(ctx, v, query, primary)
|
||
|
}
|
||
|
|
||
|
func (m *defaultUserConfigModel) tableName() string {
|
||
|
return m.table
|
||
|
}
|