|
|
@ -10,8 +10,6 @@ import (
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/builder"
|
|
|
|
"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/stores/sqlx"
|
|
|
|
"github.com/zeromicro/go-zero/core/stringx"
|
|
|
|
"github.com/zeromicro/go-zero/core/stringx"
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -21,11 +19,6 @@ var (
|
|
|
|
userRows = strings.Join(userFieldNames, ",")
|
|
|
|
userRows = strings.Join(userFieldNames, ",")
|
|
|
|
userRowsExpectAutoSet = strings.Join(stringx.Remove(userFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
|
|
|
userRowsExpectAutoSet = strings.Join(stringx.Remove(userFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
|
|
|
userRowsWithPlaceHolder = strings.Join(stringx.Remove(userFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
|
|
|
userRowsWithPlaceHolder = strings.Join(stringx.Remove(userFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
|
|
|
|
|
|
|
|
|
|
|
cacheOaUserIdPrefix = "cache:oa:user:id:"
|
|
|
|
|
|
|
|
cacheOaUserEmailPrefix = "cache:oa:user:email:"
|
|
|
|
|
|
|
|
cacheOaUserPhonePrefix = "cache:oa:user:phone:"
|
|
|
|
|
|
|
|
cacheOaUserUsernamePrefix = "cache:oa:user:username:"
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type (
|
|
|
|
type (
|
|
|
@ -40,7 +33,7 @@ type (
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
defaultUserModel struct {
|
|
|
|
defaultUserModel struct {
|
|
|
|
sqlc.CachedConn
|
|
|
|
conn sqlx.SqlConn
|
|
|
|
table string
|
|
|
|
table string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -61,41 +54,27 @@ type (
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func newUserModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultUserModel {
|
|
|
|
func newUserModel(conn sqlx.SqlConn) *defaultUserModel {
|
|
|
|
return &defaultUserModel{
|
|
|
|
return &defaultUserModel{
|
|
|
|
CachedConn: sqlc.NewConn(conn, c, opts...),
|
|
|
|
conn: conn,
|
|
|
|
table: "`user`",
|
|
|
|
table: "`user`",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUserModel) Delete(ctx context.Context, id string) error {
|
|
|
|
func (m *defaultUserModel) Delete(ctx context.Context, id string) error {
|
|
|
|
data, err := m.FindOne(ctx, id)
|
|
|
|
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
|
|
|
if err != nil {
|
|
|
|
_, err := m.conn.ExecCtx(ctx, query, id)
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
oaUserEmailKey := fmt.Sprintf("%s%v", cacheOaUserEmailPrefix, data.Email)
|
|
|
|
|
|
|
|
oaUserIdKey := fmt.Sprintf("%s%v", cacheOaUserIdPrefix, id)
|
|
|
|
|
|
|
|
oaUserPhoneKey := fmt.Sprintf("%s%v", cacheOaUserPhonePrefix, data.Phone)
|
|
|
|
|
|
|
|
oaUserUsernameKey := fmt.Sprintf("%s%v", cacheOaUserUsernamePrefix, data.Username)
|
|
|
|
|
|
|
|
_, 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)
|
|
|
|
|
|
|
|
}, oaUserEmailKey, oaUserIdKey, oaUserPhoneKey, oaUserUsernameKey)
|
|
|
|
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUserModel) FindOne(ctx context.Context, id string) (*User, error) {
|
|
|
|
func (m *defaultUserModel) FindOne(ctx context.Context, id string) (*User, error) {
|
|
|
|
oaUserIdKey := fmt.Sprintf("%s%v", cacheOaUserIdPrefix, id)
|
|
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", userRows, m.table)
|
|
|
|
var resp User
|
|
|
|
var resp User
|
|
|
|
err := m.QueryRowCtx(ctx, &resp, oaUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
|
|
|
err := m.conn.QueryRowCtx(ctx, &resp, query, id)
|
|
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", userRows, m.table)
|
|
|
|
|
|
|
|
return conn.QueryRowCtx(ctx, v, query, id)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
case nil:
|
|
|
|
return &resp, nil
|
|
|
|
return &resp, nil
|
|
|
|
case sqlc.ErrNotFound:
|
|
|
|
case sqlx.ErrNotFound:
|
|
|
|
return nil, ErrNotFound
|
|
|
|
return nil, ErrNotFound
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
@ -103,19 +82,13 @@ func (m *defaultUserModel) FindOne(ctx context.Context, id string) (*User, error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUserModel) FindOneByEmail(ctx context.Context, email sql.NullString) (*User, error) {
|
|
|
|
func (m *defaultUserModel) FindOneByEmail(ctx context.Context, email sql.NullString) (*User, error) {
|
|
|
|
oaUserEmailKey := fmt.Sprintf("%s%v", cacheOaUserEmailPrefix, email)
|
|
|
|
|
|
|
|
var resp User
|
|
|
|
var resp User
|
|
|
|
err := m.QueryRowIndexCtx(ctx, &resp, oaUserEmailKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
|
|
|
query := fmt.Sprintf("select %s from %s where `email` = ? limit 1", userRows, m.table)
|
|
|
|
query := fmt.Sprintf("select %s from %s where `email` = ? limit 1", userRows, m.table)
|
|
|
|
err := m.conn.QueryRowCtx(ctx, &resp, query, email)
|
|
|
|
if err := conn.QueryRowCtx(ctx, &resp, query, email); err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Id, nil
|
|
|
|
|
|
|
|
}, m.queryPrimary)
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
case nil:
|
|
|
|
return &resp, nil
|
|
|
|
return &resp, nil
|
|
|
|
case sqlc.ErrNotFound:
|
|
|
|
case sqlx.ErrNotFound:
|
|
|
|
return nil, ErrNotFound
|
|
|
|
return nil, ErrNotFound
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
@ -123,19 +96,13 @@ func (m *defaultUserModel) FindOneByEmail(ctx context.Context, email sql.NullStr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUserModel) FindOneByPhone(ctx context.Context, phone sql.NullString) (*User, error) {
|
|
|
|
func (m *defaultUserModel) FindOneByPhone(ctx context.Context, phone sql.NullString) (*User, error) {
|
|
|
|
oaUserPhoneKey := fmt.Sprintf("%s%v", cacheOaUserPhonePrefix, phone)
|
|
|
|
|
|
|
|
var resp User
|
|
|
|
var resp User
|
|
|
|
err := m.QueryRowIndexCtx(ctx, &resp, oaUserPhoneKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
|
|
|
query := fmt.Sprintf("select %s from %s where `phone` = ? limit 1", userRows, m.table)
|
|
|
|
query := fmt.Sprintf("select %s from %s where `phone` = ? limit 1", userRows, m.table)
|
|
|
|
err := m.conn.QueryRowCtx(ctx, &resp, query, phone)
|
|
|
|
if err := conn.QueryRowCtx(ctx, &resp, query, phone); err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Id, nil
|
|
|
|
|
|
|
|
}, m.queryPrimary)
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
case nil:
|
|
|
|
return &resp, nil
|
|
|
|
return &resp, nil
|
|
|
|
case sqlc.ErrNotFound:
|
|
|
|
case sqlx.ErrNotFound:
|
|
|
|
return nil, ErrNotFound
|
|
|
|
return nil, ErrNotFound
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
@ -143,19 +110,13 @@ func (m *defaultUserModel) FindOneByPhone(ctx context.Context, phone sql.NullStr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUserModel) FindOneByUsername(ctx context.Context, username string) (*User, error) {
|
|
|
|
func (m *defaultUserModel) FindOneByUsername(ctx context.Context, username string) (*User, error) {
|
|
|
|
oaUserUsernameKey := fmt.Sprintf("%s%v", cacheOaUserUsernamePrefix, username)
|
|
|
|
|
|
|
|
var resp User
|
|
|
|
var resp User
|
|
|
|
err := m.QueryRowIndexCtx(ctx, &resp, oaUserUsernameKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
|
|
|
|
query := fmt.Sprintf("select %s from %s where `username` = ? limit 1", userRows, m.table)
|
|
|
|
query := fmt.Sprintf("select %s from %s where `username` = ? limit 1", userRows, m.table)
|
|
|
|
err := m.conn.QueryRowCtx(ctx, &resp, query, username)
|
|
|
|
if err := conn.QueryRowCtx(ctx, &resp, query, username); err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Id, nil
|
|
|
|
|
|
|
|
}, m.queryPrimary)
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
case nil:
|
|
|
|
return &resp, nil
|
|
|
|
return &resp, nil
|
|
|
|
case sqlc.ErrNotFound:
|
|
|
|
case sqlx.ErrNotFound:
|
|
|
|
return nil, ErrNotFound
|
|
|
|
return nil, ErrNotFound
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
@ -163,43 +124,17 @@ func (m *defaultUserModel) FindOneByUsername(ctx context.Context, username strin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUserModel) Insert(ctx context.Context, data *User) (sql.Result, error) {
|
|
|
|
func (m *defaultUserModel) Insert(ctx context.Context, data *User) (sql.Result, error) {
|
|
|
|
oaUserEmailKey := fmt.Sprintf("%s%v", cacheOaUserEmailPrefix, data.Email)
|
|
|
|
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, userRowsExpectAutoSet)
|
|
|
|
oaUserIdKey := fmt.Sprintf("%s%v", cacheOaUserIdPrefix, data.Id)
|
|
|
|
ret, err := m.conn.ExecCtx(ctx, query, data.Id, data.Created, data.Updated, data.Username, data.Nickname, data.Email, data.Phone, data.Icon, data.RealCode, data.CheckCode, data.Status, data.Used, data.Space)
|
|
|
|
oaUserPhoneKey := fmt.Sprintf("%s%v", cacheOaUserPhonePrefix, data.Phone)
|
|
|
|
|
|
|
|
oaUserUsernameKey := fmt.Sprintf("%s%v", cacheOaUserUsernamePrefix, data.Username)
|
|
|
|
|
|
|
|
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, userRowsExpectAutoSet)
|
|
|
|
|
|
|
|
return conn.ExecCtx(ctx, query, data.Id, data.Created, data.Updated, data.Username, data.Nickname, data.Email, data.Phone, data.Icon, data.RealCode, data.CheckCode, data.Status, data.Used, data.Space)
|
|
|
|
|
|
|
|
}, oaUserEmailKey, oaUserIdKey, oaUserPhoneKey, oaUserUsernameKey)
|
|
|
|
|
|
|
|
return ret, err
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUserModel) Update(ctx context.Context, newData *User) error {
|
|
|
|
func (m *defaultUserModel) Update(ctx context.Context, newData *User) error {
|
|
|
|
data, err := m.FindOne(ctx, newData.Id)
|
|
|
|
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, userRowsWithPlaceHolder)
|
|
|
|
if err != nil {
|
|
|
|
_, err := m.conn.ExecCtx(ctx, query, newData.Created, newData.Updated, newData.Username, newData.Nickname, newData.Email, newData.Phone, newData.Icon, newData.RealCode, newData.CheckCode, newData.Status, newData.Used, newData.Space, newData.Id)
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
oaUserEmailKey := fmt.Sprintf("%s%v", cacheOaUserEmailPrefix, data.Email)
|
|
|
|
|
|
|
|
oaUserIdKey := fmt.Sprintf("%s%v", cacheOaUserIdPrefix, data.Id)
|
|
|
|
|
|
|
|
oaUserPhoneKey := fmt.Sprintf("%s%v", cacheOaUserPhonePrefix, data.Phone)
|
|
|
|
|
|
|
|
oaUserUsernameKey := fmt.Sprintf("%s%v", cacheOaUserUsernamePrefix, data.Username)
|
|
|
|
|
|
|
|
_, 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, userRowsWithPlaceHolder)
|
|
|
|
|
|
|
|
return conn.ExecCtx(ctx, query, newData.Created, newData.Updated, newData.Username, newData.Nickname, newData.Email, newData.Phone, newData.Icon, newData.RealCode, newData.CheckCode, newData.Status, newData.Used, newData.Space, newData.Id)
|
|
|
|
|
|
|
|
}, oaUserEmailKey, oaUserIdKey, oaUserPhoneKey, oaUserUsernameKey)
|
|
|
|
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUserModel) formatPrimary(primary any) string {
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s%v", cacheOaUserIdPrefix, primary)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUserModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
|
|
|
|
|
|
|
|
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", userRows, m.table)
|
|
|
|
|
|
|
|
return conn.QueryRowCtx(ctx, v, query, primary)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUserModel) tableName() string {
|
|
|
|
func (m *defaultUserModel) tableName() string {
|
|
|
|
return m.table
|
|
|
|
return m.table
|
|
|
|
}
|
|
|
|
}
|
|
|
|