126 lines
4.8 KiB
Go
126 lines
4.8 KiB
Go
|
// Code generated by goctl. DO NOT EDIT.
|
||
|
// versions:
|
||
|
// goctl version: 1.7.5
|
||
|
|
||
|
package model
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"database/sql"
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
|
"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 (
|
||
|
appUserFieldNames = builder.RawFieldNames(&AppUser{})
|
||
|
appUserRows = strings.Join(appUserFieldNames, ",")
|
||
|
appUserRowsExpectAutoSet = strings.Join(stringx.Remove(appUserFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||
|
appUserRowsWithPlaceHolder = strings.Join(stringx.Remove(appUserFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||
|
|
||
|
cacheEcpmAppUserIdPrefix = "cache:ecpm:appUser:id:"
|
||
|
)
|
||
|
|
||
|
type (
|
||
|
appUserModel interface {
|
||
|
Insert(ctx context.Context, data *AppUser) (sql.Result, error)
|
||
|
FindOne(ctx context.Context, id uint64) (*AppUser, error)
|
||
|
Update(ctx context.Context, data *AppUser) error
|
||
|
Delete(ctx context.Context, id uint64) error
|
||
|
UpdateNicknameAvatar(ctx context.Context, data *AppUser) error
|
||
|
}
|
||
|
|
||
|
defaultAppUserModel struct {
|
||
|
sqlc.CachedConn
|
||
|
table string
|
||
|
}
|
||
|
|
||
|
AppUser struct {
|
||
|
Id uint64 `db:"id"`
|
||
|
AppAccountId uint64 `db:"app_account_id"` // app_account表外键
|
||
|
Openid string `db:"openid"`
|
||
|
Unionid string `db:"unionid"`
|
||
|
Nickname string `db:"nickname"` // 昵称
|
||
|
Avatar string `db:"avatar"` // 头像
|
||
|
AnonymousOpenid string `db:"anonymous_openid"` // 匿名openid
|
||
|
}
|
||
|
)
|
||
|
|
||
|
func newAppUserModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultAppUserModel {
|
||
|
return &defaultAppUserModel{
|
||
|
CachedConn: sqlc.NewConn(conn, c, opts...),
|
||
|
table: "`app_user`",
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (m *defaultAppUserModel) Delete(ctx context.Context, id uint64) error {
|
||
|
ecpmAppUserIdKey := fmt.Sprintf("%s%v", cacheEcpmAppUserIdPrefix, 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)
|
||
|
}, ecpmAppUserIdKey)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (m *defaultAppUserModel) FindOne(ctx context.Context, id uint64) (*AppUser, error) {
|
||
|
ecpmAppUserIdKey := fmt.Sprintf("%s%v", cacheEcpmAppUserIdPrefix, id)
|
||
|
var resp AppUser
|
||
|
err := m.QueryRowCtx(ctx, &resp, ecpmAppUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
||
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", appUserRows, 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 *defaultAppUserModel) Insert(ctx context.Context, data *AppUser) (sql.Result, error) {
|
||
|
ecpmAppUserIdKey := fmt.Sprintf("%s%v", cacheEcpmAppUserIdPrefix, 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, appUserRowsExpectAutoSet)
|
||
|
return conn.ExecCtx(ctx, query, data.AppAccountId, data.Openid, data.Unionid, data.Nickname, data.Avatar, data.AnonymousOpenid)
|
||
|
}, ecpmAppUserIdKey)
|
||
|
return ret, err
|
||
|
}
|
||
|
|
||
|
func (m *defaultAppUserModel) Update(ctx context.Context, data *AppUser) error {
|
||
|
ecpmAppUserIdKey := fmt.Sprintf("%s%v", cacheEcpmAppUserIdPrefix, 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, appUserRowsWithPlaceHolder)
|
||
|
return conn.ExecCtx(ctx, query, data.AppAccountId, data.Openid, data.Unionid, data.Nickname, data.Avatar, data.AnonymousOpenid, data.Id)
|
||
|
}, ecpmAppUserIdKey)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (m *defaultAppUserModel) UpdateNicknameAvatar(ctx context.Context, data *AppUser) error {
|
||
|
ecpmAppUserIdKey := fmt.Sprintf("%s%v", cacheEcpmAppUserIdPrefix, 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, "`nickname`=?,`avatar`=?")
|
||
|
return conn.ExecCtx(ctx, query, data.Nickname, data.Avatar, data.Id)
|
||
|
}, ecpmAppUserIdKey)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (m *defaultAppUserModel) formatPrimary(primary any) string {
|
||
|
return fmt.Sprintf("%s%v", cacheEcpmAppUserIdPrefix, primary)
|
||
|
}
|
||
|
|
||
|
func (m *defaultAppUserModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
||
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", appUserRows, m.table)
|
||
|
return conn.QueryRowCtx(ctx, v, query, primary)
|
||
|
}
|
||
|
|
||
|
func (m *defaultAppUserModel) tableName() string {
|
||
|
return m.table
|
||
|
}
|