temp
This commit is contained in:
@@ -17,7 +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"
|
||||
"qnc-server/common/globalkey"
|
||||
"tyc-server/common/globalkey"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -26,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`"), "=?,") + "=?"
|
||||
|
||||
cacheQncUserIdPrefix = "cache:qnc:user:id:"
|
||||
cacheQncUserMobilePrefix = "cache:qnc:user:mobile:"
|
||||
cacheTycUserIdPrefix = "cache:tyc:user:id:"
|
||||
cacheTycUserMobilePrefix = "cache:tyc:user:mobile:"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -64,7 +64,7 @@ type (
|
||||
Version int64 `db:"version"` // 版本号
|
||||
Mobile string `db:"mobile"`
|
||||
Password sql.NullString `db:"password"`
|
||||
Nickname string `db:"nickname"`
|
||||
Nickname sql.NullString `db:"nickname"`
|
||||
Info string `db:"info"`
|
||||
Inside int64 `db:"inside"`
|
||||
}
|
||||
@@ -79,21 +79,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
|
||||
qncUserIdKey := fmt.Sprintf("%s%v", cacheQncUserIdPrefix, data.Id)
|
||||
qncUserMobileKey := fmt.Sprintf("%s%v", cacheQncUserMobilePrefix, data.Mobile)
|
||||
tycUserIdKey := fmt.Sprintf("%s%v", cacheTycUserIdPrefix, data.Id)
|
||||
tycUserMobileKey := fmt.Sprintf("%s%v", cacheTycUserMobilePrefix, 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)
|
||||
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 conn.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info, data.Inside)
|
||||
}, qncUserIdKey, qncUserMobileKey)
|
||||
}, tycUserIdKey, tycUserMobileKey)
|
||||
}
|
||||
|
||||
func (m *defaultUserModel) FindOne(ctx context.Context, id int64) (*User, error) {
|
||||
qncUserIdKey := fmt.Sprintf("%s%v", cacheQncUserIdPrefix, id)
|
||||
tycUserIdKey := fmt.Sprintf("%s%v", cacheTycUserIdPrefix, id)
|
||||
var resp User
|
||||
err := m.QueryRowCtx(ctx, &resp, qncUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error {
|
||||
err := m.QueryRowCtx(ctx, &resp, tycUserIdKey, 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)
|
||||
})
|
||||
@@ -108,9 +108,9 @@ func (m *defaultUserModel) FindOne(ctx context.Context, id int64) (*User, error)
|
||||
}
|
||||
|
||||
func (m *defaultUserModel) FindOneByMobile(ctx context.Context, mobile string) (*User, error) {
|
||||
qncUserMobileKey := fmt.Sprintf("%s%v", cacheQncUserMobilePrefix, mobile)
|
||||
tycUserMobileKey := fmt.Sprintf("%s%v", cacheTycUserMobilePrefix, mobile)
|
||||
var resp User
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, qncUserMobileKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) {
|
||||
err := m.QueryRowIndexCtx(ctx, &resp, tycUserMobileKey, 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
|
||||
@@ -132,15 +132,15 @@ func (m *defaultUserModel) Update(ctx context.Context, session sqlx.Session, new
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
qncUserIdKey := fmt.Sprintf("%s%v", cacheQncUserIdPrefix, data.Id)
|
||||
qncUserMobileKey := fmt.Sprintf("%s%v", cacheQncUserMobilePrefix, data.Mobile)
|
||||
tycUserIdKey := fmt.Sprintf("%s%v", cacheTycUserIdPrefix, data.Id)
|
||||
tycUserMobileKey := fmt.Sprintf("%s%v", cacheTycUserMobilePrefix, 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 conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id)
|
||||
}, qncUserIdKey, qncUserMobileKey)
|
||||
}, tycUserIdKey, tycUserMobileKey)
|
||||
}
|
||||
|
||||
func (m *defaultUserModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, newData *User) error {
|
||||
@@ -155,15 +155,15 @@ func (m *defaultUserModel) UpdateWithVersion(ctx context.Context, session sqlx.S
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
qncUserIdKey := fmt.Sprintf("%s%v", cacheQncUserIdPrefix, data.Id)
|
||||
qncUserMobileKey := fmt.Sprintf("%s%v", cacheQncUserMobilePrefix, data.Mobile)
|
||||
tycUserIdKey := fmt.Sprintf("%s%v", cacheTycUserIdPrefix, data.Id)
|
||||
tycUserMobileKey := fmt.Sprintf("%s%v", cacheTycUserMobilePrefix, 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 conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id, oldVersion)
|
||||
}, qncUserIdKey, qncUserMobileKey)
|
||||
}, tycUserIdKey, tycUserMobileKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -386,19 +386,19 @@ func (m *defaultUserModel) Delete(ctx context.Context, session sqlx.Session, id
|
||||
return err
|
||||
}
|
||||
|
||||
qncUserIdKey := fmt.Sprintf("%s%v", cacheQncUserIdPrefix, id)
|
||||
qncUserMobileKey := fmt.Sprintf("%s%v", cacheQncUserMobilePrefix, data.Mobile)
|
||||
tycUserIdKey := fmt.Sprintf("%s%v", cacheTycUserIdPrefix, id)
|
||||
tycUserMobileKey := fmt.Sprintf("%s%v", cacheTycUserMobilePrefix, 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)
|
||||
}, qncUserIdKey, qncUserMobileKey)
|
||||
}, tycUserIdKey, tycUserMobileKey)
|
||||
return err
|
||||
}
|
||||
func (m *defaultUserModel) formatPrimary(primary interface{}) string {
|
||||
return fmt.Sprintf("%s%v", cacheQncUserIdPrefix, primary)
|
||||
return fmt.Sprintf("%s%v", cacheTycUserIdPrefix, 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