71 lines
1.8 KiB
Go
Executable File
71 lines
1.8 KiB
Go
Executable File
package model
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/cache"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
)
|
|
|
|
var _ SysRoleModel = (*customSysRoleModel)(nil)
|
|
|
|
type (
|
|
SysRoleModel interface {
|
|
sysRoleModel
|
|
FindList(ctx context.Context, page, pageSize int64, roleName string, status int64) ([]*SysRole, int64, error)
|
|
FindAll(ctx context.Context) ([]*SysRole, error)
|
|
}
|
|
|
|
customSysRoleModel struct {
|
|
*defaultSysRoleModel
|
|
}
|
|
)
|
|
|
|
func NewSysRoleModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) SysRoleModel {
|
|
return &customSysRoleModel{
|
|
defaultSysRoleModel: newSysRoleModel(conn, c, opts...),
|
|
}
|
|
}
|
|
|
|
func (m *customSysRoleModel) FindList(ctx context.Context, page, pageSize int64, roleName string, status int64) ([]*SysRole, int64, error) {
|
|
where := "WHERE 1=1"
|
|
args := make([]interface{}, 0)
|
|
|
|
if roleName != "" {
|
|
where += " AND role_name LIKE ?"
|
|
args = append(args, "%"+roleName+"%")
|
|
}
|
|
if status >= 0 {
|
|
where += " AND status = ?"
|
|
args = append(args, status)
|
|
}
|
|
|
|
var total int64
|
|
countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s %s", m.table, where)
|
|
err := m.QueryRowNoCacheCtx(ctx, &total, countQuery, args...)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
var list []*SysRole
|
|
query := fmt.Sprintf("SELECT %s FROM %s %s ORDER BY sort_order ASC, created_at DESC LIMIT ? OFFSET ?", sysRoleRows, m.table, where)
|
|
args = append(args, pageSize, (page-1)*pageSize)
|
|
err = m.QueryRowsNoCacheCtx(ctx, &list, query, args...)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return list, total, nil
|
|
}
|
|
|
|
func (m *customSysRoleModel) FindAll(ctx context.Context) ([]*SysRole, error) {
|
|
var list []*SysRole
|
|
query := fmt.Sprintf("SELECT %s FROM %s ORDER BY sort_order ASC", sysRoleRows, m.table)
|
|
err := m.QueryRowsNoCacheCtx(ctx, &list, query)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return list, nil
|
|
}
|