fix: assign each user to single leaf dept to prevent duplicates
This commit is contained in:
@@ -352,25 +352,19 @@ export class DingTalkService {
|
||||
};
|
||||
collectIds(deptTree);
|
||||
|
||||
// 3. 从每个部门拉用户,找到用户所属的叶子部门(支持多分支)
|
||||
// 3. 从每个部门拉用户,每人只挂到一个部门(dept_id_list 最后一个)
|
||||
const allDeptIdsSet = new Set(allDeptIds);
|
||||
const usersByDept = new Map<number, Array<{ userid: string; name: string; mobile: string; deptIds: number[] }>>();
|
||||
const seenUsers = new Set<string>();
|
||||
const placedUsers = new Set<string>();
|
||||
for (const did of allDeptIds) {
|
||||
const deptUsers = await this.getDeptUsers(token, did);
|
||||
for (const u of deptUsers) {
|
||||
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 });
|
||||
if (placedUsers.has(u.userid)) continue;
|
||||
const targetDept = u.dept_id_list[u.dept_id_list.length - 1];
|
||||
if (allDeptIdsSet.has(targetDept)) {
|
||||
placedUsers.add(u.userid);
|
||||
if (!usersByDept.has(targetDept)) usersByDept.set(targetDept, []);
|
||||
usersByDept.get(targetDept)!.push({ userid: u.userid, name: u.name, mobile: u.mobile, deptIds: u.dept_id_list });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user