160 lines
6.0 KiB
Go
160 lines
6.0 KiB
Go
|
// Code generated by goctl. DO NOT EDIT.
|
||
|
|
||
|
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 (
|
||
|
userTableFieldNames = builder.RawFieldNames(&UserTable{})
|
||
|
userTableRows = strings.Join(userTableFieldNames, ",")
|
||
|
userTableRowsExpectAutoSet = strings.Join(stringx.Remove(userTableFieldNames, "`id`"), ",")
|
||
|
userTableRowsWithPlaceHolder = strings.Join(stringx.Remove(userTableFieldNames, "`id`"), "=?,") + "=?"
|
||
|
|
||
|
cacheUserTableIdPrefix = "cache:userTable:id:"
|
||
|
cacheUserTableUidPrefix = "cache:userTable:uid:"
|
||
|
)
|
||
|
|
||
|
type (
|
||
|
userTableModel interface {
|
||
|
Insert(ctx context.Context, data *UserTable) (sql.Result, error)
|
||
|
FindOne(ctx context.Context, id int64) (*UserTable, error)
|
||
|
FindOneByUid(ctx context.Context, uid string) (*UserTable, error)
|
||
|
Update(ctx context.Context, data *UserTable) error
|
||
|
Delete(ctx context.Context, id int64) error
|
||
|
}
|
||
|
|
||
|
defaultUserTableModel struct {
|
||
|
sqlc.CachedConn
|
||
|
table string
|
||
|
}
|
||
|
|
||
|
UserTable struct {
|
||
|
Id int64 `db:"id"`
|
||
|
VerifyType int64 `db:"verify_type"` // 驗證類型 0. 異常 1.信箱 2.手機 3. GA 4.不驗證
|
||
|
AlarmType int64 `db:"alarm_type"` // 告警狀態 0. 異常 1. 正常(未告警) 2.系統告警中
|
||
|
Status int64 `db:"status"` // 會員狀態 0. 異常 1. 尚未驗證 2. 啟用 3. 停權中 4. 信箱以驗證 5. 手機以驗證 6. GA 以驗證
|
||
|
Uid string `db:"uid"` // 唯一辨識碼
|
||
|
NickName string `db:"nick_name"` // 暱稱
|
||
|
Language string `db:"language"` // 使用語言
|
||
|
Currency string `db:"currency"` // 使用幣別
|
||
|
Avatar string `db:"avatar"` // 會員頭像
|
||
|
CreateTime int64 `db:"create_time"`
|
||
|
UpdateTime int64 `db:"update_time"`
|
||
|
}
|
||
|
)
|
||
|
|
||
|
func newUserTableModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultUserTableModel {
|
||
|
return &defaultUserTableModel{
|
||
|
CachedConn: sqlc.NewConn(conn, c, opts...),
|
||
|
table: "`user_table`",
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (m *defaultUserTableModel) withSession(session sqlx.Session) *defaultUserTableModel {
|
||
|
return &defaultUserTableModel{
|
||
|
CachedConn: m.CachedConn.WithSession(session),
|
||
|
table: "`user_table`",
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (m *defaultUserTableModel) Delete(ctx context.Context, id int64) error {
|
||
|
data, err := m.FindOne(ctx, id)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
userTableIdKey := fmt.Sprintf("%s%v", cacheUserTableIdPrefix, id)
|
||
|
userTableUidKey := fmt.Sprintf("%s%v", cacheUserTableUidPrefix, data.Uid)
|
||
|
_, 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)
|
||
|
}, userTableIdKey, userTableUidKey)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (m *defaultUserTableModel) FindOne(ctx context.Context, id int64) (*UserTable, error) {
|
||
|
userTableIdKey := fmt.Sprintf("%s%v", cacheUserTableIdPrefix, id)
|
||
|
var resp UserTable
|
||
|
err := m.QueryRowCtx(ctx, &resp, userTableIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
||
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", userTableRows, 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 *defaultUserTableModel) FindOneByUid(ctx context.Context, uid string) (*UserTable, error) {
|
||
|
userTableUidKey := fmt.Sprintf("%s%v", cacheUserTableUidPrefix, uid)
|
||
|
var resp UserTable
|
||
|
err := m.QueryRowIndexCtx(ctx, &resp, userTableUidKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
||
|
query := fmt.Sprintf("select %s from %s where `uid` = ? limit 1", userTableRows, m.table)
|
||
|
if err := conn.QueryRowCtx(ctx, &resp, query, uid); 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 *defaultUserTableModel) Insert(ctx context.Context, data *UserTable) (sql.Result, error) {
|
||
|
userTableIdKey := fmt.Sprintf("%s%v", cacheUserTableIdPrefix, data.Id)
|
||
|
userTableUidKey := fmt.Sprintf("%s%v", cacheUserTableUidPrefix, data.Uid)
|
||
|
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, userTableRowsExpectAutoSet)
|
||
|
return conn.ExecCtx(ctx, query, data.VerifyType, data.AlarmType, data.Status, data.Uid, data.NickName, data.Language, data.Currency, data.Avatar, data.CreateTime, data.UpdateTime)
|
||
|
}, userTableIdKey, userTableUidKey)
|
||
|
return ret, err
|
||
|
}
|
||
|
|
||
|
func (m *defaultUserTableModel) Update(ctx context.Context, newData *UserTable) error {
|
||
|
data, err := m.FindOne(ctx, newData.Id)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
userTableIdKey := fmt.Sprintf("%s%v", cacheUserTableIdPrefix, data.Id)
|
||
|
userTableUidKey := fmt.Sprintf("%s%v", cacheUserTableUidPrefix, data.Uid)
|
||
|
_, 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, userTableRowsWithPlaceHolder)
|
||
|
return conn.ExecCtx(ctx, query, newData.VerifyType, newData.AlarmType, newData.Status, newData.Uid, newData.NickName, newData.Language, newData.Currency, newData.Avatar, newData.CreateTime, newData.UpdateTime, newData.Id)
|
||
|
}, userTableIdKey, userTableUidKey)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (m *defaultUserTableModel) formatPrimary(primary any) string {
|
||
|
return fmt.Sprintf("%s%v", cacheUserTableIdPrefix, primary)
|
||
|
}
|
||
|
|
||
|
func (m *defaultUserTableModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
||
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", userTableRows, m.table)
|
||
|
return conn.QueryRowCtx(ctx, v, query, primary)
|
||
|
}
|
||
|
|
||
|
func (m *defaultUserTableModel) tableName() string {
|
||
|
return m.table
|
||
|
}
|