roleService.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/system/dao"
  5. "iot_manager_service/app/system/model"
  6. "iot_manager_service/util/cache"
  7. "iot_manager_service/util/common"
  8. "strconv"
  9. )
  10. // 角色管理服务
  11. var RoleService = new(roleService)
  12. type roleService struct{}
  13. func (s *roleService) Get(id int64) (*dao.Role, error) {
  14. role := &dao.Role{
  15. ID: id,
  16. }
  17. err := role.Get()
  18. if err != nil {
  19. return nil, err
  20. }
  21. return role, nil
  22. }
  23. func (s *roleService) List(roleName, tenantId, roleAlias string, current, size int) ([]dao.Role, error) {
  24. role := &dao.Role{}
  25. offset := (current - 1) * size
  26. limit := size
  27. if roleName != "" {
  28. role.RoleName = roleName
  29. }
  30. if tenantId != "" {
  31. role.TenantId = tenantId
  32. }
  33. if roleAlias != "" {
  34. role.RoleAlias = roleAlias
  35. }
  36. roles, err := role.GetRoles(offset, limit)
  37. if err != nil {
  38. return nil, err
  39. }
  40. return roles, nil
  41. }
  42. func (s *roleService) Tree() (*model.RspRoleTree, error) {
  43. role := &dao.Role{}
  44. roles, err := role.GetAll()
  45. if err != nil {
  46. return nil, err
  47. }
  48. rsp := &model.RspRoleTree{}
  49. //上级角色——多个下级角色数组
  50. tmp := make(map[int64][]dao.Role)
  51. for _, role := range roles {
  52. childes, isExist := tmp[role.ParentId]
  53. if isExist {
  54. childes = append(childes, role)
  55. tmp[role.ParentId] = childes
  56. } else {
  57. tmp[role.ParentId] = []dao.Role{role}
  58. }
  59. }
  60. if len(tmp) == 0 {
  61. return nil, nil
  62. }
  63. roots, _ := tmp[0]
  64. rsp.RoleDetails = dfs(rsp.RoleDetails, roots, tmp)
  65. return rsp, nil
  66. }
  67. func dfs(result []model.RoleDetail, roles []dao.Role, tmp map[int64][]dao.Role) []model.RoleDetail {
  68. if len(roles) == 0 {
  69. return nil
  70. }
  71. for _, child := range roles {
  72. id := strconv.FormatInt(child.ID, 10)
  73. parentId := strconv.FormatInt(child.ParentId, 10)
  74. detail := model.RoleDetail{
  75. ID: id,
  76. ParentId: parentId,
  77. Title: child.RoleName,
  78. Key: id,
  79. Value: id,
  80. }
  81. childes, isExist := tmp[child.ID]
  82. if isExist {
  83. detail.HasChildren = true
  84. detail.Children = dfs([]model.RoleDetail{}, childes, tmp)
  85. result = append(result, detail)
  86. } else {
  87. detail.HasChildren = false
  88. result = append(result, detail)
  89. }
  90. }
  91. return result
  92. }
  93. func (s *roleService) Submit(req dao.Role) *common.Errors {
  94. role := &req
  95. if role.ID == 0 {
  96. err := role.Save()
  97. if err != nil {
  98. return common.FailResponse(err.Error(), nil)
  99. }
  100. return common.SuccessResponse(common.Succeeded, nil)
  101. }
  102. err := role.Update()
  103. if err != nil {
  104. return common.FailResponse(err.Error(), nil)
  105. }
  106. return common.SuccessResponse(common.Succeeded, nil)
  107. }
  108. func (s *roleService) GetList() ([]dao.Role, error) {
  109. role := &dao.Role{}
  110. roles, err := role.GetAll()
  111. if err != nil {
  112. return nil, err
  113. }
  114. return roles, nil
  115. }
  116. func (s *roleService) Remove(id int64) *common.Errors {
  117. role := &dao.Role{
  118. ID: id,
  119. IsDeleted: 1,
  120. }
  121. queryRole := &dao.Role{
  122. ParentId: id,
  123. IsDeleted: 0,
  124. }
  125. if queryRole.IsExistChild() {
  126. return common.FailResponse(model.ExistChild, nil)
  127. }
  128. err := role.Remove()
  129. if err != nil {
  130. return common.FailResponse(err.Error(), nil)
  131. }
  132. return nil
  133. }
  134. func (s *roleService) Grant(req *model.ReqRoleGrant) error {
  135. roleIds := common.StringToIntArray(req.RoleIds)
  136. //todo 数据权限 接口权限
  137. menuIds := common.StringToIntArray(req.MenuIds)
  138. menuErr := RoleMenuService.UpdateRoleMenus(roleIds, menuIds)
  139. return menuErr
  140. }
  141. const RoleNameKey = "%d:sys:role_name:%d"
  142. func (s *roleService) GetRoleName(tenantId int, roleId int64) string {
  143. var name string
  144. key := fmt.Sprintf(RoleNameKey, tenantId, roleId)
  145. name, err := cache.Redis.Get(key).Result()
  146. if err != nil {
  147. role := &dao.Role{ID: roleId}
  148. if err := role.GetRole(); err != nil {
  149. fmt.Printf("GetRole err = %s \n", err)
  150. } else {
  151. name = role.RoleName
  152. cache.Redis.Set(key, name, -1)
  153. }
  154. }
  155. return name
  156. }