fix: align admin behavior and Kratos integrations
This commit is contained in:
@@ -44,6 +44,30 @@ func (authorityDepartmentPO) TableName() string { return "sys_authority_departme
|
||||
func deptFromPO(po departmentPO) *biz.Department {
|
||||
return &biz.Department{ID: po.ID, CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, Name: po.Name, ParentID: po.ParentID, Ancestors: po.Ancestors, Sort: po.Sort, LeaderID: po.LeaderID, Status: po.Status}
|
||||
}
|
||||
|
||||
func (r *organizationRepo) attachDepartmentLeaders(ctx context.Context, departments []*biz.Department) error {
|
||||
leaderIDs := make([]uint, 0, len(departments))
|
||||
for _, department := range departments {
|
||||
if department.LeaderID != 0 {
|
||||
leaderIDs = append(leaderIDs, department.LeaderID)
|
||||
}
|
||||
}
|
||||
if len(leaderIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
var leaders []userPO
|
||||
if err := r.data.gormDB.WithContext(ctx).Where("id IN ?", leaderIDs).Find(&leaders).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
leaderByID := make(map[uint]*biz.User, len(leaders))
|
||||
for i := range leaders {
|
||||
leaderByID[leaders[i].ID] = baseBizUser(&leaders[i])
|
||||
}
|
||||
for _, department := range departments {
|
||||
department.Leader = leaderByID[department.LeaderID]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (r *organizationRepo) CreateDepartment(ctx context.Context, v *biz.Department) error {
|
||||
v.Ancestors = "0"
|
||||
if v.ParentID != 0 {
|
||||
@@ -99,7 +123,11 @@ func (r *organizationRepo) FindDepartment(ctx context.Context, id uint) (*biz.De
|
||||
if err := r.data.gormDB.WithContext(ctx).First(&po, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return deptFromPO(po), nil
|
||||
department := deptFromPO(po)
|
||||
if err := r.attachDepartmentLeaders(ctx, []*biz.Department{department}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return department, nil
|
||||
}
|
||||
func (r *organizationRepo) ListDepartments(ctx context.Context, name string) ([]*biz.Department, error) {
|
||||
var pos []departmentPO
|
||||
@@ -111,8 +139,17 @@ func (r *organizationRepo) ListDepartments(ctx context.Context, name string) ([]
|
||||
return nil, err
|
||||
}
|
||||
nodes := map[uint]*biz.Department{}
|
||||
items := make([]*biz.Department, 0, len(pos))
|
||||
for _, po := range pos {
|
||||
nodes[po.ID] = deptFromPO(po)
|
||||
item := deptFromPO(po)
|
||||
nodes[po.ID] = item
|
||||
items = append(items, item)
|
||||
}
|
||||
if err := r.attachDepartmentLeaders(ctx, items); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if name != "" {
|
||||
return items, nil
|
||||
}
|
||||
var roots []*biz.Department
|
||||
for _, po := range pos {
|
||||
|
||||
Reference in New Issue
Block a user