roleService.go 3.9 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/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 int) (*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) ([]model.RoleVO, int, 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, counts, err := role.GetRoles(offset, limit)
  38. var result []model.RoleVO
  39. var temp model.RoleVO
  40. for _, v := range roles {
  41. temp.ID = v.ID
  42. temp.TenantId = v.TenantId
  43. temp.ParentId = v.ParentId
  44. temp.RoleName = v.RoleName
  45. temp.IsDeleted = v.IsDeleted
  46. temp.RoleAlias = v.RoleAlias
  47. temp.Sort = v.Sort
  48. for _, va := range roles {
  49. if va.ID == temp.ParentId {
  50. temp.ParentName = va.RoleName
  51. }
  52. }
  53. result = append(result, temp)
  54. }
  55. if err != nil {
  56. return nil, 0, err
  57. }
  58. return result, counts, nil
  59. }
  60. func (s *roleService) Submit(req dao.Role) *common.Errors {
  61. role := &req
  62. if role.ID == 0 {
  63. err := role.Save()
  64. if err != nil {
  65. return common.FailResponse(err.Error(), nil)
  66. }
  67. return common.SuccessResponse(common.Succeeded, nil)
  68. }
  69. err := role.Update()
  70. if err != nil {
  71. return common.FailResponse(err.Error(), nil)
  72. }
  73. return common.SuccessResponse(common.Succeeded, nil)
  74. }
  75. func (s *roleService) GetList() ([]dao.Role, error) {
  76. role := &dao.Role{}
  77. roles, err := role.GetAll()
  78. if err != nil {
  79. return nil, err
  80. }
  81. return roles, nil
  82. }
  83. func (s *roleService) Remove(id int) *common.Errors {
  84. role := &dao.Role{
  85. ID: id,
  86. IsDeleted: 1,
  87. }
  88. queryRole := &dao.Role{
  89. ParentId: id,
  90. IsDeleted: 0,
  91. }
  92. if queryRole.IsExistChild() {
  93. return common.FailResponse(model.ExistChild, nil)
  94. }
  95. err := role.Remove()
  96. if err != nil {
  97. return common.FailResponse(err.Error(), nil)
  98. }
  99. return nil
  100. }
  101. func (s *roleService) Grant(req *model.ReqRoleGrant) error {
  102. //todo 数据权限 接口权限
  103. roleIds := req.RoleIds
  104. menuIds := req.MenuIds
  105. menuErr := RoleMenuService.UpdateRoleMenus(roleIds, menuIds)
  106. return menuErr
  107. }
  108. const RoleNameKey = "%d:sys:role_name:%d"
  109. func (s *roleService) GetRoleName(tenantId string, roleId int) string {
  110. var name string
  111. key := fmt.Sprintf(RoleNameKey, tenantId, roleId)
  112. name, err := cache.Redis.Get(key).Result()
  113. if err != nil {
  114. role := &dao.Role{ID: roleId}
  115. if err := role.GetRole(); err != nil {
  116. logger.Logger.Errorf("GetRole err = %s \n", err)
  117. } else {
  118. name = role.RoleName
  119. cache.Redis.Set(key, name, -1)
  120. }
  121. }
  122. return name
  123. }
  124. func (s *roleService) GetTree() ([]model.RoleDetail, error) {
  125. roleDao := dao.Role{ParentId: 0}
  126. var results []model.RoleDetail
  127. var temp model.RoleDetail
  128. //所有role包装为roleDetail
  129. roles, err := roleDao.GetAll()
  130. var details []model.RoleDetail
  131. for _, role := range roles {
  132. temp.ID = role.ID
  133. temp.ParentId = role.ParentId
  134. temp.Title = role.RoleName
  135. details = append(details, temp)
  136. }
  137. topRoles, err := roleDao.GetByParentId()
  138. for _, v := range topRoles {
  139. temp.ID = v.ID
  140. temp.ParentId = v.ParentId
  141. temp.Title = v.RoleName
  142. temp.Children, temp.HasChildren = s.getChildren(temp, details)
  143. results = append(results, temp)
  144. }
  145. return results, err
  146. }
  147. func (s *roleService) getChildren(parent model.RoleDetail,
  148. all []model.RoleDetail) ([]model.RoleDetail, bool) {
  149. pId := parent.ID
  150. var children []model.RoleDetail
  151. for _, v := range all {
  152. if pId == v.ParentId {
  153. v.Children, v.HasChildren = s.getChildren(v, all)
  154. children = append(children, v)
  155. }
  156. }
  157. return children, children != nil
  158. }