MappingPanel.tsx
6.06 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
import { Boxes, ChevronDown, ChevronRight, Link2, Plus, Search } from "lucide-react";
import { useCallback, useEffect, useMemo, useState } from "react";
import { Badge, Card, CardHeader, Empty, Spinner } from "@/components/ui";
import { api } from "@/lib/api";
import { go } from "@/lib/nav";
import { errText, onto } from "@/lib/onto";
import type { SmModel } from "@/lib/types";
import { toast } from "@/store/useAppStore";
/**
* 表 → 实体映射:数据源里的每张表映射到了哪个对象类型。
* 没映射的表可以一键建成对象(属性 = 列,并绑定这张表),之后在对象编辑器里微调。
*/
export function MappingPanel({ dsId, canEdit }: { dsId: number; canEdit: boolean }) {
const [models, setModels] = useState<SmModel[] | null>(null);
const [map, setMap] = useState<Record<string, { concept_id: string; name: string; status: string }[]>>({});
const [q, setQ] = useState("");
const [open, setOpen] = useState<Set<number>>(new Set());
const [busy, setBusy] = useState(0);
const load = useCallback(() => {
void api.sqlModels(dsId).then(setModels).catch(() => setModels([]));
void onto.mappings(dsId).then(setMap).catch(() => setMap({}));
}, [dsId]);
useEffect(load, [load]);
const shown = useMemo(() => (models || []).filter((m) => !q || `${m.name} ${m.description}`.includes(q)), [models, q]);
const mappedN = (models || []).filter((m) => map[m.physical_table || m.name]?.length).length;
const createObject = async (m: SmModel) => {
setBusy(m.id);
try {
const cols = m.columns || [];
const key = m.primary_key || cols.find((c) => c.name.toLowerCase() === "id")?.name || cols[0]?.name || "";
const label = cols.find((c) => /name|title|label|名称/i.test(c.name))?.name || "";
const props = cols.slice(0, 60).map((c) => ({
name: c.name, display_name: (c.description || "").slice(0, 40), description: c.description || "",
data_type: c.is_measure || /int|dec|double|float|real|numeric/i.test(c.data_type) ? "number"
: /date|time/i.test(c.data_type) ? "date" : "string",
is_key: c.name === key, is_dimension: !!c.is_dimension,
}));
const name = (m.description || "").split(/[,。,]/)[0].slice(0, 30) || m.name;
const c = await onto.createConcept({ kind: "object_type", name, synonyms: name !== m.name ? [m.name] : [],
description: m.description, spec: { properties: props, label_property: label } });
await onto.setBinding(c.id, { ds_id: dsId, target_kind: "table", ref: {
table: m.physical_table || m.name, key_column: cols.find((x) => x.name === key)?.physical_column || key,
label_column: cols.find((x) => x.name === label)?.physical_column || label,
columns: Object.fromEntries(cols.slice(0, 60).map((x) => [x.name, x.physical_column || x.name])) } });
toast("ok", `已建对象「${c.name}」并绑定到 ${m.name}`, "是草稿:在对象类型里补属性含义,再提交变更提案");
load();
} catch (e) { toast("error", "创建失败", errText(e)); } finally { setBusy(0); }
};
if (!models) return <Card className="p-4"><Spinner /></Card>;
return (
<Card>
<CardHeader title={`表 · ${models.length} 张 · 已映射 ${mappedN}`}
desc="每张表映射成一个对象类型(表 = 对象,行 = 实例,列 = 属性)。映射后才能被查询、遍历、写回。"
right={<div className="relative"><Search className="absolute left-2 top-2 h-3.5 w-3.5 text-muted" />
<input className="input h-8 w-52 pl-7" placeholder="搜表名 / 描述" value={q} onChange={(e) => setQ(e.target.value)} /></div>} />
{!models.length ? <Empty title="还没有内省过" desc="回到「数据源」点「内省」抓表结构。" /> : (
<div className="divide-y divide-line">
{shown.map((m) => {
const hit = map[m.physical_table || m.name] || [];
const isOpen = open.has(m.id);
return (
<div key={m.id}>
<div className="flex items-center gap-2 px-4 py-2 text-label">
<button className="text-muted" onClick={() => { const n = new Set(open); if (isOpen) n.delete(m.id); else n.add(m.id); setOpen(n); }}>
{isOpen ? <ChevronDown className="h-3.5 w-3.5" /> : <ChevronRight className="h-3.5 w-3.5" />}</button>
<span className="font-mono font-medium">{m.name}</span>
<span className="min-w-0 flex-1 truncate text-muted">{m.description || ""}</span>
<span className="w-14 text-right text-caption text-muted">{m.columns?.length || 0} 列</span>
{!m.enabled && <Badge>未启用</Badge>}
{hit.length ? hit.map((h) => (
<button key={h.concept_id} className="chip text-ok" onClick={() => go("b_object", { id: h.concept_id })}>
<Link2 className="h-3 w-3" />{h.name}</button>
)) : canEdit ? (
<button className="btn btn-sm" disabled={!!busy} onClick={() => void createObject(m)}>
{busy === m.id ? <Spinner className="h-3.5 w-3.5" /> : <Plus className="h-3.5 w-3.5" />}配置实体映射</button>
) : <Badge tone="bg-warn/10 text-warn">未映射</Badge>}
</div>
{isOpen && (
<div className="flex flex-wrap gap-1 bg-ink/[0.02] px-10 py-2">
{(m.columns || []).map((c) => (
<span key={c.id} className="rounded border border-line bg-surface px-1.5 py-0.5 font-mono text-caption" title={c.description}>
{c.name}<span className="ml-1 text-muted">{c.data_type}</span></span>
))}
</div>
)}
</div>
);
})}
</div>
)}
<p className="flex items-center gap-1.5 border-t border-line px-4 py-2 text-label text-muted"><Boxes className="h-3.5 w-3.5" />
也可以在「业务建模」里让 AI 一次把所有已启用的表提议成对象与关系。</p>
</Card>
);
}