forked from wangziqi/gongxue-base
fix: assign users to leaf departments only, handle multi-branch dept chains
This commit is contained in:
@@ -352,18 +352,28 @@ export class DingTalkService {
|
||||
};
|
||||
collectIds(deptTree);
|
||||
|
||||
// 3. 从每个部门拉用户,按 dept_id_list 分配
|
||||
// 3. 从每个部门拉用户,找到用户所属的叶子部门(支持多分支)
|
||||
const usersByDept = new Map<number, Array<{ userid: string; name: string; mobile: string; deptIds: number[] }>>();
|
||||
const seenUsers = new Set<string>();
|
||||
for (const did of allDeptIds) {
|
||||
const deptUsers = await this.getDeptUsers(token, did);
|
||||
for (const u of deptUsers) {
|
||||
for (const udid of u.dept_id_list) {
|
||||
if (!usersByDept.has(udid)) usersByDept.set(udid, []);
|
||||
usersByDept.get(udid)!.push({ userid: u.userid, name: u.name, mobile: u.mobile, deptIds: u.dept_id_list });
|
||||
if (seenUsers.has(u.userid)) continue;
|
||||
seenUsers.add(u.userid);
|
||||
// 找到所有叶子部门:链 [1,2,3,1,4,5] → 叶子 [3,5]
|
||||
const leafDepts: number[] = [];
|
||||
for (let i = 0; i < u.dept_id_list.length; i++) {
|
||||
const next = u.dept_id_list[i + 1];
|
||||
if (next === undefined || next <= u.dept_id_list[i]) {
|
||||
leafDepts.push(u.dept_id_list[i]);
|
||||
}
|
||||
}
|
||||
for (const ld of leafDepts) {
|
||||
if (!usersByDept.has(ld)) usersByDept.set(ld, []);
|
||||
usersByDept.get(ld)!.push({ userid: u.userid, name: u.name, mobile: u.mobile, deptIds: u.dept_id_list });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 递归挂用户到树节点
|
||||
const attachUsers = (node: OrgDeptNode): OrgDeptNodeWithUsers => ({
|
||||
id: node.id,
|
||||
|
||||
Reference in New Issue
Block a user