filedConfig.jsx 12.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
import React, { useState, useEffect } from 'react';
import {
    Form,
    Modal,
    Space,
    Table,
    Button,
    Popconfirm,
    Spin,
    notification,
11
    Tooltip
12
} from 'antd';
13 14
import { EditOutlined, DeleteOutlined,SortDescendingOutlined,PlusSquareOutlined,RollbackOutlined  } from '@ant-design/icons';
import { reloadTableFields, removeFields, loadUnattachedTables } from '@/services/platform/bs';
15 16 17
import FieldEditor from './fieldEditor';
import { useHistory } from 'react-router-dom';
import styles from './index.less'
18 19
import AffiliateAdd from '../bsmanager/tablemanager/components/Field/affiliateAdd'
import LoadGroup from '../bsmanager/tablemanager/components/Field/loadGroup'
20 21
const AddModal = props => {
    const history = useHistory();
22
    const [allData, setAllData] = useState([]);
23
    const [tableList, setTableList] = useState([])
24 25 26 27 28 29 30
    const [tableData, setTableData] = useState([]);
    const [treeLoading, setTreeLoading] = useState(false);
    const [formObj, setFormObj] = useState('');
    const [flag, setFlag] = useState(0); // 弹窗类型
    const [isVisible, setIsVisible] = useState(false); // 弹窗
    const [isType, setIsType] = useState(''); // 弹窗类型
    const [itemData, setItemData] = useState({});
shaoan123's avatar
shaoan123 committed
31 32
    const [select, setSelect] = useState([])
    const [selectTableName, setSelectTableName] = useState({})
33 34 35
    const [pramFormObj, setPramFormObj] = useState({});
    const [visible, setVisible] = useState(false); // 弹窗
    const [type, setType] = useState(''); // 弹窗类型
36 37 38 39 40 41 42 43 44
    const editor = record => {
        setIsType('edit');
        setIsVisible(true);
        setItemData(record);
    };
    const Submit = prop => {
        setIsVisible(false);
        setFlag(flag + 1);
    };
45 46 47 48 49
    const getField = () => {
        loadUnattachedTables().then(res => {
            if (res.data.root && res.data.root.length) {
                setTableList(res.data.root)
            }
50

51 52
        })
    }
53 54 55 56 57 58
    const expandedRowRender = (item) => {
        const columns = [
            {
                title: '字段名',
                dataIndex: 'name',
                key: 'name',
shaoan123's avatar
shaoan123 committed
59 60
                width: 190,
                align: 'left',
61
                render: text => <div style={{ paddingLeft: '2rem' }}>{text}</div>,
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
            },
            {
                title: '别名',
                dataIndex: 'alias',
                key: 'alias',
                align: 'center',
                width: 200,
            },
            {
                title: '字段类型',
                dataIndex: 'storeType',
                key: 'storeType',
                align: 'center',
                width: 200,
            },
            {
                title: '形态',
                dataIndex: 'shape',
                key: 'shape',
                align: 'center',
                width: 200,
            },
            {
                title: '配置',
                dataIndex: 'config',
                key: 'config',
                align: 'center',
                width: 200,
            },
shaoan123's avatar
shaoan123 committed
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
            {
                title: '只读',
                dataIndex: 'readOnly',
                key: 'readOnly',
                align: 'center',
                width: 200,
            },
            {
                title: '同步',
                dataIndex: 'syncEvent',
                key: 'syncEvent',
                align: 'center',
                width: 200,
            },
            {
                title: '操作',
                width: 250,
                ellipsis: true,
                align: 'center',
                render: (text, record) => (
                    <Space>
113 114 115 116 117 118 119 120
                        <Tooltip title="修改">
                            <EditOutlined  style={{ fontSize: '16px', color: '#1890FF' }}
                                onClick={() => {
                                    editor(record);
                                }}>
                                编辑
                            </EditOutlined>
                        </Tooltip>
121 122 123 124 125 126 127 128 129
                        <div onClick={e => e.stopPropagation()}>
                            <Popconfirm
                                title="是否删除该字段?"
                                okText="确认"
                                cancelText="取消"
                                onConfirm={() => {
                                    deleteChart(record);
                                }}
                            >
130 131 132
                                <Tooltip title="删除">
                                    <DeleteOutlined style={{ fontSize: '16px', color: '#e86060' }}>删除</DeleteOutlined>
                                </Tooltip>
133 134 135 136 137 138 139
                            </Popconfirm>
                        </div>
                    </Space>
                ),
            },
        ];

shaoan123's avatar
shaoan123 committed
140 141 142 143 144 145 146 147 148 149 150 151
        return <Table columns={columns} onRow={record => {
            return {
                onDoubleClick: event => {
                    event.stopPropagation()
                    editor(record);
                },
                onClick: event => {
                    event.stopPropagation()
                    setSelectTableName(record)
                }, // 点击行
            }
        }} bordered rowClassName={setRowClassName} showHeader={false} dataSource={allData[item.type]} pagination={false} />;
152
    };
shaoan123's avatar
shaoan123 committed
153 154
    const setRowClassName = (record) =>
        Object.entries(record).toString() === Object.entries(selectTableName).toString() ? styles.clickRowStyle : '';
155
    const columns = [
shaoan123's avatar
shaoan123 committed
156

157
        {
shaoan123's avatar
shaoan123 committed
158 159 160 161 162
            title: '字段名',
            dataIndex: 'type',
            key: 'type',
            align: 'left',
            width: 150,
163
            render: text => {
shaoan123's avatar
shaoan123 committed
164
                return (<a >{text}(共{allData[text] ? allData[text].length : ''}条)</a>)
165
            }
shaoan123's avatar
shaoan123 committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

        },
        {
            title: '别名',
            dataIndex: 'alias',
            key: 'alias',
            align: 'center',
            width: 200,
        },
        {
            title: '字段类型',
            dataIndex: 'storeType',
            key: 'storeType',
            align: 'center',
            width: 200,
        },
        {
            title: '形态',
            dataIndex: 'shape',
            key: 'shape',
            align: 'center',
            width: 200,
        },
        {
            title: '配置',
            dataIndex: 'config',
            key: 'config',
            align: 'center',
            width: 200,
        },

        {
            title: '只读',
            dataIndex: 'readOnly',
            key: 'readOnly',
            align: 'center',
            width: 200,
        },
        {
            title: '同步',
            dataIndex: 'syncEvent',
            key: 'syncEvent',
            align: 'center',
            width: 200,
        },
        {
            title: '操作',
            width: 250,
            ellipsis: true,
            key: 'title',
            align: 'center',

218 219 220
        },

    ];
221 222 223 224 225 226 227 228 229
    useEffect(() => {
        if (props.match.params.id) {
            setFormObj(props.match.params.id)
            setTreeLoading(true);
            reloadTableFields({
                tableName: props.match.params.id,
            }).then(res => {
                setTreeLoading(false);
                if (res.msg === 'Ok') {
shaoan123's avatar
shaoan123 committed
230
                    let arr = formateArrDataA(res.data.root, 'group')
231 232
                    let newArr = []
                    Object.keys(arr).map((item, index) => {
shaoan123's avatar
shaoan123 committed
233
                        newArr.push({ type: item, key: index, id: index })
234 235 236
                    })
                    setAllData(arr);
                    setTableData(newArr);
shaoan123's avatar
shaoan123 committed
237
                    setSelect(newArr)
238 239
                }
            });
240
            getField()
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
        }
    }, [flag]);
    const formateArrDataA = (initialArr, name) => {
        // 判定传参是否符合规则
        if (!(initialArr instanceof Array)) {
            return '请传入正确格式的数组'
        }
        if (!name) {
            return '请传入对象属性'
        }
        //先获取一下这个数组中有多少个"name"
        let nameArr = []
        for (let i in initialArr) {
            if (nameArr.indexOf(initialArr[i][`${name}`]) === -1) {
                nameArr.push(initialArr[i][`${name}`])
            }
        }
        //新建一个包含多个list的结果对象
        let tempObj = {}
        // 根据不同的"name"生成多个数组
        for (let k in nameArr) {
            for (let j in initialArr) {
                if (initialArr[j][`${name}`] == nameArr[k]) {
                    // 每次外循环时新建一个对应"name"的数组, 内循环时当前数组不变
                    tempObj[nameArr[k]] = tempObj[nameArr[k]] || []
                    tempObj[nameArr[k]].push(initialArr[j])
                }
            }
        }
270
        for (let keys in tempObj) {
271
            let arr = []
shaoan123's avatar
shaoan123 committed
272
            tempObj[keys].map((item, index) => {
273 274 275
                tempObj[keys] = arr;
                item.key = index
                arr.push(item)
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
            })
        }
        return tempObj
    }
    // 删除表字段
    const deleteChart = record => {
        removeFields({ fieldIDs: record.ID }).then(res => {
            if (res.msg === 'Ok' || res.msg === '') {
                notification.success({
                    message: '提示',
                    duration: 3,
                    description: '删除成功',
                });
                setFlag(flag + 1);
            } else {
                notification.error({
                    message: '提示',
                    duration: 3,
                    description: res.msg,
                });
            }
        });
    };
    // 返回上一级
    const back = () => {
shaoan123's avatar
shaoan123 committed
301 302 303 304 305 306 307 308 309 310 311 312
        let template = props.history.location.state.template
        history.push({ pathname: '/platformCenter/bsmanger/tablemanger', state: { template } })
    }
    const onUnfold = (expanded, record) => {
        const data = [...select]
        let index = data.indexOf(record)
        if (expanded) {
            data.push(record)
        } else {
            data.splice(index, 1)
        }
        setSelect(data)
313
    }
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    //附加
    const add = (record) => {
        setPramFormObj(props.history.location.state.template);
        setType('affiliateAdd');
        setVisible(true);
    }
    //分组与排序
    const sort = (record) => {
        setPramFormObj(props.history.location.state.template);
        setType('sort');
        setVisible(true);
    }
    const onSubmit = prop => {
        setVisible(false);
        setFlag(flag + 1)
    };
330
    return (
shaoan123's avatar
shaoan123 committed
331 332 333

        <><Spin tip="loading..." spinning={treeLoading}>
            <div className={styles.containerBox}>
334 335 336 337 338 339 340
                <div className={styles.config}><div className={styles.title}>{formObj}(字段配置)</div>
                    <div className={styles.btn}>
                        <Button type="primary" icon={<PlusSquareOutlined />} onClick={add}>附加</Button>
                        <Button type="primary" icon={<SortDescendingOutlined /> }onClick={sort}>分组排序</Button>
                        <Button type="primary" icon={<RollbackOutlined />} onClick={back}>返回</Button></div>
                </div>

341 342 343
                <Table
                    columns={columns}
                    dataSource={tableData}
344
                    expandable={{ expandedRowRender }}
345
                    size="small"
346
                    rowKey='id'
shaoan123's avatar
shaoan123 committed
347 348 349 350 351 352 353 354 355
                    expandedRowKeys={select.map(item => item.key)} //展开的行
                    expandRowByClick={true}
                    defaultExpandAllRows={true}
                    pagination={false}
                    scroll={{ y: 'calc(100vh - 186px)' }}
                    size="small"
                    onExpand={onUnfold} />
            </div>
        </Spin><FieldEditor
356 357 358 359 360
                isVisible={isVisible}
                isType={isType}
                itemData={itemData}
                formObj1={formObj}
                onCancel={() => setIsVisible(false)}
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
                callBackSubmit={Submit} />
            {visible && type === 'affiliateAdd' && (
                <AffiliateAdd
                    visible={visible}
                    tableList={tableList}
                    type={type}
                    onCancel={() => setVisible(false)}
                    callBackSubmit={onSubmit}
                    formObj={pramFormObj}
                />
            )}

            {visible && type === 'sort' && (
                <LoadGroup
                    visible={visible}
                    type={type}
                    formObj={pramFormObj}
                    onCancel={() => setVisible(false)}
                    callBackSubmit={onSubmit}
                />
            )}</>
shaoan123's avatar
shaoan123 committed
382

383 384 385
    );
};
export default AddModal;