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.

50 lines
1.4 KiB
Go

package model
import (
"time"
"gorm.io/gorm"
)
type Role struct {
ID string `json:"id" gorm:"column:id; primaryKey;autoIncrement:false"`
RoleName string `json:"role_name" gorm:"comment:'角色名称'"`
RoleCode string `json:"role_code" gorm:"comment:'角色code'"`
Description string `json:"description" gorm:"comment:'描述'"`
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 (r Role) TableName() string {
return "sys_auth_role"
}
func GetRoleByCode(db *gorm.DB, code string) (Role, error) {
role := Role{}
err := db.Where("role_code = ?", code).First(&role).Error
if err != nil && err != gorm.ErrRecordNotFound {
return role, err
}
return role, nil
}
func GetRoleById(db *gorm.DB, id string) (Role, error) {
role := Role{}
err := db.Where("id = ?", id).First(&role).Error
if err != nil && err != gorm.ErrRecordNotFound {
return role, err
}
return role, nil
}
func GetRoles(db *gorm.DB, offset int, limit int) ([]Role, error) {
roles := []Role{}
err := db.Order("created_at desc").Offset(offset).Limit(limit).Find(&roles).Error
if err != nil && err != gorm.ErrRecordNotFound {
return roles, err
}
return roles, nil
}