feat(datalink,platform): add ledger math audit, continuity check, and in-line declarative batch update
This commit is contained in:
1 parent
d3f7d3efbc
commit
cd0aa2bf41
8 files changed
+984
-26
No files matched your search
@@ -4,7 +4,9 @@ import {
|
||||
buildSafeRecordEditPayload,
|
||||
editRecord,
|
||||
fetchAllEntityRecords,
|
||||
filterRecordsByPredicate,
|
||||
getRecordDetail,
|
||||
parseSetClause,
|
||||
recomputeRecordLedger,
|
||||
toggleRecordState,
|
||||
type BatchEditTargetItem,
|
||||
@@ -137,20 +139,22 @@ async function handleBatchEdit(
|
||||
const resolved = await resolveAppAndEntity(parsed, runtime, appIndex, entityIndex);
|
||||
if (!resolved.entity) {
|
||||
throw new Error(
|
||||
"用法: hose-use platform record batch-edit <应用> <业务对象> (--ids=<id1,id2> | --filter=<expr>) (--data='<json>' | --file=<patch.json>) [--concurrency=8] [--dry-run] [--yes]"
|
||||
"用法: hose-use platform record batch-edit <应用> <业务对象> (--where='<expr>' | --ids=<id1,id2> | --filter=<expr>) (--set='<expr>' | --data='<json>' | --file=<patch.json>) [--concurrency=8] [--dry-run] [--yes]"
|
||||
);
|
||||
}
|
||||
|
||||
const idsOpt = stringOption(parsed.options, ["ids", "id"]);
|
||||
const filterBy = stringOption(parsed.options, ["filter", "filter-by", "filterBy"]);
|
||||
const whereClause = stringOption(parsed.options, ["where"]);
|
||||
const setClause = stringOption(parsed.options, ["set"]);
|
||||
const dataStr = stringOption(parsed.options, ["data", "form", "d"]);
|
||||
const filePath = stringOption(parsed.options, ["file", "f"]);
|
||||
const concurrency = numberOption(parsed.options, ["concurrency", "c"], 8);
|
||||
const activeOpt =
|
||||
parsed.options.active !== undefined ? booleanOption(parsed.options, ["active"], true) : undefined;
|
||||
|
||||
if (!dataStr && !filePath) {
|
||||
throw new Error("请通过 --data='<json>' 或 --file=<patch.json> 提供要批量更新的字段内容");
|
||||
if (!dataStr && !filePath && !setClause) {
|
||||
throw new Error("请通过 --set='字段=值'、--data='<json>' 或 --file=<patch.json> 提供要批量更新的字段内容");
|
||||
}
|
||||
|
||||
requireMutationConfirmation(
|
||||
@@ -170,7 +174,9 @@ async function handleBatchEdit(
|
||||
}
|
||||
|
||||
let uniformPatch: Record<string, unknown> | undefined;
|
||||
if (dataStr) {
|
||||
if (setClause) {
|
||||
uniformPatch = parseSetClause(setClause, resolved.entity);
|
||||
} else if (dataStr) {
|
||||
try {
|
||||
uniformPatch = JSON.parse(dataStr) as Record<string, unknown>;
|
||||
} catch {
|
||||
@@ -223,14 +229,18 @@ async function handleBatchEdit(
|
||||
throw new Error("未解析到有效的批量更新内容");
|
||||
}
|
||||
let recordIds = splitFields(idsOpt);
|
||||
if (recordIds.length === 0 && filterBy) {
|
||||
if (recordIds.length === 0 && (whereClause || filterBy)) {
|
||||
const page = await fetchAllEntityRecords(
|
||||
resolved.clients.ekbClient,
|
||||
resolved.corpId,
|
||||
resolved.entity.id,
|
||||
{ filterBy }
|
||||
);
|
||||
recordIds = page.data
|
||||
let records = page.data;
|
||||
if (whereClause) {
|
||||
records = filterRecordsByPredicate(records, whereClause, resolved.entity);
|
||||
}
|
||||
recordIds = records
|
||||
.map((r) => {
|
||||
const dl =
|
||||
r.dataLink && typeof r.dataLink === "object"
|
||||
@@ -241,7 +251,14 @@ async function handleBatchEdit(
|
||||
.filter(Boolean);
|
||||
}
|
||||
if (recordIds.length === 0) {
|
||||
throw new Error("请通过 --ids=<id1,id2,...> 或 --filter=<表达式> 指定要批量更新的目标记录");
|
||||
if (whereClause) {
|
||||
return {
|
||||
stdout: `未找到符合条件 [--where="${whereClause}"] 的记录,无需更新。\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
throw new Error("请通过 --where=<条件>、--ids=<id1,id2,...> 或 --filter=<表达式> 指定要批量更新的目标记录");
|
||||
}
|
||||
for (const id of recordIds) {
|
||||
targets.push({
|
||||
@@ -289,14 +306,23 @@ async function handleBatchEdit(
|
||||
item.error || "-",
|
||||
]);
|
||||
|
||||
const outputLines = [
|
||||
`====== 批量安全编辑业务对象记录 (${dryRun ? "Dry-Run 预览" : "已执行"}) ======`,
|
||||
`应用: ${resolved.platform.name} (${resolved.platform.id}) | 业务对象: ${resolved.entity.name} (${resolved.entity.id})`,
|
||||
`总计: ${results.length} | 成功/预览: ${succeeded} | 失败: ${failed}`,
|
||||
];
|
||||
if (uniformPatch) {
|
||||
outputLines.push(`应用变更 Patch: ${JSON.stringify(uniformPatch)}`);
|
||||
}
|
||||
outputLines.push(printTable(["#", "记录 ID", "状态", "合并后字段数", "错误信息"], rows));
|
||||
if (dryRun) {
|
||||
outputLines.push("");
|
||||
outputLines.push("💡 这是 Dry-Run 预演模式,数据未发生任何改变。如需真正执行修改,请追加 --yes(或 --confirm)参数。");
|
||||
}
|
||||
outputLines.push("");
|
||||
|
||||
return {
|
||||
stdout: [
|
||||
`====== 批量安全编辑业务对象记录 (${dryRun ? "Dry-Run 预览" : "已执行"}) ======`,
|
||||
`应用: ${resolved.platform.name} (${resolved.platform.id}) | 业务对象: ${resolved.entity.name} (${resolved.entity.id})`,
|
||||
`总计: ${results.length} | 成功/预览: ${succeeded} | 失败: ${failed}`,
|
||||
printTable(["#", "记录 ID", "状态", "合并后字段数", "错误信息"], rows),
|
||||
"",
|
||||
].join("\n"),
|
||||
stdout: outputLines.join("\n"),
|
||||
stderr: "",
|
||||
exitCode: failed > 0 ? 1 : 0,
|
||||
};
|
||||
@@ -309,7 +335,11 @@ export async function handleRecordAction(parsed: CliParsedArgs, runtime: CliRunt
|
||||
let entityIndex: number;
|
||||
let recordIdIndex: number;
|
||||
|
||||
if (action === "batch-edit" || action === "record-batch-edit") {
|
||||
if (
|
||||
action === "batch-edit" ||
|
||||
action === "record-batch-edit" ||
|
||||
action === "batch-update"
|
||||
) {
|
||||
return handleBatchEdit(parsed, runtime, 0, 1);
|
||||
}
|
||||
|
||||
@@ -318,6 +348,11 @@ export async function handleRecordAction(parsed: CliParsedArgs, runtime: CliRunt
|
||||
appIndex = 0;
|
||||
entityIndex = 1;
|
||||
recordIdIndex = 2;
|
||||
} else if (action === "update") {
|
||||
subAction = "update";
|
||||
appIndex = 0;
|
||||
entityIndex = 1;
|
||||
recordIdIndex = 2;
|
||||
} else {
|
||||
const firstArg = parsed.args[0]?.toLowerCase();
|
||||
if (
|
||||
@@ -330,6 +365,7 @@ export async function handleRecordAction(parsed: CliParsedArgs, runtime: CliRunt
|
||||
"edit",
|
||||
"update",
|
||||
"batch-edit",
|
||||
"batch-update",
|
||||
"recompute",
|
||||
"recalc",
|
||||
].includes(firstArg)
|
||||
@@ -346,7 +382,12 @@ export async function handleRecordAction(parsed: CliParsedArgs, runtime: CliRunt
|
||||
}
|
||||
}
|
||||
|
||||
if (subAction === "batch-edit") {
|
||||
if (subAction === "batch-edit" || subAction === "batch-update") {
|
||||
return handleBatchEdit(parsed, runtime, appIndex, entityIndex);
|
||||
}
|
||||
|
||||
const whereClause = stringOption(parsed.options, ["where"]);
|
||||
if ((subAction === "update" || subAction === "edit") && (whereClause || parsed.args.length <= recordIdIndex)) {
|
||||
return handleBatchEdit(parsed, runtime, appIndex, entityIndex);
|
||||
}
|
||||
|
||||
@@ -442,7 +483,8 @@ export async function handleRecordAction(parsed: CliParsedArgs, runtime: CliRunt
|
||||
}
|
||||
case "edit":
|
||||
case "update": {
|
||||
const form = parseJsonDataOption(parsed);
|
||||
const setClause = stringOption(parsed.options, ["set"]);
|
||||
const form = setClause ? parseSetClause(setClause, resolved.entity) : parseJsonDataOption(parsed);
|
||||
const activeOpt = parsed.options.active !== undefined ? booleanOption(parsed.options, ["active"], true) : undefined;
|
||||
const isRaw = booleanOption(parsed.options, ["raw"], false);
|
||||
|
||||
|
||||
Reference in new issue
Block a user