filedConfig.jsx 12.1 KB
Newer Older
1
/* eslint-disable react/jsx-boolean-value */
2
import React, { useState, useEffect } from 'react';
3
import { Form, Modal, Space, Table, Button, Popconfirm, Spin, notification, Tooltip } from 'antd';
皮倩雯's avatar
皮倩雯 committed
4 5 6 7 8 9 10
import {
  EditOutlined,
  DeleteOutlined,
  SortDescendingOutlined,
  PlusSquareOutlined,
  RollbackOutlined,
} from '@ant-design/icons';
11 12 13 14 15
import {
  reloadTableFields,
  removeFields,
  loadUnattachedTables,
} from '@/services/tablemanager/tablemanager';
16
import { useHistory } from 'react-router-dom';
17
import FieldEditor from './fieldEditor';
皮倩雯's avatar
皮倩雯 committed
18
import styles from './index.less';
19 20
import AffiliateAdd from '../components/Field/affiliateAdd';
import LoadGroup from '../components/Field/loadGroupNew';
21
const AddModal = props => {
皮倩雯's avatar
皮倩雯 committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  const history = useHistory();
  const [allData, setAllData] = useState([]);
  const [tableList, setTableList] = useState([]);
  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({});
  const [select, setSelect] = useState([]);
  const [selectTableName, setSelectTableName] = useState({});
  const [pramFormObj, setPramFormObj] = useState({});
  const [visible, setVisible] = useState(false); // 弹窗
  const [type, setType] = useState(''); // 弹窗类型
37
  const [chooseLine, setChooseLine] = useState('');
皮倩雯's avatar
皮倩雯 committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  const editor = record => {
    setIsType('edit');
    setIsVisible(true);
    setItemData(record);
  };
  const Submit = prop => {
    setIsVisible(false);
    setFlag(flag + 1);
  };
  const getField = () => {
    loadUnattachedTables().then(res => {
      if (res.data.root && res.data.root.length) {
        setTableList(res.data.root);
      }
    });
  };
  // 处理表格特殊样式
  const styleConfig = record => {
    if (record.Type === 1) {
      return 'red';
58
    }
皮倩雯's avatar
皮倩雯 committed
59 60 61 62 63 64
    if (record.Type === 2) {
      return 'grey';
    }
    return '000000D9';
  };
  const expandedRowRender = item => {
65
    const columns = [
皮倩雯's avatar
皮倩雯 committed
66 67 68 69 70 71 72
      {
        title: '字段名',
        dataIndex: 'name',
        key: 'name',
        width: 190,
        align: 'left',
        render: (text, record) => (
73
          <div style={{ paddingLeft: '2rem', color: styleConfig(record) }}>{text}</div>
皮倩雯's avatar
皮倩雯 committed
74 75 76 77 78 79 80 81
        ),
      },
      {
        title: '别名',
        dataIndex: 'alias',
        key: 'alias',
        align: 'center',
        width: 200,
82
        render: (text, record) => <span style={{ color: styleConfig(record) }}>{text} </span>,
皮倩雯's avatar
皮倩雯 committed
83 84 85 86 87 88 89
      },
      {
        title: '字段类型',
        dataIndex: 'storeType',
        key: 'storeType',
        align: 'center',
        width: 200,
90
        render: (text, record) => <span style={{ color: styleConfig(record) }}>{text} </span>,
皮倩雯's avatar
皮倩雯 committed
91 92 93 94 95 96 97
      },
      {
        title: '形态',
        dataIndex: 'shape',
        key: 'shape',
        align: 'center',
        width: 200,
98
        render: (text, record) => <span style={{ color: styleConfig(record) }}>{text} </span>,
皮倩雯's avatar
皮倩雯 committed
99 100 101 102 103 104 105
      },
      {
        title: '配置',
        dataIndex: 'config',
        key: 'config',
        align: 'center',
        width: 200,
106
        render: (text, record) => <span style={{ color: styleConfig(record) }}>{text} </span>,
皮倩雯's avatar
皮倩雯 committed
107
      },
shaoan123's avatar
shaoan123 committed
108

皮倩雯's avatar
皮倩雯 committed
109 110 111 112 113 114
      {
        title: '只读',
        dataIndex: 'readOnly',
        key: 'readOnly',
        align: 'center',
        width: 200,
115
        render: (text, record) => <span style={{ color: styleConfig(record) }}>{text} </span>,
皮倩雯's avatar
皮倩雯 committed
116 117 118 119 120 121 122
      },
      {
        title: '同步',
        dataIndex: 'syncEvent',
        key: 'syncEvent',
        align: 'center',
        width: 200,
123
        render: (text, record) => <span style={{ color: styleConfig(record) }}>{text} </span>,
皮倩雯's avatar
皮倩雯 committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
      },
      {
        title: '操作',
        width: 250,
        ellipsis: true,
        align: 'center',
        render: (text, record) => (
          <Space>
            <Tooltip title="修改">
              <EditOutlined
                style={{ fontSize: '16px', color: '#1890FF' }}
                onClick={() => {
                  editor(record);
                }}
              >
                编辑
              </EditOutlined>
            </Tooltip>
            <div onClick={e => e.stopPropagation()}>
              <Popconfirm
                title="是否删除该字段?"
                okText="确认"
                cancelText="取消"
                onConfirm={() => {
                  deleteChart(record);
                }}
              >
                <Tooltip title="删除">
152
                  <DeleteOutlined style={{ fontSize: '16px', color: '#e86060' }}>
皮倩雯's avatar
皮倩雯 committed
153 154 155 156 157 158 159 160 161
                    删除
                  </DeleteOutlined>
                </Tooltip>
              </Popconfirm>
            </div>
          </Space>
        ),
      },
    ];
shaoan123's avatar
shaoan123 committed
162

皮倩雯's avatar
皮倩雯 committed
163 164 165
    return (
      <Table
        columns={columns}
166 167 168 169 170 171 172 173 174 175
        onRow={record => ({
          onDoubleClick: event => {
            event.stopPropagation();
            editor(record);
          },
          onClick: event => {
            event.stopPropagation();
            setSelectTableName(record);
          }, // 点击行
        })}
皮倩雯's avatar
皮倩雯 committed
176 177 178 179 180 181 182 183 184
        bordered
        rowClassName={setRowClassName}
        showHeader={false}
        dataSource={allData[item.type]}
        pagination={false}
      />
    );
  };
  const setRowClassName = record =>
185
    Object.entries(record).toString() === Object.entries(selectTableName).toString()
皮倩雯's avatar
皮倩雯 committed
186 187 188 189 190 191 192 193 194
      ? styles.clickRowStyle
      : '';
  const columns = [
    {
      title: '字段名',
      dataIndex: 'type',
      key: 'type',
      align: 'left',
      width: 150,
195 196 197 198 199
      render: text => (
        <a>
          {text}(共{allData[text] ? allData[text].length : ''}条)
        </a>
      ),
皮倩雯's avatar
皮倩雯 committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    },
    {
      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,
    },
229

皮倩雯's avatar
皮倩雯 committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    {
      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',
    },
  ];
  useEffect(() => {
253
    console.log(props);
皮倩雯's avatar
皮倩雯 committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    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') {
          let arr = formateArrDataA(res.data.root, 'group');
          let newArr = [];
          Object.keys(arr).map((item, index) => {
            newArr.push({ type: item, key: index, id: index });
          });
          setAllData(arr);
          setTableData(newArr);
269
          console.log(newArr);
皮倩雯's avatar
皮倩雯 committed
270
          setSelect(newArr);
271
        }
皮倩雯's avatar
皮倩雯 committed
272 273
      });
      getField();
