BindingEditor.tsx
26.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import { Database, Play, Trash2 } from "lucide-react";
import { useEffect, useMemo, useState } from "react";
import { Badge, Field, Spinner } from "@/components/ui";
import { api } from "@/lib/api";
import { errText, onto, type Binding, type Concept } from "@/lib/onto";
import type { DataSource, SmModel } from "@/lib/types";
import { toast, loadFailed } from "@/store/useAppStore";
import { ResultGrid, Select } from "@/components/kit";
import { DimensionList, ExprBuilder, FilterList } from "./MetricBuilder";
// 只有对象、关系、聚合函数需要绑定数据
const TARGET: Record<string, string> = { object_type: "table", function: "metric_expr", link_type: "join" };
const SOURCE_KINDS: [string, string, string][] = [
["table", "一张表", "对象的每一条就是表里的一行"],
["view", "多表组合", "主表再关联几张表,别的表的列写成「别名.列」;只能改主表的列"],
["filtered", "表的一部分", "一张表里满足条件的那些行(如在途的采购单)"],
["union", "多源合并", "几张同构的表合在一起,主键带各自的前缀;只读"],
["sql", "SQL 视图", "一条只读的 SELECT"],
["managed", "本体自建", "业务库里没有,由本系统建表保存(发布时自动建表)"],
];
/**
* 数据绑定:把概念落到某个数据源的表/列/表达式上。一个概念可以在多个数据源各绑一条。
* 下拉里的表和列来自数据源内省结果,不用手敲物理名。
* 对象的数据来源有几种(见 SOURCE_KINDS);关系可以是外键,也可以经中间表;指标可以统计一张表、一类对象或一个对象集。
*/
export function BindingEditor({ concept, canEdit, onChanged }: { concept: Concept; canEdit: boolean; onChanged: () => void }) {
const kind = TARGET[concept.kind];
const [dss, setDss] = useState<DataSource[]>([]);
const [dsId, setDsId] = useState<number>(concept.bindings[0]?.ds_id || 0);
const [models, setModels] = useState<SmModel[]>([]);
const existing = concept.bindings.find((b) => b.ds_id === dsId && b.target_kind === kind);
const [ref, setRef] = useState<any>(existing?.ref || {});
const [busy, setBusy] = useState(false);
const [test, setTest] = useState<any>(null);
useEffect(() => {
void api.datasources().then((d) => { setDss(d); if (!dsId && d[0]) setDsId(d[0].id); }).catch((e) => { setDss([]); loadFailed(e); });
}, []); // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
if (!dsId) return;
void api.sqlModels(dsId, true).then(setModels).catch((e) => { setModels([]); loadFailed(e); });
}, [dsId]);
useEffect(() => {
setRef(concept.bindings.find((b) => b.ds_id === dsId && b.target_kind === kind)?.ref || {});
setTest(null);
}, [concept.id, dsId]); // eslint-disable-line react-hooks/exhaustive-deps
const tableName = (m: SmModel) => m.physical_table || m.name;
const table = kind === "table" ? ref.table : ref.base_table;
const srcKind: string = ref.kind || "table";
const [sets, setSets] = useState<{ id: string; name: string }[]>([]);
useEffect(() => {
if (kind === "metric_expr") void onto.concepts({ kind: "function" })
.then((r) => setSets(r.filter((x) => x.category === "object_set").map((x) => ({ id: x.id, name: x.name })))).catch(() => undefined);
}, [kind]);
const colsOf = (t: string) => models.find((m) => tableName(m) === t)?.columns?.map((c: any) => c.physical_column || c.name) || [];
// 指标可以建在「一类对象」上(推荐:能用算出来的属性、每个属性都能当维度),也可以直接建在一张表上
const onObject = kind === "metric_expr" && !ref.base_set && (!!ref.base_object || !ref.base_table);
const onSet = kind === "metric_expr" && !!ref.base_set;
const [objProps, setObjProps] = useState<{ name: string; label: string }[]>([]);
useEffect(() => {
if (kind !== "metric_expr" || !ref.base_object) { setObjProps([]); return; }
let alive = true;
void onto.concepts({ kind: "object_type" }).then(async (all) => {
const hit = all.find((x) => x.id === ref.base_object || x.name === ref.base_object);
if (!hit) return;
const c = await onto.concept(hit.id);
if (alive) setObjProps(((c.spec?.properties || []) as any[]).map((p) => ({ name: p.name, label: p.display_name || p.name })));
}).catch(() => undefined);
return () => { alive = false; };
}, [kind, ref.base_object]);
const tableCols = useMemo(() => models.find((m) => tableName(m) === table)?.columns?.map((c: any) => c.physical_column || c.name) || [],
[models, table]);
const cols: string[] = onObject ? objProps.map((p) => p.name) : tableCols;
const labels: Record<string, string> = onObject ? Object.fromEntries(objProps.map((p) => [p.name, p.label])) : {};
// 关系两端的对象各绑在哪张表上:选关联列时直接从那张表的列里挑
const [objects, setObjects] = useState<{ id: string; name: string }[]>([]);
const [ends, setEnds] = useState<Record<"from" | "to", { name: string; table: string; key: string; cols: string[] } | null>>({ from: null, to: null });
useEffect(() => {
if (kind === "metric_expr") {
void onto.concepts({ kind: "object_type" }).then((r) => setObjects(r.map((x) => ({ id: x.id, name: x.name })))).catch((e) => { setObjects([]); loadFailed(e); });
}
}, [kind]);
useEffect(() => {
if (kind !== "join") return;
let alive = true;
const one = async (refId: string) => {
if (!refId) return null;
try {
const all = await onto.concepts({ kind: "object_type" });
const hit = all.find((x) => x.id === refId || x.name === refId);
if (!hit) return null;
const c = await onto.concept(hit.id);
const b = c.bindings.find((x) => x.target_kind === "table");
const t = b?.ref.table || "";
const ms = b ? await api.sqlModels(b.ds_id, true) : [];
const m = ms.find((x) => (x.physical_table || x.name) === t);
return { name: c.name, table: t, key: b?.ref.key_column || "", cols: (m?.columns || []).map((x: any) => x.physical_column || x.name) };
} catch { return null; }
};
void Promise.all([one(concept.spec?.from_type), one(concept.spec?.to_type)]).then(([f, t]) => { if (alive) setEnds({ from: f, to: t }); });
return () => { alive = false; };
}, [kind, concept.spec?.from_type, concept.spec?.to_type]);
if (!kind) return null;
const save = async () => {
setBusy(true);
try {
await onto.setBinding(concept.id, { ds_id: srcKind === "managed" ? 0 : dsId, target_kind: kind, ref });
toast("ok", "已保存", "发布后才会生效");
onChanged();
} catch (e) { toast("error", "保存失败", errText(e)); } finally { setBusy(false); }
};
const remove = async (b: Binding) => {
await onto.deleteBinding(b.id);
onChanged();
};
const colSelect = (key: string, label: string, hint?: string) => (
<Field label={label} hint={hint}>
<Select className="input" value={ref[key] || ""} disabled={!canEdit} onChange={(e) => setRef({ ...ref, [key]: e.target.value })}>
<option value="">不设置</option>
{cols.map((c: string) => <option key={c}>{c}</option>)}
</Select>
</Field>
);
const props: string[] = (concept.spec?.properties || []).map((p: any) => p.name);
return (
<div className="space-y-3">
<div className="flex flex-wrap items-end gap-3">
<Field label="数据源" className="w-56">
<Select className="input" value={dsId} onChange={(e) => setDsId(Number(e.target.value))}>
{!dss.length && <option value={0}>还没有接入数据源</option>}
{dss.map((d) => <option key={d.id} value={d.id}>{d.name}</option>)}
</Select>
</Field>
{concept.bindings.filter((b) => b.ds_id !== dsId).map((b) => (
<Badge key={b.id} variant="ok">
<Database className="h-3 w-3" />{dss.find((d) => d.id === b.ds_id)?.name || "另一个数据源"}里也接了
{canEdit && <button onClick={() => void remove(b)} title="去掉"><Trash2 className="h-3 w-3" /></button>}
</Badge>
))}
</div>
{kind === "metric_expr" && (
<Field label="统计什么">
<div className="flex flex-wrap items-center gap-2">
<Select className="input w-40" value={onSet ? "set" : onObject ? "object" : "table"} disabled={!canEdit}
onChange={(e) => setRef(e.target.value === "object" ? { ...ref, base_table: "", base_set: "", base_object: ref.base_object || concept.spec?.object_type || "" }
: e.target.value === "set" ? { ...ref, base_table: "", base_object: "", base_set: ref.base_set || sets[0]?.id || "" }
: { ...ref, base_object: "", base_set: "", base_table: ref.base_table || tableName(models[0] || ({} as SmModel)) || "" })}>
<option value="object">一类对象</option><option value="set">一个对象集</option><option value="table">一张数据表</option>
</Select>
{onSet ? (
<Select className="input w-56" value={sets.find((o) => o.id === ref.base_set || o.name === ref.base_set)?.id || ""} disabled={!canEdit}
onChange={(e) => setRef({ ...ref, base_set: e.target.value })}>
<option value="">选对象集</option>
{sets.map((o) => <option key={o.id} value={o.id}>{o.name}</option>)}
</Select>
) : onObject ? (
<Select className="input w-56" value={objects.find((o) => o.id === ref.base_object || o.name === ref.base_object)?.id || ""} disabled={!canEdit}
onChange={(e) => setRef({ ...ref, base_object: e.target.value })}>
<option value="">选对象</option>
{objects.map((o) => <option key={o.id} value={o.id}>{o.name}</option>)}
</Select>
) : (
<Select className="input w-72" value={table || ""} disabled={!canEdit} onChange={(e) => setRef({ ...ref, base_table: e.target.value })}>
<option value="">选表</option>
{models.map((m) => <option key={m.id} value={tableName(m)}>{m.description ? `${m.description.slice(0, 20)}(${tableName(m)})` : tableName(m)}</option>)}
</Select>
)}
</div>
</Field>
)}
{kind === "table" && (
<Field label="数据从哪来" hint={SOURCE_KINDS.find((x) => x[0] === srcKind)?.[2]}>
<div className="flex flex-wrap gap-1.5">
{SOURCE_KINDS.map(([k, label]) => (
<button key={k} disabled={!canEdit} className={`chip ${srcKind === k ? "border-brand text-brand" : ""}`}
onClick={() => setRef(k === "managed" ? { kind: "managed" } : { ...ref, kind: k })}>{label}</button>
))}
</div>
</Field>
)}
{kind === "table" && srcKind === "managed" && (
<p className="rounded-md bg-ink/[0.03] px-3 py-2 text-label">这类对象由本系统保存:发布时按属性自动建表(以后加属性会自动补列,不会删列),
动作可以新建、修改、删除它,查询、指标、预警都照常能用。</p>
)}
{kind === "table" && srcKind === "sql" && (
<>
<Field label="SQL(只读,一条 SELECT)">
<textarea className="input min-h-[96px] font-mono text-label" value={ref.sql || ""} disabled={!canEdit} spellCheck={false}
onChange={(e) => setRef({ ...ref, sql: e.target.value })} />
</Field>
<div className="grid grid-cols-2 gap-3">
<Field label="主键是哪一列"><input className="input font-mono" value={ref.key_column || ""} disabled={!canEdit} onChange={(e) => setRef({ ...ref, key_column: e.target.value })} /></Field>
<Field label="名称是哪一列"><input className="input font-mono" value={ref.label_column || ""} disabled={!canEdit} onChange={(e) => setRef({ ...ref, label_column: e.target.value })} /></Field>
</div>
<Field label="每个属性对应 SQL 结果的哪一列">
<div className="grid gap-1.5 sm:grid-cols-2">
{props.map((p) => (
<div key={p} className="flex items-center gap-2"><span className="w-24 shrink-0 truncate text-label">{p}</span>
<input className="input h-8 font-mono" value={ref.columns?.[p] || ""} disabled={!canEdit}
onChange={(e) => setRef({ ...ref, columns: { ...(ref.columns || {}), [p]: e.target.value } })} /></div>
))}
</div>
</Field>
</>
)}
{kind === "table" && srcKind === "union" && (
<Field label="合并哪几张表" hint="每张表各写主键列、名称列、键前缀(区分同号对象)和属性对应的列">
<div className="space-y-2">
{(ref.branches || []).map((br: any, i: number) => {
const upB = (patch: any) => setRef({ ...ref, branches: ref.branches.map((x: any, j: number) => (j === i ? { ...x, ...patch } : x)) });
const bc = colsOf(br.table || "");
return (
<div key={i} className="space-y-1.5 rounded-lg border border-line p-2">
<div className="flex flex-wrap items-center gap-2">
<Select className="input h-8 w-56" value={br.table || ""} disabled={!canEdit} onChange={(e) => upB({ table: e.target.value })}>
<option value="">选表</option>{models.map((m) => <option key={m.id} value={tableName(m)}>{tableName(m)}</option>)}
</Select>
<Select className="input h-8 w-36" value={br.key_column || ""} disabled={!canEdit} onChange={(e) => upB({ key_column: e.target.value })}>
<option value="">主键列</option>{bc.map((c: string) => <option key={c}>{c}</option>)}</Select>
<Select className="input h-8 w-36" value={br.label_column || ""} disabled={!canEdit} onChange={(e) => upB({ label_column: e.target.value })}>
<option value="">名称列</option>{bc.map((c: string) => <option key={c}>{c}</option>)}</Select>
<input className="input h-8 w-24" placeholder="键前缀" value={br.prefix || ""} disabled={!canEdit} onChange={(e) => upB({ prefix: e.target.value })} />
{canEdit && <button className="rounded p-1 text-muted hover:text-bad" onClick={() => setRef({ ...ref, branches: ref.branches.filter((_: any, j: number) => j !== i) })}><Trash2 className="h-3.5 w-3.5" /></button>}
</div>
<div className="grid gap-1.5 sm:grid-cols-3">
{props.map((p) => (
<div key={p} className="flex items-center gap-1"><span className="w-20 shrink-0 truncate text-caption">{p}</span>
<Select className="input h-7" value={br.columns?.[p] || ""} disabled={!canEdit} onChange={(e) => upB({ columns: { ...(br.columns || {}), [p]: e.target.value } })}>
<option value="">无</option>{bc.map((c: string) => <option key={c}>{c}</option>)}</Select></div>
))}
</div>
</div>
);
})}
{canEdit && <button className="btn btn-sm" onClick={() => setRef({ ...ref, kind: "union", branches: [...(ref.branches || []), { prefix: "", columns: {} }] })}>加一张表</button>}
</div>
</Field>
)}
{kind === "table" && ["table", "view", "filtered"].includes(srcKind) && (
<Field label={srcKind === "view" ? "主表" : "对应的表"}>
<Select className="input" value={table || ""} disabled={!canEdit} onChange={(e) => setRef({ ...ref, table: e.target.value })}>
<option value="">选表</option>
{models.map((m) => <option key={m.id} value={tableName(m)}>{m.description ? `${m.description.slice(0, 24)}(${tableName(m)})` : tableName(m)}</option>)}
</Select>
</Field>
)}
{kind === "table" && srcKind === "filtered" && (
<Field label="只要满足这个条件的行" hint="用列名写,如 status in ['已下单', '运输中']、qty > 0">
<input className="input font-mono" value={ref.where || ""} disabled={!canEdit} onChange={(e) => setRef({ ...ref, where: e.target.value })} />
</Field>
)}
{kind === "table" && srcKind === "view" && table && (
<Field label="再关联哪些表" hint="关联后,属性可以写「别名.列」取别的表的列">
<div className="space-y-1.5">
{(ref.joins || []).map((j: any, i: number) => {
const upJ = (patch: any) => setRef({ ...ref, joins: ref.joins.map((x: any, k: number) => (k === i ? { ...x, ...patch } : x)) });
return (
<div key={i} className="flex flex-wrap items-center gap-2 text-label">
<input className="input h-8 w-20 font-mono" placeholder="别名" value={j.alias || ""} disabled={!canEdit} onChange={(e) => upJ({ alias: e.target.value })} />
<Select className="input h-8 w-48" value={j.table || ""} disabled={!canEdit} onChange={(e) => upJ({ table: e.target.value })}>
<option value="">选表</option>{models.map((m) => <option key={m.id} value={tableName(m)}>{tableName(m)}</option>)}</Select>
<span className="text-muted">主表的</span>
<Select className="input h-8 w-36" value={j.from_column || ""} disabled={!canEdit} onChange={(e) => upJ({ from_column: e.target.value })}>
<option value="">列</option>{tableCols.map((c: string) => <option key={c}>{c}</option>)}</Select>
<span className="text-muted">= 它的</span>
<Select className="input h-8 w-36" value={j.to_column || ""} disabled={!canEdit} onChange={(e) => upJ({ to_column: e.target.value })}>
<option value="">列</option>{colsOf(j.table || "").map((c: string) => <option key={c}>{c}</option>)}</Select>
{canEdit && <button className="rounded p-1 text-muted hover:text-bad" onClick={() => setRef({ ...ref, joins: ref.joins.filter((_: any, k: number) => k !== i) })}><Trash2 className="h-3.5 w-3.5" /></button>}
</div>
);
})}
{canEdit && <button className="btn btn-sm" onClick={() => setRef({ ...ref, joins: [...(ref.joins || []), { alias: `j${(ref.joins || []).length + 1}` }] })}>关联一张表</button>}
</div>
</Field>
)}
{kind === "table" && ["table", "view", "filtered"].includes(srcKind) && table && (
<>
<div className="grid grid-cols-2 gap-3">
{colSelect("key_column", "主键是哪一列")}
{colSelect("label_column", "名称是哪一列")}
</div>
<Field label="每个属性对应哪一列">
<div className="grid gap-1.5 sm:grid-cols-2">
{props.map((p) => (
<div key={p} className="flex items-center gap-2">
<span className="w-24 shrink-0 truncate text-label">{p}</span>
<Select className="input h-8" value={ref.columns?.[p] || ""} disabled={!canEdit}
onChange={(e) => setRef({ ...ref, columns: { ...(ref.columns || {}), [p]: e.target.value } })}>
<option value="">不对应</option>
{cols.map((c: string) => <option key={c}>{c}</option>)}
{srcKind === "view" && (ref.joins || []).flatMap((j: any) => colsOf(j.table || "").map((c: string) => `${j.alias}.${c}`))
.map((c: string) => <option key={c}>{c}</option>)}
</Select>
</div>
))}
{!props.length && <p className="text-label text-muted">先在「定义」里加属性</p>}
</div>
</Field>
</>
)}
{kind === "metric_expr" && (onSet ? !!ref.base_set : onObject ? !!ref.base_object : !!table) && (
<>
<Field label="怎么算">
<ExprBuilder value={ref.expression || ""} cols={cols} labels={labels} disabled={!canEdit}
onChange={(expression) => setRef((r: any) => ({ ...r, expression }))} />
</Field>
<Field label="只算哪些" hint="不加就是全部都算">
<FilterList value={ref.filters || []} cols={cols} labels={labels} disabled={!canEdit}
onChange={(filters) => setRef((r: any) => ({ ...r, filters }))} />
</Field>
<div className="grid grid-cols-2 gap-3">
<Field label="按哪个日期统计" hint="问「上周」「本月」时按它算;不随时间变的指标不用选">
<Select className="input" value={ref.time_column || ""} disabled={!canEdit} onChange={(e) => setRef({ ...ref, time_column: e.target.value })}>
<option value="">不设置</option>
{cols.map((c) => <option key={c} value={c}>{labels[c] || c}</option>)}
</Select>
</Field>
</div>
{onObject ? <p className="hint">这类对象的每个属性都可以用来拆开看,不用另外设置。</p> : (
<Field label="可以按什么拆开看">
<DimensionList dims={ref.dimensions || {}} types={ref.dimension_types || {}} cols={cols} objects={objects} disabled={!canEdit}
onChange={(dimensions, dimension_types) => setRef((r: any) => ({ ...r, dimensions, dimension_types }))} />
</Field>
)}
</>
)}
{kind === "join" && (
<Field label="怎么连">
<div className="flex gap-1.5">
<button disabled={!canEdit} className={`chip ${!ref.via_table ? "border-brand text-brand" : ""}`}
onClick={() => setRef({ from_column: ref.from_column || "", to_column: ref.to_column || "" })}>外键(两边各一列相等)</button>
<button disabled={!canEdit} className={`chip ${ref.via_table ? "border-brand text-brand" : ""}`}
onClick={() => setRef({ via_table: ref.via_table || tableName(models[0] || ({} as SmModel)) || "", via_from: "", via_to: "", props: {} })}>经中间表(多对多,可带属性)</button>
</div>
</Field>
)}
{kind === "join" && ref.via_table !== undefined && ref.via_table !== "" && (
<div className="grid gap-3 sm:grid-cols-3">
<Field label="中间表">
<Select className="input" value={ref.via_table || ""} disabled={!canEdit} onChange={(e) => setRef({ ...ref, via_table: e.target.value })}>
{models.map((m) => <option key={m.id} value={tableName(m)}>{tableName(m)}</option>)}</Select>
</Field>
<Field label={`哪一列指向「${ends.from?.name || "起点"}」`}>
<Select className="input" value={ref.via_from || ""} disabled={!canEdit} onChange={(e) => setRef({ ...ref, via_from: e.target.value })}>
<option value="">选一列</option>{colsOf(ref.via_table).map((c: string) => <option key={c}>{c}</option>)}</Select>
</Field>
<Field label={`哪一列指向「${ends.to?.name || "终点"}」`}>
<Select className="input" value={ref.via_to || ""} disabled={!canEdit} onChange={(e) => setRef({ ...ref, via_to: e.target.value })}>
<option value="">选一列</option>{colsOf(ref.via_table).map((c: string) => <option key={c}>{c}</option>)}</Select>
</Field>
<Field label="关系属性" hint="中间表里描述这条关系的列,写成 属性=列,逗号隔开" className="sm:col-span-3">
<input className="input font-mono" disabled={!canEdit} value={Object.entries(ref.props || {}).map(([k, v]) => `${k}=${v}`).join(",")}
onChange={(e) => setRef({ ...ref, props: Object.fromEntries(e.target.value.split(/[,,]/).map((x) => x.split("="))
.filter((x) => x.length === 2 && x[0].trim() && x[1].trim()).map(([k, v]) => [k.trim(), v.trim()])) })} />
</Field>
<p className="hint sm:col-span-3">两端默认按各自的主键对上;中间表里有一行,就有一条关系。</p>
</div>
)}
{kind === "join" && !ref.via_table && (
<div className="grid grid-cols-2 gap-3">
{(["from", "to"] as const).map((side) => {
const end = ends[side];
const key = `${side}_column`;
return (
<Field key={side} label={`${end ? `「${end.name}」` : side === "from" ? "起点对象" : "终点对象"}的哪一列`}
hint={end?.table ? undefined : "这个对象还没接数据,先去接"}>
{end?.cols.length ? (
<Select className="input" value={ref[key] || ""} disabled={!canEdit} onChange={(e) => setRef({ ...ref, [key]: e.target.value })}>
<option value="">选一列</option>
{ref[key] && !end.cols.includes(ref[key]) && <option value={ref[key]}>{ref[key]}</option>}
{end.cols.map((x) => <option key={x} value={x}>{x}{x === end.key ? "(主键)" : ""}</option>)}
</Select>
) : (
<input className="input" value={ref[key] || ""} disabled={!canEdit} onChange={(e) => setRef({ ...ref, [key]: e.target.value })} />
)}
</Field>
);
})}
<p className="col-span-2 hint">两边这两列的值相等,就算有这条关系。</p>
</div>
)}
<div className="flex flex-wrap gap-2">
{canEdit && <button className="btn btn-primary" disabled={busy || !dsId} onClick={() => void save()}>
{busy && <Spinner className="h-3.5 w-3.5" />}保存</button>}
{kind === "metric_expr" && (
<button className="btn" disabled={!existing} title={existing ? "" : "先保存"}
onClick={async () => {
try {
const dims = Object.keys(existing?.ref.dimensions || {}).slice(0, 1);
const r = await onto.metricQuery({ metric: concept.id, dimensions: dims, limit: 20, draft: true });
setTest(dims.length ? r : { columns: [(r as any).value_label || "结果"], rows: r.rows });
} catch (e) { toast("error", "算不出来", errText(e)); }
}}><Play className="h-3.5 w-3.5" />算一次看看</button>
)}
{concept.kind === "object_type" && (
<button className="btn" onClick={async () => {
try { setTest(await onto.queryObjects(concept.id, { limit: 10 })); }
catch (e) { toast("error", "读不到(对象要先发布)", errText(e)); }
}}><Play className="h-3.5 w-3.5" />看几条数据</button>
)}
</div>
{test && (
<div className="space-y-2">
<ResultGrid data={test} max={20} />
</div>
)}
</div>
);
}