roleService.go 3.8 KB

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