import { formatFormPriorityList, getFieldDictionary, getBillByCodeOrId, hasLinkageOverridePriority, inspectFormSpecification, } from "../../../domains/bill/tools.js"; import { stringOption } from "../../parser.js"; import { printJson, printTable } from "../../printer.js"; import type { CliResult } from "../../types.js"; import type { BillActionContext } from "./context.js"; export async function handleSchema(ctx: BillActionContext): Promise { const { runtime, corp, corpId } = ctx; const { entities } = await getFieldDictionary(await ctx.getClient(), corpId, true); if (runtime.jsonMode) { return { stdout: printJson( { corpId, corpName: corp.name, totalEntities: entities.length, entities, }, runtime.outputOptions ), stderr: "", exitCode: 0, }; } const headers = ["#", "实体 ID", "实体名称", "类型", "字段数"]; const rows = entities.map((e, idx) => [ idx + 1, e.id, e.name || "-", e.type || "-", e.fields.length, ]); const tableOutput = printTable(headers, rows); const title = `====== 单据实体与数据模型字典 (企业: ${corp.name} | 共 ${entities.length} 个实体) ======\n`; return { stdout: title + tableOutput + "\n", stderr: "", exitCode: 0, }; } export async function handleLink(ctx: BillActionContext): Promise { const { parsed, runtime, corp, corpId } = ctx; const target = parsed.args[0] || stringOption(parsed.options, ["code", "id", "c"]); if (!target) { return { stdout: "", stderr: "用法: hose-use bill link <单号或ID>", exitCode: 1, }; } const items = await getBillByCodeOrId(await ctx.getClient(), corpId, target); if (!items || items.length === 0) { return { stdout: "", stderr: `未找到单据: ${target}`, exitCode: 1, }; } const item = items[0]; const billId = item.billId; const billCode = item.billCode || target; const baseUrl = (corp.baseUrl || "https://app.ekuaibao.com").replace(/\/+$/, ""); const webUrl = `${baseUrl}/#/invoice/detail?id=${encodeURIComponent(billId)}&corporationId=${encodeURIComponent(corpId)}`; if (runtime.jsonMode) { return { stdout: printJson( { code: billCode, id: billId, corpId, corpName: corp.name, url: webUrl, }, runtime.outputOptions ), stderr: "", exitCode: 0, }; } const text = [ `====== 合思单据 Web 直达链接 ======`, ` 单据编号: ${billCode}`, ` 单据 ID : ${billId}`, ` 所属企业: ${corp.name} (${corpId})`, ` 直达链接:`, ` ${webUrl}`, ``, `Markdown 链接: [查看单据 ${billCode}](${webUrl})`, ].join("\n"); return { stdout: text + "\n", stderr: "", exitCode: 0, }; } export async function handleSpec(ctx: BillActionContext): Promise { const { parsed, runtime, corp, corpId } = ctx; const target = parsed.args[0] || stringOption(parsed.options, ["id", "spec", "specification", "specification-id", "specificationId"]); if (!target) { throw new Error("请提供单据模板 ID (specificationId): hose-use bill spec "); } const inspection = await inspectFormSpecification(await ctx.getClient(), corpId, target); if (!inspection) { if (runtime.jsonMode) { return { stdout: printJson({ found: false, specificationId: target }, runtime.outputOptions), stderr: "", exitCode: 1, }; } return { stdout: "", stderr: `[NOT FOUND] 未找到单据模板定义: ${target}\n`, exitCode: 1, }; } const { specification: spec, warnings } = inspection; if (runtime.jsonMode) { return { stdout: printJson( { corpId, corpName: corp.name, ...spec, warnings, }, runtime.outputOptions ), stderr: "", exitCode: 0, }; } const rows = spec.components.map((comp, idx) => { const priorityStr = formatFormPriorityList(comp.priorityList); const overrideRisk = hasLinkageOverridePriority(comp.priorityList) ? "⚠️ 联动覆盖手动" : "-"; return [ idx + 1, comp.name, comp.label || "-", comp.type, comp.optional === false ? "🔴 必填" : "⚪ 选填", comp.isLinkageAssignment ? "🟢 开启(true)" : "⚪ 关闭(false)", comp.assignmentRule || "-", priorityStr, overrideRisk, ]; }); const outLines = [ `====== 单据模板定义 (${spec.name || spec.id}) ======`, ` 模板 ID : ${spec.id}`, ` 模板名称 : ${spec.name || "-"}`, ` 单据类型 : ${spec.type || "-"}`, ` 状态 : ${spec.active ? "🟢 启用" : "🔴 停用"}`, ` 字段总数 : ${spec.components.length} 个`, "", rows.length ? printTable( ["#", "字段标识 (name)", "显示名称 (label)", "类型 (type)", "必填", "联动赋值 (isLinkageAssignment)", "联动规则 (assignmentRule)", "赋值优先级 (priorityList)", "覆盖风险"], rows ) : "暂无表单组件定义", ]; if (warnings.length > 0) { outLines.push(""); outLines.push("── ⚠️ 联动赋值与优先级诊断 ──"); for (const w of warnings) { outLines.push(` • ${w}`); } } return { stdout: `${outLines.join("\n")}\n`, stderr: "", exitCode: 0, }; }