274
    }
皮倩雯's avatar
皮倩雯 committed
275 276 277 278 279
  }, [flag]);
  const formateArrDataA = (initialArr, name) => {
    // 判定传参是否符合规则
    if (!(initialArr instanceof Array)) {
      return '请传入正确格式的数组';
shaoan123's avatar
shaoan123 committed
280
    }
皮倩雯's avatar
皮倩雯 committed
281 282 283
    if (!name) {
      return '请传入对象属性';
    }
284
    // 先获取一下这个数组中有多少个"name"
皮倩雯's avatar
皮倩雯 committed
285
    let nameArr = [];
286
    // eslint-disable-next-line no-restricted-syntax
皮倩雯's avatar
皮倩雯 committed
287 288 289 290 291
    for (let i in initialArr) {
      if (nameArr.indexOf(initialArr[i][`${name}`]) === -1) {
        nameArr.push(initialArr[i][`${name}`]);
      }
    }
292
    // 新建一个包含多个list的结果对象
皮倩雯's avatar
皮倩雯 committed
293 294 295 296 297 298 299 300
    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]);
shaoan123's avatar
shaoan123 committed
301
        }
皮倩雯's avatar
皮倩雯 committed
302
      }
303
    }
皮倩雯's avatar
皮倩雯 committed
304 305 306 307 308 309 310
    for (let keys in tempObj) {
      let arr = [];
      tempObj[keys].map((item, index) => {
        tempObj[keys] = arr;
        item.key = index;
        arr.push(item);
      });
311
    }
