123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- import type { TableItemType, ColumnItem } from "@/type";
- import React, { useEffect, useState } from "react";
- import {
- ProDescriptions,
- ProDescriptionsProps,
- } from "@ant-design/pro-components";
- import LangInput from "@/components/LangInput";
- import LangInputTextarea from "@/components/LangInputTextarea";
- import { Button, Table, Tooltip, TableProps, Spin } from "antd";
- import { validateAliasName, validateTableCode } from "@/utils/validator";
- import { GetBusinessTablesByTableId, GetAllBusinessTableColumns, ListLangByKey } from "@/api";
- import { useRequest } from "umi";
- import { DoubleLeftOutlined } from "@ant-design/icons";
- import { createColumn } from "@/utils";
- import { pick } from "lodash-es";
- const baseColumns: TableProps["columns"] = [
- {
- title: "字段代码",
- dataIndex: "schemaName",
- },
- {
- title: "字段名称",
- dataIndex: "name",
- render: (_dom, entity) => {
- return entity.langNameList
- ? entity.langNameList.find(
- (item: Record<string, string>) => item.name === "zh-CN"
- )?.value || "-"
- : entity.name;
- },
- },
- {
- title: "类型",
- dataIndex: "type",
- },
- {
- title: "长度",
- dataIndex: "maxLength",
- },
- {
- title: "必填",
- dataIndex: "isRequired",
- },
- {
- title: "唯一",
- dataIndex: "isUnique",
- },
- {
- title: "默认值",
- dataIndex: "defaultValue",
- },
- {
- title: "字符集",
- dataIndex: "chartset",
- },
- {
- title: "内容",
- dataIndex: "whereInputContent",
- },
- {
- title: "描述",
- dataIndex: "memo",
- },
- ];
- // 表详情组件
- const DescComp = function ({
- detail,
- editable,
- title,
- }: {
- detail: TableItemType["table"];
- editable?: ProDescriptionsProps["editable"];
- title?: string;
- }) {
- return (
- <ProDescriptions
- title={title}
- dataSource={detail}
- editable={editable}
- columns={[
- {
- label: "类型",
- dataIndex: "type",
- valueType: "select",
- valueEnum: {
- 3: "业务表",
- 2: "流程表",
- },
- editable: false,
- },
- {
- label: "编码",
- dataIndex: "schemaName",
- valueType: "text",
- formItemProps: {
- rules: [
- { required: true, message: "请输入编码" },
- validateTableCode,
- ],
- },
- },
- {
- label: "别名",
- dataIndex: "aliasName",
- valueType: "text",
- formItemProps: {
- rules: [
- { required: true, message: "请输入别名" },
- validateAliasName,
- ],
- },
- },
- {
- label: "名称",
- dataIndex: "langNameList",
- render: (_, record) => {
- return (
- record.langNameList?.find((item: any) => item.name === "zh-CN")
- ?.value || "-"
- );
- },
- renderFormItem: (_schema, config, form) => {
- return (
- <LangInput
- style={{ width: 200 }}
- onChange={(val) => form.setFieldValue("langNameList", val)}
- />
- );
- },
- },
- {
- label: "描述",
- dataIndex: "langDescriptionList",
- render: (_, record) => {
- return (
- record?.langDescriptionList?.find(
- (item: any) => item.name === "zh-CN"
- )?.value || "-"
- );
- },
- renderFormItem: (_schema, config, form) => {
- return (
- <>
- <LangInputTextarea
- value={form.getFieldValue("langDescriptionList")}
- onChange={(val) =>
- form.setFieldValue("langDescriptionList", val)
- }
- />
- </>
- );
- },
- },
- ]}
- ></ProDescriptions>
- );
- };
- export default function DiffTable({
- sourceTable,
- targetTableId,
- onChange
- }: {
- sourceTable: TableItemType;
- targetTableId?: string;
- onChange: (tableItem: TableItemType) => void;
- }) {
- const [selectedKeys, setSelectedKeys] = useState<React.Key[]>([]);
- const [selectedRows, setSelectedRows] = useState<any[]>([]);
- const {
- data: tableDetailRes,
- run: run1,
- loading: loading1,
- } = useRequest(GetBusinessTablesByTableId, { manual: true });
- const {
- data: tableColumnRes,
- run: run2,
- loading: loading2,
- } = useRequest(GetAllBusinessTableColumns, { manual: true });
- useEffect(() => {
- if (targetTableId) {
- run1(targetTableId);
- run2({
- currentPage: 1,
- pageSize: 2000,
- orderByProperty: "isPreDefined DESC, DisplayOrder",
- Ascending: true,
- totalPage: 1,
- totalCount: 1,
- filters: [
- {
- name: "BusinessTableId",
- value: targetTableId,
- },
- ],
- });
- }
- }, []);
- // 右侧选中数据覆盖左侧数据
- const handleCoverTable = async () => {
- if(!selectedRows.length) return;
- const arr = [...sourceTable.tableColumnList];
- // 获取全部多语言数据
- const langList = await Promise.all(
- [...new Set(selectedRows.map(item => item.langName))].map((key) =>
- ListLangByKey({ key }).then((res) => res.result)
- )
- );
- // 1、存在的数据进行更新
- // 2、不存在的数据进行新增
- selectedRows.forEach((item) => {
- const index = arr.findIndex(
- (item2: any) => item2.schemaName === item.schemaName
- );
- const newColumn = createColumn(sourceTable.table.id);
- const langName = langList.find((lang) => lang.key === item.langName);
- const column = {
- ...newColumn,
- ...pick(item, Object.keys(newColumn)),
- langNameList: [
- { name: "zh-CN", value: langName?.["zh-CN"] || "" },
- { name: "en", value: langName?.["en"] || "" },
- ],
- };
- if (index !== -1) {
- arr[index] = column;
- } else {
- arr.push(column);
- }
- });
- onChange({
- ...sourceTable,
- tableColumnList: arr,
- });
- setSelectedKeys([]);
- setSelectedRows([]);
- };
- return (
- <Spin spinning={loading1 || loading2}>
- <div className="flex">
- <div className="left rounded-8px border border-1px border-solid border-#979797 overflow-hidden">
- <div className="h-120px p-y-10px p-l-20px">
- <DescComp
- title="当前模型"
- detail={sourceTable.table}
- editable={{
- onSave: async (keypath, newInfo, oriInfo) => {
- onChange({
- ...sourceTable,
- table: {
- ...sourceTable.table,
- ...newInfo
- }
- });
- return true;
- },
- }}
- />
- </div>
- <Table
- pagination={false}
- columns={baseColumns}
- rowKey={'id'}
- dataSource={sourceTable.tableColumnList || []}
- />
- </div>
- <div className="middle w-100px flex-shrink-0">
- <Tooltip title="使用右侧数据">
- <Button
- className="block m-x-auto m-y-0 m-t-140px"
- icon={<DoubleLeftOutlined />}
- disabled={!selectedKeys.length}
- onClick={handleCoverTable}
- />
- </Tooltip>
- </div>
- <div className="right rounded-8px border border-1px border-solid border-#979797 overflow-hidden">
- <div className="h-120px p-y-10px p-l-20px">
- <DescComp
- title="数据表"
- detail={tableDetailRes?.result?.[0] || {}}
- />
- </div>
- <Table
- pagination={false}
- rowKey={'id'}
- columns={baseColumns}
- dataSource={tableColumnRes?.result?.model || []}
- rowSelection={{
- type: "checkbox",
- selectedRowKeys: selectedKeys,
- onChange: (keys, selectedRows) => {
- setSelectedKeys(keys);
- setSelectedRows(selectedRows);
- },
- }}
- />
- </div>
- </div>
- </Spin>
- );
- }
|