BookConfig.jsx 15.7 KB
Newer Older
1
/* eslint-disable no-lonely-if */
2 3
/* eslint-disable no-else-return */
/* eslint-disable prefer-promise-reject-errors */
4 5 6 7
/* eslint-disable camelcase */
import React, { useState, useEffect } from 'react';
import { Form, Input, Select, Tooltip, Button, notification, Drawer, Space } from 'antd';
import { PlusOutlined, InfoCircleOutlined } from '@ant-design/icons';
邓超's avatar
邓超 committed
8
import { LoadEventFields } from '@/services/tablemanager/tablemanager';
9 10 11 12 13 14 15 16 17 18
import {
  GetCM_Ledger_LoadLedgerTable,
  GetCMLedger_QueryLedgers,
  GetCMLedger_OperateLedger,
} from '@/services/standingBook/api';
import ChangeAdd from './changeAdd';

const { Option } = Select;
const { TextArea } = Input;
const BookConfig = props => {
19 20 21 22 23 24
  const {
    callBackSubmit,
    type,
    formObj,
    visible,
    tableData,
25
    pickItem1,
26
    onCancel,
27
    data,
28 29 30
    maxLength,
    keepTableData,
  } = props;
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  const [standingTable, setStandingTable] = useState([]);
  const [isVisible, setIsVisible] = useState(false); // 弹窗
  const [pickItem, setPickItem] = useState(''); // 选择的字段
  const [Order, setOrder] = useState(''); // 当前编辑序号
  const [filed, setFiled] = useState({}); // 传给子组件列表数据
  const [checkedList, setCheckedList] = useState([]);
  const [allFileds, setAllFileds] = useState([]); // 当前表所有的字段
  const [form] = Form.useForm();
  const layout = {
    layout: 'horizontal',
    labelCol: {
      span: 5,
    },
    wrapperCol: {
      span: 19,
    },
  };
  const { Item } = Form;
  // 提交
  const onSubmit = () => {
    form.validateFields().then(validate => {
      if (validate) {
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        let aa = form.getFieldsValue().Type;
        if (aa == '全部') {
          notification.warning({
            message: '提示',
            duration: 3,
            description: '分组名称不能为全部',
          });
        } else {
          let obj =
            type === 'add'
              ? { ...validate, Order: maxLength }
              : { ...validate, Order, ID: formObj.ID };
          GetCMLedger_OperateLedger(obj)
            .then(res => {
              if (res.code === 0) {
                form.resetFields();
                callBackSubmit();
                notification.success({
                  message: '提示',
                  duration: 3,
                  description: type === 'add' ? '新增成功' : '编辑成功',
                });
              } else {
                notification.error({
                  message: '提示',
                  duration: 3,
                  description: res.msg,
                });
              }
            })
            .catch(() => {
84 85 86
              notification.error({
                message: '提示',
                duration: 3,
87
                description: '网络异常请稍后再试',
88 89
              });
            });
90
        }
91 92 93 94
      }
    });
  };
  useEffect(() => {
95 96
    console.log(pickItem1);
    console.log(data[0]);
97 98 99 100 101 102 103 104 105 106 107
    if (visible) {
      // 获取台账表
      getTableData();
      if (type === 'edit') {
        GetCMLedger_QueryLedgers({ ledgerId: formObj.ID }).then(res => {
          if (res.code === 0) {
            form.setFieldsValue(res.data.root);
            setOrder(res.data.root.Order);
            changTable(res.data.root.TableName);
          }
        });
108 109 110 111 112 113
      } else {
        if (!pickItem1 || pickItem1 == '全部') {
          form.setFieldsValue({ Type: data[0] });
        } else {
          form.setFieldsValue({ Type: pickItem1 });
        }
114 115 116 117 118 119 120 121 122 123 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 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
      }
    } else {
      setFiled({});
      form.resetFields();
      form.setFieldsValue({ AccountType: '台账' });
    }
  }, [visible]);

  // 获取台账表
  const getTableData = () => {
    GetCM_Ledger_LoadLedgerTable().then(res => {
      if (res.code === 0) {
        setStandingTable(res.data.root);
      }
    });
  };
  // 切换表后数据处理为对应格式
  const changTable = value => {
    LoadEventFields({ eventTableName: value, distinctFields: '' }).then(res => {
      if (res.data.root) {
        let fileMap = new Map();
        let initList = [];
        // 处理为子组件需要的格式
        res.data.root.forEach(item => {
          initList.push(item.fieldName);
          if (fileMap.has(item.group)) {
            let list = [...fileMap.get(item.group)];
            list.push(item.fieldName);
            fileMap.set(item.group, list);
          } else {
            fileMap.set(item.group, [item.fieldName]);
          }
        });
        // 给Map格式转为对象
        fileMap = Object.fromEntries(fileMap.entries());
        // 处理外部字段
        Object.keys(form.getFieldsValue()).forEach(key => {
          saveOutFieldsLength(key, initList);
        });
        setAllFileds(initList);
        setFiled(fileMap);
      }
    });
  };
  // 保存外部字段个数
  const saveOutFieldsLength = (key, initList) => {
    switch (key) {
      case 'Fields':
        form.setFieldsValue({ outListFileds: dealExternal(key, initList) });
        break;
      case 'SearchFields':
        form.setFieldsValue({ outSearchFields: dealExternal(key, initList) });
        break;
      case 'AddFields':
        form.setFieldsValue({ outAddFields: dealExternal(key, initList) });
        break;
      case 'EditFields':
        form.setFieldsValue({ outEditFields: dealExternal(key, initList) });
        break;
      case 'WebFields':
        form.setFieldsValue({ outWebFields: dealExternal(key, initList) });
        break;
      case 'MobileFields':
        form.setFieldsValue({ outMobileFields: dealExternal(key, initList) });
        break;
      default:
        break;
    }
  };
  // 选择字段回调函数
  const onOK = prop => {
    setIsVisible(false);
    let obj = {};
    obj[prop.pickItem] = prop.str;
    form.setFieldsValue(obj);
    saveOutFieldsLength(prop.pickItem, allFileds);
  };
  // 处理外部字段
  const dealExternal = (fileds, list) => {
    let isExternal;
    let externalLength = 0;
    if (form.getFieldValue(fileds)) {
      form
        .getFieldValue(fileds)
        .split(',')
        .forEach(item => {
          isExternal = list.some(val => val === item);
          if (!isExternal && item !== '') {
            // eslint-disable-next-line no-plusplus
            externalLength++;
          }
        });
    }

    return externalLength;
  };
  // 勾选字段
  const pickFiled = fileds => {
    if (!form.getFieldValue('TableName')) {
      notification.error({ message: '提示', duration: 3, description: '请选择台账表' });
      return;
    }
    // 添加外部字段
217 218 219 220 221 222 223 224
    let fil = { ...filed };
    fil['外部字段'] = [];
    let isExternal;
    let list = form.getFieldValue(fileds) ? form.getFieldValue(fileds).split(',') : [];
    list.forEach(item => {
      isExternal = allFileds.some(val => val === item);
      if (!isExternal && item !== '') {
        fil['外部字段'].push(item);
225
      }
226 227 228
    });
    if (fil['外部字段'].length === 0) {
      delete fil['外部字段'];
229
    }
230 231
    setFiled(fil);
    setCheckedList(list);
232 233 234 235 236 237 238 239 240
    setPickItem(fileds);
    setIsVisible(true);
  };
  // 搜索框监听
  const onSearch = value => {
    if (value) {
      form.setFieldsValue({ Type: value });
    }
  };
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
  return (
    <Drawer
      title={`${type === 'add' ? '台账配置' : '台账编辑'}`}
      width="500px"
      visible={visible}
      onClose={onCancel}
      destroyOnClose
      footer={
        <Space>
          <Button onClick={onCancel}>取消</Button>
          <Button onClick={onSubmit} type="primary">
            确定
          </Button>
        </Space>
      }
    >
      <Form form={form} {...layout}>
        <Item label="分组" name="Type" rules={[{ required: true, message: '请选择分组' }]}>
          <Select
            showSearch
            filterOption={false}
            onSearch={onSearch}
            placeholder="请输入分组名称"
            allowClear
          >
267
            {data.map((item, index) => (
268 269 270 271 272 273 274 275 276 277 278 279 280
              <Option value={item} key={index}>
                {item}
              </Option>
            ))}
          </Select>
        </Item>
        <Item label="台账类型" name="AccountType">
          <Select placeholder="请选择台账类型">
            <Option value="台账">台账</Option>
            <Option value="反馈">反馈</Option>
            <Option value="设备">设备</Option>
          </Select>
        </Item>
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
        <Item
          label="台账名称"
          name="Name"
          rules={[
            {
              required: true,
              validator: (rule, value) => {
                if (
                  keepTableData.find(i => i.name == form.getFieldsValue().Name) &&
                  form.getFieldsValue().Name != formObj.name
                ) {
                  return Promise.reject('台账名称已存在');
                } else if (form.getFieldsValue().Name == '') {
                  return Promise.reject('台账名称不能为空');
                }
                return Promise.resolve();
              },
            },
          ]}
        >
301 302 303
          <Input placeholder="台账名称不可重复" allowClear />
        </Item>
        <Item label="台账表" name="TableName" rules={[{ required: true, message: '请选择台账表' }]}>
304
          <Select placeholder="" optionFilterProp="children" onChange={changTable} showSearch>
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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
            {standingTable.map((item, index) => (
              <Option key={index} value={item.value}>
                {item.text}
              </Option>
            ))}
          </Select>
        </Item>
        <Item
          label={
            <>
              {form.getFieldValue('outListFileds') > 0 ? (
                <Tooltip title={`外部字段${form.getFieldValue('outListFileds')}个`}>
                  <InfoCircleOutlined style={{ color: 'red', margin: '2px 3px 0 3px' }} />
                </Tooltip>
              ) : (
                ''
              )}
              <span>台账字段</span>
            </>
          }
          name="Fields"
        >
          <div style={{ display: 'flex' }}>
            <Form.Item name="Fields" style={{ marginBottom: 0, width: '100%' }}>
              <TextArea placeholder="前端详情查看字段" allowClear />
            </Form.Item>
            <Button
              type="dashed"
              style={{ height: '54px', width: '50px', marginLeft: '10px' }}
              icon={<PlusOutlined />}
              onClick={() => {
                pickFiled('Fields');
              }}
            />
          </div>
        </Item>
        <Item
          label={
            <>
              {form.getFieldValue('outSearchFields') > 0 ? (
                <Tooltip title={`外部字段${form.getFieldValue('outSearchFields')}个`}>
                  <InfoCircleOutlined style={{ color: 'red', margin: '2px 3px 0 3px' }} />
                </Tooltip>
              ) : (
                ''
              )}
              <span>检索字段</span>
            </>
          }
          name="SearchFields"
        >
          <div style={{ display: 'flex' }}>
            <Form.Item name="SearchFields" style={{ marginBottom: 0, width: '100%' }}>
              <TextArea placeholder="前端列表检索字段" allowClear />
            </Form.Item>
            <Button
              type="dashed"
              style={{ height: '54px', width: '50px', marginLeft: '10px' }}
              icon={<PlusOutlined />}
              onClick={() => {
                pickFiled('SearchFields');
              }}
            />
          </div>
        </Item>
        <Item
          label={
            <>
              {form.getFieldValue('outAddFields') > 0 ? (
                <Tooltip title={`外部字段${form.getFieldValue('outAddFields')}个`}>
                  <InfoCircleOutlined style={{ color: 'red', margin: '2px 3px 0 3px' }} />
                </Tooltip>
              ) : (
                ''
              )}
              <span>添加字段</span>
            </>
          }
          name="AddFields"
        >
          <div style={{ display: 'flex' }}>
            <Form.Item name="AddFields" style={{ marginBottom: 0, width: '100%' }}>
              <TextArea placeholder="前端数据添加字段" allowClear />
            </Form.Item>
            <Button
              type="dashed"
              style={{ height: '54px', width: '50px', marginLeft: '10px' }}
              icon={<PlusOutlined />}
              onClick={() => {
                pickFiled('AddFields');
              }}
            />
          </div>
        </Item>
        <Item
          label={
            <>
              {form.getFieldValue('outEditFields') > 0 ? (
                <Tooltip title={`外部字段${form.getFieldValue('outEditFields')}个`}>
                  <InfoCircleOutlined style={{ color: 'red', margin: '2px 3px 0 3px' }} />
                </Tooltip>
              ) : (
                ''
              )}
              <span>编辑字段</span>
            </>
          }
          name="EditFields"
        >
          <div style={{ display: 'flex' }}>
            <Form.Item name="EditFields" style={{ marginBottom: 0, width: '100%' }}>
              <TextArea placeholder="前端可编辑字段" allowClear />
            </Form.Item>
            <Button
              type="dashed"
              style={{ height: '54px', width: '50px', marginLeft: '10px' }}
              icon={<PlusOutlined />}
              onClick={() => {
                pickFiled('EditFields');
              }}
            />
          </div>
        </Item>
        <Item
          label={
            <>
              {form.getFieldValue('outWebFields') > 0 ? (
                <Tooltip title={`外部字段${form.getFieldValue('outWebFields')}个`}>
                  <InfoCircleOutlined style={{ color: 'red', margin: '2px 3px 0 3px' }} />
                </Tooltip>
              ) : (
                ''
              )}
              <span>前端字段</span>
            </>
          }
          name="WebFields"
        >
          <div style={{ display: 'flex' }}>
            <Form.Item name="WebFields" style={{ marginBottom: 0, width: '100%' }}>
              <TextArea placeholder="前端列表展示字段" allowClear />
            </Form.Item>
            <Button
              type="dashed"
              style={{ height: '54px', width: '50px', marginLeft: '10px' }}
              icon={<PlusOutlined />}
              onClick={() => {
                pickFiled('WebFields');
              }}
            />
          </div>
        </Item>
        <Item
          label={
            <>
              {form.getFieldValue('outMobileFields') > 0 ? (
                <Tooltip title={`外部字段${form.getFieldValue('outMobileFields')}个`}>
                  <InfoCircleOutlined style={{ color: 'red', margin: '2px 3px 0 3px' }} />
                </Tooltip>
              ) : (
                ''
              )}
              <span>手持字段</span>
            </>
          }
          name="MobileFields"
        >
          <div style={{ display: 'flex' }}>
            <Form.Item name="MobileFields" style={{ marginBottom: 0, width: '100%' }}>
              <TextArea placeholder="手持展示字段" allowClear />
            </Form.Item>
            <Button
              type="dashed"
              style={{ height: '54px', width: '50px', marginLeft: '10px' }}
              icon={<PlusOutlined />}
              onClick={() => {
                pickFiled('MobileFields');
              }}
            />
          </div>
        </Item>
        <Item label="接口配置" name="Interface">
          <Input placeholder="服务项目dll库" allowClear />
        </Item>
      </Form>
      <ChangeAdd
        visible={isVisible}
492 493 494 495
        onCancel={() => {
          setIsVisible(false);
          setCheckedList([]);
        }}
496 497 498 499 500 501 502 503 504 505
        callBackSubmit={onOK}
        newCheckedList={checkedList}
        filed={filed}
        pickItem={pickItem}
        formObj={formObj}
      />
    </Drawer>
  );
};
export default BookConfig;