86133 | aaa3f5d | 2025-04-20 21:33:29 +0800 | [diff] [blame^] | 1 | import { DownloadOutlined, PlusOutlined } from '@ant-design/icons'; |
| 2 | import { Button, message, Drawer, Modal, Card, Layout } from 'antd'; |
| 3 | import type { FormInstance } from 'antd'; |
| 4 | import React, { useState, useRef } from 'react'; |
| 5 | import { history, FormattedMessage, useAccess } from '@umijs/max'; |
| 6 | import PreviewForm from './components/PreviewCode'; |
| 7 | import type { GenCodeTableListParams, GenCodeType } from './data.d'; |
| 8 | import { |
| 9 | batchGenCode, |
| 10 | genCode, |
| 11 | previewCode, |
| 12 | getGenCodeList, |
| 13 | removeData, |
| 14 | syncDbInfo, |
| 15 | } from './service'; |
| 16 | import { |
| 17 | ActionType, |
| 18 | FooterToolbar, |
| 19 | ProColumns, |
| 20 | ProDescriptions, |
| 21 | ProDescriptionsItemProps, |
| 22 | ProTable, |
| 23 | } from '@ant-design/pro-components'; |
| 24 | |
| 25 | const { Content } = Layout; |
| 26 | |
| 27 | /** |
| 28 | * 删除节点 |
| 29 | * |
| 30 | * @param selectedRows |
| 31 | */ |
| 32 | const handleRemove = async (selectedRows: GenCodeType[]) => { |
| 33 | const hide = message.loading('正在删除'); |
| 34 | if (!selectedRows) return true; |
| 35 | try { |
| 36 | await removeData({ |
| 37 | ids: selectedRows.map((row) => row.tableId), |
| 38 | }); |
| 39 | hide(); |
| 40 | message.success('删除成功,即将刷新'); |
| 41 | return true; |
| 42 | } catch (error) { |
| 43 | hide(); |
| 44 | message.error('删除失败,请重试'); |
| 45 | return false; |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | const handleRemoveOne = async (selectedRow: GenCodeType) => { |
| 50 | const hide = message.loading('正在删除'); |
| 51 | if (!selectedRow) return true; |
| 52 | try { |
| 53 | const params = [selectedRow.tableId]; |
| 54 | await removeData({ |
| 55 | ids: params, |
| 56 | }); |
| 57 | hide(); |
| 58 | message.success('删除成功,即将刷新'); |
| 59 | return true; |
| 60 | } catch (error) { |
| 61 | hide(); |
| 62 | message.error('删除失败,请重试'); |
| 63 | return false; |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | const GenCodeView: React.FC = () => { |
| 68 | const formTableRef = useRef<FormInstance>(); |
| 69 | |
| 70 | const [showDetail, setShowDetail] = useState<boolean>(false); |
| 71 | const [showPreview, setShowPreview] = useState<boolean>(false); |
| 72 | const [preivewData, setPreivewData] = useState<boolean>(false); |
| 73 | |
| 74 | const actionRef = useRef<ActionType>(); |
| 75 | const [currentRow, setCurrentRow] = useState<GenCodeType>(); |
| 76 | const [selectedRows, setSelectedRows] = useState<GenCodeType[]>([]); |
| 77 | |
| 78 | const access = useAccess(); |
| 79 | |
| 80 | const columns: ProColumns<GenCodeType>[] = [ |
| 81 | { |
| 82 | title: '编号', |
| 83 | dataIndex: 'tableId', |
| 84 | tip: '编号', |
| 85 | render: (dom, entity) => { |
| 86 | return ( |
| 87 | <a |
| 88 | onClick={() => { |
| 89 | setCurrentRow(entity); |
| 90 | setShowDetail(true); |
| 91 | }} |
| 92 | > |
| 93 | {dom} |
| 94 | </a> |
| 95 | ); |
| 96 | }, |
| 97 | }, |
| 98 | { |
| 99 | title: '表名', |
| 100 | dataIndex: 'tableName', |
| 101 | valueType: 'textarea', |
| 102 | }, |
| 103 | { |
| 104 | title: '表描述', |
| 105 | dataIndex: 'tableComment', |
| 106 | hideInForm: true, |
| 107 | hideInSearch: true, |
| 108 | }, |
| 109 | { |
| 110 | title: '实体', |
| 111 | dataIndex: 'className', |
| 112 | valueType: 'textarea', |
| 113 | }, |
| 114 | { |
| 115 | title: '创建时间', |
| 116 | dataIndex: 'createTime', |
| 117 | valueType: 'textarea', |
| 118 | hideInSearch: true, |
| 119 | }, |
| 120 | { |
| 121 | title: '更新时间', |
| 122 | dataIndex: 'updateTime', |
| 123 | valueType: 'textarea', |
| 124 | hideInSearch: true, |
| 125 | }, |
| 126 | { |
| 127 | title: '操作', |
| 128 | dataIndex: 'option', |
| 129 | width: '220px', |
| 130 | valueType: 'option', |
| 131 | render: (_, record) => [ |
| 132 | <Button |
| 133 | type="link" |
| 134 | size="small" |
| 135 | key="preview" |
| 136 | hidden={!access.hasPerms('tool:gen:edit')} |
| 137 | onClick={() => { |
| 138 | previewCode(record.tableId).then((res) => { |
| 139 | if (res.code === 200) { |
| 140 | setPreivewData(res.data); |
| 141 | setShowPreview(true); |
| 142 | } else { |
| 143 | message.error('获取数据失败'); |
| 144 | } |
| 145 | }); |
| 146 | }} |
| 147 | > |
| 148 | 预览 |
| 149 | </Button>, |
| 150 | <Button |
| 151 | type="link" |
| 152 | size="small" |
| 153 | key="config" |
| 154 | hidden={!access.hasPerms('tool:gen:edit')} |
| 155 | onClick={() => { |
| 156 | history.push(`/tool/gen/edit?id=${record.tableId}`); |
| 157 | }} |
| 158 | > |
| 159 | 编辑 |
| 160 | </Button>, |
| 161 | <Button |
| 162 | type="link" |
| 163 | size="small" |
| 164 | danger |
| 165 | key="delete" |
| 166 | hidden={!access.hasPerms('tool:gen:del')} |
| 167 | onClick={async () => { |
| 168 | Modal.confirm({ |
| 169 | title: '删除任务', |
| 170 | content: '确定删除该任务吗?', |
| 171 | okText: '确认', |
| 172 | cancelText: '取消', |
| 173 | onOk: async () => { |
| 174 | const success = await handleRemoveOne(record); |
| 175 | if (success) { |
| 176 | if (actionRef.current) { |
| 177 | actionRef.current.reload(); |
| 178 | } |
| 179 | } |
| 180 | }, |
| 181 | }); |
| 182 | }} |
| 183 | > |
| 184 | 删除 |
| 185 | </Button>, |
| 186 | <Button |
| 187 | type="link" |
| 188 | size="small" |
| 189 | key="sync" |
| 190 | hidden={!access.hasPerms('tool:gen:edit')} |
| 191 | onClick={() => { |
| 192 | syncDbInfo(record.tableName).then((res) => { |
| 193 | if (res.code === 200) { |
| 194 | message.success('同步成功'); |
| 195 | } else { |
| 196 | message.error('同步失败'); |
| 197 | } |
| 198 | }); |
| 199 | }} |
| 200 | > |
| 201 | 同步 |
| 202 | </Button>, |
| 203 | <Button |
| 204 | type="link" |
| 205 | size="small" |
| 206 | key="gencode" |
| 207 | hidden={!access.hasPerms('tool:gen:edit')} |
| 208 | onClick={() => { |
| 209 | if (record.genType === '1') { |
| 210 | genCode(record.tableName).then((res) => { |
| 211 | if (res.code === 200) { |
| 212 | message.success(`成功生成到自定义路径:${record.genPath}`); |
| 213 | } else { |
| 214 | message.error(res.msg); |
| 215 | } |
| 216 | }); |
| 217 | } else { |
| 218 | batchGenCode(record.tableName); |
| 219 | } |
| 220 | }} |
| 221 | > |
| 222 | 生成代码 |
| 223 | </Button>, |
| 224 | ], |
| 225 | }, |
| 226 | ]; |
| 227 | |
| 228 | return ( |
| 229 | <Content> |
| 230 | <Card bordered={false}> |
| 231 | <ProTable<GenCodeType> |
| 232 | headerTitle="代码生成信息" |
| 233 | actionRef={actionRef} |
| 234 | formRef={formTableRef} |
| 235 | rowKey="tableId" |
| 236 | search={{ |
| 237 | labelWidth: 120, |
| 238 | }} |
| 239 | toolBarRender={() => [ |
| 240 | <Button |
| 241 | type="primary" |
| 242 | key="gen" |
| 243 | hidden={!access.hasPerms('tool:gen:edit')} |
| 244 | onClick={() => { |
| 245 | if (selectedRows.length === 0) { |
| 246 | message.error('请选择要生成的数据'); |
| 247 | return; |
| 248 | } |
| 249 | const tableNames = selectedRows.map((row) => row.tableName); |
| 250 | if (selectedRows[0].genType === '1') { |
| 251 | genCode(tableNames.join(',')).then((res) => { |
| 252 | if (res.code === 200) { |
| 253 | message.success(`成功生成到自定义路径:${selectedRows[0].genPath}`); |
| 254 | } else { |
| 255 | message.error(res.msg); |
| 256 | } |
| 257 | }); |
| 258 | } else { |
| 259 | batchGenCode(tableNames.join(',')); |
| 260 | } |
| 261 | }} |
| 262 | > |
| 263 | <DownloadOutlined /> <FormattedMessage id="gen.gencode" defaultMessage="生成" /> |
| 264 | </Button>, |
| 265 | <Button |
| 266 | type="primary" |
| 267 | key="import" |
| 268 | hidden={!access.hasPerms('tool:gen:add')} |
| 269 | onClick={() => { |
| 270 | history.push('/tool/gen/import'); |
| 271 | }} |
| 272 | > |
| 273 | <PlusOutlined /> <FormattedMessage id="gen.import" defaultMessage="导入" /> |
| 274 | </Button>, |
| 275 | ]} |
| 276 | request={(params) => |
| 277 | getGenCodeList({ ...params } as GenCodeTableListParams).then((res) => { |
| 278 | return { |
| 279 | data: res.rows, |
| 280 | total: res.rows.length, |
| 281 | success: true, |
| 282 | }; |
| 283 | }) |
| 284 | } |
| 285 | columns={columns} |
| 286 | rowSelection={{ |
| 287 | onChange: (_, selectedRows) => { |
| 288 | setSelectedRows(selectedRows); |
| 289 | }, |
| 290 | }} |
| 291 | /> |
| 292 | {selectedRows?.length > 0 && ( |
| 293 | <FooterToolbar |
| 294 | extra={ |
| 295 | <div> |
| 296 | <FormattedMessage id="pages.searchTable.chosen" defaultMessage="已选择" />{' '} |
| 297 | <a style={{ fontWeight: 600 }}>{selectedRows.length}</a>{' '} |
| 298 | <FormattedMessage id="pages.searchTable.item" defaultMessage="项" /> |
| 299 | </div> |
| 300 | } |
| 301 | > |
| 302 | <Button |
| 303 | key="delete" |
| 304 | hidden={!access.hasPerms('tool:gen:remove')} |
| 305 | onClick={async () => { |
| 306 | Modal.confirm({ |
| 307 | title: '删除任务', |
| 308 | content: '确定删除该任务吗?', |
| 309 | okText: '确认', |
| 310 | cancelText: '取消', |
| 311 | onOk: async () => { |
| 312 | const success = await handleRemove(selectedRows); |
| 313 | if (success) { |
| 314 | setSelectedRows([]); |
| 315 | actionRef.current?.reloadAndRest?.(); |
| 316 | } |
| 317 | }, |
| 318 | }); |
| 319 | }} |
| 320 | > |
| 321 | <FormattedMessage id="pages.searchTable.batchDeletion" defaultMessage="批量删除" /> |
| 322 | </Button> |
| 323 | </FooterToolbar> |
| 324 | )} |
| 325 | <PreviewForm |
| 326 | open={showPreview} |
| 327 | data={preivewData} |
| 328 | onHide={() => { |
| 329 | setShowPreview(false); |
| 330 | }} |
| 331 | /> |
| 332 | <Drawer |
| 333 | width={600} |
| 334 | open={showDetail} |
| 335 | onClose={() => { |
| 336 | setCurrentRow(undefined); |
| 337 | setShowDetail(false); |
| 338 | }} |
| 339 | closable={false} |
| 340 | > |
| 341 | {currentRow?.tableName && ( |
| 342 | <ProDescriptions<GenCodeType> |
| 343 | column={2} |
| 344 | title={currentRow?.tableName} |
| 345 | request={async () => ({ |
| 346 | data: currentRow || {}, |
| 347 | })} |
| 348 | params={{ |
| 349 | id: currentRow?.tableName, |
| 350 | }} |
| 351 | columns={columns as ProDescriptionsItemProps<GenCodeType>[]} |
| 352 | /> |
| 353 | )} |
| 354 | </Drawer> |
| 355 | </Card> |
| 356 | </Content> |
| 357 | ); |
| 358 | }; |
| 359 | |
| 360 | export default GenCodeView; |