package user import ( "errors" "github.com/astaxie/beego/logs" "github.com/astaxie/beego/orm" "nginx-ui/server/models" ) type RoleService struct { } var Role = RoleService{} func (u *RoleService) List() ([]models.UserRole, error) { qs := orm.NewOrm().QueryTable(new(models.UserRole)) var list []models.UserRole _, err := qs.All(&list) if err != nil { return nil, err } return list, err } func (u *RoleService) Save(body *models.UserRole) (*models.UserRole, error) { o := orm.NewOrm() _, err := models.InsertOrUpdate[models.UserRole](o, body) return body, err } func (u *RoleService) Delete(id int) error { o := orm.NewOrm() role := models.UserRole{} err := o.Read(&role) if err != nil && errors.Is(err, orm.ErrNoRows) { return nil } else if err != nil { return err } if role.RefCount > 0 { return errors.New("角色有引用资源,请先删除后再操作!") } _, err = o.Delete(&models.UserRole{Id: id}) return err } func (u *RoleService) Ref(size int, role string) error { o := orm.NewOrm() _, err := o.Raw("UPDATE user_role SET ref_count=ref_count+? WHERE code=?", size, role).Exec() if err != nil { logs.Error("update role ref error", err) return err } return nil }