feat: Role 增删改查
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
"sundynix-go/global"
|
||||
common "sundynix-go/model/commom/request"
|
||||
"sundynix-go/model/system"
|
||||
systemReq "sundynix-go/model/system/request"
|
||||
)
|
||||
|
||||
type RoleService struct {
|
||||
}
|
||||
|
||||
var RoleServiceApp = new(RoleService)
|
||||
|
||||
func (s *RoleService) SaveRole(role system.Role) error {
|
||||
if !errors.Is(global.DB.Where("code = ?", role.Code).First(&system.Role{}).Error, gorm.ErrRecordNotFound) {
|
||||
return errors.New("存在重复角色")
|
||||
}
|
||||
return global.DB.Create(&role).Error
|
||||
}
|
||||
|
||||
func (s *RoleService) UpdateRole(role system.Role) error {
|
||||
return global.DB.Model(&role).Where("id = ?", role.Id).Updates(&role).Error
|
||||
}
|
||||
|
||||
func (s *RoleService) GetRoleList(info systemReq.GetRoleList) (list interface{}, total int64, err error) {
|
||||
limit := info.PageSize
|
||||
offset := info.PageSize * (info.Current - 1)
|
||||
db := global.DB.Model(&system.Role{})
|
||||
var roleList []system.Role
|
||||
if info.Code != "" {
|
||||
db = db.Where("code = ?", info.Code)
|
||||
}
|
||||
if info.Name != "" {
|
||||
db = db.Where("name LIKE ?", "%"+info.Name+"%")
|
||||
}
|
||||
err = db.Count(&total).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = db.Limit(limit).Offset(offset).Find(&roleList).Error
|
||||
return roleList, total, err
|
||||
}
|
||||
|
||||
func (s *RoleService) DeleteRoleByIds(ids common.IdsReq) error {
|
||||
return global.DB.Where("id in ?", ids.Ids).Delete(&system.Role{}).Error
|
||||
}
|
||||
|
||||
func (s *RoleService) GetRoleById(id int) (role system.Role, err error) {
|
||||
err = global.DB.Where("id = ?", id).First(&role).Error
|
||||
return role, err
|
||||
}
|
||||
Reference in New Issue
Block a user