menuService.go 5.6 KB

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