roleService.go 3.6 KB

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