import { applyAssistTicket } from "../../../domains/assist-platform/services.js"; import { isDryRun, mutationPreview, requireMutationConfirmation } from "../../mutation.js"; import { numberOption, stringOption } from "../../parser.js"; import { printJson, printKeyValue } from "../../printer.js"; import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js"; import { resolveAssistClient } from "./common.js"; export async function handleApply( parsed: CliParsedArgs, runtime: CliRuntime ): Promise { const client = resolveAssistClient(parsed, runtime); const corpId = parsed.args[0] || stringOption(parsed.options, ["corp", "corpid", "id"]) || runtime.selectedCorp?.id; if (!corpId) { return { stdout: "", stderr: "用法: hose-use assist-platform apply <企业ID> [--message <事由>] [--hours <小时数(1-24)>] [--dry-run/--yes]\n示例: hose-use assist-platform apply ID01KrLMQJWdMH --message \"测试环境接口联调\" --hours 24 --yes\n", exitCode: 1, }; } const days = numberOption(parsed.options, ["days", "day"]); let hours = numberOption(parsed.options, ["hours", "hour", "duration"]); if (days !== undefined) { hours = days * 24; } if (hours === undefined) { hours = 24; } if (hours < 1 || hours > 24) { return { stdout: "", stderr: `[ERROR] 申请协助时长不能超过 24 小时 (1 天),当前传入: ${days !== undefined ? `${days} 天 (${hours} 小时)` : `${hours} 小时`}。\n` + `提示: 合思协助平台服务端(POST /api/assistPlatform/create)实施了硬性安全限制:单次协助工单最长有效期限为 24 小时 (1天),超过 24 小时服务端将直接拒绝受理。\n` + `如需持续协助支持,可在工单到期后再次快速发起或在端内续签。\n`, exitCode: 1, }; } const message = stringOption(parsed.options, ["message", "msg", "reason", "matter"]) || "测试环境接口联调与问题排查"; const targetCorp = runtime.corpContext?.getAllCorps?.()?.find((c) => c.id === corpId || c.corpId === corpId) || (runtime.selectedCorp?.id === corpId || runtime.selectedCorp?.corpId === corpId ? runtime.selectedCorp : undefined); const corpName = targetCorp?.name || corpId; const nextConfirmCmd = `hose-use assist-platform apply ${corpId} --message "${message}" --hours ${hours} --yes`; // 写操作两阶段确认守卫 if (isDryRun(parsed, runtime)) { return mutationPreview( parsed, runtime, "发起合思协助工单申请", `${corpName} (${corpId})`, { 申请事项: message, 申请时长: `${hours} 小时 (${hours * 3600 * 1000} ms)`, 初始状态: "待提供 (需企业管理员在端内开启授权)", } ); } requireMutationConfirmation( parsed, runtime, `向企业 [${corpName}] 发起协助授权申请 (时长: ${hours}h, 事由: ${message})`, nextConfirmCmd ); const res = await applyAssistTicket(client, { corporationId: corpId, applyMessage: message, assistTime: hours * 3600 * 1000, }); if (runtime.jsonMode) { return { stdout: printJson(res, runtime.outputOptions), stderr: "", exitCode: 0, }; } const pairs: Record = { 企业名称: corpName, 企业ID: res.corporationId, 新建工单ID: res.id, 工单状态: "🟡 待提供 (等待客户管理员在企业端授权)", 申请时长: `${hours} 小时`, 申请事项: res.applyMessage, 申请人: res.userName || "运营实施人员", }; return { stdout: `====== 合思协助工单申请提交成功 ======\n${printKeyValue(pairs)}\n\n后续步骤:\n 1. 提示企业管理员登录合思 Web/桌面端,在【远程协助/授权中心】确认开启授权;\n 2. 授权通过后工单变为【🟢 生效中】,即可运行: hose-use assist-platform enter ${corpId} --staff <员工姓名> --save --yes\n`, stderr: "", exitCode: 0, }; }