package system

import (
	"eta_gn/eta_api/global"
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type SysMenuButton struct {
	MenuButtonId int `gorm:"primaryKey" description:"菜单按钮id"`
	MenuId       int
	ButtonName   int
	CreateTime   time.Time
}

type SysMenuButtonItem struct {
	MenuButtonId int    `description:"菜单按钮id"`
	MenuId       int    `description:"菜单id"`
	ButtonName   string `description:"按钮名称"`
}

func GetSysMenuButtonItems(menuId int) (items []*SysMenuButtonItem, err error) {
	sql := ` SELECT * FROM sys_menu_button  WHERE menu_id=? `
	_, err = orm.NewOrm().Raw(sql, menuId).QueryRows(&items)
	return
}

type MenuButtonsResp struct {
	List      []*ButtonMenu
	CheckList []*int
}

type SysRoleButton struct {
	Id           int `gorm:"primaryKey" description:"关联id"`
	RoleId       int
	MenuButtonId int
	CreateTime   time.Time
}

func AddSysRoleButton(item *SysRoleButton) (lastId int64, err error) {
	err = global.DEFAULT_DmSQL.Create(item).Error
	return
}

func GetRoleButtonCheck(roleId int) (items []*SysRoleButton, err error) {
	sql := `SELECT b.* FROM  sys_menu_button AS a
			INNER JOIN sys_role_button AS b ON a.menu_button_id=b.menu_button_id
			WHERE b.role_id=?`
	err = global.DEFAULT_DmSQL.Raw(sql, roleId).Find(&items).Error
	return
}

type ButtonMenu struct {
	MenuId int    `description:"导航唯一标识"`
	Name   string `json:"name" description:"导航名称"`
	List   []*SysMenuButtonItem
}

func GetButtonMenu() (items []*ButtonMenu, err error) {
	sql := `SELECT b.menu_id,b.name FROM sys_menu_button AS a
			INNER JOIN sys_menu AS b ON a.menu_id=b.menu_id
			GROUP BY  b.menu_id `
	err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
	return
}

func DeleteSysRoleButton(roleId int) (err error) {
	sql := `DELETE FROM sys_role_button WHERE role_id=? `
	err = global.DEFAULT_DmSQL.Exec(sql, roleId).Error
	return
}

func GetSysRoleButtonCount(roleId, buttonId int) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM sys_role_button WHERE role_id=? AND menu_button_id=? `
	err = global.DEFAULT_DmSQL.Exec(sql, roleId, buttonId).Error
	return
}