package system

import (
	"errors"
	"gorm.io/gorm"
	"server/dao"
	"server/model/system/request"
	"server/model/system/response"
)

type AuthorityBtnService struct{}

func (a *AuthorityBtnService) GetAuthorityBtn(req request.SysAuthorityBtnReq) (res response.SysAuthorityBtnRes, err error) {
	authorityBtn, err := dao.QueryAuthorityBtnByAuthorityIdMenuId(req.AuthorityId, req.MenuID)
	if err != nil {
		return
	}
	var selected []uint
	for _, v := range authorityBtn {
		selected = append(selected, v.SysBaseMenuBtnID)
	}
	res.Selected = selected
	return res, err
}

func (a *AuthorityBtnService) SetAuthorityBtn(req request.SysAuthorityBtnReq) (err error) {
	return dao.SetAuthorityBtn(req.MenuID, req.AuthorityId, req.Selected)
}

func (a *AuthorityBtnService) CanRemoveAuthorityBtn(ID string) (err error) {
	_, fErr := dao.QueryAuthorityBtn(ID)
	if errors.Is(fErr, gorm.ErrRecordNotFound) {
		return nil
	}
	return errors.New("此按钮正在被使用无法删除")
}
