This commit is contained in:
2025-01-10 11:15:14 +08:00
parent f54ead0f90
commit 0a2aa69aeb
12 changed files with 241 additions and 226 deletions

View File

@@ -7,6 +7,7 @@ import (
"database/sql"
"fmt"
"strings"
model2 "tydata-server/deploy/script/model"
"time"
@@ -26,9 +27,9 @@ var (
userAuthRowsExpectAutoSet = strings.Join(stringx.Remove(userAuthFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
userAuthRowsWithPlaceHolder = strings.Join(stringx.Remove(userAuthFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
cacheQncUserAuthIdPrefix = "cache:qnc:userAuth:id:"
cacheQncUserAuthAuthTypeAuthKeyPrefix = "cache:qnc:userAuth:authType:authKey:"
cacheQncUserAuthUserIdAuthTypePrefix = "cache:qnc:userAuth:userId:authType:"
cacheTydataUserAuthIdPrefix = "cache:tydata:userAuth:id:"
cacheTydataUserAuthAuthTypeAuthKeyPrefix = "cache:tydata:userAuth:authType:authKey:"
cacheTydataUserAuthUserIdAuthTypePrefix = "cache:tydata:userAuth:userId:authType:"
)
type (
@@ -79,22 +80,22 @@ func newUserAuthModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultUserAuthMode
func (m *defaultUserAuthModel) Insert(ctx context.Context, session sqlx.Session, data *UserAuth) (sql.Result, error) {
data.DelState = globalkey.DelStateNo
qncUserAuthAuthTypeAuthKeyKey := fmt.Sprintf("%s%v:%v", cacheQncUserAuthAuthTypeAuthKeyPrefix, data.AuthType, data.AuthKey)
qncUserAuthIdKey := fmt.Sprintf("%s%v", cacheQncUserAuthIdPrefix, data.Id)
qncUserAuthUserIdAuthTypeKey := fmt.Sprintf("%s%v:%v", cacheQncUserAuthUserIdAuthTypePrefix, data.UserId, data.AuthType)
tydataUserAuthAuthTypeAuthKeyKey := fmt.Sprintf("%s%v:%v", cacheTydataUserAuthAuthTypeAuthKeyPrefix, data.AuthType, data.AuthKey)
tydataUserAuthIdKey := fmt.Sprintf("%s%v", cacheTydataUserAuthIdPrefix, data.Id)
tydataUserAuthUserIdAuthTypeKey := fmt.Sprintf("%s%v:%v", cacheTydataUserAuthUserIdAuthTypePrefix, data.UserId, data.AuthType)
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, userAuthRowsExpectAutoSet)
if session != nil {
return session.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.UserId, data.AuthKey, data.AuthType)
}
return conn.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.UserId, data.AuthKey, data.AuthType)
}, qncUserAuthAuthTypeAuthKeyKey, qncUserAuthIdKey, qncUserAuthUserIdAuthTypeKey)
}, tydataUserAuthAuthTypeAuthKeyKey, tydataUserAuthIdKey, tydataUserAuthUserIdAuthTypeKey)
}
func (m *defaultUserAuthModel) FindOne(ctx context.Context, id int64) (*UserAuth, error) {
qncUserAuthIdKey := fmt.Sprintf("%s%v", cacheQncUserAuthIdPrefix, id)
tydataUserAuthIdKey := fmt.Sprintf("%s%v", cacheTydataUserAuthIdPrefix, id)
var resp UserAuth
err := m.QueryRowCtx(ctx, &resp, qncUserAuthIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
err := m.QueryRowCtx(ctx, &resp, tydataUserAuthIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", userAuthRows, m.table)
return conn.QueryRowCtx(ctx, v, query, id, globalkey.DelStateNo)
})
@@ -102,16 +103,16 @@ func (m *defaultUserAuthModel) FindOne(ctx context.Context, id int64) (*UserAuth
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
return nil, model2.ErrNotFound
default:
return nil, err
}
}
func (m *defaultUserAuthModel) FindOneByAuthTypeAuthKey(ctx context.Context, authType string, authKey string) (*UserAuth, error) {
qncUserAuthAuthTypeAuthKeyKey := fmt.Sprintf("%s%v:%v", cacheQncUserAuthAuthTypeAuthKeyPrefix, authType, authKey)
tydataUserAuthAuthTypeAuthKeyKey := fmt.Sprintf("%s%v:%v", cacheTydataUserAuthAuthTypeAuthKeyPrefix, authType, authKey)
var resp UserAuth
err := m.QueryRowIndexCtx(ctx, &resp, qncUserAuthAuthTypeAuthKeyKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
err := m.QueryRowIndexCtx(ctx, &resp, tydataUserAuthAuthTypeAuthKeyKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
query := fmt.Sprintf("select %s from %s where `auth_type` = ? and `auth_key` = ? and del_state = ? limit 1", userAuthRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, authType, authKey, globalkey.DelStateNo); err != nil {
return nil, err
@@ -122,16 +123,16 @@ func (m *defaultUserAuthModel) FindOneByAuthTypeAuthKey(ctx context.Context, aut
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
return nil, model2.ErrNotFound
default:
return nil, err
}
}
func (m *defaultUserAuthModel) FindOneByUserIdAuthType(ctx context.Context, userId int64, authType string) (*UserAuth, error) {
qncUserAuthUserIdAuthTypeKey := fmt.Sprintf("%s%v:%v", cacheQncUserAuthUserIdAuthTypePrefix, userId, authType)
tydataUserAuthUserIdAuthTypeKey := fmt.Sprintf("%s%v:%v", cacheTydataUserAuthUserIdAuthTypePrefix, userId, authType)
var resp UserAuth
err := m.QueryRowIndexCtx(ctx, &resp, qncUserAuthUserIdAuthTypeKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
err := m.QueryRowIndexCtx(ctx, &resp, tydataUserAuthUserIdAuthTypeKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
query := fmt.Sprintf("select %s from %s where `user_id` = ? and `auth_type` = ? and del_state = ? limit 1", userAuthRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, userId, authType, globalkey.DelStateNo); err != nil {
return nil, err
@@ -142,7 +143,7 @@ func (m *defaultUserAuthModel) FindOneByUserIdAuthType(ctx context.Context, user
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
return nil, model2.ErrNotFound
default:
return nil, err
}
@@ -153,16 +154,16 @@ func (m *defaultUserAuthModel) Update(ctx context.Context, session sqlx.Session,
if err != nil {
return nil, err
}
qncUserAuthAuthTypeAuthKeyKey := fmt.Sprintf("%s%v:%v", cacheQncUserAuthAuthTypeAuthKeyPrefix, data.AuthType, data.AuthKey)
qncUserAuthIdKey := fmt.Sprintf("%s%v", cacheQncUserAuthIdPrefix, data.Id)
qncUserAuthUserIdAuthTypeKey := fmt.Sprintf("%s%v:%v", cacheQncUserAuthUserIdAuthTypePrefix, data.UserId, data.AuthType)
tydataUserAuthAuthTypeAuthKeyKey := fmt.Sprintf("%s%v:%v", cacheTydataUserAuthAuthTypeAuthKeyPrefix, data.AuthType, data.AuthKey)
tydataUserAuthIdKey := fmt.Sprintf("%s%v", cacheTydataUserAuthIdPrefix, data.Id)
tydataUserAuthUserIdAuthTypeKey := fmt.Sprintf("%s%v:%v", cacheTydataUserAuthUserIdAuthTypePrefix, data.UserId, data.AuthType)
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, userAuthRowsWithPlaceHolder)
if session != nil {
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.UserId, newData.AuthKey, newData.AuthType, newData.Id)
}
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.UserId, newData.AuthKey, newData.AuthType, newData.Id)
}, qncUserAuthAuthTypeAuthKeyKey, qncUserAuthIdKey, qncUserAuthUserIdAuthTypeKey)
}, tydataUserAuthAuthTypeAuthKeyKey, tydataUserAuthIdKey, tydataUserAuthUserIdAuthTypeKey)
}
func (m *defaultUserAuthModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, newData *UserAuth) error {
@@ -177,16 +178,16 @@ func (m *defaultUserAuthModel) UpdateWithVersion(ctx context.Context, session sq
if err != nil {
return err
}
qncUserAuthAuthTypeAuthKeyKey := fmt.Sprintf("%s%v:%v", cacheQncUserAuthAuthTypeAuthKeyPrefix, data.AuthType, data.AuthKey)
qncUserAuthIdKey := fmt.Sprintf("%s%v", cacheQncUserAuthIdPrefix, data.Id)
qncUserAuthUserIdAuthTypeKey := fmt.Sprintf("%s%v:%v", cacheQncUserAuthUserIdAuthTypePrefix, data.UserId, data.AuthType)
tydataUserAuthAuthTypeAuthKeyKey := fmt.Sprintf("%s%v:%v", cacheTydataUserAuthAuthTypeAuthKeyPrefix, data.AuthType, data.AuthKey)
tydataUserAuthIdKey := fmt.Sprintf("%s%v", cacheTydataUserAuthIdPrefix, data.Id)
tydataUserAuthUserIdAuthTypeKey := fmt.Sprintf("%s%v:%v", cacheTydataUserAuthUserIdAuthTypePrefix, data.UserId, data.AuthType)
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, userAuthRowsWithPlaceHolder)
if session != nil {
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.UserId, newData.AuthKey, newData.AuthType, newData.Id, oldVersion)
}
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.UserId, newData.AuthKey, newData.AuthType, newData.Id, oldVersion)
}, qncUserAuthAuthTypeAuthKeyKey, qncUserAuthIdKey, qncUserAuthUserIdAuthTypeKey)
}, tydataUserAuthAuthTypeAuthKeyKey, tydataUserAuthIdKey, tydataUserAuthUserIdAuthTypeKey)
if err != nil {
return err
}
@@ -195,7 +196,7 @@ func (m *defaultUserAuthModel) UpdateWithVersion(ctx context.Context, session sq
return err
}
if updateCount == 0 {
return ErrNoRowsUpdate
return model2.ErrNoRowsUpdate
}
return nil
@@ -409,20 +410,20 @@ func (m *defaultUserAuthModel) Delete(ctx context.Context, session sqlx.Session,
return err
}
qncUserAuthAuthTypeAuthKeyKey := fmt.Sprintf("%s%v:%v", cacheQncUserAuthAuthTypeAuthKeyPrefix, data.AuthType, data.AuthKey)
qncUserAuthIdKey := fmt.Sprintf("%s%v", cacheQncUserAuthIdPrefix, id)
qncUserAuthUserIdAuthTypeKey := fmt.Sprintf("%s%v:%v", cacheQncUserAuthUserIdAuthTypePrefix, data.UserId, data.AuthType)
tydataUserAuthAuthTypeAuthKeyKey := fmt.Sprintf("%s%v:%v", cacheTydataUserAuthAuthTypeAuthKeyPrefix, data.AuthType, data.AuthKey)
tydataUserAuthIdKey := fmt.Sprintf("%s%v", cacheTydataUserAuthIdPrefix, id)
tydataUserAuthUserIdAuthTypeKey := fmt.Sprintf("%s%v:%v", cacheTydataUserAuthUserIdAuthTypePrefix, data.UserId, data.AuthType)
_, 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)
}, qncUserAuthAuthTypeAuthKeyKey, qncUserAuthIdKey, qncUserAuthUserIdAuthTypeKey)
}, tydataUserAuthAuthTypeAuthKeyKey, tydataUserAuthIdKey, tydataUserAuthUserIdAuthTypeKey)
return err
}
func (m *defaultUserAuthModel) formatPrimary(primary interface{}) string {
return fmt.Sprintf("%s%v", cacheQncUserAuthIdPrefix, primary)
return fmt.Sprintf("%s%v", cacheTydataUserAuthIdPrefix, primary)
}
func (m *defaultUserAuthModel) 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", userAuthRows, m.table)