index.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import { Card, Col, Input, Popconfirm, Row, Tree } from 'antd';
  3. import { useEffect, useRef, useState } from 'react';
  4. import { service } from '@/pages/system/DataSource';
  5. import { useIntl, useLocation } from 'umi';
  6. import type { ActionType, ProColumns } from '@jetlinks/pro-table';
  7. import PermissionButton from '@/components/PermissionButton';
  8. import { DeleteOutlined, EditOutlined, PlusOutlined } from '@ant-design/icons';
  9. import usePermissions from '@/hooks/permission';
  10. import SearchComponent from '@/components/SearchComponent';
  11. import ProTable from '@jetlinks/pro-table';
  12. import DataTable from './DataTable';
  13. const Management = () => {
  14. const location = useLocation<{ id: string }>();
  15. const id = (location as any).query?.id;
  16. const intl = useIntl();
  17. const actionRef = useRef<ActionType>();
  18. const [param, setParam] = useState({});
  19. const { permission: userPermission } = usePermissions('system/User');
  20. const [rdbList, setRdbList] = useState<any[]>([]);
  21. const [defaultSelectedKeys, setDefaultSelectedKeys] = useState<any[]>([]);
  22. const [visible, setVisible] = useState<boolean>(false);
  23. const [current, setCurrent] = useState<any>({});
  24. useEffect(() => {
  25. service.rdbTree(id).then((resp) => {
  26. if (resp.status === 200) {
  27. setRdbList(resp.result);
  28. setDefaultSelectedKeys([resp.result[0]?.name]);
  29. }
  30. });
  31. }, []);
  32. useEffect(() => {
  33. actionRef.current?.reload();
  34. }, [defaultSelectedKeys]);
  35. const columns: ProColumns<DataSourceType>[] = [
  36. {
  37. title: '列名',
  38. dataIndex: 'name',
  39. ellipsis: true,
  40. },
  41. {
  42. title: '类型',
  43. dataIndex: 'type',
  44. ellipsis: true,
  45. },
  46. {
  47. title: '长度',
  48. dataIndex: 'length',
  49. ellipsis: true,
  50. },
  51. {
  52. title: '精度',
  53. dataIndex: 'scale',
  54. ellipsis: true,
  55. },
  56. {
  57. title: '不能为空',
  58. dataIndex: 'notnull',
  59. ellipsis: true,
  60. render: (text, record) => {
  61. console.log(record.notnull);
  62. return <span>{text ? '是' : '否'}</span>;
  63. },
  64. valueType: 'select',
  65. valueEnum: {
  66. true: {
  67. text: '是',
  68. status: true,
  69. },
  70. false: {
  71. text: '否',
  72. status: false,
  73. },
  74. },
  75. },
  76. {
  77. dataIndex: 'description',
  78. title: '说明',
  79. ellipsis: true,
  80. },
  81. {
  82. title: intl.formatMessage({
  83. id: 'pages.data.option',
  84. defaultMessage: '操作',
  85. }),
  86. valueType: 'option',
  87. render: (_, record) => [
  88. <PermissionButton
  89. style={{ padding: 0 }}
  90. type="link"
  91. isPermission={userPermission.update}
  92. key="editable"
  93. onClick={() => {
  94. setCurrent(record);
  95. setVisible(true);
  96. }}
  97. tooltip={{
  98. title: intl.formatMessage({
  99. id: 'pages.data.option.edit',
  100. defaultMessage: '编辑',
  101. }),
  102. }}
  103. >
  104. <EditOutlined />
  105. </PermissionButton>,
  106. <PermissionButton
  107. type="link"
  108. key="delete"
  109. style={{ padding: 0 }}
  110. isPermission={userPermission.delete}
  111. tooltip={{ title: '删除' }}
  112. >
  113. <Popconfirm
  114. onConfirm={async () => {
  115. // await service.remove(record.id);
  116. // actionRef.current?.reload();
  117. }}
  118. title="确认删除?"
  119. >
  120. <DeleteOutlined />
  121. </Popconfirm>
  122. </PermissionButton>,
  123. ],
  124. },
  125. ];
  126. return (
  127. <PageContainer>
  128. <Card>
  129. <Row gutter={24}>
  130. <Col span={6}>
  131. <Input.Search
  132. placeholder="请输入"
  133. onSearch={() => {}}
  134. style={{ width: '100%', marginBottom: 10 }}
  135. />
  136. <Tree
  137. showLine
  138. defaultExpandAll
  139. height={500}
  140. selectedKeys={[...defaultSelectedKeys]}
  141. onSelect={(selectedKeys) => {
  142. if (!selectedKeys.includes('tables')) {
  143. setDefaultSelectedKeys([...selectedKeys]);
  144. }
  145. }}
  146. >
  147. <Tree.TreeNode
  148. title={() => {
  149. return (
  150. <div style={{ display: 'flex', justifyContent: 'space-between', width: 230 }}>
  151. <div>数据源名称</div>
  152. <div>
  153. <PlusOutlined
  154. onClick={() => {
  155. setCurrent({});
  156. setVisible(true);
  157. }}
  158. />
  159. </div>
  160. </div>
  161. );
  162. }}
  163. key={'tables'}
  164. >
  165. {rdbList.map((item) => (
  166. <Tree.TreeNode
  167. key={item.name}
  168. title={() => {
  169. return (
  170. <div
  171. style={{ display: 'flex', justifyContent: 'space-between', width: 200 }}
  172. >
  173. <div>{item.name}</div>
  174. <div>
  175. <PlusOutlined
  176. onClick={() => {
  177. setCurrent(item);
  178. setVisible(true);
  179. }}
  180. />
  181. </div>
  182. </div>
  183. );
  184. }}
  185. />
  186. ))}
  187. </Tree.TreeNode>
  188. </Tree>
  189. </Col>
  190. <Col span={18}>
  191. <div>
  192. <SearchComponent<DataSourceType>
  193. field={columns}
  194. target="datasource-manage"
  195. onSearch={(data) => {
  196. // 重置分页数据
  197. actionRef.current?.reset?.();
  198. setParam(data);
  199. }}
  200. />
  201. <ProTable<DataSourceType>
  202. actionRef={actionRef}
  203. params={param}
  204. columns={columns}
  205. search={false}
  206. rowKey="name"
  207. headerTitle={
  208. <PermissionButton
  209. onClick={() => {}}
  210. isPermission={userPermission.add}
  211. key="add"
  212. icon={<PlusOutlined />}
  213. type="primary"
  214. >
  215. 新增列
  216. </PermissionButton>
  217. }
  218. request={async (params) => {
  219. if (defaultSelectedKeys.length > 0) {
  220. const response = await service.rdbTables(id, defaultSelectedKeys[0], {
  221. ...params,
  222. sorts: [{ name: 'createTime', order: 'desc' }],
  223. });
  224. return {
  225. result: { data: response.result?.columns || [] },
  226. success: true,
  227. status: 200,
  228. } as any;
  229. } else {
  230. return {
  231. result: { data: [] },
  232. success: true,
  233. status: 200,
  234. } as any;
  235. }
  236. }}
  237. />
  238. </div>
  239. </Col>
  240. </Row>
  241. </Card>
  242. {visible && (
  243. <DataTable
  244. data={current}
  245. reload={() => {
  246. setVisible(false);
  247. }}
  248. close={() => {
  249. setVisible(false);
  250. }}
  251. />
  252. )}
  253. </PageContainer>
  254. );
  255. };
  256. export default Management;