package system

import "github.com/beego/beego/v2/client/orm"

type MenuList struct {
	MenuId    int          `description:"导航唯一标识"`
	IsLevel   int          `description:"1,只有一级；2,有多级"`
	Name      string       `json:"name" description:"导航名称"`
	Path      string       `json:"path"`
	IconPath  string       `json:"icon_path"`
	LevelPath string       `json:"level_path"`
	Component string       `json:"component"`
	Hidden    bool         `json:"hidden"`
	Children  []*ChildMenu `json:"children"`
}

type ChildMenu struct {
	MenuId    int    `description:"导航唯一标识"`
	Name      string `json:"name" description:"导航名称"`
	Path      string `json:"path"`
	Component string `json:"component"`
	IconPath  string `json:"icon_path"`
	Hidden    bool   `json:"hidden"`
}

type MenuListResp struct {
	List []*MenuList
}

func GetMenuList(roleId int) (items []*MenuList, err error) {
	sql := ` SELECT a.* FROM sys_menu_crm AS a
			INNER JOIN sys_role_menu_crm AS b ON a.menu_id=b.menu_id
			INNER JOIN sys_role AS c ON b.role_id=c.role_id
			WHERE c.role_id=?
			AND a.parent_id=0 
            ORDER BY sort ASC `
	_, err = orm.NewOrm().Raw(sql, roleId).QueryRows(&items)
	return
}

func GetMenuByParentId(roleId, parentId int) (items []*ChildMenu, err error) {
	sql := ` SELECT a.* FROM sys_menu_crm AS a
			INNER JOIN sys_role_menu_crm AS b ON a.menu_id=b.menu_id
			INNER JOIN sys_role AS c ON b.role_id=c.role_id
			WHERE c.role_id=?
			AND a.parent_id=? 
            ORDER BY sort ASC `
	_, err = orm.NewOrm().Raw(sql, roleId, parentId).QueryRows(&items)
	return
}

// GetMenuListByRoleIds 根据管理员多个角色查询菜单ID
func GetMenuListByRoleIds(roleIds string) (items []*MenuList, err error) {
	sql := ` SELECT DISTINCT a.* FROM sys_menu_crm AS a
			INNER JOIN sys_role_menu_crm AS b ON a.menu_id=b.menu_id
			INNER JOIN sys_role AS c ON b.role_id=c.role_id
			WHERE c.role_id in (` + roleIds + `)
			AND a.parent_id=0 
            ORDER BY sort ASC `
	_, err = orm.NewOrm().Raw(sql).QueryRows(&items)
	return
}

// GetMenuByParentIdRoleIds 根据管理员多个角色查询子菜单ID
func GetMenuByParentIdRoleIds(roleIds string, parentId int) (items []*ChildMenu, err error) {
	sql := ` SELECT DISTINCT a.* FROM sys_menu_crm AS a
			INNER JOIN sys_role_menu_crm AS b ON a.menu_id=b.menu_id
			INNER JOIN sys_role AS c ON b.role_id=c.role_id
			WHERE c.role_id in (` + roleIds + `)
			AND a.parent_id=? 
            ORDER BY a.sort ASC`
	_, err = orm.NewOrm().Raw(sql, parentId).QueryRows(&items)
	return
}
