package Menu import ( "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" "gogs.baozhida.cn/zoie/ERP_libs/lib" ) const ( // Menu 菜单 MenuType string = "M" // Button 按钮 ButtonType string = "B" ) type Menu struct { Id int `orm:"column(ID);size(11);auto;pk"` T_mid int `orm:"size(200);null"` // 上一级 ID T_name string `orm:"size(256);null"` // 菜单标题 T_permission string `orm:"size(256);null"` // 权限表示 T_icon string `orm:"size(256);null"` // 图标 T_uri string `orm:"size(256);null"` // 路径 T_type string `orm:"size(2);null"` // 菜单类型 T_sort int `orm:"size(11);default(0)"` // 排序 越小的越靠前,可以为负数 T_State int `orm:"size(2);default(1)"` // 0删除 1正常 2共有菜单 Children []Menu `orm:"-"` } func (t *Menu) TableName() string { return "menu" // 数据库名称 // ************** 替换 FormulaList ************** } func NewMenu(orm orm.Ormer) MenuDaoImpl { return MenuDaoImpl{orm: orm} } type MenuDaoImpl struct { orm orm.Ormer } func (m MenuDaoImpl) Read_Menu_List() ([]Menu, error) { qs := m.orm.QueryTable(new(Menu)) var menuList []Menu _, err := qs.Filter("T_State", 1).OrderBy("T_sort", "Id").All(&menuList) if err != nil { return nil, err } return m.Menu_Tree(menuList, 0), nil } // 获取权限绑定菜单列表 func (m *MenuDaoImpl) Read_Menu_List_ByPower_T_Menu(T_menu string) (maps []Menu, err error) { if len(T_menu) == 0 { return } // 也可以直接使用 Model 结构体作为表名 qs := m.orm.QueryTable(new(Menu)) cond := orm.NewCondition() cond = cond.And("T_State__gt", 0) if T_menu == "*" { _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_sort", "Id").All(&maps) if err != nil { return } menu := m.Menu_Tree(maps, 0) return menu, nil } list := lib.SplitStringToIntIds(T_menu, "M") AllIds := list if err = m.recursiveMenu(list, &AllIds); err != nil { return } cond = cond.And("Id__in", lib.IntIdsDistinct(AllIds)).Or("T_State", 2) _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_sort", "Id").All(&maps) if err != nil { return } return m.Menu_Tree(maps, 0), nil } func (m *MenuDaoImpl) Menu_Tree(menuList []Menu, parentId int) []Menu { var res []Menu for _, v := range menuList { if v.T_type != MenuType { continue } if v.T_mid == parentId { v.Children = m.Menu_Tree(menuList, v.Id) res = append(res, v) } } return res } // 获取MenuIds下父id func (m *MenuDaoImpl) recursiveMenu(MenuIds []int, AllIds *[]int) error { if len(MenuIds) == 0 { return nil } var subMenus []Menu qs := m.orm.QueryTable(new(Menu)) cond := orm.NewCondition() cond = cond.And("Id__in", MenuIds) _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_sort", "Id").All(&subMenus) if err != nil { return err } subIds := make([]int, 0) for _, menu := range subMenus { if menu.T_mid != 0 { subIds = append(subIds, menu.T_mid) *AllIds = append(*AllIds, menu.T_mid) } } return m.recursiveMenu(subIds, AllIds) }