AppDic.jsx 12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import React, { useState, useEffect } from 'react';
import {
  Table,
  Tooltip,
  Spin,
  Modal,
  Form,
  Input,
  Space,
  Button,
  Popconfirm,
  notification,
  message,
} from 'antd';
15
import { get, CITY_SERVICE, PUBLISH_SERVICE } from '@/services';
16 17
import { GetKeyValue, EditKeyValue, DeleteKeyValue, AddKeyValue } from '@/services/dataCenter/api';
import { EditTwoTone, DeleteOutlined, PlusSquareOutlined, SyncOutlined } from '@ant-design/icons';
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
import styles from './AppDic.less';

const AppDic = () => {
  const [loading, setLoading] = useState(false);
  const [addVisible, setAddVisible] = useState(false);
  const [editVisible, setEditVisible] = useState(false);
  const [data, setData] = useState([]); // 表数据
  const [select, setSelect] = useState({}); // 当前选中条目,修改/删除时设置
  const [addForm] = Form.useForm();
  const [editForm] = Form.useForm();

  const columns = [
    {
      title: '名称',
      dataIndex: 'Label',
      key: 'Label',
    },
    {
      title: '键名',
      dataIndex: 'Key',
      key: 'Key',
    },
    {
      title: '值',
      dataIndex: 'Value',
      key: 'Value',
    },
    {
      title: '描述',
      dataIndex: 'Description',
      key: 'Description',
      render: record => {
        if (!record) {
          return '-';
        }
        return record;
      },
    },
    {
      title: '操作',
      key: 'action',
      width: 100,
      align: 'center',
      render: record => (
        <Space>
          <Tooltip title="编辑">
            <EditTwoTone
              onClick={() => {
                setSelect(record);
                setEditVisible(true);
                editForm.setFieldsValue({
                  label: record.Label,
                  key: record.Key,
                  value: record.Value,
                  description: record.Description,
                });
              }}
              style={{ fontSize: '16px' }}
            />
          </Tooltip>
          <Popconfirm
            title="是否删除该数据?"
            okText="确认"
            cancelText="取消"
            onConfirm={submitDelete}
          >
皮倩雯's avatar
皮倩雯 committed
84 85 86 87 88 89 90 91 92 93 94 95
            <Tooltip title="删除">
              <DeleteOutlined
                onClick={() => {
                  setSelect(record);
                }}
                style={{
                  fontSize: '16px',
                  margin: '0px 10px',
                  color: '#e86060',
                }}
              />
            </Tooltip>
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
          </Popconfirm>
        </Space>
      ),
    },
  ];

  useEffect(() => {
    getData('-1');
  }, []);
  const getData = () => {
    setLoading(true);
    // get(`${CITY_SERVICE}/OMS.svc/M_GetKeyValue`, {
    //   _version: 9999,
    //   _dc: new Date().getTime(),
    // })
    //   .then(res => {
    //     if (res) {
    //       if (res.length > 0) {
    //         res.map(item => {
    //           item.key = item.id;
    //           return item;
    //         });
    //       }
    //       setData(res);
    //     } else {
    //       notification.error({
    //         message: '获取失败',
    //         description: res.message,
    //       });
    //     }
    //     setLoading(false);
    //   })
    //   .catch(err => {
    //     setLoading(false);
    //     message.error(err);
    //   });
皮倩雯's avatar
皮倩雯 committed
132
    GetKeyValue({}).then(resnew => {
133
      if (resnew.code === 0) {
皮倩雯's avatar
皮倩雯 committed
134 135 136 137 138
        let res = resnew.data;
        if (res.length > 0) {
          res.map(item => {
            item.key = item.id;
            return item;
139 140
          });
        }
皮倩雯's avatar
皮倩雯 committed
141 142 143 144
        setData(res);
      } else {
        notification.error({
          message: '获取失败',
145
          // eslint-disable-next-line no-undef
皮倩雯's avatar
皮倩雯 committed
146 147
          description: res.msg,
        });
148
      }
皮倩雯's avatar
皮倩雯 committed
149 150
      setLoading(false);
    });
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
  };
  // 提交-添加
  const submitAdd = () => {
    const label = addForm.getFieldValue('label');
    const key = addForm.getFieldValue('key');
    const value = addForm.getFieldValue('value');
    const description = addForm.getFieldValue('description');
    if (label && key && value) {
      // get(`${CITY_SERVICE}/OMS.svc/M_AddKeyValue`, {
      //   _version: 9999,
      //   _dc: new Date().getTime(),
      //   label,
      //   key,
      //   value,
      //   desc: description,
      // })
      //   .then(res => {
      //     if (res.success) {
      //       setAddVisible(false);
      //       getData();
      //       notification.success({
      //         message: '提交成功',
      //       });
      //     } else {
      //       notification.error({
      //         message: '提交失败',
      //         description: res.message,
      //       });
      //     }
      //   })
      //   .catch(err => {
      //     message.error(err);
      //   });
      AddKeyValue({
        label,
        key,
        value,
        desc: description,
皮倩雯's avatar
皮倩雯 committed
189
      }).then(res => {
190
        if (res.code === 0) {
皮倩雯's avatar
皮倩雯 committed
191 192 193 194 195 196 197 198 199 200
          setAddVisible(false);
          getData();
          notification.success({
            message: '提交成功',
          });
        } else {
          notification.error({
            message: '提交失败',
            description: res.msg,
          });
201
        }
皮倩雯's avatar
皮倩雯 committed
202
      });
203 204 205 206 207 208 209 210 211
    } else {
      notification.error({
        message: '提交失败',
        description: '名称/键名/值不能为空',
      });
    }
  };
  // 提交-重置
  const submitReset = () => {
皮倩雯's avatar
皮倩雯 committed
212
    get(`${PUBLISH_SERVICE}/DataManger/ResetKeyValue`, {})
213
      .then(res => {
214
        if (res.code === 0) {
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 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
          getData();
          notification.success({
            message: '重置成功',
          });
        } else {
          notification.error({
            message: '重置失败',
            description: res.message,
          });
        }
      })
      .catch(err => {
        message.error(err);
      });
  };
  // 提交-编辑
  const submitEdit = () => {
    const label = editForm.getFieldValue('label');
    const key = editForm.getFieldValue('key');
    const value = editForm.getFieldValue('value');
    const description = editForm.getFieldValue('description');
    if (label && key && value) {
      // get(`${CITY_SERVICE}/OMS.svc/M_EditKeyValue`, {
      //   _version: 9999,
      //   _dc: new Date().getTime(),
      //   id: select.id,
      //   label,
      //   key,
      //   value,
      //   desc: description,
      // })
      //   .then(res => {
      //     if (res.success) {
      //       setEditVisible(false);
      //       getData();
      //       notification.success({
      //         message: '提交成功',
      //       });
      //     } else {
      //       notification.error({
      //         message: '提交失败',
      //         description: res.message,
      //       });
      //     }
      //   })
      //   .catch(err => {
      //     message.error(err);
      //   });
      EditKeyValue({
        id: select.id,
        label,
        key,
        value,
        desc: description,
皮倩雯's avatar
皮倩雯 committed
269
      }).then(res => {
270
        if (res.code === 0) {
皮倩雯's avatar
皮倩雯 committed
271 272 273 274 275 276 277 278 279 280
          setEditVisible(false);
          getData();
          notification.success({
            message: '提交成功',
          });
        } else {
          notification.error({
            message: '提交失败',
            description: res.msg,
          });
281
        }
皮倩雯's avatar
皮倩雯 committed
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
    } else {
      notification.error({
        message: '提交失败',
        description: '名称/键名/值不能为空',
      });
    }
  };

  const submitDelete = () => {
    // get(`${CITY_SERVICE}/OMS.svc/M_DeleteKeyValue`, {
    //   _version: 9999,
    //   _dc: new Date().getTime(),
    //   key: select.Key,
    // })
    //   .then(res => {
    //     if (res.success) {
    //       getData(select.parentID);
    //       notification.success({
    //         message: '删除成功',
    //       });
    //     } else {
    //       notification.error({
    //         message: '删除失败',
    //         description: res.message,
    //       });
    //     }
    //   })
    //   .catch(err => {
    //     message.error(err);
    //   });
皮倩雯's avatar
皮倩雯 committed
313

314 315
    DeleteKeyValue({
      key: select.Key,
皮倩雯's avatar
皮倩雯 committed
316 317 318 319 320 321 322 323 324 325 326
    }).then(res => {
      if (res.code === 0) {
        getData(select.parentID);
        notification.success({
          message: '删除成功',
        });
      } else {
        notification.error({
          message: '删除失败',
          description: res.msg,
        });
327
      }
皮倩雯's avatar
皮倩雯 committed
328
    });
329 330
  };

331 332 333 334 335 336
  const pagination = {
    showTotal: (total, range) => `第${range[0]}-${range[1]} 条/共 ${total} 条`,
    pageSizeOptions: [10, 20, 50, 100],
    defaultPageSize: 20,
    showQuickJumper: true,
    showSizeChanger: true,
皮倩雯's avatar
皮倩雯 committed
337
  };
338 339 340 341 342 343 344 345
  return (
    <div className={styles.AppDic}>
      <Spin spinning={loading} tip="loading...">
        <div
          style={{
            marginBottom: '10px',
            fontSize: '16px',
            height: 'calc(100vh-200px)',
皮倩雯's avatar
皮倩雯 committed
346 347
            display: 'flex',
            alignItems: 'center',
348
            justifyContent: 'space-between',
349 350
          }}
        >
皮倩雯's avatar
皮倩雯 committed
351
          <span>数据字典</span>
352
          <div>
353 354 355 356 357 358
            <Tooltip title="添加">
              <PlusSquareOutlined
                onClick={() => {
                  setAddVisible(true);
                  addForm.resetFields();
                }}
359 360 361
                style={{
                  color: '#1890FF',
                  fontSize: '20px',
362
                  marginRight: '22px',
363 364 365
                }}
              />
            </Tooltip>
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

            <Popconfirm
              title="是否重置默认配置?"
              okText="确认"
              cancelText="取消"
              onConfirm={submitReset}
            >
              <Tooltip title="还原">
                <SyncOutlined
                  style={{
                    color: '#1890FF',
                    fontSize: '20px',
                    marginRight: '30px',
                  }}
                />
              </Tooltip>
            </Popconfirm>
383
          </div>
384 385 386 387 388 389 390
        </div>
        <Table
          size="small"
          bordered
          columns={columns}
          dataSource={data}
          scroll={{ x: 'max-content' }}
391
          pagination={pagination}
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
        />
      </Spin>

      {/* 添加 */}
      <Modal
        title="添加数据字典"
        visible={addVisible}
        onOk={submitAdd}
        onCancel={() => {
          setAddVisible(false);
        }}
        okText="确认"
        cancelText="取消"
      >
        <Form form={addForm} labelCol={{ span: 3 }}>
407
          <Form.Item name="label" label="名称" rules={[{ required: true, message: '不能为空' }]}>
408 409
            <Input placeholder="请输入名称" />
          </Form.Item>
410
          <Form.Item name="key" label="键名" rules={[{ required: true, message: '不能为空' }]}>
411 412
            <Input placeholder="请输入键名" />
          </Form.Item>
413
          <Form.Item name="value" label="值" rules={[{ required: true, message: '不能为空' }]}>
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
            <Input placeholder="请输入值" />
          </Form.Item>
          <Form.Item name="description" label="描述">
            <Input placeholder="请输入相关描述" />
          </Form.Item>
        </Form>
      </Modal>
      {/* 修改 */}
      <Modal
        title="修改数据字典"
        visible={editVisible}
        onOk={submitEdit}
        onCancel={() => {
          setEditVisible(false);
        }}
        okText="确认"
        cancelText="取消"
      >
        <Form form={editForm} labelCol={{ span: 3 }}>
433
          <Form.Item name="label" label="名称" rules={[{ required: true, message: '不能为空' }]}>
434 435 436 437 438
            <Input placeholder="请输入名称" />
          </Form.Item>
          <Form.Item name="key" label="键名">
            <Input placeholder="请输入键名" />
          </Form.Item>
439
          <Form.Item name="value" label="值" rules={[{ required: true, message: '不能为空' }]}>
440 441 442 443 444 445 446 447 448 449 450 451
            <Input placeholder="请输入值" />
          </Form.Item>
          <Form.Item name="description" label="描述">
            <Input placeholder="请输入相关描述" />
          </Form.Item>
        </Form>
      </Modal>
    </div>
  );
};

export default AppDic;