menuService.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. "strconv"
  8. )
  9. var MenuService = new(menuService)
  10. type menuService struct{}
  11. func (s *menuService) Get(id int64) (*dao.Menu, error) {
  12. menu := &dao.Menu{
  13. ID: id,
  14. }
  15. err := menu.Get()
  16. if err != nil {
  17. return nil, err
  18. }
  19. return menu, nil
  20. }
  21. func (s *menuService) Submit(req dao.Menu) *common.Errors {
  22. role := &req
  23. if role.ID == 0 {
  24. err := role.Save()
  25. if err != nil {
  26. return common.FailResponse(err.Error(), nil)
  27. }
  28. return common.SuccessResponse(common.Succeeded, nil)
  29. }
  30. err := role.Update()
  31. if err != nil {
  32. return common.FailResponse(err.Error(), nil)
  33. }
  34. return common.SuccessResponse(common.Succeeded, nil)
  35. }
  36. func (s *menuService) GetAll() []dao.Menu {
  37. menu := &dao.Menu{}
  38. menus, err := menu.GetAll()
  39. if err != nil {
  40. return nil
  41. }
  42. return menus
  43. }
  44. func (s *menuService) Tree() ([]model.MenuTreeDetail, error) {
  45. allMenus := s.GetAll()
  46. var rsp []model.MenuTreeDetail
  47. //上级角色——多个下级角色数组
  48. tmp := make(map[int64][]dao.Menu)
  49. for _, menu := range allMenus {
  50. childes, isExist := tmp[menu.ParentId]
  51. if isExist {
  52. childes = append(childes, menu)
  53. tmp[menu.ParentId] = childes
  54. } else {
  55. tmp[menu.ParentId] = []dao.Menu{menu}
  56. }
  57. }
  58. if len(tmp) == 0 {
  59. return nil, nil
  60. }
  61. roots, _ := tmp[0]
  62. rsp = menuTreeDfs(rsp, roots, tmp)
  63. return rsp, nil
  64. }
  65. func (s *menuService) Routes(roleId int64) ([]model.MenuRouteDetail, error) {
  66. allMenus := s.GetAll()
  67. menuMap := make(map[int64]dao.Menu)
  68. for _, menu := range allMenus {
  69. menuMap[menu.ID] = menu
  70. }
  71. roleMenus := RoleMenuService.GetMenuByRole(roleId)
  72. var rsp []model.MenuRouteDetail
  73. //上级角色——多个下级角色数组
  74. tmp := make(map[int64][]dao.Menu)
  75. for _, roleMenu := range roleMenus {
  76. if menu, isExist := menuMap[roleMenu.MenuId]; isExist {
  77. childes, isExist := tmp[menu.ParentId]
  78. if isExist {
  79. childes = append(childes, menu)
  80. tmp[menu.ParentId] = childes
  81. } else {
  82. tmp[menu.ParentId] = []dao.Menu{menu}
  83. }
  84. }
  85. }
  86. if len(tmp) == 0 {
  87. return nil, nil
  88. }
  89. roots, _ := tmp[0]
  90. rsp = menuDfs(rsp, roots, tmp)
  91. sort.Slice(rsp, func(i, j int) bool {
  92. if rsp[i].Sort < rsp[j].Sort {
  93. return true
  94. }
  95. return false
  96. })
  97. return rsp, nil
  98. }
  99. func menuDfs(result []model.MenuRouteDetail, menus []dao.Menu, tmp map[int64][]dao.Menu) []model.MenuRouteDetail {
  100. if len(menus) == 0 {
  101. return nil
  102. }
  103. for _, child := range menus {
  104. detail := model.MenuRouteDetail{}
  105. detail.Menu = child
  106. childes, isExist := tmp[child.ID]
  107. if isExist {
  108. detail.HasChildren = true
  109. detail.Children = menuDfs([]model.MenuRouteDetail{}, childes, tmp)
  110. result = append(result, detail)
  111. } else {
  112. detail.HasChildren = false
  113. result = append(result, detail)
  114. }
  115. }
  116. return result
  117. }
  118. func menuTreeDfs(result []model.MenuTreeDetail, menus []dao.Menu, tmp map[int64][]dao.Menu) []model.MenuTreeDetail {
  119. if len(menus) == 0 {
  120. return nil
  121. }
  122. for _, child := range menus {
  123. detail := model.MenuTreeDetail{}
  124. detail.Title = child.Name
  125. detail.ID = strconv.FormatInt(child.ID, 10)
  126. detail.Key = strconv.FormatInt(child.ID, 10)
  127. detail.Value = strconv.FormatInt(child.ID, 10)
  128. childes, isExist := tmp[child.ID]
  129. if isExist {
  130. detail.HasChildren = true
  131. detail.Children = menuTreeDfs([]model.MenuTreeDetail{}, childes, tmp)
  132. result = append(result, detail)
  133. } else {
  134. detail.HasChildren = false
  135. result = append(result, detail)
  136. }
  137. }
  138. return result
  139. }
  140. func (s *menuService) LazyList(parentId int64, name, code, alias string) ([]model.MenuLazyListDetail, error) {
  141. menu := &dao.Menu{
  142. ParentId: parentId,
  143. Name: name,
  144. Code: code,
  145. Alias: alias,
  146. }
  147. menus, err := menu.GetByParentId()
  148. if err != nil {
  149. return nil, err
  150. }
  151. var parentIds []int64
  152. for _, m := range menus {
  153. parentIds = append(parentIds, m.ID)
  154. }
  155. hasChildrenMap, err := menu.HasChildren(parentIds)
  156. if err != nil {
  157. return nil, err
  158. }
  159. var rsp []model.MenuLazyListDetail
  160. for _, m := range menus {
  161. hasChildren := hasChildrenMap[m.ID]
  162. rsp = append(rsp, model.MenuLazyListDetail{
  163. Menu: m,
  164. HasChildren: hasChildren,
  165. })
  166. }
  167. return rsp, nil
  168. }
  169. func (s *menuService) GetTree() ([]model.MenuRouteDetail, error) {
  170. menuDao := dao.Menu{ParentId: int64(0)}
  171. var menuDetails []model.MenuRouteDetail
  172. var temp model.MenuRouteDetail
  173. //所有菜单包装为菜单详情
  174. menus, err := menuDao.GetAll()
  175. var allDetails []model.MenuRouteDetail
  176. for _, v := range menus {
  177. temp.Menu = v
  178. allDetails = append(allDetails, temp)
  179. }
  180. //一级菜单 parent_id为0 包装为菜单详情
  181. lv1s, _ := menuDao.GetByParentId()
  182. for _, v := range lv1s {
  183. temp.Menu = v
  184. temp.Children, temp.HasChildren = s.getChildren(temp, allDetails)
  185. menuDetails = append(menuDetails, temp)
  186. }
  187. return menuDetails, err
  188. }
  189. func (s *menuService) getChildren(parent model.MenuRouteDetail,
  190. all []model.MenuRouteDetail) ([]model.MenuRouteDetail, bool) {
  191. id := parent.ID
  192. var children []model.MenuRouteDetail
  193. for _, detail := range all {
  194. if detail.ParentId == id {
  195. detail.Children, detail.HasChildren = s.getChildren(detail, all)
  196. children = append(children, detail)
  197. }
  198. }
  199. return children, children != nil
  200. }