You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
package model
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type UserRole struct {
|
|
ID string `json:"id" gorm:"column:id; primaryKey;autoIncrement:false"`
|
|
UserID string `json:"user_id" gorm:"comment:'用户ID'"`
|
|
RoleID string `json:"role_id" gorm:"comment:'角色ID'"`
|
|
CreatedAt time.Time `json:"created_at" gorm:"column:created_at;comment:'创建时间'"`
|
|
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at;comment:'修改时间'"`
|
|
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"column:deleted_at;comment:'删除时间'"`
|
|
}
|
|
|
|
func (UserRole) TableName() string {
|
|
return "sys_auth_user_role"
|
|
}
|
|
|
|
func GetRoleByUserId(db *gorm.DB, user_id string) (UserRole, error) {
|
|
r := UserRole{}
|
|
err := db.Where("user_id = ?", user_id).First(&r).Error
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
|
return r, err
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
func GetCountByRoleId(db *gorm.DB, role_id string) int64 {
|
|
var count int64 = 0
|
|
db.Find(&UserRole{}).Count(&count)
|
|
return count
|
|
}
|
|
|
|
func GetUsersByRoleId(db *gorm.DB, role_id string, offset int, limit int) ([]User, error) {
|
|
r := []UserRole{}
|
|
err := db.Where("role_id = ?", role_id).Offset(offset).Limit(limit).Find(&r).Error
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
|
return nil, err
|
|
}
|
|
|
|
a := []string{}
|
|
for _, role := range r {
|
|
a = append(a, role.UserID)
|
|
}
|
|
|
|
users := []User{}
|
|
for _, uid := range a {
|
|
u, _ := GetUserById(db, uid)
|
|
users = append(users, u)
|
|
}
|
|
|
|
return users, nil
|
|
}
|
|
|
|
func GetUserRole(db *gorm.DB, user_id string, role_id string) (UserRole, error) {
|
|
r := UserRole{}
|
|
err := db.Where("user_id = ? and role_id = ?", user_id, role_id).First(&r).Error
|
|
if err != nil && err != gorm.ErrRecordNotFound {
|
|
return r, err
|
|
}
|
|
return r, nil
|
|
}
|