|
@@ -370,8 +370,8 @@ func (this *SysDepartmentController) DepartmentUserTree() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 构建分组树
|
|
|
- groupTree := buildGroupTree(groups, groupAdmins, keywords)
|
|
|
+ // 构建分组树(以前这里还有个keyword匹配,不知道用作干嘛的,先去掉吧)
|
|
|
+ groupTree := buildGroupTree(groups, groupAdmins)
|
|
|
|
|
|
// 部门
|
|
|
list := make([]*system.DepartmentUserTree, 0)
|
|
@@ -400,64 +400,57 @@ func (this *SysDepartmentController) DepartmentUserTree() {
|
|
|
br.Data = list
|
|
|
}
|
|
|
|
|
|
-// 构建分组树
|
|
|
-func buildGroupTree(groups []*system.SysFullGroup, groupAdmins map[int][]*system.DepartmentUserTree, keywords string) map[int][]*system.DepartmentUserTree {
|
|
|
+// buildGroupTree 构建分组树
|
|
|
+func buildGroupTree(groups []*system.SysFullGroup, groupAdmins map[int][]*system.DepartmentUserTree) map[int][]*system.DepartmentUserTree {
|
|
|
+ // 创建分组ID到节点的映射
|
|
|
groupMap := make(map[int]*system.DepartmentUserTree)
|
|
|
+ idList := make([]int, 0, len(groups))
|
|
|
|
|
|
- // 第一次遍历:构建所有分组节点
|
|
|
- for _, group := range groups {
|
|
|
- if _, exists := groupMap[group.GroupId]; !exists {
|
|
|
- groupMap[group.GroupId] = &system.DepartmentUserTree{
|
|
|
- NodeId: group.GroupId,
|
|
|
- NodeName: group.GroupName,
|
|
|
- NodeType: 2,
|
|
|
- Children: make([]*system.DepartmentUserTree, 0),
|
|
|
- }
|
|
|
+ // 初始化所有节点,不管父子顺序
|
|
|
+ for _, g := range groups {
|
|
|
+ idList = append(idList, g.GroupId)
|
|
|
+ node := &system.DepartmentUserTree{
|
|
|
+ NodeId: g.GroupId,
|
|
|
+ NodeType: 2,
|
|
|
+ NodeName: g.GroupName,
|
|
|
+ Children: make([]*system.DepartmentUserTree, 0),
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- // 第二次遍历:将子节点挂载到父节点上
|
|
|
- for _, group := range groups {
|
|
|
- if group.ParentId == 0 {
|
|
|
- continue
|
|
|
- }
|
|
|
- if parent, exists := groupMap[group.ParentId]; exists {
|
|
|
- parent.Children = append(parent.Children, groupMap[group.GroupId])
|
|
|
+ // 添加该分组的管理员
|
|
|
+ if admins, ok := groupAdmins[g.GroupId]; ok {
|
|
|
+ node.Children = append(node.Children, admins...)
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- // 添加用户到分组
|
|
|
- for groupId, users := range groupAdmins {
|
|
|
- if group, exists := groupMap[groupId]; exists {
|
|
|
- group.Children = append(group.Children, users...)
|
|
|
- }
|
|
|
+ groupMap[g.GroupId] = node
|
|
|
}
|
|
|
|
|
|
- // 过滤关键词
|
|
|
- filteredGroupTree := make(map[int][]*system.DepartmentUserTree)
|
|
|
- for _, group := range groupMap {
|
|
|
- if keywords != "" {
|
|
|
- // 如果关键词不为空,仅保留符合条件的分组及其子节点
|
|
|
- if strings.Contains(strings.ToLower(group.NodeName), strings.ToLower(keywords)) {
|
|
|
- filteredGroupTree[group.NodeId] = []*system.DepartmentUserTree{group}
|
|
|
- } else {
|
|
|
- // 检查子节点是否包含关键词
|
|
|
- hasMatchingChild := false
|
|
|
- for _, child := range group.Children {
|
|
|
- if strings.Contains(strings.ToLower(child.NodeName), strings.ToLower(keywords)) {
|
|
|
- hasMatchingChild = true
|
|
|
- break
|
|
|
- }
|
|
|
- }
|
|
|
- if hasMatchingChild {
|
|
|
- filteredGroupTree[group.NodeId] = []*system.DepartmentUserTree{group}
|
|
|
- }
|
|
|
+ // 构建父子关系
|
|
|
+ //rootNodes := make([]*system.DepartmentUserTree, 0)
|
|
|
+
|
|
|
+ // 部门与分组的关系
|
|
|
+ groupTree := make(map[int][]*system.DepartmentUserTree)
|
|
|
+
|
|
|
+ for _, g := range groups {
|
|
|
+ node := groupMap[g.GroupId]
|
|
|
+
|
|
|
+ if g.ParentId == 0 || g.ParentId == -1 {
|
|
|
+ // 如果是根节点
|
|
|
+ //rootNodes = append(rootNodes, node)
|
|
|
+ nodeList, ok := groupTree[g.DepartmentId]
|
|
|
+ if !ok {
|
|
|
+ nodeList = make([]*system.DepartmentUserTree, 0)
|
|
|
}
|
|
|
+ groupTree[g.DepartmentId] = append(nodeList, node)
|
|
|
} else {
|
|
|
- // 如果关键词为空,保留所有分组
|
|
|
- filteredGroupTree[group.NodeId] = []*system.DepartmentUserTree{group}
|
|
|
+ // 如果是非根节点,找到父节点并添加到父节点的Children中
|
|
|
+ parentNode, ok := groupMap[g.ParentId]
|
|
|
+ if !ok {
|
|
|
+ // 父节点不存在,继续处理下一个
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ parentNode.Children = append(parentNode.Children, node)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return filteredGroupTree
|
|
|
+ return groupTree
|
|
|
}
|