接口名称
部门管理API(租户令牌)- IFeishuTenantV3Departments
功能描述
飞书组织机构部门是指企业组织架构树上的某一个节点。当前接口使用租户令牌访问,适用于租户应用场景,提供完整的部门管理功能,包括部门创建、更新、删除、查询等操作。在部门内部,可添加用户作为部门成员,也可添加新的部门作为子部门。
参考文档
https://open.feishu.cn/document/server-docs/contact-v3/department/field-overview
函数列表
| 函数名称 | 功能描述 | 认证方式 | HTTP 方法 |
|---|---|---|---|
| CreateDepartmentAsync | 创建新部门 | 租户令牌 | POST |
| UpdatePartDepartmentAsync | 部分更新部门信息 | 租户令牌 | PATCH |
| UpdateDepartmentAsync | 更新部门信息 | 租户令牌 | PUT |
| UpdateDepartmentIdAsync | 更新部门自定义ID | 租户令牌 | PATCH |
| UnbindDepartmentChatAsync | 解绑部门群聊 | 租户令牌 | POST |
| DeleteDepartmentByIdAsync | 删除指定部门 | 租户令牌 | DELETE |
| GetDepartmentInfoByIdAsync | 获取单个部门信息 | 租户令牌 | GET |
| GetDepartmentsByIdsAsync | 批量获取部门信息 | 租户令牌 | GET |
| GetDepartmentsByParentIdAsync | 获取子部门列表 | 租户令牌 | GET |
| GetParentDepartmentsByIdAsync | 获取父部门信息 | 租户令牌 | GET |
函数详细内容
函数名称:创建部门
函数签名:
csharp
Task<FeishuApiResult<DepartmentCreateUpdateResult>?> CreateDepartmentAsync(
[Body] DepartmentCreateRequest departmentCreateRequest,
[Query("user_id_type")] string? user_id_type = "open_id",
[Query("department_id_type")] string? department_id_type = "open_department_id",
[Query("client_token")] string? client_token = null,
CancellationToken cancellationToken = default);认证:租户令牌
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| departmentCreateRequest | DepartmentCreateRequest | ✅ 必填 | 创建部门的请求体 |
| user_id_type | string | ⚪ 可选 | 用户ID类型,默认为"open_id" |
| department_id_type | string | ⚪ 可选 | 部门ID类型,默认为"open_department_id" |
| client_token | string | ⚪ 可选 | 幂等判断令牌,避免重复创建 |
响应:
json
{
"code": 0,
"msg": "success",
"data": {
"department": {
"department_id": "od-1234567890",
"name": "技术部",
"parent_department_id": "0",
"leader_user_id": "ou_1234567890",
"create_group_chat": true,
"status": {
"is_deleted": false
}
}
}
}说明:
- 在通讯录内创建一个新部门
- 部门名称不可包含斜杠(/),不能与存量部门名称重复
- 父部门ID为"0"时表示在根部门下创建
- client_token用于防止重复创建,建议使用UUID
代码示例:
javascript
const createDepartmentRequest = {
department_id: "tech_dept_001", // 自定义部门ID
name: "技术部",
i18n_name: {
zh_cn: "技术部",
en_us: "Technology Department"
},
parent_department_id: "0", // 根部门下创建
leader_user_id: "ou_1234567890", // 部门主管
order: "100", // 排序权重
create_group_chat: true, // 创建部门群
leaders: [
{
user_id: "ou_1234567891",
leader_type: 1 // 1:部门主管 2:部门分管
}
],
department_hrbps: ["ou_1234567892"], // HRBP
unit_ids: ["unit_001"] // 单位ID
};
const result = await feishuTenantV3Departments.createDepartmentAsync(
createDepartmentRequest,
"open_id",
"open_department_id",
"create-dept-" + Date.now() // client_token
);
if (result.code === 0) {
const dept = result.data.department;
console.log("部门创建成功:");
console.log(`- 部门ID: ${dept.department_id}`);
console.log(`- 部门名称: ${dept.name}`);
console.log(`- 父部门ID: ${dept.parent_department_id}`);
console.log(`- 是否创建群聊: ${dept.create_group_chat}`);
} else {
console.error("创建部门失败:", result.msg);
// 错误处理
switch (result.code) {
case 400:
console.log("请求参数错误,可能部门名称重复或包含非法字符");
break;
case 403:
console.log("权限不足,无法创建部门");
break;
default:
console.log("创建失败,请稍后重试");
}
}函数名称:部分更新部门信息
函数签名:
csharp
Task<FeishuApiResult<DepartmentCreateUpdateResult>?> UpdatePartDepartmentAsync(
[Path] string department_id,
[Body] DepartmentPartUpdateRequest departmentCreateRequest,
[Query("user_id_type")] string? user_id_type = "open_id",
[Query("department_id_type")] string? department_id_type = "open_department_id",
CancellationToken cancellationToken = default);认证:租户令牌
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| department_id | string | ✅ 必填 | 部门ID |
| departmentCreateRequest | DepartmentPartUpdateRequest | ✅ 必填 | 部分更新部门的请求体 |
| user_id_type | string | ⚪ 可选 | 用户ID类型,默认为"open_id" |
| department_id_type | string | ⚪ 可选 | 部门ID类型,默认为"open_department_id" |
响应:
json
{
"code": 0,
"msg": "success",
"data": {
"department": {
"department_id": "od-1234567890",
"name": "技术研发部",
"parent_department_id": "od-1234567891"
}
}
}说明:
- 更新指定部门的部分信息,仅更新显式传入的字段
- 支持更新名称、父部门、排序、负责人等字段
- 部门ID类型需要与department_id_type参数保持一致
代码示例:
javascript
// 只更新部门名称和排序
const partialUpdateRequest = {
name: "技术研发部",
i18n_name: {
zh_cn: "技术研发部",
en_us: "R&D Technology Department"
},
order: "200" // 调整排序
};
const result = await feishuTenantV3Departments.updatePartDepartmentAsync(
"tech_dept_001",
partialUpdateRequest,
"open_id",
"open_department_id"
);
if (result.code === 0) {
console.log("部门部分信息更新成功");
console.log(`新名称: ${result.data.department.name}`);
console.log(`排序: ${result.data.department.order}`);
} else {
console.error("部分更新失败:", result.msg);
}函数名称:更新部门信息
函数签名:
csharp
Task<FeishuApiResult<DepartmentUpdateResult>?> UpdateDepartmentAsync(
[Path] string department_id,
[Body] DepartmentUpdateRequest departmentCreateRequest,
[Query("user_id_type")] string? user_id_type = "open_id",
[Query("department_id_type")] string? department_id_type = "open_department_id",
CancellationToken cancellationToken = default);认证:租户令牌
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| department_id | string | ✅ 必填 | 部门ID |
| departmentCreateRequest | DepartmentUpdateRequest | ✅ 必填 | 更新部门的请求体 |
| user_id_type | string | ⚪ 可选 | 用户ID类型,默认为"open_id" |
| department_id_type | string | ⚪ 可选 | 部门ID类型,默认为"open_department_id" |
响应:
json
{
"code": 0,
"msg": "success",
"data": {
"department": {
"department_id": "od-1234567890",
"name": "技术研发部",
"parent_department_id": "od-1234567891",
"leader_user_id": "ou_1234567890"
}
}
}说明:
- 完整更新指定部门的信息
- 需要提供完整的部门信息,未提供的字段将被置空
- 适用于需要大规模调整部门信息的场景
代码示例:
javascript
const fullUpdateRequest = {
name: "技术研发部",
i18n_name: {
zh_cn: "技术研发部",
en_us: "R&D Technology Department"
},
parent_department_id: "od-1234567891", // 移动到新父部门下
leader_user_id: "ou_1234567890", // 更新部门主管
order: "150",
create_group_chat: false, // 不创建群聊
leaders: [
{
user_id: "ou_1234567891",
leader_type: 1
}
]
};
const result = await feishuTenantV3Departments.updateDepartmentAsync(
"tech_dept_001",
fullUpdateRequest,
"open_id",
"open_department_id"
);
if (result.code === 0) {
console.log("部门信息完整更新成功");
const dept = result.data.department;
console.log(`部门名称: ${dept.name}`);
console.log(`新父部门: ${dept.parent_department_id}`);
} else {
console.error("完整更新失败:", result.msg);
}函数名称:更新部门自定义ID
函数签名:
csharp
Task<FeishuNullDataApiResult?> UpdateDepartmentIdAsync(
[Path] string department_id,
[Body] DepartMentUpdateIdRequest departMentUpdateIdRequest,
[Query("department_id_type")] string? department_id_type = "open_department_id",
CancellationToken cancellationToken = default);认证:租户令牌
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| department_id | string | ✅ 必填 | 部门ID |
| departMentUpdateIdRequest | DepartMentUpdateIdRequest | ✅ 必填 | 更新部门ID的请求体 |
| department_id_type | string | ⚪ 可选 | 部门ID类型,默认为"open_department_id" |
响应:
json
{
"code": 0,
"msg": "success"
}说明:
- 更新部门的自定义ID,即department_id
- 新的部门ID需要符合飞书ID命名规范
- 更新后旧的department_id将不再有效
代码示例:
javascript
const updateIdRequest = {
department_id: "rd_tech_dept_001" // 新的自定义ID
};
const result = await feishuTenantV3Departments.updateDepartmentIdAsync(
"tech_dept_001", // 旧的部门ID
updateIdRequest,
"open_department_id"
);
if (result.code === 0) {
console.log("部门ID更新成功");
console.log("新ID: rd_tech_dept_001");
} else {
console.error("更新部门ID失败:", result.msg);
}函数名称:解绑部门群聊
函数签名:
csharp
Task<FeishuNullDataApiResult?> UnbindDepartmentChatAsync(
[Body] DepartmentRequest departmentRequest,
[Query("department_id_type")] string? department_id_type = "open_department_id",
CancellationToken cancellationToken = default);认证:租户令牌
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| departmentRequest | DepartmentRequest | ✅ 必填 | 部门请求体 |
| department_id_type | string | ⚪ 可选 | 部门ID类型,默认为"open_department_id" |
响应:
json
{
"code": 0,
"msg": "success"
}说明:
- 将指定部门的部门群转为普通群
- 解绑后部门成员仍可使用群聊,但群聊不再自动同步部门成员变化
- 适用于不再需要部门自动同步群聊的场景
代码示例:
javascript
const departmentRequest = {
department_id: "tech_dept_001"
};
const result = await feishuTenantV3Departments.unbindDepartmentChatAsync(
departmentRequest,
"open_department_id"
);
if (result.code === 0) {
console.log("部门群解绑成功,已转为普通群");
} else {
console.error("解绑失败:", result.msg);
}函数名称:删除部门
函数签名:
csharp
Task<FeishuNullDataApiResult?> DeleteDepartmentByIdAsync(
[Path] string department_id,
[Query("department_id_type")] string? department_id_type = "open_department_id",
CancellationToken cancellationToken = default);认证:租户令牌
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| department_id | string | ✅ 必填 | 部门ID |
| department_id_type | string | ⚪ 可选 | 部门ID类型,默认为"open_department_id" |
响应:
json
{
"code": 0,
"msg": "success"
}说明:
- 从通讯录中删除指定的部门
- 删除前请确保部门下没有子部门和用户
- 删除操作不可恢复,请谨慎操作
- 删除后部门群聊也会被删除
代码示例:
javascript
async function deleteDepartmentWithChecks(departmentId) {
// 先检查是否有子部门
const childrenResult = await feishuTenantV3Departments.getDepartmentsByParentIdAsync(
departmentId,
false,
1, // 只查1个,检查是否存在
null,
"open_id",
"open_department_id"
);
if (childrenResult.code === 0 && childrenResult.data.items.length > 0) {
console.log("该部门下还有子部门,请先删除子部门");
return false;
}
// 执行删除
const result = await feishuTenantV3Departments.deleteDepartmentByIdAsync(
departmentId,
"open_department_id"
);
if (result.code === 0) {
console.log("部门删除成功");
return true;
} else {
console.error("删除部门失败:", result.msg);
if (result.code === 400) {
console.log("可能是部门下还有用户或其他依赖");
}
return false;
}
}
deleteDepartmentWithChecks("tech_dept_001");函数名称:获取单个部门信息
函数签名:
csharp
Task<FeishuApiResult<GetDepartmentInfoResult>?> GetDepartmentInfoByIdAsync(
[Path] string department_id,
[Query("user_id_type")] string? user_id_type = "open_id",
[Query("department_id_type")] string? department_id_type = "open_department_id",
CancellationToken cancellationToken = default);认证:租户令牌
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| department_id | string | ✅ 必填 | 部门ID |
| user_id_type | string | ⚪ 可选 | 用户ID类型,默认为"open_id" |
| department_id_type | string | ⚪ 可选 | 部门ID类型,默认为"open_department_id" |
响应:
json
{
"code": 0,
"msg": "success",
"data": {
"department": {
"department_id": "od-1234567890",
"name": "技术部",
"parent_department_id": "0",
"leader_user_id": "ou_1234567890",
"leaders": [
{
"user_id": "ou_1234567890",
"leader_type": 1
}
],
"department_hrbps": ["ou_1234567891"],
"primary_member_count": 25,
"status": {
"is_deleted": false
},
"create_group_chat": true
}
}
}说明:
- 获取单个部门的详细信息
- 包含部门名称、ID、父部门、负责人、状态以及成员个数等信息
- 使用租户令牌可以获取所有部门的完整信息
代码示例:
javascript
const result = await feishuTenantV3Departments.getDepartmentInfoByIdAsync(
"tech_dept_001",
"open_id",
"open_department_id"
);
if (result.code === 0) {
const dept = result.data.department;
console.log("=== 部门详细信息 ===");
console.log(`部门ID: ${dept.department_id}`);
console.log(`部门名称: ${dept.name}`);
console.log(`父部门ID: ${dept.parent_department_id}`);
console.log(`部门主管: ${dept.leader_user_id}`);
console.log(`主要成员数量: ${dept.primary_member_count}`);
console.log(`是否已删除: ${dept.status.is_deleted}`);
console.log(`是否创建群聊: ${dept.create_group_chat}`);
// 显示负责人信息
if (dept.leaders && dept.leaders.length > 0) {
console.log("\n负责人列表:");
dept.leaders.forEach((leader, index) => {
const leaderType = leader.leader_type === 1 ? "部门主管" : "部门分管";
console.log(`${index + 1}. ${leader.user_id} (${leaderType})`);
});
}
// 显示HRBP信息
if (dept.department_hrbps && dept.department_hrbps.length > 0) {
console.log("\nHRBP列表:");
dept.department_hrbps.forEach((hrbp, index) => {
console.log(`${index + 1}. ${hrbp}`);
});
}
} else {
console.error("获取部门信息失败:", result.msg);
if (result.code === 404) {
console.log("部门不存在或已被删除");
}
}函数名称:批量获取部门信息
函数签名:
csharp
Task<FeishuApiResult<BatchGetDepartmentRequest>?> GetDepartmentsByIdsAsync(
[Query("department_ids")] string[] department_ids,
[Query("user_id_type")] string? user_id_type = "open_id",
[Query("department_id_type")] string? department_id_type = "open_department_id",
CancellationToken cancellationToken = default);认证:租户令牌
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| department_ids | string[] | ✅ 必填 | 部门ID数组 |
| user_id_type | string | ⚪ 可选 | 用户ID类型,默认为"open_id" |
| department_id_type | string | ⚪ 可选 | 部门ID类型,默认为"open_department_id" |
响应:
json
{
"code": 0,
"msg": "success",
"data": {
"departments": [
{
"department_id": "od-1234567890",
"name": "技术部",
"parent_department_id": "0",
"primary_member_count": 25
},
{
"department_id": "od-1234567891",
"name": "产品部",
"parent_department_id": "0",
"primary_member_count": 15
}
]
}
}说明:
- 批量获取多个部门的详细信息
- 最多支持50个部门ID
- 返回每个部门的基本信息和状态
代码示例:
javascript
// 批量获取部门信息
async function getBatchDepartmentInfo(departmentIds) {
if (!departmentIds || departmentIds.length === 0) {
console.log("部门ID列表不能为空");
return [];
}
const maxBatchSize = 50;
const allDepartments = [];
for (let i = 0; i < departmentIds.length; i += maxBatchSize) {
const batch = departmentIds.slice(i, i + maxBatchSize);
const result = await feishuTenantV3Departments.getDepartmentsByIdsAsync(
batch,
"open_id",
"open_department_id"
);
if (result.code === 0) {
allDepartments.push(...result.data.departments);
console.log(`批次 ${Math.floor(i / maxBatchSize) + 1}: 获取到 ${result.data.departments.length} 个部门`);
} else {
console.error(`批次查询失败:`, result.msg);
}
}
return allDepartments;
}
// 使用示例:分析部门成员分布
async function analyzeDepartmentMemberDistribution(departmentIds) {
const departments = await getBatchDepartmentInfo(departmentIds);
if (departments.length === 0) {
console.log("未获取到部门信息");
return;
}
// 按成员数量排序
departments.sort((a, b) => b.primary_member_count - a.primary_member_count);
console.log("=== 部门成员分布分析 ===");
console.log(`总部门数: ${departments.length}`);
const totalMembers = departments.reduce((sum, dept) => sum + dept.primary_member_count, 0);
console.log(`总成员数: ${totalMembers}`);
const avgMembers = Math.round(totalMembers / departments.length);
console.log(`平均部门人数: ${avgMembers}`);
console.log("\n=== 部门详情(按人数排序) ===");
departments.forEach((dept, index) => {
const percentage = ((dept.primary_member_count / totalMembers) * 100).toFixed(1);
console.log(`${index + 1}. ${dept.name} (${dept.primary_member_count}人, ${percentage}%)`);
console.log(` ID: ${dept.department_id}`);
console.log(` 父部门: ${dept.parent_department_id}`);
});
}
analyzeDepartmentMemberDistribution(["tech_dept_001", "product_dept_001", "sales_dept_001"]);函数名称:获取子部门列表
函数签名:
csharp
Task<FeishuApiPageListResult<GetDepartmentInfo>?> GetDepartmentsByParentIdAsync(
[Path] string department_id,
[Query("fetch_child")] bool fetch_child = false,
[Query("page_size")] int? page_size = 10,
[Query("page_token")] string? page_token = null,
[Query("user_id_type")] string? user_id_type = "open_id",
[Query("department_id_type")] string? department_id_type = "open_department_id",
CancellationToken cancellationToken = default);认证:租户令牌
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| department_id | string | ✅ 必填 | 父部门ID |
| fetch_child | bool | ⚪ 可选 | 是否递归获取子部门,默认false |
| page_size | int? | ⚪ 可选 | 分页大小,默认10,最大50 |
| page_token | string | ⚪ 可选 | 分页标记,首次请求不填 |
| user_id_type | string | ⚪ 可选 | 用户ID类型,默认为"open_id" |
| department_id_type | string | ⚪ 可选 | 部门ID类型,默认为"open_department_id" |
响应:
json
{
"code": 0,
"msg": "success",
"data": {
"items": [
{
"department_id": "od-1234567891",
"name": "前端团队",
"parent_department_id": "od-1234567890",
"primary_member_count": 8
}
],
"page_token": "next_page_token",
"has_more": true
}
}说明:
- 查询指定部门下的子部门列表
- 支持递归查询获取所有层级的子部门
- 支持分页查询,适合获取大量子部门
代码示例:
javascript
// 获取部门树结构
async function getDepartmentTree(rootDepartmentId, maxDepth = 3) {
async function getChildren(parentId, currentDepth = 0) {
if (currentDepth >= maxDepth) {
return [];
}
const result = await feishuTenantV3Departments.getDepartmentsByParentIdAsync(
parentId,
false, // 不递归,手动控制递归深度
50, // 获取更多子部门
null,
"open_id",
"open_department_id"
);
if (result.code !== 0) {
console.error(`获取部门 ${parentId} 的子部门失败:`, result.msg);
return [];
}
const children = result.data.items;
// 为每个子部门递归获取其子部门
for (const child of children) {
child.children = await getChildren(child.department_id, currentDepth + 1);
}
return children;
}
const tree = {
department_id: rootDepartmentId,
children: await getChildren(rootDepartmentId)
};
return tree;
}
// 打印部门树
function printDepartmentTree(node, indent = 0) {
const prefix = " ".repeat(indent);
const memberCount = node.primary_member_count || 0;
const leaderInfo = node.leader_user_id ? ` (主管: ${node.leader_user_id})` : "";
console.log(`${prefix}├─ ${node.name} (${memberCount}人)${leaderInfo}`);
if (node.children && node.children.length > 0) {
node.children.forEach(child => {
printDepartmentTree(child, indent + 1);
});
}
}
// 使用示例:获取并显示部门树
async function displayDepartmentTree(rootDeptId) {
console.log(`正在获取部门 ${rootDeptId} 的树结构...`);
const tree = await getDepartmentTree(rootDeptId, 3); // 最多3层深度
console.log("\n=== 部门树结构 ===");
printDepartmentTree(tree);
// 统计信息
function countDepartments(node) {
let count = 1; // 当前节点
if (node.children) {
node.children.forEach(child => {
count += countDepartments(child);
});
}
return count;
}
const totalDepts = countDepartments(tree);
console.log(`\n总部门数: ${totalDepts}`);
}
displayDepartmentTree("root_dept_001");函数名称:获取父部门信息
函数签名:
csharp
Task<FeishuApiPageListResult<GetDepartmentInfo>?> GetParentDepartmentsByIdAsync(
[Query("department_id")] string department_id,
[Query("page_size")] int? page_size = 10,
[Query("page_token")] string? page_token = null,
[Query("user_id_type")] string? user_id_type = "open_id",
[Query("department_id_type")] string? department_id_type = "open_department_id",
CancellationToken cancellationToken = default);认证:租户令牌
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| department_id | string | ✅ 必填 | 部门ID |
| page_size | int? | ⚪ 可选 | 分页大小,默认10,最大50 |
| page_token | string | ⚪ 可选 | 分页标记,首次请求不填 |
| user_id_type | string | ⚪ 可选 | 用户ID类型,默认为"open_id" |
| department_id_type | string | ⚪ 可选 | 部门ID类型,默认为"open_department_id" |
响应:
json
{
"code": 0,
"msg": "success",
"data": {
"items": [
{
"department_id": "od-1234567890",
"name": "技术部",
"parent_department_id": "0"
},
{
"department_id": "od-1234567891",
"name": "总公司",
"parent_department_id": "0"
}
],
"page_token": "next_page_token",
"has_more": false
}
}说明:
- 递归获取指定部门的父部门信息
- 从直接父部门一直追溯到根部门
- 返回结果按层级顺序排列
代码示例:
javascript
// 获取完整的部门路径
async function getDepartmentPath(departmentId) {
const result = await feishuTenantV3Departments.getParentDepartmentsByIdAsync(
departmentId,
50, // 获取更多父部门
null,
"open_id",
"open_department_id"
);
if (result.code !== 0) {
console.error("获取部门路径失败:", result.msg);
return null;
}
// 获取当前部门信息
const currentDeptResult = await feishuTenantV3Departments.getDepartmentInfoByIdAsync(
departmentId,
"open_id",
"open_department_id"
);
const parentDepartments = result.data.items || [];
const currentDepartment = currentDeptResult.code === 0 ? currentDeptResult.data.department : null;
// 构建完整路径:根部门 -> ... -> 当前部门
const fullPath = [...parentDepartments.reverse(), currentDepartment]
.filter(dept => dept) // 过滤空值
.map(dept => ({
id: dept.department_id,
name: dept.name,
memberCount: dept.primary_member_count || 0
}));
return fullPath;
}
// 显示部门路径
async function displayDepartmentPath(departmentId) {
console.log(`正在获取部门 ${departmentId} 的完整路径...`);
const path = await getDepartmentPath(departmentId);
if (!path || path.length === 0) {
console.log("未找到部门路径信息");
return;
}
console.log("\n=== 部门完整路径 ===");
path.forEach((dept, index) => {
const arrow = index === path.length - 1 ? "" : " → ";
const memberInfo = dept.memberCount > 0 ? ` (${dept.memberCount}人)` : "";
console.log(`${dept.name}${memberInfo}${arrow}`);
});
console.log(`\n路径层级: ${path.length} 层`);
console.log(`总成员数: ${path.reduce((sum, dept) => sum + dept.memberCount, 0)} 人`);
}
// 使用示例:分析多个部门的组织层级
async function analyzeOrganizationHierarchy(departmentIds) {
console.log("=== 组织层级分析 ===");
for (const deptId of departmentIds) {
const path = await getDepartmentPath(deptId);
if (path) {
const deptName = path[path.length - 1].name;
const level = path.length;
console.log(`${deptName}: 第${level}层`);
}
}
}
// 示例使用
displayDepartmentPath("tech_frontend_dept_001");
analyzeOrganizationHierarchy(["tech_frontend_dept_001", "product_design_dept_001", "sales_beijing_dept_001"]);