|
@@ -7,7 +7,7 @@ import (
|
|
|
"iot_manager_service/util/cache"
|
|
|
"iot_manager_service/util/common"
|
|
|
"iot_manager_service/util/logger"
|
|
|
- "strconv"
|
|
|
+ //"strconv"
|
|
|
)
|
|
|
|
|
|
// 角色管理服务
|
|
@@ -47,62 +47,6 @@ func (s *roleService) List(roleName, tenantId, roleAlias string, current, size i
|
|
|
return roles, counts, nil
|
|
|
}
|
|
|
|
|
|
-func (s *roleService) Tree() (*model.RspRoleTree, error) {
|
|
|
- role := &dao.Role{}
|
|
|
- roles, err := role.GetAll()
|
|
|
-
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
- rsp := &model.RspRoleTree{}
|
|
|
- //上级角色——多个下级角色数组
|
|
|
- tmp := make(map[int64][]dao.Role)
|
|
|
- for _, role := range roles {
|
|
|
- childes, isExist := tmp[role.ParentId]
|
|
|
- if isExist {
|
|
|
- childes = append(childes, role)
|
|
|
- tmp[role.ParentId] = childes
|
|
|
- } else {
|
|
|
- tmp[role.ParentId] = []dao.Role{role}
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if len(tmp) == 0 {
|
|
|
- return nil, nil
|
|
|
- }
|
|
|
- roots, _ := tmp[0]
|
|
|
- rsp.RoleDetails = dfs(rsp.RoleDetails, roots, tmp)
|
|
|
-
|
|
|
- return rsp, nil
|
|
|
-}
|
|
|
-
|
|
|
-func dfs(result []model.RoleDetail, roles []dao.Role, tmp map[int64][]dao.Role) []model.RoleDetail {
|
|
|
- if len(roles) == 0 {
|
|
|
- return nil
|
|
|
- }
|
|
|
- for _, child := range roles {
|
|
|
- id := strconv.FormatInt(child.ID, 10)
|
|
|
- parentId := strconv.FormatInt(child.ParentId, 10)
|
|
|
- detail := model.RoleDetail{
|
|
|
- ID: id,
|
|
|
- ParentId: parentId,
|
|
|
- Title: child.RoleName,
|
|
|
- Key: id,
|
|
|
- Value: id,
|
|
|
- }
|
|
|
- childes, isExist := tmp[child.ID]
|
|
|
- if isExist {
|
|
|
- detail.HasChildren = true
|
|
|
- detail.Children = dfs([]model.RoleDetail{}, childes, tmp)
|
|
|
- result = append(result, detail)
|
|
|
- } else {
|
|
|
- detail.HasChildren = false
|
|
|
- result = append(result, detail)
|
|
|
- }
|
|
|
- }
|
|
|
- return result
|
|
|
-}
|
|
|
-
|
|
|
func (s *roleService) Submit(req dao.Role) *common.Errors {
|
|
|
role := &req
|
|
|
if role.ID == 0 {
|
|
@@ -174,3 +118,40 @@ func (s *roleService) GetRoleName(tenantId int, roleId int64) string {
|
|
|
}
|
|
|
return name
|
|
|
}
|
|
|
+
|
|
|
+func (s *roleService) GetTree() ([]model.RoleDetail, error) {
|
|
|
+ roleDao := dao.Role{ParentId: int64(0)}
|
|
|
+ var results []model.RoleDetail
|
|
|
+ var temp model.RoleDetail
|
|
|
+ //所有role包装为roleDetail
|
|
|
+ roles, err := roleDao.GetAll()
|
|
|
+ var details []model.RoleDetail
|
|
|
+ for _, role := range roles {
|
|
|
+ temp.ID = role.ID
|
|
|
+ temp.ParentId = role.ParentId
|
|
|
+ temp.Title = role.RoleName
|
|
|
+ details = append(details, temp)
|
|
|
+ }
|
|
|
+ topRoles, err := roleDao.GetByParentId()
|
|
|
+ for _, v := range topRoles {
|
|
|
+ temp.ID = v.ID
|
|
|
+ temp.ParentId = v.ParentId
|
|
|
+ temp.Title = v.RoleName
|
|
|
+ temp.Children, temp.HasChildren = s.getChildren(temp, details)
|
|
|
+ results = append(results, temp)
|
|
|
+ }
|
|
|
+ return results, err
|
|
|
+}
|
|
|
+
|
|
|
+func (s *roleService) getChildren(parent model.RoleDetail,
|
|
|
+ all []model.RoleDetail) ([]model.RoleDetail, bool) {
|
|
|
+ pId := parent.ID
|
|
|
+ var children []model.RoleDetail
|
|
|
+ for _, v := range all {
|
|
|
+ if pId == v.ParentId {
|
|
|
+ v.Children, v.HasChildren = s.getChildren(v, all)
|
|
|
+ children = append(children, v)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return children, children != nil
|
|
|
+}
|