f
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
"tydata-server/common/globalkey"
|
||||
)
|
||||
|
||||
var _ UserModel = (*customUserModel)(nil)
|
||||
@@ -12,6 +18,8 @@ type (
|
||||
// and implement the added methods in customUserModel.
|
||||
UserModel interface {
|
||||
userModel
|
||||
// FindDisableByUserId 查用户封禁状态(不走缓存,封禁中间件用)
|
||||
FindDisableByUserId(ctx context.Context, id int64) (int64, error)
|
||||
}
|
||||
|
||||
customUserModel struct {
|
||||
@@ -19,6 +27,20 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
// FindDisableByUserId 查用户 disable 字段,不走缓存,确保封禁后立即生效
|
||||
func (m *customUserModel) FindDisableByUserId(ctx context.Context, id int64) (int64, error) {
|
||||
var disable int64
|
||||
query := fmt.Sprintf("SELECT `disable` FROM %s WHERE `id` = ? and del_state = ? limit 1", m.table)
|
||||
err := m.QueryRowNoCacheCtx(ctx, &disable, query, id, globalkey.DelStateNo)
|
||||
if err != nil {
|
||||
if err == sqlc.ErrNotFound {
|
||||
return 0, ErrNotFound
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
return disable, nil
|
||||
}
|
||||
|
||||
// NewUserModel returns a model for the database table.
|
||||
func NewUserModel(conn sqlx.SqlConn, c cache.CacheConf) UserModel {
|
||||
return &customUserModel{
|
||||
|
||||
@@ -10,8 +10,6 @@ import (
|
||||
|
||||
"time"
|
||||
|
||||
"tydata-server/common/globalkey"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
@@ -19,6 +17,7 @@ import (
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlc"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
"tydata-server/common/globalkey"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -27,8 +26,8 @@ var (
|
||||
userRowsExpectAutoSet = strings.Join(stringx.Remove(userFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
|
||||
userRowsWithPlaceHolder = strings.Join(stringx.Remove(userFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
|
||||
|
||||
cacheHmUserIdPrefix = "cache:tydata:user:id:"
|
||||
cacheHmUserMobilePrefix = "cache:tydata:user:mobile:"
|
||||
cacheZncUserIdPrefix = "cache:znc:user:id:"
|
||||
cacheZncUserMobilePrefix = "cache:znc:user:mobile:"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -68,6 +67,7 @@ type (
|
||||
Nickname sql.NullString `db:"nickname"`
|
||||
Info string `db:"info"`
|
||||
Inside int64 `db:"inside"`
|
||||
Disable int64 `db:"disable"` // 0可用 1禁用
|
||||
}
|
||||
)
|
||||
|
||||
@@ -80,21 +80,21 @@ func newUserModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultUserModel {
|
||||
|
||||
func (m *defaultUserModel) Insert(ctx context.Context, session sqlx.Session, data *User) (sql.Result, error) {
|
||||
data.DelState = globalkey.DelStateNo
|
||||
hmUserIdKey := fmt.Sprintf("%s%v", cacheHmUserIdPrefix, data.Id)
|
||||
hmUserMobileKey := fmt.Sprintf("%s%v", cacheHmUserMobilePrefix, data.Mobile)
|
||||
zncUserIdKey := fmt.Sprintf("%s%v", cacheZncUserIdPrefix, data.Id)
|
||||
zncUserMobileKey := fmt.Sprintf("%s%v", cacheZncUserMobilePrefix, data.Mobile)
|
||||
return 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, userRowsExpectAutoSet)
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, userRowsExpectAutoSet)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info, data.Inside)
|
||||
return session.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info, data.Inside, data.Disable)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info, data.Inside)
|
||||
}, hmUserIdKey, hmUserMobileKey)
|
||||
return conn.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info, data.Inside, data.Disable)
|
||||
}, zncUserIdKey, zncUserMobileKey)
|
||||
}
|
||||
|
||||
func (m *defaultUserModel) FindOne(ctx context.Context, id int64) (*User, error) {
|
||||
hmUserIdKey := fmt.Sprintf("%s%v", cacheHmUserIdPrefix, id)
|
||||
zncUserIdKey := fmt.Sprintf("%s%v", cacheZncUserIdPrefix, id)
|
||||
var resp User
|
||||
err := m.QueryRowCtx(ctx, &resp, hmUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
err := m.QueryRowCtx(ctx, &resp, zncUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", userRows, m.table)
|
||||
return conn.QueryRowCtx(ctx, v, query, id, globalkey.DelStateNo)
|
||||
})
|
||||
@@ -109,9 +109,9 @@ func (m *defaultUserModel) FindOne(ctx context.Context, id int64) (*User, error)
|
||||
}
|
||||
|
||||
func (m *defaultUserModel) FindOneByMobile(ctx context.Context, mobile sql.NullString) (*User, error) {
|
||||
hmUserMobileKey := fmt.Sprintf("%s%v", cacheHmUserMobilePrefix, mobile)
|
||||
zncUserMobileKey := fmt.Sprintf("%s%v", cacheZncUserMobilePrefix, mobile)
|
||||
var resp User
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, hmUserMobileKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, zncUserMobileKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
query := fmt.Sprintf("select %s from %s where `mobile` = ? and del_state = ? limit 1", userRows, m.table)
|
||||
if err := conn.QueryRowCtx(ctx, &resp, query, mobile, globalkey.DelStateNo); err != nil {
|
||||
return nil, err
|
||||
@@ -133,15 +133,15 @@ func (m *defaultUserModel) Update(ctx context.Context, session sqlx.Session, new
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hmUserIdKey := fmt.Sprintf("%s%v", cacheHmUserIdPrefix, data.Id)
|
||||
hmUserMobileKey := fmt.Sprintf("%s%v", cacheHmUserMobilePrefix, data.Mobile)
|
||||
zncUserIdKey := fmt.Sprintf("%s%v", cacheZncUserIdPrefix, data.Id)
|
||||
zncUserMobileKey := fmt.Sprintf("%s%v", cacheZncUserMobilePrefix, data.Mobile)
|
||||
return 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, userRowsWithPlaceHolder)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id)
|
||||
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Disable, newData.Id)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id)
|
||||
}, hmUserIdKey, hmUserMobileKey)
|
||||
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Disable, newData.Id)
|
||||
}, zncUserIdKey, zncUserMobileKey)
|
||||
}
|
||||
|
||||
func (m *defaultUserModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, newData *User) error {
|
||||
@@ -156,15 +156,15 @@ func (m *defaultUserModel) UpdateWithVersion(ctx context.Context, session sqlx.S
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hmUserIdKey := fmt.Sprintf("%s%v", cacheHmUserIdPrefix, data.Id)
|
||||
hmUserMobileKey := fmt.Sprintf("%s%v", cacheHmUserMobilePrefix, data.Mobile)
|
||||
zncUserIdKey := fmt.Sprintf("%s%v", cacheZncUserIdPrefix, data.Id)
|
||||
zncUserMobileKey := fmt.Sprintf("%s%v", cacheZncUserMobilePrefix, data.Mobile)
|
||||
sqlResult, 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` = ? and version = ? ", m.table, userRowsWithPlaceHolder)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id, oldVersion)
|
||||
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Disable, newData.Id, oldVersion)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id, oldVersion)
|
||||
}, hmUserIdKey, hmUserMobileKey)
|
||||
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Disable, newData.Id, oldVersion)
|
||||
}, zncUserIdKey, zncUserMobileKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -387,19 +387,19 @@ func (m *defaultUserModel) Delete(ctx context.Context, session sqlx.Session, id
|
||||
return err
|
||||
}
|
||||
|
||||
hmUserIdKey := fmt.Sprintf("%s%v", cacheHmUserIdPrefix, id)
|
||||
hmUserMobileKey := fmt.Sprintf("%s%v", cacheHmUserMobilePrefix, data.Mobile)
|
||||
zncUserIdKey := fmt.Sprintf("%s%v", cacheZncUserIdPrefix, id)
|
||||
zncUserMobileKey := fmt.Sprintf("%s%v", cacheZncUserMobilePrefix, data.Mobile)
|
||||
_, 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)
|
||||
if session != nil {
|
||||
return session.ExecCtx(ctx, query, id)
|
||||
}
|
||||
return conn.ExecCtx(ctx, query, id)
|
||||
}, hmUserIdKey, hmUserMobileKey)
|
||||
}, zncUserIdKey, zncUserMobileKey)
|
||||
return err
|
||||
}
|
||||
func (m *defaultUserModel) formatPrimary(primary interface{}) string {
|
||||
return fmt.Sprintf("%s%v", cacheHmUserIdPrefix, primary)
|
||||
return fmt.Sprintf("%s%v", cacheZncUserIdPrefix, primary)
|
||||
}
|
||||
func (m *defaultUserModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", userRows, m.table)
|
||||
|
||||
Reference in New Issue
Block a user