156 lines
4.2 KiB
TypeScript
156 lines
4.2 KiB
TypeScript
import { numberOption, stringOption } from "../parser.js";
|
|
import { printJson, printKeyValue, printTable } from "../printer.js";
|
|
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
|
import {
|
|
getStaffById,
|
|
listStaffsProtected,
|
|
searchStaffs,
|
|
DEFAULT_STAFF_LIST_LIMIT,
|
|
} from "../../domains/contact/tools.js";
|
|
|
|
export async function handleContactCommand(
|
|
parsed: CliParsedArgs,
|
|
runtime: CliRuntime
|
|
): Promise<CliResult> {
|
|
const corp = runtime.selectedCorp;
|
|
if (!corp) {
|
|
return {
|
|
stdout: "",
|
|
stderr: "未指定或未找到有效企业配置。请运行 'hose-use corps list' 或指定 '--corp=<id>'",
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
|
|
const action = parsed.action || parsed.args[0] || "list";
|
|
const limit = numberOption(parsed.options, ["limit", "l"]);
|
|
|
|
// 1. 搜索: hose-use contact search <keyword> 或 hose-use contact find <keyword>
|
|
if (action === "search" || action === "find") {
|
|
const keyword = parsed.args[0] || stringOption(parsed.options, ["keyword", "query", "k"]) || "";
|
|
if (!keyword.trim()) {
|
|
return {
|
|
stdout: "",
|
|
stderr: "用法: hose-use contact search <关键词 (姓名/邮箱/工号)>",
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
|
|
const results = await searchStaffs(corp, { keyword, limit });
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson({ count: results.length, keyword, staffs: results }, runtime.outputOptions),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
if (results.length === 0) {
|
|
return {
|
|
stdout: `未找到匹配关键词 "${keyword}" 的员工。\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const headers = ["姓名", "工号 / staffId", "邮箱", "手机号"];
|
|
const rows = results.map((s) => [
|
|
s.name || "-",
|
|
s.id,
|
|
s.email || "-",
|
|
s.cellphone || "-",
|
|
]);
|
|
|
|
return {
|
|
stdout: `====== 员工搜索结果 (关键词: "${keyword}", 找到 ${results.length} 人) ======\n${printTable(headers, rows)}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
// 2. 详情: hose-use contact get <staffId>
|
|
if (action === "get") {
|
|
const staffId = parsed.args[0] || stringOption(parsed.options, ["id", "staff-id"]);
|
|
if (!staffId) {
|
|
return {
|
|
stdout: "",
|
|
stderr: "用法: hose-use contact get <staffId>",
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
|
|
const staff = await getStaffById(corp, staffId);
|
|
if (!staff) {
|
|
return {
|
|
stdout: "",
|
|
stderr: `未找到员工档案: ${staffId}`,
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson(staff, runtime.outputOptions),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const pairs: Record<string, unknown> = {
|
|
"员工姓名": staff.name,
|
|
"工号/staffId": staff.id,
|
|
"电子邮箱": staff.email || "-",
|
|
"联系手机": staff.cellphone || "-",
|
|
"所属部门": staff.department || "-",
|
|
};
|
|
|
|
return {
|
|
stdout: `====== 员工详细档案 ======\n${printKeyValue(pairs)}\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
// 3. 列表采样: hose-use contact list (默认限流截断防爆)
|
|
if (action === "list") {
|
|
const isFull = runtime.outputOptions?.allowFull === true;
|
|
const result = await listStaffsProtected(corp, {
|
|
limit: limit ?? (isFull ? undefined : DEFAULT_STAFF_LIST_LIMIT),
|
|
allowFull: isFull,
|
|
});
|
|
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson(result, runtime.outputOptions),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const headers = ["姓名", "工号 / staffId", "邮箱", "手机号"];
|
|
const rows = result.staffs.map((s) => [
|
|
s.name || "-",
|
|
s.id,
|
|
s.email || "-",
|
|
s.cellphone || "-",
|
|
]);
|
|
|
|
let output = `====== 企业通讯录采样 (共 ${result.total} 人,当前显示 ${result.staffs.length} 条) ======\n`;
|
|
output += printTable(headers, rows) + "\n";
|
|
if (result.hint) {
|
|
output += `\n💡 提示: ${result.hint}\n`;
|
|
}
|
|
|
|
return {
|
|
stdout: output,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
return {
|
|
stdout: "",
|
|
stderr: `未知通讯录子命令: ${action}。支持的子命令: list, search <关键词>, get <staffId>`,
|
|
exitCode: 1,
|
|
};
|
|
}
|