Menu.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package Account
  2. import (
  3. "github.com/beego/beego/v2/adapter/orm"
  4. orm2 "github.com/beego/beego/v2/client/orm"
  5. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  6. )
  7. const (
  8. // Menu 菜单
  9. MenuType string = "M"
  10. // Button 按钮
  11. ButtonType string = "B"
  12. )
  13. type Menu struct {
  14. Id int `orm:"column(ID);size(11);auto;pk"`
  15. T_mid int `orm:"size(200);null"` // 上一级 ID
  16. T_name string `orm:"size(256);null"` // 菜单标题
  17. T_permission string `orm:"size(256);null"` // 权限表示
  18. T_icon string `orm:"size(256);null"` // 图标
  19. T_uri string `orm:"size(256);null"` // 路径
  20. T_type string `orm:"size(2);null"` // 菜单类型
  21. T_sort int `orm:"size(11);default(0)"` // 排序 越小的越靠前,可以为负数
  22. T_State int `orm:"size(2);default(1)"` // 0删除 1正常 2共有菜单
  23. Children []Menu `orm:"-"`
  24. }
  25. func (t *Menu) TableName() string {
  26. return "menu" // 数据库名称 // ************** 替换 FormulaList **************
  27. }
  28. func NewMenu(orm orm.Ormer) MenuDaoImpl {
  29. return MenuDaoImpl{orm: orm}
  30. }
  31. type MenuDaoImpl struct {
  32. orm orm.Ormer
  33. }
  34. func (m MenuDaoImpl) Read_Menu_List() ([]Menu, error) {
  35. qs := m.orm.QueryTable(new(Menu))
  36. var menuList []Menu
  37. _, err := qs.Filter("T_State", 1).OrderBy("T_sort", "Id").All(&menuList)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return m.Menu_Tree(menuList, 0), nil
  42. }
  43. // 获取权限绑定菜单列表
  44. func (m *MenuDaoImpl) Read_Menu_List_ByPower_T_Menu(T_menu string) (maps []Menu, err error) {
  45. if len(T_menu) == 0 {
  46. return
  47. }
  48. // 也可以直接使用 Model 结构体作为表名
  49. qs := m.orm.QueryTable(new(Menu))
  50. cond := orm.NewCondition()
  51. cond = cond.And("T_State__gt", 0)
  52. if T_menu == "*" {
  53. _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_sort", "Id").All(&maps)
  54. if err != nil {
  55. return
  56. }
  57. menu := m.Menu_Tree(maps, 0)
  58. return menu, nil
  59. }
  60. list := lib.SplitStringToIntIds(T_menu, "M")
  61. AllIds := list
  62. if err = m.recursiveMenu(list, &AllIds); err != nil {
  63. return
  64. }
  65. cond = cond.And("Id__in", lib.IntIdsDistinct(AllIds)).Or("T_State", 2)
  66. _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_sort", "Id").All(&maps)
  67. if err != nil {
  68. return
  69. }
  70. return m.Menu_Tree(maps, 0), nil
  71. }
  72. func (m *MenuDaoImpl) Menu_Tree(menuList []Menu, parentId int) []Menu {
  73. var res []Menu
  74. for _, v := range menuList {
  75. if v.T_type != MenuType {
  76. continue
  77. }
  78. if v.T_mid == parentId {
  79. v.Children = m.Menu_Tree(menuList, v.Id)
  80. res = append(res, v)
  81. }
  82. }
  83. return res
  84. }
  85. // 获取MenuIds下父id
  86. func (m *MenuDaoImpl) recursiveMenu(MenuIds []int, AllIds *[]int) error {
  87. if len(MenuIds) == 0 {
  88. return nil
  89. }
  90. var subMenus []Menu
  91. qs := m.orm.QueryTable(new(Menu))
  92. cond := orm.NewCondition()
  93. cond = cond.And("Id__in", MenuIds)
  94. _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_sort", "Id").All(&subMenus)
  95. if err != nil {
  96. return err
  97. }
  98. subIds := make([]int, 0)
  99. for _, menu := range subMenus {
  100. if menu.T_mid != 0 {
  101. subIds = append(subIds, menu.T_mid)
  102. *AllIds = append(*AllIds, menu.T_mid)
  103. }
  104. }
  105. return m.recursiveMenu(subIds, AllIds)
  106. }