皮倩雯's avatar
皮倩雯 committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
    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 = () => {
335 336 337
    console.log(props.location.state);
    let { template, tableScroll, keepValue } = props.location.state;
    console.log(keepValue);
皮倩雯's avatar
皮倩雯 committed
338
    history.push({
339
      pathname: '/bsmanger/base/tablemanger',
340 341 342
      query: {
        template,
        tableScroll,
343
        keepValue,
344
      },
皮倩雯's avatar
皮倩雯 committed
345 346 347 348 349 350 351 352 353
    });
  };
  const onUnfold = (expanded, record) => {
    const data = [...select];
    let index = data.indexOf(record);
    if (expanded) {
      data.push(record);
    } else {
      data.splice(index, 1);
354
    }
皮倩雯's avatar
皮倩雯 committed
355
    setSelect(data);
356 357 358 359 360 361 362
    let arr = [];
    arr.push(record.key);
    if (expanded === true) {
      setChooseLine(arr);
    } else {
      setChooseLine([]);
    }
皮倩雯's avatar
皮倩雯 committed
363
  };
364
  // 附加
皮倩雯's avatar
皮倩雯 committed
365
  const add = record => {
皮倩雯's avatar
皮倩雯 committed
366
    setPramFormObj(props.location.state.template);
皮倩雯's avatar
皮倩雯 committed
367 368 369
    setType('affiliateAdd');
    setVisible(true);
  };
370
  // 分组与排序
皮倩雯's avatar
皮倩雯 committed
371
  const sort = record => {
皮倩雯's avatar
皮倩雯 committed
372
    setPramFormObj(props.location.state.template);
皮倩雯's avatar
皮倩雯 committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386
    setType('sort');
    setVisible(true);
  };
  const onSubmit = prop => {
    setVisible(false);
    setFlag(flag + 1);
  };
  return (
    <>
      <Spin tip="loading..." spinning={treeLoading}>
        <div className={styles.containerBox}>
          <div className={styles.config}>
            <div className={styles.title}>{formObj}(字段配置)</div>
            <div className={styles.btn}>
387
              <Button type="primary" icon={<PlusSquareOutlined />} onClick={add}>
皮倩雯's avatar
皮倩雯 committed
388 389
                附加
              </Button>
390
              <Button type="primary" icon={<SortDescendingOutlined />} onClick={sort}>
皮倩雯's avatar
皮倩雯 committed
391 392 393 394 395
                分组排序
              </Button>
              <Button type="primary" icon={<RollbackOutlined />} onClick={back}>
                返回
              </Button>
shaoan123's avatar
shaoan123 committed
396
            </div>
皮倩雯's avatar
皮倩雯 committed
397
          </div>
398

皮倩雯's avatar
皮倩雯 committed
399 400 401 402 403 404
          <Table
            columns={columns}
            dataSource={tableData}
            expandable={{ expandedRowRender }}
            size="small"
            rowKey="id"
405 406 407 408 409
            defaultExpandAllRows={true}
            // expandedRowKeys={chooseLine} // 展开的行
            // defaultExpandedRowKeys={[0]} // 展开的行
            expandedRowKeys={select.map(item => item.key)} // 展开的行
            // defaultExpandedRowKeys={0}
410
            expandRowByClick
皮倩雯's avatar
皮倩雯 committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
            pagination={false}
            scroll={{ y: 'calc(100vh - 186px)' }}
            onExpand={onUnfold}
          />
        </div>
      </Spin>
      <FieldEditor
        isVisible={isVisible}
        isType={isType}
        itemData={itemData}
        formObj1={formObj}
        onCancel={() => setIsVisible(false)}
        callBackSubmit={Submit}
      />
      {visible && type === 'affiliateAdd' && (
        <AffiliateAdd
          visible={visible}
          tableList={tableList}
          type={type}
          onCancel={() => setVisible(false)}
          callBackSubmit={onSubmit}
          formObj={pramFormObj}
        />
      )}
shaoan123's avatar
shaoan123 committed
435

皮倩雯's avatar
皮倩雯 committed
436 437 438 439 440 441 442 443 444 445 446
      {visible && type === 'sort' && (
        <LoadGroup
          visible={visible}
          type={type}
          formObj={pramFormObj}
          onCancel={() => setVisible(false)}
          callBackSubmit={onSubmit}
        />
      )}
    </>
  );
447 448
};
export default AddModal;