You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

60 lines
1.5 KiB

package model
import (
"context"
"errors"
"gorm.io/gorm"
)
// RoleInsert 插入角色
func RoleInsert(ctx context.Context, db *gorm.DB, role *Role) (int64, error) {
result := db.WithContext(ctx).Create(role)
if result.Error != nil {
return 0, result.Error
}
return role.Id, nil
}
// RoleFindOne 根据ID查询角色
func RoleFindOne(ctx context.Context, db *gorm.DB, id int64) (*Role, error) {
var role Role
result := db.WithContext(ctx).First(&role, id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, ErrNotFound
}
return nil, result.Error
}
return &role, nil
}
// RoleFindOneByCode 根据编码查询角色
func RoleFindOneByCode(ctx context.Context, db *gorm.DB, code string) (*Role, error) {
var role Role
result := db.WithContext(ctx).Where("code = ?", code).First(&role)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, ErrNotFound
}
return nil, result.Error
}
return &role, nil
}
// RoleFindAll 查询所有启用的角色
func RoleFindAll(ctx context.Context, db *gorm.DB) ([]Role, error) {
var roles []Role
err := db.WithContext(ctx).Where("status = 1").Order("sort_order ASC, id ASC").Find(&roles).Error
return roles, err
}
// RoleUpdate 更新角色
func RoleUpdate(ctx context.Context, db *gorm.DB, role *Role) error {
return db.WithContext(ctx).Save(role).Error
}
// RoleDelete 删除角色
func RoleDelete(ctx context.Context, db *gorm.DB, id int64) error {
return db.WithContext(ctx).Delete(&Role{}, id).Error
}