app-cloudep-member-server/internal/model/account_model.go

48 lines
1.4 KiB
Go
Raw Normal View History

package model
import (
"context"
"database/sql"
"fmt"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ AccountModel = (*customAccountModel)(nil)
type (
// AccountModel is an interface to be customized, add more methods here,
// and implement the added methods in customAccountModel.
AccountModel interface {
accountModel
UpdateTokenByLoginID(ctx context.Context, account string, token string) error
}
customAccountModel struct {
*defaultAccountModel
}
)
// NewAccountModel returns a model for the database table.
func NewAccountModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) AccountModel {
return &customAccountModel{
defaultAccountModel: newAccountModel(conn, c, opts...),
}
}
func (m *defaultAccountModel) UpdateTokenByLoginID(ctx context.Context, account string, token string) error {
data, err := m.FindOneByAccount(ctx, account)
if err != nil {
return err
}
accountAccountKey := fmt.Sprintf("%s%v", cacheAccountAccountPrefix, data.Account)
accountIdKey := fmt.Sprintf("%s%v", cacheAccountIdPrefix, data.Id)
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set `token` = ? where `id` = ?", m.table)
return conn.ExecCtx(ctx, query, token, data.Id)
}, accountAccountKey, accountIdKey)
return err
}