142 lines
3.4 KiB
Go
142 lines
3.4 KiB
Go
package util
|
|
|
|
import (
|
|
orgModel "busniess-user-center/internal/models/organization"
|
|
"busniess-user-center/internal/repo"
|
|
"sort"
|
|
)
|
|
|
|
func GetOrgParentsWihtSelf(orgId uint, allOrgs []*repo.Organization) (orgs []*repo.Organization) {
|
|
orgMap, _ := GetOrgMap(allOrgs)
|
|
orgs = GetParents(orgId, orgMap)
|
|
|
|
if org, ok := orgMap[orgId]; ok {
|
|
orgs = append(orgs, org)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func GetOrgParents(orgId uint, allOrgs []*repo.Organization) (orgs []*repo.Organization) {
|
|
orgMap, _ := GetOrgMap(allOrgs)
|
|
orgs = GetParents(orgId, orgMap)
|
|
return
|
|
}
|
|
|
|
func GetParents(orgId uint, orgParentMap map[uint]*repo.Organization) (orgs []*repo.Organization) {
|
|
org, ok := orgParentMap[orgId]
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
if org.ParentID == 0 {
|
|
return
|
|
}
|
|
|
|
parentOrg, ok := orgParentMap[org.ParentID]
|
|
if ok {
|
|
orgs = append(orgs, parentOrg)
|
|
orgs = append(orgs, GetParents(parentOrg.ID, orgParentMap)...)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func GetOrgMap(allOrgs []*repo.Organization) (map[uint]*repo.Organization, map[uint][]*repo.Organization) {
|
|
orgMap := make(map[uint]*repo.Organization, 0)
|
|
orgChildMap := make(map[uint][]*repo.Organization, 0)
|
|
for _, org := range allOrgs {
|
|
if org.Status == 1 {
|
|
orgMap[org.ID] = org
|
|
orgChildMap[org.ParentID] = append(orgChildMap[org.ParentID], org)
|
|
}
|
|
}
|
|
|
|
return orgMap, orgChildMap
|
|
}
|
|
|
|
func GetOrgChildrenWithSelf(orgId uint, allOrgs []*repo.Organization) (orgs []*repo.Organization) {
|
|
orgMap, orgChildMap := GetOrgMap(allOrgs)
|
|
orgs = GetChildren(orgId, orgChildMap)
|
|
|
|
if org, ok := orgMap[orgId]; ok {
|
|
orgs = append(orgs, org)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func GetOrgChildren(orgId uint, allOrgs []*repo.Organization) (orgs []*repo.Organization) {
|
|
_, orgChildMap := GetOrgMap(allOrgs)
|
|
orgs = GetChildren(orgId, orgChildMap)
|
|
|
|
return
|
|
}
|
|
|
|
func GetChildren(orgId uint, orgChildrenMap map[uint][]*repo.Organization) (orgs []*repo.Organization) {
|
|
if children, ok := orgChildrenMap[orgId]; ok {
|
|
for _, child := range children {
|
|
orgs = append(orgs, GetChildren(child.ID, orgChildrenMap)...)
|
|
orgs = append(orgs, child)
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func GetOrgTree(orgs []*repo.Organization) orgModel.OrgTree {
|
|
// top
|
|
topOrgs := []*orgModel.Organization{}
|
|
parentMap := make(map[uint][]*orgModel.Organization)
|
|
for _, org := range orgs {
|
|
mOrg := convertOrgDTM(org)
|
|
if org.ParentID == 0 {
|
|
topOrgs = append(topOrgs, mOrg)
|
|
} else {
|
|
if _, ok := parentMap[org.ParentID]; !ok {
|
|
parentMap[org.ParentID] = []*orgModel.Organization{mOrg}
|
|
} else {
|
|
parentMap[org.ParentID] = append(parentMap[org.ParentID], mOrg)
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, subOrgs := range parentMap {
|
|
for _, org := range subOrgs {
|
|
children := parentMap[org.ID]
|
|
sort.Slice(children, func(i, j int) bool {
|
|
return children[i].Sort < children[j].Sort
|
|
})
|
|
|
|
org.Children = children
|
|
}
|
|
}
|
|
|
|
for _, org := range topOrgs {
|
|
children := parentMap[org.ID]
|
|
sort.Slice(children, func(i, j int) bool {
|
|
return children[i].Sort < children[j].Sort
|
|
})
|
|
|
|
org.Children = children
|
|
}
|
|
|
|
sort.Slice(topOrgs, func(i, j int) bool {
|
|
return topOrgs[i].Sort < topOrgs[j].Sort
|
|
})
|
|
|
|
return topOrgs
|
|
}
|
|
|
|
func convertOrgDTM(org *repo.Organization) *orgModel.Organization {
|
|
return &orgModel.Organization{
|
|
ID: org.ID, // id
|
|
Name: org.Name, // 组织名
|
|
ParentID: org.ParentID, // 上级组织id
|
|
Sort: org.Sort, // 层级序号
|
|
Status: org.Status, // 状态:0-无效,1-有效
|
|
Path: org.Path, // 全路径
|
|
CreatedOn: org.CreatedOn, // 记录创建时间
|
|
}
|
|
}
|