86133 | aaa3f5d | 2025-04-20 21:33:29 +0800 | [diff] [blame^] | 1 | import { Button, Col, Divider, Form, Row, TreeSelect } from 'antd'; |
| 2 | import React, { Fragment, useEffect, useState } from 'react'; |
| 3 | import { history } from '@umijs/max'; |
| 4 | import type { TableInfo } from '../data'; |
| 5 | import styles from '../style.less'; |
| 6 | import { DataNode } from 'antd/es/tree'; |
| 7 | import { ProForm, ProFormRadio, ProFormSelect, ProFormText } from '@ant-design/pro-components'; |
| 8 | |
| 9 | export type GenInfoProps = { |
| 10 | values?: any; |
| 11 | menuData?: DataNode[]; |
| 12 | tableInfo?: TableInfo[]; |
| 13 | onStepSubmit?: any; |
| 14 | }; |
| 15 | |
| 16 | const GenInfo: React.FC<GenInfoProps> = (props) => { |
| 17 | const [form] = Form.useForm(); |
| 18 | |
| 19 | const [pathType, setPathType] = useState<string>('0'); |
| 20 | const [tlpType, setTlpType] = useState<string>('curd'); |
| 21 | |
| 22 | const [subTablesColumnOptions, setSubTablesColumnOptions] = useState<any[]>(); |
| 23 | |
| 24 | const { menuData, tableInfo, onStepSubmit } = props; |
| 25 | |
| 26 | const tablesOptions = tableInfo?.map((item: any) => { |
| 27 | return { |
| 28 | value: item.tableName, |
| 29 | label: `${item.tableName}:${item.tableComment}`, |
| 30 | }; |
| 31 | }); |
| 32 | |
| 33 | if (tableInfo) { |
| 34 | for (let index = 0; index < tableInfo?.length; index += 1) { |
| 35 | const tbl = tableInfo[index]; |
| 36 | if (tbl.tableName === props.values.subTableName) { |
| 37 | const opts = []; |
| 38 | tbl.columns.forEach((item) => { |
| 39 | opts.push({ |
| 40 | value: item.columnName, |
| 41 | label: `${item.columnName}: ${item.columnComment}`, |
| 42 | }); |
| 43 | }); |
| 44 | break; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | const treeColumns = props.values.columns.map((item: any) => { |
| 50 | return { |
| 51 | value: item.columnName, |
| 52 | label: `${item.columnName}: ${item.columnComment}`, |
| 53 | }; |
| 54 | }); |
| 55 | |
| 56 | const onSubmit = async (direction: string) => { |
| 57 | const values = await form.validateFields(); |
| 58 | onStepSubmit('gen', values, direction); |
| 59 | }; |
| 60 | |
| 61 | useEffect(() => { |
| 62 | setPathType(props.values.genType); |
| 63 | setTlpType(props.values.tplCategory); |
| 64 | }, [props.values.genType, props.values.tplCategory]); |
| 65 | |
| 66 | return ( |
| 67 | <Fragment> |
| 68 | <Row> |
| 69 | <Col span={24}> |
| 70 | <ProForm |
| 71 | form={form} |
| 72 | onFinish={async () => { |
| 73 | const values = await form.validateFields(); |
| 74 | onStepSubmit('gen', values); |
| 75 | }} |
| 76 | initialValues={{ |
| 77 | curd: props.values.curd, |
| 78 | tree: props.values.tree, |
| 79 | sub: props.values.sub, |
| 80 | tplCategory: props.values.tplCategory, |
| 81 | packageName: props.values.packageName, |
| 82 | moduleName: props.values.moduleName, |
| 83 | businessName: props.values.businessName, |
| 84 | functionName: props.values.functionName, |
| 85 | parentMenuId: props.values.parentMenuId, |
| 86 | genType: props.values.genType, |
| 87 | genPath: props.values.genPath, |
| 88 | treeCode: props.values.treeCode, |
| 89 | treeParentCode: props.values.treeParentCode, |
| 90 | treeName: props.values.treeName, |
| 91 | subTableName: props.values.subTableName, |
| 92 | subTableFkName: props.values.subTableFkName, |
| 93 | }} |
| 94 | submitter={{ |
| 95 | resetButtonProps: { |
| 96 | style: { display: 'none' }, |
| 97 | }, |
| 98 | submitButtonProps: { |
| 99 | style: { display: 'none' }, |
| 100 | }, |
| 101 | }} |
| 102 | > |
| 103 | <Row gutter={[16, 16]}> |
| 104 | <Col span={12} order={1}> |
| 105 | <ProFormSelect |
| 106 | fieldProps={{ |
| 107 | onChange: (val) => { |
| 108 | setTlpType(val); |
| 109 | }, |
| 110 | }} |
| 111 | valueEnum={{ |
| 112 | crud: '单表(增删改查)', |
| 113 | tree: '树表(增删改查)', |
| 114 | sub: '主子表(增删改查)', |
| 115 | }} |
| 116 | name="tplCategory" |
| 117 | label="生成模板" |
| 118 | rules={[ |
| 119 | { |
| 120 | required: true, |
| 121 | message: '选择类型', |
| 122 | }, |
| 123 | ]} |
| 124 | /> |
| 125 | </Col> |
| 126 | <Col span={12} order={2}> |
| 127 | <ProFormText name="packageName" label="生成包路径" /> |
| 128 | </Col> |
| 129 | </Row> |
| 130 | <Row gutter={[16, 16]}> |
| 131 | <Col span={12} order={1}> |
| 132 | <ProFormText name="moduleName" label="生成模块名" /> |
| 133 | </Col> |
| 134 | <Col span={12} order={2}> |
| 135 | <ProFormText name="businessName" label="生成业务名" /> |
| 136 | </Col> |
| 137 | </Row> |
| 138 | <Row gutter={[16, 16]}> |
| 139 | <Col span={12} order={1}> |
| 140 | <ProFormText name="functionName" label="生成功能名" /> |
| 141 | </Col> |
| 142 | <Col span={12} order={2}> |
| 143 | <ProForm.Item |
| 144 | labelCol={{ span: 20 }} |
| 145 | name="parentMenuId" |
| 146 | label="父菜单" |
| 147 | > |
| 148 | <TreeSelect |
| 149 | style={{ width: '74%' }} |
| 150 | defaultValue={props.values.parentMenuId} |
| 151 | treeData={menuData} |
| 152 | placeholder="请选择父菜单" |
| 153 | /> |
| 154 | </ProForm.Item> |
| 155 | </Col> |
| 156 | </Row> |
| 157 | <Row gutter={[16, 16]}> |
| 158 | <Col span={24}> |
| 159 | <ProFormRadio.Group |
| 160 | valueEnum={{ |
| 161 | '0': 'zip压缩包', |
| 162 | '1': '自定义路径', |
| 163 | }} |
| 164 | name="genType" |
| 165 | label="生成代码方式" |
| 166 | rules={[ |
| 167 | { |
| 168 | required: true, |
| 169 | message: '选择类型', |
| 170 | }, |
| 171 | ]} |
| 172 | fieldProps={{ |
| 173 | onChange: (e) => { |
| 174 | setPathType(e.target.value); |
| 175 | }, |
| 176 | }} |
| 177 | /> |
| 178 | </Col> |
| 179 | </Row> |
| 180 | <Row gutter={[16, 16]}> |
| 181 | <Col span={24} order={1}> |
| 182 | <ProFormText |
| 183 | hidden={pathType === '0'} |
| 184 | width="md" |
| 185 | name="genPath" |
| 186 | label="自定义路径" |
| 187 | /> |
| 188 | </Col> |
| 189 | </Row> |
| 190 | <div hidden={tlpType !== 'tree'}> |
| 191 | <Divider orientation="left">其他信息</Divider> |
| 192 | <Row gutter={[16, 16]}> |
| 193 | <Col span={12} order={1}> |
| 194 | <ProFormSelect |
| 195 | name="treeCode" |
| 196 | label="树编码字段" |
| 197 | options={treeColumns} |
| 198 | rules={[ |
| 199 | { |
| 200 | required: tlpType === 'tree', |
| 201 | message: '树编码字段', |
| 202 | }, |
| 203 | ]} |
| 204 | /> |
| 205 | </Col> |
| 206 | <Col span={12} order={2}> |
| 207 | <ProFormSelect |
| 208 | name="treeParentCode" |
| 209 | label="树父编码字段" |
| 210 | options={treeColumns} |
| 211 | rules={[ |
| 212 | { |
| 213 | required: tlpType === 'tree', |
| 214 | message: '树父编码字段', |
| 215 | }, |
| 216 | ]} |
| 217 | /> |
| 218 | </Col> |
| 219 | </Row> |
| 220 | <Row gutter={[16, 16]}> |
| 221 | <Col span={12} order={1}> |
| 222 | <ProFormSelect |
| 223 | name="treeName" |
| 224 | label="树名称字段" |
| 225 | options={treeColumns} |
| 226 | rules={[ |
| 227 | { |
| 228 | required: tlpType === 'tree', |
| 229 | message: '树名称字段', |
| 230 | }, |
| 231 | ]} |
| 232 | /> |
| 233 | </Col> |
| 234 | </Row> |
| 235 | </div> |
| 236 | <div hidden={tlpType !== 'sub'}> |
| 237 | <Divider orientation="left">关联信息</Divider> |
| 238 | <Row gutter={[16, 16]}> |
| 239 | <Col span={12} order={1}> |
| 240 | <ProFormSelect |
| 241 | name="subTableName" |
| 242 | label="关联子表的表名" |
| 243 | options={tablesOptions} |
| 244 | rules={[ |
| 245 | { |
| 246 | required: tlpType === 'sub', |
| 247 | message: '关联子表的表名', |
| 248 | }, |
| 249 | ]} |
| 250 | fieldProps={{ |
| 251 | onChange: (val) => { |
| 252 | form.setFieldsValue({ |
| 253 | subTableFkName: '', |
| 254 | }); |
| 255 | if (tableInfo) { |
| 256 | for (let index = 0; index < tableInfo?.length; index += 1) { |
| 257 | const tbl = tableInfo[index]; |
| 258 | if (tbl.tableName === val) { |
| 259 | const opts: any[] = []; |
| 260 | tbl.columns.forEach((item) => { |
| 261 | opts.push({ |
| 262 | value: item.columnName, |
| 263 | label: `${item.columnName}:${item.columnComment}`, |
| 264 | }); |
| 265 | }); |
| 266 | setSubTablesColumnOptions(opts); |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | }, |
| 272 | }} |
| 273 | /> |
| 274 | </Col> |
| 275 | <Col span={12} order={2}> |
| 276 | <ProFormSelect |
| 277 | name="subTableFkName" |
| 278 | options={subTablesColumnOptions} |
| 279 | label="子表关联的外键名" |
| 280 | rules={[ |
| 281 | { |
| 282 | required: tlpType === 'sub', |
| 283 | message: '子表关联的外键名', |
| 284 | }, |
| 285 | ]} |
| 286 | /> |
| 287 | </Col> |
| 288 | </Row> |
| 289 | </div> |
| 290 | </ProForm> |
| 291 | </Col> |
| 292 | </Row> |
| 293 | <Row justify="center"> |
| 294 | <Col span={4}> |
| 295 | <Button |
| 296 | type="primary" |
| 297 | onClick={() => { |
| 298 | history.back(); |
| 299 | }} |
| 300 | > |
| 301 | 返回 |
| 302 | </Button> |
| 303 | </Col> |
| 304 | <Col span={4}> |
| 305 | <Button |
| 306 | type="primary" |
| 307 | className={styles.step_buttons} |
| 308 | onClick={() => { |
| 309 | onSubmit('prev'); |
| 310 | }} |
| 311 | > |
| 312 | 上一步 |
| 313 | </Button> |
| 314 | </Col> |
| 315 | <Col span={4}> |
| 316 | <Button |
| 317 | type="primary" |
| 318 | onClick={() => { |
| 319 | onSubmit('next'); |
| 320 | }} |
| 321 | > |
| 322 | 提交 |
| 323 | </Button> |
| 324 | </Col> |
| 325 | </Row> |
| 326 | </Fragment> |
| 327 | ); |
| 328 | }; |
| 329 | |
| 330 | export default GenInfo; |