refactor(cli): split commands/platform.ts into platform/{shared,app-info,records,record-actions,index}.ts

Verbatim code moves grouped by concern: app metadata & permissions,
record query + ledger audit, and record write actions (incl. batch
safe edit). No behavior change: tsc clean, 316 tests pass.
This commit is contained in:
gitea committed 2026-10-04 14:14:57 +08:00
1 parent c8450b9908
commit 6cb05ebc37
7 files changed
+317 -286

No files matched your search

+131
View File
@@ -0,0 +1,131 @@
import {
auditEntityRecords,
buildKeywordFilter,
fetchAllEntityRecords,
searchEntityRecords,
selectRecordFields,
} from "../../../domains/datalink/services.js";
import { booleanOption, numberOption, stringOption } from "../../parser.js";
import { printJson, printTable, printKeyValue } from "../../printer.js";
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
import { cell, resolveAppAndEntity, splitFields } from "./shared.js";
export async function handleRecords(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const resolved = await resolveAppAndEntity(parsed, runtime);
if (!resolved.entity) throw new Error("请提供业务对象 ID、编码或名称: hose-use platform records <应用> <业务对象>");
const keyword = stringOption(parsed.options, ["keyword", "q", "query"]);
const filterBy = stringOption(parsed.options, ["filter", "filter-by", "filterBy"]) || (keyword ? buildKeywordFilter(keyword) : undefined);
const fetchAll = booleanOption(parsed.options, ["all", "a"], false);
const start = numberOption(parsed.options, ["start", "offset"], 0);
const count = numberOption(parsed.options, ["count", "limit"], 20);
const pageSize = numberOption(parsed.options, ["page-size", "pageSize"], 500);
const fields = splitFields(stringOption(parsed.options, ["fields", "select"]));
const page = fetchAll
? await fetchAllEntityRecords(resolved.clients.ekbClient, resolved.corpId, resolved.entity.id, { filterBy, pageSize })
: await searchEntityRecords(resolved.clients.ekbClient, resolved.corpId, resolved.entity.id, { start, count, filterBy });
const data = fields.length ? page.data.map((item) => selectRecordFields(item, fields, resolved.entity!.fields)) : page.data;
const result = {
platform: resolved.platform,
entity: resolved.entity,
query: { start: fetchAll ? 0 : start, count: fetchAll ? data.length : count, all: fetchAll, filterBy, fields },
total: page.total,
returned: data.length,
data,
};
if (runtime.jsonMode) return { stdout: printJson(result, runtime.outputOptions), stderr: "", exitCode: 0 };
const nameField = resolved.entity.fields.find((f) => f.label === "名称" || f.label.includes("名称") || f.name.endsWith("_name"));
const codeField = resolved.entity.fields.find((f) => f.label === "编码" || f.label.includes("编码") || f.name.endsWith("_code"));
const rows = data.map((item, i): Array<string | number | boolean | null | undefined> => {
const source = item.dataLink && typeof item.dataLink === "object" && !Array.isArray(item.dataLink) ? (item.dataLink as Record<string, unknown>) : item;
const id = source.id ?? item.id;
const active = source.active ?? item.active;
const updateTime = source.updateTime ?? item.updateTime;
const nameVal = source.name ?? source["名称"] ?? (nameField ? source[nameField.name] : undefined) ?? source[`E_${resolved.entity!.id}_name`];
const codeVal = source.code ?? source["编码"] ?? (codeField ? source[codeField.name] : undefined) ?? source[`E_${resolved.entity!.id}_code`];
const formattedTime = typeof updateTime === "number" ? new Date(updateTime).toLocaleString("zh-CN", { hour12: false }) : cell(updateTime);
return [i + 1, cell(id), cell(nameVal), cell(codeVal), active === false ? "停用" : "启用", formattedTime];
});
return { stdout: `====== [${resolved.platform.name}] ${resolved.entity.name} 记录 (总数: ${page.total}, 当前: ${data.length}${fetchAll ? ", 全量导出" : ""}) ======\n${printTable(["#", "记录 ID", "名称", "编码", "状态", "更新时间"], rows)}\n`, stderr: "", exitCode: 0 };
}
export async function handleAudit(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
const resolved = await resolveAppAndEntity(parsed, runtime);
if (!resolved.entity) {
throw new Error("请提供业务对象 ID、编码或名称: hose-use platform audit <应用> <业务对象> [--check-fields=字段1,字段2] [--active-only]");
}
const checkFields = splitFields(stringOption(parsed.options, ["check-fields", "checkFields", "fields"]));
const activeOnly = booleanOption(parsed.options, ["active-only", "activeOnly"], false);
const filterBy = stringOption(parsed.options, ["filter", "filter-by", "filterBy"]) || (activeOnly ? "(active==true)" : undefined);
const pageSize = numberOption(parsed.options, ["page-size", "pageSize"], 500);
const page = await fetchAllEntityRecords(resolved.clients.ekbClient, resolved.corpId, resolved.entity.id, {
filterBy,
pageSize,
});
const report = auditEntityRecords(page.data, resolved.entity, {
checkFields,
activeOnly,
total: page.total,
});
const output = {
platform: { id: resolved.platform.id, name: resolved.platform.name, code: resolved.platform.code },
entity: { id: resolved.entity.id, name: resolved.entity.name, code: resolved.entity.code },
...report,
};
if (runtime.jsonMode) {
return { stdout: printJson(output, runtime.outputOptions), stderr: "", exitCode: 0 };
}
const issueRows = report.issues.map((issue, idx) => [
idx + 1,
issue.recordId,
issue.recordCode || "-",
issue.recordName || "-",
issue.active ? "🟢 启用" : "🔴 停用",
issue.type,
issue.fieldLabel,
issue.message,
]);
const patchRows = report.suggestedPatches.map((patch, idx) => [
idx + 1,
patch.id,
patch.code || "-",
patch.name || "-",
JSON.stringify(patch.form),
patch.reason,
]);
const outLines = [
`====== 台账数据健康度体检: [${resolved.platform.name}] ${resolved.entity.name} (${resolved.entity.id}) ======`,
printKeyValue({
"总记录数": report.totalRecords,
"本次体检记录数": report.auditedRecords,
"健康记录数": report.healthyRecords,
"异常记录数": report.affectedRecordCount,
"总问题项数": report.issueCount,
"关键字段缺失 (MISSING_FIELD)": report.summary.missingFieldCount,
"引用对象已停用 (DISABLED_REFERENCE)": report.summary.disabledReferenceCount,
"跨表同名维度待回填 (CROSS_LEDGER_MISSING_SYNC)": report.summary.crossLedgerMissingSyncCount,
"跨表同名维度不一致 (CROSS_LEDGER_MISMATCH)": report.summary.crossLedgerMismatchCount,
}),
"",
"── 异常诊断明细 ──",
issueRows.length
? printTable(["#", "记录 ID", "编码", "名称", "状态", "问题类型", "字段", "详细说明"], issueRows)
: "✅ 未发现任何字段缺失、停用引用或跨表维度不一致问题",
];
if (patchRows.length > 0) {
outLines.push("");
outLines.push(`── 💡 建议自动回填修复清单 (共 ${patchRows.length} 条记录,可通过 platform record batch-edit 执行) ──`);
outLines.push(printTable(["#", "记录 ID", "编码", "名称", "建议 Patch Form", "依据"], patchRows));
}
return {
stdout: `${outLines.join("\n")}\n`,
stderr: "",
exitCode: 0,
};
}