muyu-apiserver/model/sysusermodel.go

68 lines
1.7 KiB
Go
Raw Normal View History

package model
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ SysUserModel = (*customSysUserModel)(nil)
type (
SysUserModel interface {
sysUserModel
FindList(ctx context.Context, tenantId string, page, pageSize int64, username, realName, phone string, status int64) ([]*SysUser, int64, error)
}
customSysUserModel struct {
*defaultSysUserModel
}
)
func NewSysUserModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) SysUserModel {
return &customSysUserModel{
defaultSysUserModel: newSysUserModel(conn, c, opts...),
}
}
func (m *customSysUserModel) FindList(ctx context.Context, tenantId string, page, pageSize int64, username, realName, phone string, status int64) ([]*SysUser, int64, error) {
where := "WHERE deleted_at IS NULL AND tenant_id = ?"
args := []interface{}{tenantId}
if username != "" {
where += " AND username LIKE ?"
args = append(args, "%"+username+"%")
}
if realName != "" {
where += " AND real_name LIKE ?"
args = append(args, "%"+realName+"%")
}
if phone != "" {
where += " AND phone LIKE ?"
args = append(args, "%"+phone+"%")
}
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 []*SysUser
query := fmt.Sprintf("SELECT %s FROM %s %s ORDER BY created_at DESC LIMIT ? OFFSET ?", sysUserRows, 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
}