fix: dingding多级部门同步 (#72)
This commit is contained in:
parent
d2158b35d7
commit
4180c3938e
|
@ -364,3 +364,23 @@ func InitCron() {
|
|||
}
|
||||
c.Start()
|
||||
}
|
||||
|
||||
func GroupListToTree(rootId string, groupList []*model.Group) *model.Group {
|
||||
// 创建空根节点
|
||||
rootGroup := &model.Group{SourceDeptId: rootId}
|
||||
rootGroup.Children = groupListToTree(rootGroup, groupList)
|
||||
return rootGroup
|
||||
}
|
||||
|
||||
func groupListToTree(rootGroup *model.Group, list []*model.Group) []*model.Group {
|
||||
children := make([]*model.Group, 0)
|
||||
for _, group := range list {
|
||||
if group.SourceDeptParentId == rootGroup.SourceDeptId {
|
||||
children = append(children, group)
|
||||
}
|
||||
}
|
||||
for _, group := range children {
|
||||
group.Children = groupListToTree(group, list)
|
||||
}
|
||||
return children
|
||||
}
|
||||
|
|
|
@ -26,32 +26,31 @@ func (d *DingTalkLogic) SyncDingTalkDepts(c *gin.Context, req interface{}) (data
|
|||
if err != nil {
|
||||
return nil, tools.NewOperationError(fmt.Errorf("转换钉钉部门数据失败:%s", err.Error()))
|
||||
}
|
||||
// 2.将部门这个数组进行拆分,一组是父ID为1的,一组是父ID不为1的
|
||||
var firstDepts []*model.Group
|
||||
var otherDepts []*model.Group
|
||||
|
||||
// 2.将远程数据转换成树
|
||||
deptTree := GroupListToTree(fmt.Sprintf("%s_1", config.Conf.DingTalk.Flag), depts)
|
||||
|
||||
// 3.根据树进行创建
|
||||
err = d.addDepts(deptTree.Children)
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 添加部门
|
||||
func (d DingTalkLogic) addDepts(depts []*model.Group) error {
|
||||
for _, dept := range depts {
|
||||
if dept.SourceDeptParentId == fmt.Sprintf("%s_1", config.Conf.DingTalk.Flag) {
|
||||
firstDepts = append(firstDepts, dept)
|
||||
} else {
|
||||
otherDepts = append(otherDepts, dept)
|
||||
}
|
||||
}
|
||||
|
||||
// 3.先写父ID为1的,再写父ID不为1的,因为数据库中需要获取父部门的ID,从远程过来的数据并没有这个ID,而父ID为1的则是固定根部门下的一级部门
|
||||
for _, dept := range firstDepts {
|
||||
err := d.AddDepts(dept)
|
||||
if err != nil {
|
||||
return nil, tools.NewOperationError(fmt.Errorf("DsyncDingTalkDepts添加根部门失败:%s", err.Error()))
|
||||
return tools.NewOperationError(fmt.Errorf("DsyncDingTalkDepts添加部门失败: %s", err.Error()))
|
||||
}
|
||||
if len(dept.Children) != 0 {
|
||||
err = d.addDepts(dept.Children)
|
||||
if err != nil {
|
||||
return tools.NewOperationError(fmt.Errorf("DsyncDingTalkDepts添加部门失败: %s", err.Error()))
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, dept := range otherDepts {
|
||||
err := d.AddDepts(dept)
|
||||
if err != nil {
|
||||
return nil, tools.NewOperationError(fmt.Errorf("DsyncDingTalkDepts添加其他部门失败:%s", err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddGroup 添加部门数据
|
||||
|
|
Loading…
Reference in New Issue