61 lines
1.6 KiB
Go
Executable File
61 lines
1.6 KiB
Go
Executable File
package model
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
)
|
|
|
|
var _ NhRoleModel = (*customNhRoleModel)(nil)
|
|
|
|
type (
|
|
// NhRoleModel is an interface to be customized, add more methods here,
|
|
// and implement the added methods in customNhRoleModel.
|
|
NhRoleModel interface {
|
|
nhRoleModel
|
|
withSession(session sqlx.Session) NhRoleModel
|
|
AccountExist(ctx context.Context, account string) (bool, error)
|
|
FindRoleUserId(ctx context.Context, roleId int64) (uint, error)
|
|
}
|
|
|
|
customNhRoleModel struct {
|
|
*defaultNhRoleModel
|
|
}
|
|
)
|
|
|
|
func (m *customNhRoleModel) FindRoleUserId(ctx context.Context, roleId int64) (uint, error) {
|
|
query := fmt.Sprintf("SELECT u.id FROM `nh_user` u JOIN %s r ON u.email = r.account WHERE r.role_id = ?", m.table)
|
|
var resp uint
|
|
err := m.conn.QueryRowPartialCtx(ctx, &resp, query, roleId)
|
|
switch {
|
|
case err == nil:
|
|
return resp, nil
|
|
case errors.Is(err, sqlx.ErrNotFound):
|
|
return 0, nil
|
|
default:
|
|
return 0, err
|
|
}
|
|
}
|
|
|
|
func (m *customNhRoleModel) AccountExist(ctx context.Context, account string) (bool, error) {
|
|
query := fmt.Sprintf("select count(*) as `count` from %s where `account` = ?", m.table)
|
|
var count int64
|
|
err := m.conn.QueryRowCtx(ctx, &count, query, account)
|
|
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
|
|
return false, err
|
|
}
|
|
return count > 0, nil
|
|
}
|
|
|
|
// NewNhRoleModel returns a model for the database table.
|
|
func NewNhRoleModel(conn sqlx.SqlConn) NhRoleModel {
|
|
return &customNhRoleModel{
|
|
defaultNhRoleModel: newNhRoleModel(conn),
|
|
}
|
|
}
|
|
|
|
func (m *customNhRoleModel) withSession(session sqlx.Session) NhRoleModel {
|
|
return NewNhRoleModel(sqlx.NewSqlConnFromSession(session))
|
|
}
|