index.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. import React, { useEffect, useMemo, useRef, useState } from "react";
  2. import {
  3. Layout,
  4. Menu,
  5. Button,
  6. Descriptions,
  7. Input,
  8. Tooltip,
  9. Empty,
  10. Spin,
  11. } from "antd";
  12. import { ProDescriptions } from "@ant-design/pro-components";
  13. import type { DescriptionsProps, MenuProps } from "antd";
  14. import { DownOutlined, PlusOutlined, SearchOutlined } from "@ant-design/icons";
  15. import TableEdit from "@/components/TableEdit";
  16. import ER from "./components/ER";
  17. import AddTable from "./components/AddTable";
  18. import { useModel, history, useRequest, useParams } from "umi";
  19. import { GetDataModelDetail } from "@/api";
  20. import NoData from "@/assets/no-data.png";
  21. import { ColumnItem, ProjectInfo, TableItemType } from "@/type";
  22. import { useFullscreen } from "ahooks";
  23. import AddModel from "@/components/AddModel";
  24. import insertCss from "insert-css";
  25. import LangInput from "@/components/LangInput";
  26. import LangInputTextarea from "@/components/LangInputTextarea";
  27. import { validateAliasName, validateTableCode } from "@/utils/validator";
  28. import SyncModal from "@/components/SyncModal";
  29. const { Content, Header } = Layout;
  30. export default function index() {
  31. const [active, setActive] = useState(0);
  32. const [showNavigator, setShowNavigator] = useState(true);
  33. const addTableRef = useRef<{ open: () => void }>();
  34. const {
  35. project,
  36. setProject,
  37. setPlayModeEnable,
  38. exitPlayMode,
  39. updateTable,
  40. graph,
  41. } = useModel("erModel");
  42. const [searchKeyword, setSearchKeyword] = useState("");
  43. const [selectKey, setSelectKey] = useState<string>(
  44. project.tables?.[0]?.table?.id || ""
  45. );
  46. const erRef = useRef<HTMLDivElement>(null);
  47. const addModelRef = useRef<{ edit: (info: ProjectInfo) => void }>();
  48. const syncModalRef = useRef<{ open: () => void }>();
  49. const [isFullscreen, { enterFullscreen, exitFullscreen }] =
  50. useFullscreen(erRef);
  51. const [collapsed, setCollapsed] = useState(false);
  52. const [seachColumn, setSearchColumn] = useState("");
  53. useEffect(() => {
  54. insertCss(`
  55. .ant-descriptions-header {
  56. margin-bottom: ${collapsed ? "0 !important" : "20px !important"};
  57. }
  58. `);
  59. }, [collapsed]);
  60. useEffect(() => {
  61. setPlayModeEnable(true);
  62. graph?.disableKeyboard();
  63. }, [project, graph]);
  64. const params = useParams();
  65. const { run, loading, refresh } = useRequest(GetDataModelDetail, {
  66. manual: true,
  67. onSuccess: (res) => {
  68. console.log("模型详情:", res);
  69. const result = res?.result;
  70. setSelectKey(result?.tables?.[0]?.table?.id || "");
  71. if (result) {
  72. setProject(result, false, true);
  73. }
  74. },
  75. });
  76. useEffect(() => {
  77. if (params?.id) {
  78. run({ id: params.id });
  79. }
  80. }, []);
  81. const descItems: DescriptionsProps["items"] = useMemo(() => {
  82. return [
  83. {
  84. key: "1",
  85. label: "模型名称",
  86. children: project?.name || "-",
  87. },
  88. {
  89. key: "2",
  90. label: "创建用户",
  91. children: project?.createdByName || "-",
  92. },
  93. {
  94. key: "3",
  95. label: "创建时间",
  96. children: project?.createTime || "-",
  97. },
  98. {
  99. key: "5",
  100. label: "发布状态",
  101. children: project?.publishStatus || "-",
  102. },
  103. {
  104. key: "4-1",
  105. label: "更新用户",
  106. children: project?.updateByName || "-",
  107. },
  108. {
  109. key: "4",
  110. label: "更新时间",
  111. children: project?.updateTime || "-",
  112. },
  113. {
  114. key: "6",
  115. label: "表数",
  116. children: `${project.tables.length}`,
  117. },
  118. {
  119. key: "7",
  120. label: "描述",
  121. children: project?.description || "-",
  122. },
  123. ];
  124. }, [project]);
  125. const tableData: MenuProps["items"] = useMemo(() => {
  126. const { tables } = project;
  127. const treeList: {
  128. key: string;
  129. label: string | React.ReactNode;
  130. icon: React.ReactNode;
  131. children?: MenuProps["items"];
  132. }[] = [];
  133. tables
  134. .filter((item) => {
  135. const tableName = item.table.langNameList?.find(
  136. (item) => item.name === "zh-CN"
  137. )?.value;
  138. return (
  139. item.table.schemaName.includes(searchKeyword) ||
  140. tableName?.includes(searchKeyword)
  141. );
  142. })
  143. .forEach((item) => {
  144. const name = item.table.langNameList?.find(
  145. (item) => item.name === "zh-CN"
  146. )?.value;
  147. const newItem = {
  148. key: item.table.id,
  149. label: item.table.schemaName + `${name ? `(${name})` : ""}`,
  150. icon: <i className="iconfont icon-biaogebeifen" />,
  151. };
  152. if (!item.table.parentBusinessTableId) {
  153. treeList.push(newItem);
  154. } else {
  155. const parent = treeList.find(
  156. (tableItem) => tableItem?.key === item.table.parentBusinessTableId
  157. );
  158. if (parent) {
  159. if (!parent?.children) {
  160. parent.children = [];
  161. }
  162. parent.children?.push(newItem);
  163. }
  164. }
  165. });
  166. return treeList;
  167. }, [project, searchKeyword]);
  168. const currentTable = useMemo(() => {
  169. return project.tables.find((item) => item.table.id === selectKey);
  170. }, [project, selectKey]);
  171. const currentColumns = useMemo(() => {
  172. return (currentTable?.tableColumnList || []).filter((item) => {
  173. if (seachColumn) {
  174. return item.langName?.includes(seachColumn);
  175. }
  176. return true;
  177. });
  178. }, [currentTable, seachColumn]);
  179. const handleAddTable = (tableItem: TableItemType) => {
  180. setProject({
  181. ...project,
  182. tables: [...project.tables, tableItem],
  183. });
  184. setSelectKey(tableItem.table.id);
  185. };
  186. const handleEnterFullscreen = () => {
  187. enterFullscreen();
  188. };
  189. const handleExitFullscreen = () => {
  190. exitFullscreen();
  191. };
  192. const handleSelectTable = (key: string) => {
  193. graph?.resetSelection(key);
  194. graph?.centerCell(graph.getCellById(key));
  195. setSelectKey(key);
  196. };
  197. const handleEnterEdit = () => {
  198. exitPlayMode();
  199. const enterpriseCode = sessionStorage.getItem("enterpriseCode");
  200. history.push(`/er/${project.id}?enterpriseCode=${enterpriseCode}`);
  201. };
  202. // 修改表格字段
  203. const handleChangeColumn = (columns: readonly ColumnItem[]) => {
  204. currentTable &&
  205. updateTable({
  206. ...currentTable,
  207. tableColumnList: [...columns],
  208. });
  209. };
  210. // 同步数据表
  211. const handleSync = () => {
  212. syncModalRef.current?.open();
  213. };
  214. const extra = (
  215. <div className="flex gap-12px">
  216. <a onClick={handleSync}>
  217. <i className="iconfont icon-tongbu text-12px" />
  218. 数据同步
  219. </a>
  220. <a onClick={() => project.id && addModelRef.current?.edit(project)}>
  221. <i className="iconfont icon-bianji text-12px" />
  222. 基础信息
  223. </a>
  224. <a onClick={handleEnterEdit}>
  225. <i className="iconfont icon-bianji text-12px" />
  226. 模型编辑
  227. </a>
  228. <a>
  229. <i className="iconfont icon-moban text-14px" />
  230. 保存为模板
  231. </a>
  232. </div>
  233. );
  234. return (
  235. <Spin spinning={loading}>
  236. {/* 基础信息修改弹窗 */}
  237. <AddModel
  238. ref={addModelRef}
  239. onChange={(info) => {
  240. typeof info === "object" && setProject(info);
  241. }}
  242. />
  243. {/* 同步弹窗 */}
  244. <SyncModal ref={syncModalRef} onPush={refresh}/>
  245. <Layout className="h-100vh flex flex-col bg-#fafafa p-12px">
  246. <Header
  247. className="shadow-sm"
  248. style={{
  249. backgroundColor: "#fff",
  250. padding: 16,
  251. height: "auto",
  252. borderRadius: 8,
  253. // boxShadow: "0 2px 4px rgba(0, 0, 0, 0.15)",
  254. marginBottom: 12,
  255. }}
  256. >
  257. <Descriptions
  258. title={
  259. <span>
  260. <svg className="iconfont w-14px h-14px m-r-4px">
  261. <use xlinkHref="#icon-shujumoxing" />
  262. </svg>
  263. {project?.name || "-"}
  264. <Button
  265. type="text"
  266. className={!collapsed ? "rotate-180" : ""}
  267. icon={<DownOutlined />}
  268. onClick={() => setCollapsed(!collapsed)}
  269. />
  270. </span>
  271. }
  272. items={collapsed ? [] : descItems}
  273. extra={extra}
  274. ></Descriptions>
  275. </Header>
  276. <Content className="flex-1 overflow-auto flex gap-12px">
  277. <div className="left w-300px shrink-0 h-full shadow-sm bg-#fff rounded-8px overflow-y-auto">
  278. <div
  279. className="
  280. flex
  281. justify-between
  282. header-tit
  283. p-x-16px
  284. p-y-12px
  285. text-16px
  286. font-bold
  287. text-rgba(0, 0, 0, 0.88)
  288. border-b-1px
  289. border-b-#eee
  290. border-b-solid"
  291. >
  292. <div>
  293. <svg className="iconfont w-14px h-14px m-r-4px">
  294. <use xlinkHref="#icon-biaoge" />
  295. </svg>
  296. <span>数据表</span>
  297. </div>
  298. <div>
  299. <Input
  300. placeholder="搜索"
  301. className="w-100px m-r-4px"
  302. suffix={<SearchOutlined />}
  303. value={searchKeyword}
  304. onChange={(e) => setSearchKeyword(e.target.value)}
  305. />
  306. <Tooltip title="添加数据表">
  307. <Button
  308. type="primary"
  309. icon={<PlusOutlined />}
  310. onClick={() => addTableRef.current?.open()}
  311. ></Button>
  312. </Tooltip>
  313. </div>
  314. </div>
  315. <Menu
  316. mode="inline"
  317. items={tableData}
  318. selectedKeys={[selectKey]}
  319. onSelect={({ key }) => handleSelectTable(key)}
  320. />
  321. {!tableData.length && (
  322. <Empty image={NoData} description="点击+添加数据表!" />
  323. )}
  324. </div>
  325. <div className="right flex-1 overflow-hidden h-full shadow-sm bg-#fff rounded-8px p-12px flex flex-col">
  326. <div className="head flex justify-between">
  327. <div className="left flex gap-8px">
  328. <Button
  329. type={active === 0 ? "primary" : "default"}
  330. onClick={() => setActive(0)}
  331. >
  332. ER图
  333. </Button>
  334. <Button
  335. type={active === 1 ? "primary" : "default"}
  336. onClick={() => setActive(1)}
  337. >
  338. 实体
  339. </Button>
  340. </div>
  341. <div className="right flex gap-8px m-b-12px">
  342. {active === 0 ? (
  343. <>
  344. {/* <Input placeholder="搜索" suffix={<SearchOutlined />} /> */}
  345. <Button
  346. type={showNavigator ? "primary" : "default"}
  347. onClick={() => setShowNavigator(!showNavigator)}
  348. icon={<i className="iconfont icon-xiaoditu text-14px" />}
  349. >
  350. 导航
  351. </Button>
  352. <Button
  353. type="primary"
  354. icon={
  355. <i className="iconfont icon-quanping_o text-14px" />
  356. }
  357. onClick={handleEnterFullscreen}
  358. >
  359. 全屏
  360. </Button>
  361. </>
  362. ) : (
  363. <>
  364. <Input
  365. placeholder="搜索"
  366. suffix={<SearchOutlined />}
  367. value={seachColumn}
  368. onChange={(e) => setSearchColumn(e.target.value)}
  369. />
  370. </>
  371. )}
  372. </div>
  373. </div>
  374. <div className="content w-full flex-1 overflow-auto">
  375. {active === 0 ? (
  376. <div
  377. className="er w-full h-full bg-#ccc overflow-auto"
  378. ref={erRef}
  379. >
  380. <ER
  381. showNavigator={showNavigator}
  382. isFullScreen={isFullscreen}
  383. onExitFullscreen={handleExitFullscreen}
  384. onChangeShowNavigator={setShowNavigator}
  385. />
  386. </div>
  387. ) : (
  388. <div>
  389. <div className="p-y-10px p-l-20px">
  390. <ProDescriptions
  391. title={currentTable?.table.schemaName}
  392. dataSource={currentTable?.table}
  393. editable={{
  394. onSave: async (keypath, newInfo, oriInfo) => {
  395. currentTable &&
  396. updateTable({
  397. ...currentTable,
  398. table: {
  399. ...currentTable?.table,
  400. ...newInfo,
  401. langName: "",
  402. langDescription: "",
  403. },
  404. });
  405. return true;
  406. },
  407. }}
  408. columns={[
  409. {
  410. label: "类型",
  411. dataIndex: "type",
  412. valueType: "select",
  413. valueEnum: {
  414. 3: "业务表",
  415. 2: "流程表",
  416. },
  417. editable: false,
  418. },
  419. {
  420. label: "编码",
  421. dataIndex: "schemaName",
  422. valueType: "text",
  423. formItemProps: {
  424. rules: [
  425. { required: true, message: "请输入编码" },
  426. validateTableCode,
  427. ],
  428. },
  429. },
  430. {
  431. label: "别名",
  432. dataIndex: "aliasName",
  433. valueType: "text",
  434. formItemProps: {
  435. rules: [
  436. { required: true, message: "请输入别名" },
  437. validateAliasName,
  438. ],
  439. },
  440. },
  441. {
  442. label: "名称",
  443. dataIndex: "langNameList",
  444. render: (_, record) => {
  445. return (
  446. record.langNameList?.find(
  447. (item) => item.name === "zh-CN"
  448. )?.value || "-"
  449. );
  450. },
  451. renderFormItem: (_schema, config, form) => {
  452. return (
  453. <LangInput
  454. style={{ width: 200 }}
  455. onChange={(val) =>
  456. form.setFieldValue("langNameList", val)
  457. }
  458. />
  459. );
  460. },
  461. },
  462. {
  463. label: "描述",
  464. dataIndex: "langDescriptionList",
  465. render: (_, record) => {
  466. return (
  467. record?.langDescriptionList?.find(
  468. (item) => item.name === "zh-CN"
  469. )?.value || "-"
  470. );
  471. },
  472. renderFormItem: (_schema, config, form) => {
  473. return (
  474. <>
  475. <LangInputTextarea
  476. value={form.getFieldValue(
  477. "langDescriptionList"
  478. )}
  479. onChange={(val) =>
  480. form.setFieldValue(
  481. "langDescriptionList",
  482. val
  483. )
  484. }
  485. />
  486. </>
  487. );
  488. },
  489. },
  490. ]}
  491. ></ProDescriptions>
  492. </div>
  493. <TableEdit
  494. key={selectKey}
  495. data={currentColumns}
  496. tableId={currentTable?.table?.id}
  497. onChange={handleChangeColumn}
  498. />
  499. </div>
  500. )}
  501. </div>
  502. </div>
  503. </Content>
  504. <AddTable ref={addTableRef} onChange={handleAddTable} />
  505. </Layout>
  506. </Spin>
  507. );
  508. }