affiliateAdd.jsx 10.4 KB
Newer Older
1 2 3
import React, { useState, useEffect } from 'react';
import { Form, Modal, Input, Select, Checkbox, notification, Card } from 'antd';

邓超's avatar
邓超 committed
4
import { loadTableFields, addTables, addFields } from '@/services/tablemanager/tablemanager';
5
import styles from './index.less';
6 7 8
const CheckboxGroup = Checkbox.Group;
const { Option } = Select;
const AddModal = props => {
9 10 11 12
  const { callBackSubmit = () => {}, type, visible, tableList, formObj } = props;
  const [loading, setLoading] = useState(false);
  const [form] = Form.useForm();
  const { Item } = Form;
13
  const [plainOptions, setPlainOptions] = useState([]); // 复选框所有内容
皮倩雯's avatar
皮倩雯 committed
14
  const [plainOptionsInside, setPlainOptionsInside] = useState([]); // 复选框所有内容
15
  const [checkedList, setCheckedList] = useState([]); // 选中的复选框内容
16 17
  const [indeterminate, setIndeterminate] = useState(true);
  const [checkAll, setCheckAll] = useState(false);
皮倩雯's avatar
皮倩雯 committed
18 19
  const [indeterminate1, setIndeterminate1] = useState(false); // 内置分组
  const [checkAll1, setCheckAll1] = useState(false); // 内置分组
20

21 22
  const onChange = list => {
    setCheckedList(list);
皮倩雯's avatar
皮倩雯 committed
23 24 25 26 27 28 29 30 31 32 33 34 35
    let data = [];
    if (plainOptionsInside.length > 0) {
      plainOptionsInside.map(i => {
        if (i.isCheck) {
          data.push(i);
        }
      });
    }

    let aa = data.length + list.length;
    let bb = plainOptionsInside.length + plainOptions.length;
    setIndeterminate(!!aa && aa < bb);
    setCheckAll(aa == bb);
36
  };
37

38 39 40 41
  const onCheckAllChange = e => {
    setCheckedList(e.target.checked ? plainOptions : []);
    setIndeterminate(false);
    setCheckAll(e.target.checked);
皮倩雯's avatar
皮倩雯 committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    let data = [...plainOptionsInside];
    data.map(i => {
      i.isCheck = e.target.checked;
    });
    setPlainOptionsInside(data);
    setIndeterminate1(false);
    setCheckAll1(e.target.checked);
  };

  const onCheckAllChange1 = e => {
    let data = [...plainOptionsInside];
    data.map(i => {
      i.isCheck = e.target.checked;
    });
    let list = [];
    data.map(j => {
      if (j.isCheck) {
        list.push(j);
      }
    });
    setPlainOptionsInside(data);
    setIndeterminate1(false);
    setCheckAll1(e.target.checked);
    let aa = list.length + checkedList.length;
    let bb = plainOptionsInside.length + plainOptions.length;
    setCheckAll(aa == bb);
    setIndeterminate(!!aa && aa < bb);
69 70 71 72 73 74 75
  };
  // 提交
  const onSubmit = () => {
    let fieldNames = [];
    (checkedList || []).map(item => {
      fieldNames.push({ fieldNames: item });
    });
皮倩雯's avatar
皮倩雯 committed
76 77 78 79 80 81 82 83 84
    let list = [];
    if (plainOptionsInside.length > 0) {
      plainOptionsInside.map(i => {
        if (i.isCheck) {
          list.push(i.fieldName);
        }
      });
    }
    let datalist = checkedList.concat(list);
85 86 87 88
    form.validateFields().then(validate => {
      if (validate) {
        let obj = form.getFieldsValue();
        setLoading(true);
皮倩雯's avatar
皮倩雯 committed
89 90
        let data = {};
        let url = JSON.stringify(formObj) == '{}' ? addTables : addFields;
91 92 93 94
        if (JSON.stringify(formObj) == '{}') {
          data = {
            tableName: obj.tableName,
            alias: obj.tableAlias || '',
皮倩雯's avatar
皮倩雯 committed
95
            fieldNames: datalist.join(','),
96 97
          };
        } else {
皮倩雯's avatar
皮倩雯 committed
98
          data = { tableName: formObj.tableName, fieldNames: datalist.join(',') };
99
        }
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        url(data)
          .then(res => {
            setLoading(false);
            if (res.msg === 'Ok' || res.msg === '') {
              form.resetFields();
              callBackSubmit();
              notification.success({
                message: '提示',
                duration: 3,
                description: JSON.stringify(formObj) == '{}' ? '新增成功' : '表字段新增成功',
              });
            } else {
              notification.error({
                message: '提示',
                duration: 3,
                description: res.msg,
              });
117
            }
118 119 120 121 122 123 124 125 126 127 128 129
          })
          .catch(err => {
            notification.error({
              message: '提示',
              duration: 3,
              description: '新增失败',
            });
            setLoading(false);
          });
      }
    });
  };
130

131 132 133 134 135 136 137 138
  useEffect(() => {
    if (type != '' && JSON.stringify(formObj) == '{}') {
      renderField(tableList[0].text);
      form.setFieldsValue({
        tableName: tableList[0].text,
      });
    } else if (type != '' && JSON.stringify(formObj) != '{}') {
      renderField(formObj.tableName);
139
    }
140
  }, [visible]);
141

142 143 144 145
  const renderField = name => {
    loadTableFields({ tableName: name }).then(response => {
      if (response.data.root.length) {
        // eslint-disable-next-line one-var
皮倩雯's avatar
皮倩雯 committed
146 147 148
        let arr = [];
        let newArr = [];
        let insideArr = [];
149
        response.data.root.map(item => {
皮倩雯's avatar
皮倩雯 committed
150 151 152 153 154 155 156
          if (item.isDefaultField == true) {
            insideArr.push({ fieldName: item.fieldName, isCheck: item.isCheck });
          } else {
            newArr.push(item.fieldName);
            if (item.isCheck) {
              arr.push(item.fieldName);
            }
157 158
          }
        });
皮倩雯's avatar
皮倩雯 committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        let list = [];
        if (insideArr.length > 0) {
          insideArr.map(i => {
            if (i.isCheck) {
              list.push(i);
            }
          });
          setCheckAll1(list.length === insideArr.length);
          setIndeterminate1(!!list.length && list.length < insideArr.length);
        }

        let aa = arr.length + list.length;
        let bb = newArr.length + insideArr.length;
        setCheckAll(aa == bb);
        setIndeterminate(!!aa && aa < bb);
174
        setPlainOptions(newArr);
皮倩雯's avatar
皮倩雯 committed
175
        setPlainOptionsInside(insideArr);
176 177 178 179 180 181 182 183 184 185
        setCheckedList(arr);
      } else {
        setCheckAll(false);
        setIndeterminate(false);
      }
    });
  };
  const layout = {
    layout: 'horizontal',
    labelCol: {
186
      span: 3,
187 188
    },
    wrapperCol: {
189
      span: 20,
190 191
    },
  };
192

193 194 195
  const handleChange = value => {
    renderField(value);
  };
196

皮倩雯's avatar
皮倩雯 committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  const onchenge = (e, item) => {
    let data = [...plainOptionsInside];
    data.map(i => {
      if (i.fieldName == item) {
        i.isCheck = e.target.checked;
      }
    });
    setPlainOptionsInside(data);
    let list = [];
    data.map(j => {
      if (j.isCheck) {
        list.push(j);
      }
    });
    setIndeterminate1(!!list.length && list.length < plainOptionsInside.length);
    setCheckAll1(list.length === plainOptionsInside.length);
    let aa = list.length + checkedList.length;
    let bb = plainOptionsInside.length + plainOptions.length;
    setIndeterminate(!!aa && aa < bb);
    setCheckAll(aa == bb);
  };

219 220 221 222 223 224 225 226 227 228 229 230 231
  return (
    <Modal
      title={JSON.stringify(formObj) == '{}' ? '附加表' : '附加字段'}
      bodyStyle={{ width: '100%', minHeight: '200px' }}
      style={{ top: '150px' }}
      width="700px"
      destroyOnClose
      maskClosable={false}
      cancelText="取消"
      okText="确认"
      {...props}
      onOk={() => onSubmit()}
      confirmLoading={loading}
皮倩雯's avatar
皮倩雯 committed
232
      forceRender
233 234 235 236 237 238 239 240 241 242 243 244 245
      getContainer={false}
    >
      {visible && (
        <Form form={form} {...layout} style={{ marginBottom: '30px' }}>
          {JSON.stringify(formObj) == '{}' ? (
            <>
              <Item
                label="表名"
                name="tableName"
                rules={[{ required: true, message: '请选择表名' }]}
              >
                <Select onChange={handleChange} showSearch>
                  {tableList.length
皮倩雯's avatar
皮倩雯 committed
246
                    ? tableList.map((item, index) => (
247 248
                        <Select.Option key={index} value={item.text}>
                          {item.text}
皮倩雯's avatar
皮倩雯 committed
249 250
                        </Select.Option>
                      ))
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
                    : ''}
                </Select>
              </Item>
              <Item label="别名" name="tableAlias">
                <Input allowClear />
              </Item>
            </>
          ) : (
            <div className={styles.paneTitle}>{formObj.tableName}</div>
          )}
          <div className={styles.field}>
            <Card
              title="字段名"
              extra={
                <Checkbox
                  indeterminate={indeterminate}
                  onChange={onCheckAllChange}
                  checked={checkAll}
                >
                  {' '}
                </Checkbox>
              }
            >
皮倩雯's avatar
皮倩雯 committed
274
              {plainOptions.length > 0 && (
275
                <CheckboxGroup options={plainOptions} value={checkedList} onChange={onChange} />
皮倩雯's avatar
皮倩雯 committed
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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
              )}
              {plainOptionsInside.length > 0 ? (
                <>
                  <div
                    style={{
                      position: 'relative',
                      top: '12px',
                      marginLeft: '16px',
                      backgroundColor: 'white',
                      width: '100px',
                      paddingLeft: '5px',
                    }}
                  >
                    <Checkbox
                      style={{ color: 'red' }}
                      indeterminate={indeterminate1}
                      onChange={onCheckAllChange1}
                      checked={checkAll1}
                    >
                      <strong>内置字段</strong>
                    </Checkbox>
                  </div>
                  <div
                    style={{
                      display: 'flex',
                      flexWrap: 'wrap',
                      border: '1px solid #d7d3d3',
                      borderRadius: '2px',
                      paddingTop: '20px',
                    }}
                  >
                    {plainOptionsInside.map(i => (
                      <div
                        style={{
                          width: '110px',
                          overflow: 'hidden',
                          paddingBottom: '10px',
                          marginLeft: '20px',
                          textOverflow: 'ellipsis',
                          whiteSpace: 'nowrap',
                        }}
                      >
                        <Checkbox
                          style={{ color: 'red' }}
                          checked={i.isCheck}
                          onChange={e => {
                            onchenge(e, i.fieldName);
                          }}
                        >
                          {i.fieldName}
                        </Checkbox>
                      </div>
                    ))}
                  </div>
                </>
331
              ) : (
皮倩雯's avatar
皮倩雯 committed
332
                <></>
333 334 335 336 337 338 339
              )}
            </Card>
          </div>
        </Form>
      )}
    </Modal>
  );
340 341
};
export default AddModal;