184 lines
4.4 KiB
Go
184 lines
4.4 KiB
Go
package organization
|
|
|
|
import (
|
|
orgModel "busniess-user-center/internal/models/organization"
|
|
"busniess-user-center/internal/repo"
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
)
|
|
|
|
func getOrgParentsWihtSelf(orgId uint, allOrgs []repo.Organization) (orgs []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)
|
|
}
|
|
}
|
|
|
|
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 := 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)
|
|
}
|
|
}
|
|
|
|
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 {
|
|
orgMap := make(map[uint]repo.Organization, 0)
|
|
|
|
for _, org := range allOrgs {
|
|
if org.Status == 1 {
|
|
orgMap[org.ID] = org
|
|
}
|
|
}
|
|
|
|
return orgMap
|
|
}
|
|
|
|
func getOrgChildrenWithSelf(orgId uint, allOrgs []repo.Organization) (orgs []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)
|
|
}
|
|
}
|
|
|
|
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) {
|
|
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)
|
|
}
|
|
}
|
|
|
|
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, // 记录创建时间
|
|
}
|
|
}
|
|
|
|
func (o *organizationService) verfyUsers(ctx context.Context, accounts []string) error {
|
|
for _, account := range accounts {
|
|
if _, err := o.userService.ExistUserByAccount(ctx, account); err != nil {
|
|
return fmt.Errorf("用户%s不存在", account)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|