Menu.go 3.0 KB

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