menuService.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package service
  2. import (
  3. "iot_manager_service/app/system/dao"
  4. "iot_manager_service/app/system/model"
  5. "iot_manager_service/util/common"
  6. "sort"
  7. )
  8. var MenuService = new(menuService)
  9. type menuService struct{}
  10. func (s *menuService) Get(id int64) (*dao.Menu, error) {
  11. menu := &dao.Menu{
  12. ID: id,
  13. }
  14. err := menu.Get()
  15. if err != nil {
  16. return nil, err
  17. }
  18. return menu, nil
  19. }
  20. func (s *menuService) Submit(req dao.Menu) *common.Errors {
  21. role := &req
  22. if role.ID == 0 {
  23. err := role.Save()
  24. if err != nil {
  25. return common.FailResponse(err.Error(), nil)
  26. }
  27. return common.SuccessResponse(common.Succeeded, nil)
  28. }
  29. err := role.Update()
  30. if err != nil {
  31. return common.FailResponse(err.Error(), nil)
  32. }
  33. return common.SuccessResponse(common.Succeeded, nil)
  34. }
  35. func (s *menuService) GetAll() []dao.Menu {
  36. menu := &dao.Menu{}
  37. menus, err := menu.GetAll()
  38. if err != nil {
  39. return nil
  40. }
  41. return menus
  42. }
  43. func (s *menuService) Tree() ([]model.MenuRouteDetail, error) {
  44. allMenus := s.GetAll()
  45. var rsp []model.MenuRouteDetail
  46. //上级角色——多个下级角色数组
  47. tmp := make(map[int64][]dao.Menu)
  48. for _, menu := range allMenus {
  49. childes, isExist := tmp[menu.ParentId]
  50. if isExist {
  51. childes = append(childes, menu)
  52. tmp[menu.ParentId] = childes
  53. } else {
  54. tmp[menu.ParentId] = []dao.Menu{menu}
  55. }
  56. }
  57. if len(tmp) == 0 {
  58. return nil, nil
  59. }
  60. roots, _ := tmp[0]
  61. rsp = menuDfs(rsp, roots, tmp)
  62. return rsp, nil
  63. }
  64. func (s *menuService) Routes(roleId int64) ([]model.MenuRouteDetail, error) {
  65. allMenus := s.GetAll()
  66. menuMap := make(map[int64]dao.Menu)
  67. for _, menu := range allMenus {
  68. menuMap[menu.ID] = menu
  69. }
  70. roleMenus := RoleMenuService.GetMenuByRole(roleId)
  71. var rsp []model.MenuRouteDetail
  72. //上级角色——多个下级角色数组
  73. tmp := make(map[int64][]dao.Menu)
  74. for _, roleMenu := range roleMenus {
  75. if menu, isExist := menuMap[roleMenu.MenuId]; isExist {
  76. childes, isExist := tmp[menu.ParentId]
  77. if isExist {
  78. childes = append(childes, menu)
  79. tmp[menu.ParentId] = childes
  80. } else {
  81. tmp[menu.ParentId] = []dao.Menu{menu}
  82. }
  83. }
  84. }
  85. if len(tmp) == 0 {
  86. return nil, nil
  87. }
  88. roots, _ := tmp[0]
  89. rsp = menuDfs(rsp, roots, tmp)
  90. sort.Slice(rsp, func(i, j int) bool {
  91. if rsp[i].Sort < rsp[j].Sort {
  92. return true
  93. }
  94. return false
  95. })
  96. return rsp, nil
  97. }
  98. func menuDfs(result []model.MenuRouteDetail, menus []dao.Menu, tmp map[int64][]dao.Menu) []model.MenuRouteDetail {
  99. if len(menus) == 0 {
  100. return nil
  101. }
  102. for _, child := range menus {
  103. detail := model.MenuRouteDetail{}
  104. detail.Menu = child
  105. childes, isExist := tmp[child.ID]
  106. if isExist {
  107. detail.HasChildren = true
  108. detail.Children = menuDfs([]model.MenuRouteDetail{}, childes, tmp)
  109. result = append(result, detail)
  110. } else {
  111. detail.HasChildren = false
  112. result = append(result, detail)
  113. }
  114. }
  115. return result
  116. }
  117. func (s *menuService) LazyList(parentId int64, name, code, alias string) ([]model.MenuLazyListDetail, error) {
  118. menu := &dao.Menu{
  119. ParentId: parentId,
  120. Name: name,
  121. Code: code,
  122. Alias: alias,
  123. }
  124. menus, err := menu.GetByParentId()
  125. if err != nil {
  126. return nil, err
  127. }
  128. var parentIds []int64
  129. for _, m := range menus {
  130. parentIds = append(parentIds, m.ID)
  131. }
  132. hasChildrenMap, err := menu.HasChildren(parentIds)
  133. if err != nil {
  134. return nil, err
  135. }
  136. var rsp []model.MenuLazyListDetail
  137. for _, m := range menus {
  138. hasChildren := hasChildrenMap[m.ID]
  139. rsp = append(rsp, model.MenuLazyListDetail{
  140. Menu: m,
  141. HasChildren: hasChildren,
  142. })
  143. }
  144. return rsp, nil
  145. }