InitDataBase.jsx 31.1 KB
Newer Older
皮倩雯's avatar
皮倩雯 committed
1 2
/* eslint-disable spaced-comment */
/* eslint-disable eqeqeq */
3
import React, { useEffect, useRef, useState } from 'react';
张烨's avatar
张烨 committed
4 5 6 7 8 9 10 11 12
import {
  Card,
  Form,
  Input,
  Button,
  Select,
  Table,
  Space,
  notification,
13 14
  Modal,
  Row,
张烨's avatar
张烨 committed
15
  Col,
Maofei94's avatar
Maofei94 committed
16
  Popconfirm,
17
  Spin,
18 19
  Tabs,
  Radio,
张烨's avatar
张烨 committed
20
} from 'antd';
21
import PageContainer from '@/components/BasePageContainer';
22
import RadioBox from '@/components/RadioGroup';
23
import { connect } from 'react-redux';
Maofei94's avatar
Maofei94 committed
24 25
import {
  setTableSQLDirName,
26 27 28 29 30 31
  deleteConnNew,
  initDBv4new,
  getInitDBLogNew,
  getConnRecordNew,
  getDataBaseConfigNew,
  saveConnectionNew,
Maofei94's avatar
Maofei94 committed
32
  getDataBaseList,
33 34
  updateConnDescNew,
  deleteInitDBLogNew,
Maofei94's avatar
Maofei94 committed
35
  connectionTest,
36 37 38
  GetProductList, // 获取产品列表
  GetDbProduct, // 获取产品方案配置
  InitAddDataBase, // 数据库初始化
39
  InitEditDataBase, // 二次初始化
Maofei94's avatar
Maofei94 committed
40
} from '@/services/database/api';
皮倩雯's avatar
皮倩雯 committed
41
import CryptoJS from 'crypto-js';
张烨's avatar
张烨 committed
42
import styles from './InitDataBase.less';
皮倩雯's avatar
皮倩雯 committed
43
import InitModal from './initModal';
44

45
const { TabPane } = Tabs;
张烨's avatar
张烨 committed
46
const { Option } = Select;
赵吉's avatar
赵吉 committed
47
const formLables = {
张烨's avatar
张烨 committed
48 49 50 51 52
  ip: '服务器名或IP地址',
  userName: '数据库用户名称',
  password: '数据库用户密码',
  dbName: '数据库名称',
};
53
let time = null;
张烨's avatar
张烨 committed
54 55
const InitDataBase = props => {
  const [form] = Form.useForm();
张烨's avatar
张烨 committed
56
  const [tableLoading, setTableLoading] = useState(false); // 连接记录
张烨's avatar
张烨 committed
57 58 59 60 61 62 63
  const [dbForm, setDbForm] = useState({
    ip: '',
    dbName: '',
    password: '',
    userName: '',
    inUse: '',
  });
张烨's avatar
张烨 committed
64 65 66 67
  const [data, setData] = useState([]); // 数据库链接记录
  const [upData, setUpData] = useState(1); // 列表刷新标记
  const [option, setOption] = useState([]); // 下拉列表数据
  const [desc, setDesc] = useState(''); // 修改描述
Maofei94's avatar
Maofei94 committed
68 69
  const [allSqlDir, setAllSqulDir] = useState([]); // 修改产品方案
  const [defaultSqlDir, setDefaultSqlDir] = useState(''); // 修改产品方案默认值
70 71 72 73 74
  const [modalVisible, setModalVisible] = useState({
    describeVisible: false, // 描述弹窗
    versionVisible: false, // 检查版本弹窗
    initVisible: false, // 初始化选择产品弹窗
  }); // 修改弹窗
75 76 77
  const [initVisible, setInitVisible] = useState(false); // 数据库初始化弹窗
  const [initContent, setInitContent] = useState(''); // 数据库初始化内容
  const [cardLoading, setCardLoading] = useState(false); // 初始化card Loading
Maofei94's avatar
Maofei94 committed
78
  const [finish, setFinish] = useState(false);
79
  const [initLoading, setInitLoading] = useState(false);
80 81
  const [initList, setInitList] = useState([]); // 数据库初始化产品数据
  const [dbExists, setDbExists] = useState(false); // 数据库是否存在
皮倩雯's avatar
皮倩雯 committed
82 83 84
  const [visibleModel, setVisibleModel] = useState(false);
  const [keepDb, setKeepDb] = useState([]);
  const [keepProduct, setKeepProduct] = useState([]);
85
  const scroll = useRef(null);
皮倩雯's avatar
皮倩雯 committed
86 87 88
  const [keep, setKeep] = useState([]);
  const [flag, setFlag] = useState(0);
  const [keepData, setKeepData] = useState({});
89
  const chooseDb = useRef({});
皮倩雯's avatar
皮倩雯 committed
90 91
  const key = CryptoJS.enc.Utf8.parse('1p2a3n4d5a6o7m8s9a10n1e2t3c4o5re'); //十六位十六进制数作为密钥
  const iv = CryptoJS.enc.Utf8.parse('1234567890000000');
92 93

  // 获取数据库配置信息
张烨's avatar
张烨 committed
94
  useEffect(() => {
95 96 97 98
    // setTimeout(() => {
    //   document.getElementById('pwd').removeAttribute('value');
    // }, 0);

99 100 101 102 103 104
    setCardLoading(true);
    // 数据库连接记录初始化
    getConnRecordData();
    getDataBaseConfigNew().then(resnew => {
      setCardLoading(false);
      let res = resnew.data;
105
      if (resnew.code === 0) {
106 107 108 109 110
        const { inUse } = res;

        let obj = {};
        Object.keys(dbForm).forEach(k => {
          obj[k] = res[k];
111
        });
112 113
        form.setFieldsValue({ ...obj });
        setDbForm(val => ({ ...val, ...obj }));
114 115 116 117 118 119 120 121
        setAllSqulDir(res.allSqlDir);
        if (res.allSqlDir.some(item => item === inUse)) {
          setDefaultSqlDir(res.tableSQLDirName);
        } else {
          // 默认切换到第一个产品方案
          handeleChangeSQLDirName(res.allSqlDir[0]);
          setDefaultSqlDir(res.allSqlDir[0]);
        }
122 123
      }
    });
124 125
    return () => {
      if (time) {
126 127
        clearTimeout(time);
        time = null;
128
      }
129 130
    };
  }, []);
皮倩雯's avatar
皮倩雯 committed
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

  // 解密
  const Decrypt = word => {
    let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
    let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    let decrypt = CryptoJS.AES.decrypt(srcs, key, {
      iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7,
    });
    let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr.toString();
  };

  //加密
  const Encrypt = word => {
    let srcs = CryptoJS.enc.Utf8.parse(word);
    let encrypted = CryptoJS.AES.encrypt(srcs, key, {
      iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7,
    });
    return encrypted.ciphertext.toString().toUpperCase();
  };

156 157 158 159
  // 弹出模态框
  const handleShowModal = (key, value) => {
    setModalVisible({ ...modalVisible, [key]: value });
  };
160 161 162 163 164 165
  // 获取数据库连接记录
  const getConnRecordData = () => {
    setTableLoading(true);
    getConnRecordNew()
      .then(resnew => {
        setTableLoading(false);
166
        if (resnew.code === 0) {
167 168 169 170
          let res = resnew.data;
          let arr = res.map((item, index) => {
            item.key = index;
            return item;
张烨's avatar
张烨 committed
171
          });
172
          setData(arr);
张烨's avatar
张烨 committed
173
        }
174 175 176 177 178 179
      })
      .catch(err => {
        setTableLoading(false);
        console.error(err);
      });
  };
Maofei94's avatar
Maofei94 committed
180 181
  // 获取日志
  const doInitLog = () => {
182
    setInitLoading(true);
183
    getInitDBLogNew()
Maofei94's avatar
Maofei94 committed
184
      .then(res => {
185
        if (res.code === 0) {
186
          if (res.data.content) {
187
            setInitLoading(false);
Maofei94's avatar
Maofei94 committed
188 189
            let arr = [];
            arr.push(
190 191 192 193
              res.data.content.split(/(\r\n)|(\n)/).map((item, index) => (
                // eslint-disable-next-line react/no-danger
                <p key={index} dangerouslySetInnerHTML={{ __html: item }} />
              )),
Maofei94's avatar
Maofei94 committed
194
            );
195
            console.log(arr);
Maofei94's avatar
Maofei94 committed
196 197 198
            setInitContent(arr);
            scroll.current.scrollTop = scroll.current.scrollHeight;
          }
199 200
          if (!res.data.finish) {
            time = setTimeout(() => {
201 202
              doInitLog();
            }, 600);
203 204
          } else {
            setInitLoading(false);
205
            if (time) {
206 207
              clearTimeout(time);
              time = null;
208 209
            }
          }
Maofei94's avatar
Maofei94 committed
210 211 212 213 214 215
        }
      })
      .catch(err => {
        setFinish(true);
      });
  };
张烨's avatar
张烨 committed
216
  // 数据库初始化
Maofei94's avatar
Maofei94 committed
217
  const initClick = () => {
Maofei94's avatar
Maofei94 committed
218 219 220
    setInitContent('');
    setCardLoading(true);
    setInitVisible(true);
Maofei94's avatar
Maofei94 committed
221
    let obj = form.getFieldsValue();
Maofei94's avatar
Maofei94 committed
222
    doInitLog();
223
    initDBv4new({
224
      ...obj,
225
    })
226 227 228 229 230 231 232 233 234 235 236 237
      .then(res => {
        setCardLoading(false);
        if (res.code == 0) {
          console.log(res);
        } else {
          notification.error({
            message: '提示',
            duration: 15,
            description: res.msg || '初始化失败',
          });
        }
      })
tianfen's avatar
tianfen committed
238 239 240 241
      .catch(err => {
        setCardLoading(false);
        console.log(err);
      });
Maofei94's avatar
Maofei94 committed
242
  };
张烨's avatar
张烨 committed
243 244 245 246
  const onValuesChange = (value, b) => {
    form.setFieldsValue(value);
  };
  const onChange = value => {
247
    const { length } = value;
张烨's avatar
张烨 committed
248
    form.setFieldsValue({
249
      dbName: value[length - 1],
张烨's avatar
张烨 committed
250 251
    });
  };
张烨's avatar
张烨 committed
252
  // 保存连接
253
  const onFinish = values => {
254
    setCardLoading(true);
张烨's avatar
张烨 committed
255
    const obj = values;
256
    console.log(chooseDb.current, 'obj');
257
    let pwd = Decrypt(obj.password) === '' ? Encrypt(obj.password) : obj.password;
258
    saveConnectionNew({
张烨's avatar
张烨 committed
259 260 261
      ip: obj.ip,
      dbName: obj.dbName,
      userName: obj.userName,
262
      password: pwd,
263
      desc: chooseDb.current.desc,
264 265
    })
      .then(resnew => {
266
        setCardLoading(false);
267
        if (resnew.code === 0) {
皮倩雯's avatar
皮倩雯 committed
268 269 270 271 272
          setFlag(flag + 1);
          setKeepData({
            ip: obj.ip,
            dbName: obj.dbName,
            userName: obj.userName,
273
            password: pwd,
皮倩雯's avatar
皮倩雯 committed
274
          });
275 276
          // setUpData(upData + 1);
          getConnRecordData();
张烨's avatar
张烨 committed
277
          notification.success({
Maofei94's avatar
Maofei94 committed
278
            message: '提示',
张烨's avatar
张烨 committed
279 280 281 282 283 284
            duration: 3,
            description: '保存成功',
          });
        } else {
          notification.error({
            message: '提示',
Maofei94's avatar
Maofei94 committed
285
            duration: 15,
286
            description: resnew.msg,
张烨's avatar
张烨 committed
287 288
          });
        }
289 290 291 292 293
      })
      .catch(err => {
        setCardLoading(false);
        console.log(err);
      });
294
  };
295
  // 测试连接
张烨's avatar
张烨 committed
296
  const onCheck = e => {
297
    setCardLoading(true);
张烨's avatar
张烨 committed
298
    const obj = form.getFieldsValue();
299
    let pwd = Decrypt(obj.password) === '' ? Encrypt(obj.password) : obj.password;
300 301 302 303
    connectionTest({
      ip: obj.ip,
      dbName: obj.dbName,
      userName: obj.userName,
304
      password: pwd,
张烨's avatar
张烨 committed
305 306
    })
      .then(res => {
307
        setCardLoading(false);
皮倩雯's avatar
皮倩雯 committed
308
        if (res.code == 0 && res.data == true) {
张烨's avatar
张烨 committed
309
          notification.success({
Maofei94's avatar
Maofei94 committed
310
            message: '提示',
张烨's avatar
张烨 committed
311 312 313 314 315 316
            duration: 3,
            description: '连接成功',
          });
        } else {
          notification.error({
            message: '提示',
Maofei94's avatar
Maofei94 committed
317
            duration: 15,
皮倩雯's avatar
皮倩雯 committed
318
            description: res.msg,
张烨's avatar
张烨 committed
319 320 321 322
          });
        }
      })
      .catch(err => {
323
        setCardLoading(false);
张烨's avatar
张烨 committed
324
        console.log(err);
325
      });
张烨's avatar
张烨 committed
326
  };
Maofei94's avatar
Maofei94 committed
327
  // 获取数据库列表
张烨's avatar
张烨 committed
328 329
  const selectFocus = e => {
    let params = form.getFieldsValue();
330
    let pwd = Decrypt(params.password) === '' ? Encrypt(params.password) : params.password;
Maofei94's avatar
Maofei94 committed
331
    getDataBaseList({
张烨's avatar
张烨 committed
332
      userName: params.userName || '',
333
      password: pwd || '',
张烨's avatar
张烨 committed
334
      ip: params.ip || '',
张烨's avatar
张烨 committed
335 336
    })
      .then(res => {
皮倩雯's avatar
皮倩雯 committed
337 338
        if (res.code == 0) {
          setOption(res.data.root);
张烨's avatar
张烨 committed
339 340
        } else {
          notification.error({
Maofei94's avatar
Maofei94 committed
341
            message: '提示',
Maofei94's avatar
Maofei94 committed
342
            duration: 15,
皮倩雯's avatar
皮倩雯 committed
343
            description: res.msg,
张烨's avatar
张烨 committed
344 345 346 347 348 349 350 351
          });
          setOption([]);
        }
      })
      .catch(err => {
        console.error(err);
      });
  };
张烨's avatar
张烨 committed
352
  // 点击表格回显到表单
张烨's avatar
张烨 committed
353 354
  const tableClick = item => {
    let obj = { ...dbForm };
张烨's avatar
张烨 committed
355
    Object.keys(obj).forEach(k => {
张烨's avatar
张烨 committed
356
      obj[k] = item[k];
张烨's avatar
张烨 committed
357
    });
358
    chooseDb.current = item;
359
    form.setFieldsValue({ ...obj });
张烨's avatar
张烨 committed
360
  };
361
  // 产品方案选择框回调
Maofei94's avatar
Maofei94 committed
362
  const handleSelect = value => {
Maofei94's avatar
Maofei94 committed
363 364 365
    handeleChangeSQLDirName(value);
  };
  const handeleChangeSQLDirName = val => {
366
    setTableSQLDirName({
Maofei94's avatar
Maofei94 committed
367
      dirName: val,
368
    })
Maofei94's avatar
Maofei94 committed
369
      .then(res => {
370
        if (res.code === 0) {
Maofei94's avatar
Maofei94 committed
371 372 373
          notification.success({
            message: '提示',
            duration: 3,
Maofei94's avatar
Maofei94 committed
374
            description: `已切换初始化脚本为:${val}`,
Maofei94's avatar
Maofei94 committed
375 376 377 378
          });
        } else {
          notification.error({
            message: '提示',
Maofei94's avatar
Maofei94 committed
379
            duration: 15,
Maofei94's avatar
Maofei94 committed
380 381 382 383 384 385 386 387
            description: res.message,
          });
        }
      })
      .catch(err => {
        console.error(err);
      });
  };
张烨's avatar
张烨 committed
388
  // 展示修改描述
Maofei94's avatar
Maofei94 committed
389 390
  const changeDesc = val => {
    setDesc(val);
391
    handleShowModal('describeVisible', true);
张烨's avatar
张烨 committed
392 393 394 395 396
  };
  const descChange = e => {
    const { value } = e.target;
    setDesc(value);
  };
Maofei94's avatar
Maofei94 committed
397 398 399 400 401
  // 关闭弹窗
  const handleClick = () => {
    setInitVisible(false);
    setInitContent('');
    setFinish(false);
402 403 404 405
    // deleteInitDBLog({
    //   _version: 9999,
    //   _dc: Date.now(),
    // });
406
    deleteInitDBLogNew();
Maofei94's avatar
Maofei94 committed
407
  };
张烨's avatar
张烨 committed
408 409 410 411
  // 弹窗确认回调
  const modalOkCallback = () => {
    const obj = form.getFieldsValue();
    // 更新描述
412
    updateConnDescNew({
张烨's avatar
张烨 committed
413 414 415
      ip: obj.ip,
      dbName: obj.dbName,
      userName: obj.userName,
416
      password: obj.password,
张烨's avatar
张烨 committed
417
      desc,
418
    })
419
      .then(res => {
420
        handleShowModal('describeVisible', false);
421 422
        // setUpData(upData + 1);
        getConnRecordData();
423 424 425
      })
      .catch(err => {
        console.error(err);
426
        handleShowModal('describeVisible', false);
427
      });
张烨's avatar
张烨 committed
428
  };
Maofei94's avatar
Maofei94 committed
429
  // 删除数据库连接记录
Maofei94's avatar
Maofei94 committed
430
  const delConfirm = value => {
Maofei94's avatar
Maofei94 committed
431
    const { key = '' } = value;
Maofei94's avatar
Maofei94 committed
432
    setTableLoading(true);
433
    deleteConnNew({
Maofei94's avatar
Maofei94 committed
434 435 436 437
      rowIndex: key,
    })
      .then(res => {
        setTableLoading(false);
438
        if (res.code === 0) {
439 440
          // setUpData(upData + 1);
          getConnRecordData();
Maofei94's avatar
Maofei94 committed
441 442 443 444 445 446 447 448
          notification.success({
            message: '提示',
            duration: 3,
            description: '操作成功',
          });
        } else {
          notification.error({
            message: '提示',
Maofei94's avatar
Maofei94 committed
449
            duration: 15,
450
            description: res.msg,
Maofei94's avatar
Maofei94 committed
451 452 453 454 455 456 457 458
          });
        }
      })
      .catch(err => {
        setTableLoading(false);
        console.error(err);
      });
  };
皮倩雯's avatar
皮倩雯 committed
459 460 461

  const GetDb = () => {
    let obj = form.getFieldsValue();
462
    GetDbProduct({ ...obj }).then(res => {
皮倩雯's avatar
皮倩雯 committed
463 464 465 466 467 468 469 470 471
      if (res.code === 0) {
        console.log(res.data);
        if (res.data.Product) {
          console.log(res.data.Product);
          let aa = [];
          res.data.Product.map(i => {
            aa.push(i.name);
          });
          console.log(aa);
472
          setKeep(res.data.IsAuthorize);
皮倩雯's avatar
皮倩雯 committed
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
          setKeepProduct(aa);
        }
        setDbExists(res.data.DBExists);
        setKeepDb(res.data);
        if (!res.data.DBExists) {
          setVisibleModel(true);
        } else if (res.data.DBExists && res.data.Product.length > 0) {
          setVisibleModel(true);
        } else {
          notification.warning({
            message: '提示',
            duration: 15,
            description: '当前数据库不可初始化',
          });
        }
      } else {
        notification.warning({
          message: '提示',
          duration: 15,
          description: res.msg,
        });
      }
    });
  };
497 498 499 500 501 502 503
  // 获取数据库初始化回显列表
  const getInitList = () => {
    setInitList([]);
    handleShowModal('initVisible', true);
    setInitLoading(true);
    let obj = form.getFieldsValue();
    let req1 = GetProductList();
504
    let req2 = GetDbProduct({ ...obj });
505 506 507 508 509 510 511 512 513 514 515
    Promise.all([req1, req2])
      .then(res => {
        if (res[1].code !== 0 || res[0].code !== 0) {
          setInitLoading(false);
          notification.error({
            message: '提示',
            duration: 3,
            description: '连接失败',
          });
          return;
        }
516
        setDbExists(res[1].data.DBExists);
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
        let fileList = res[0].data ? res[0].data : [];
        let dataList = res[1].data.Product ? res[1].data.Product : [];
        // 没有库或者dataList为[]直接用fileList作为回显
        if (!res[1].data.DBExists || dataList.length === 0) {
          setInitList(fileList);
          setInitLoading(false);
          return;
        }
        let mapList = new Map();
        // 通过map给数据库中中的节点中的key的值当作key,checkVersion当作value进行存储
        dataList.forEach(element => {
          // 存入一级目录是否有选中项的key
          mapList.set(element.key, element.check);
          element.modularSolutions.forEach(item => {
            // 存入二级目录对应的选中的checkVersion跟check
            mapList.set(item.key, {
              checkVersion: item.checkVersion,
              check: item.check,
            });
            item.functionrSolutions.forEach(val => {
              // 存入三级目录对应的选中的checkVersion跟check
              mapList.set(val.key, val.isCheck);
            });
          });
        });
542
        console.log(mapList, 'mapList');
543 544 545 546 547 548
        fileList.forEach(element => {
          // 根据key来获取第一级目录tab的check
          let tabKey = mapList.get(element.key);
          element.check = tabKey === undefined ? null : tabKey;
          element.modularSolutions.forEach(item => {
            let mapCheckVersion = mapList.get(item.key);
549 550 551
            if (mapCheckVersion === undefined) {
              mapCheckVersion = { checkVersion: null, check: null };
            }
552 553
            // 根据key值去匹配对应的checkVersion
            item.checkVersion =
554
              mapCheckVersion.checkVersion === undefined ? null : mapCheckVersion.checkVersion;
555
            // 根据key值去匹配对应的二级目录的check
556
            item.check = mapCheckVersion.check === undefined ? null : mapCheckVersion.check;
557 558 559
            if (item.check) {
              item.hasCheck = true;
            }
560 561 562 563
            item.functionrSolutions.forEach(val => {
              // 根据三级目录对应的选中key匹配isCheck
              let isCheck = mapList.get(val.key);
              val.isCheck = isCheck === undefined ? null : isCheck;
564 565 566 567 568 569
              // 版本号低于当前版本号禁止选用
              if (item.checkVersion && val.version < item.checkVersion) {
                val.disabled = true;
              } else {
                val.disabled = false;
              }
570 571 572 573 574 575
            });
          });
        });
        setInitList(fileList);
        setInitLoading(false);
      })
576 577
      .catch(err => {
        console.log(err, 'err');
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
        notification.error({
          message: '提示',
          duration: 3,
          description: '连接失败',
        });
        setInitLoading(false);
      });
  };
  // 初始化产品列表渲染
  const renderInitListItem = (tabItem, index) => (
    <TabPane tab={tabItem.productName} key={index}>
      <div className={styles.tabContainer}>
        {tabItem.modularSolutions.map((item, num) => (
          <React.Fragment key={item.key}>
            {item.functionrSolutions.length > 0 ? (
              <RadioBox
                radioTitle={item.modularName}
                radioOptions={item.functionrSolutions}
                currentVal={item.checkVersion}
                currentIndex={{ tabIndex: index, radioIndex: num }}
                callBack={radioChange}
              />
            ) : null}
          </React.Fragment>
        ))}
      </div>
    </TabPane>
  );
  // 单选选后的回调,改变数据
  const radioChange = (index, value) => {
    setInitList(val => {
      const list = JSON.parse(JSON.stringify(val));
610
      const secondList = list[index.tabIndex].modularSolutions[index.radioIndex];
611 612 613 614 615 616 617 618 619 620 621
      // 取消功能 一开始未选中的才能取消
      if (value === secondList.checkVersion && !secondList.hasCheck) {
        secondList.checkVersion = null;
        value = null;
        secondList.check = false;
      } else {
        // 修改选中的checkVersion值
        secondList.checkVersion = value;
        // 修改二级菜单的check字段
        secondList.check = true;
      }
622
      // 修改一级菜单的check字段
623 624 625
      list[index.tabIndex].check = !list[index.tabIndex].modularSolutions.every(
        version => version.checkVersion === null,
      );
626
      // 修改单选isCheck字段
627
      secondList.functionrSolutions.forEach(item => {
628 629 630 631 632 633 634 635 636
        if (item.version === value) {
          item.isCheck = true;
        } else {
          item.isCheck = false;
        }
      });
      return list;
    });
  };
皮倩雯's avatar
皮倩雯 committed
637 638 639
  const onOK = (arr, code) => {
    initDatabasePro(arr, code);
  };
640
  // 数据库初始化
皮倩雯's avatar
皮倩雯 committed
641 642
  const initDatabasePro = (e, j) => {
    let productSetting = e;
643 644 645
    let obj = form.getFieldsValue();
    // 数据库存在调用编辑接口否则调用新增接口
    setInitLoading(true);
皮倩雯's avatar
皮倩雯 committed
646
    setVisibleModel(false);
647 648 649
    handleShowModal('initVisible', false);
    setInitVisible(true);
    doInitLog();
650
    if (dbExists) {
皮倩雯's avatar
皮倩雯 committed
651 652 653 654
      InitEditDataBase({
        ...obj,
        productSetting,
        license: j,
655
        password: obj.password,
皮倩雯's avatar
皮倩雯 committed
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
      }).then(res => {
        setInitLoading(false);
        if (res.code === 0) {
          notification.success({
            message: '提示',
            duration: 3,
            description: '数据库初始化成功',
          });
        } else {
          notification.error({
            message: '提示',
            duration: 5,
            description: res.msg,
          });
        }
      });
672 673
      return;
    }
皮倩雯's avatar
皮倩雯 committed
674
    InitAddDataBase({ ...obj, productSetting, license: j }).then(res => {
675 676 677 678 679
      setInitLoading(false);
      if (res.code === 0) {
        notification.success({
          message: '提示',
          duration: 3,
680
          description: '数据库初始化成功',
681 682 683 684
        });
      } else {
        notification.error({
          message: '提示',
皮倩雯's avatar
皮倩雯 committed
685
          duration: 5,
686 687 688 689 690
          description: res.msg,
        });
      }
    });
  };
张烨's avatar
张烨 committed
691 692 693 694 695
  const columns = [
    {
      title: '服务器名或IP地址',
      dataIndex: 'ip',
      key: 'ip',
Maofei94's avatar
Maofei94 committed
696 697
      width: 180,
      ellipsis: true,
张烨's avatar
张烨 committed
698 699 700 701 702
    },
    {
      title: '数据库名称',
      dataIndex: 'dbName',
      key: 'dbName',
Maofei94's avatar
Maofei94 committed
703 704
      width: 180,
      ellipsis: true,
张烨's avatar
张烨 committed
705 706 707 708 709
    },
    {
      title: '数据库用户名称',
      dataIndex: 'userName',
      key: 'userName',
Maofei94's avatar
Maofei94 committed
710 711
      width: 180,
      ellipsis: true,
张烨's avatar
张烨 committed
712 713 714 715 716
    },
    {
      title: '保存时间',
      dataIndex: 'saveTime',
      key: 'saveTime',
Maofei94's avatar
Maofei94 committed
717 718
      width: 180,
      ellipsis: true,
张烨's avatar
张烨 committed
719 720 721 722 723
    },
    {
      title: '描述',
      dataIndex: 'desc',
      key: 'desc',
Maofei94's avatar
Maofei94 committed
724
      ellipsis: true,
张烨's avatar
张烨 committed
725 726
    },
    {
Maofei94's avatar
Maofei94 committed
727 728 729
      title: '操作',
      dataIndex: 'action',
      key: 'action',
Maofei94's avatar
Maofei94 committed
730 731
      width: 250,
      ellipsis: true,
Maofei94's avatar
Maofei94 committed
732 733 734 735 736 737
      render: (text, record) => (
        <Space>
          <Button
            type="primary"
            size="small"
            onClick={() => {
Maofei94's avatar
Maofei94 committed
738
              changeDesc(record.desc);
Maofei94's avatar
Maofei94 committed
739 740 741 742
            }}
          >
            修改描述
          </Button>
743
          {/* <div onClick={e => e.stopPropagation()}>
Maofei94's avatar
Maofei94 committed
744 745 746 747 748 749 750 751 752 753 754 755
            <Popconfirm
              title="是否删除该连接的历史记录?"
              okText="确认"
              cancelText="取消"
              onConfirm={() => {
                delConfirm(record);
              }}
            >
              <Button size="small" danger>
                删除
              </Button>
            </Popconfirm>
756
          </div> */}
Maofei94's avatar
Maofei94 committed
757
        </Space>
张烨's avatar
张烨 committed
758
      ),
张烨's avatar
张烨 committed
759 760
    },
  ];
皮倩雯's avatar
皮倩雯 committed
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781

  const flagChange = () => {
    const obj = form.getFieldsValue();
    console.log(keepData);
    console.log(obj);
    if (
      keepData.ip == obj.ip &&
      keepData.dbName == obj.dbName &&
      keepData.userName == obj.userName &&
      keepData.password == obj.password
    ) {
      GetDb();
      deleteInitDBLogNew();
    } else {
      notification.warning({
        message: '提示',
        duration: 15,
        description: '请先保存连接',
      });
    }
  };
张烨's avatar
张烨 committed
782 783
  return (
    <>
784
      <PageContainer className={styles.InitDataBaseContainer}>
张烨's avatar
张烨 committed
785
        <Card>
786
          <Spin tip="loading..." spinning={cardLoading}>
787
            {/* <div className={styles.tableTitle}>数据库初始化</div> */}
788 789 790 791 792 793 794 795
            <Form
              className={styles.mgTop20}
              layout="horizontal"
              labelAlign="left"
              labelCol={{ span: 3 }}
              form={form}
              onFinish={onFinish}
              onValuesChange={onValuesChange}
796
              autoComplete="off"
797
            >
皮倩雯's avatar
皮倩雯 committed
798 799 800 801 802 803 804 805 806 807 808 809 810 811
              <Form.Item
                label={`${formLables.ip}:`}
                name="ip"
                rules={[
                  {
                    validator: (rule, value) => {
                      if (form.getFieldValue().ip == '') {
                        return Promise.reject('ip必填');
                      }
                      return Promise.resolve();
                    },
                  },
                ]}
              >
812 813
                <Input placeholder="请输入服务器名或IP地址" />
              </Form.Item>
皮倩雯's avatar
皮倩雯 committed
814 815 816 817 818 819 820 821 822 823 824 825 826 827
              <Form.Item
                label={`${formLables.userName}:`}
                name="userName"
                rules={[
                  {
                    validator: (rule, value) => {
                      if (form.getFieldValue().userName == '') {
                        return Promise.reject('用户名称必填');
                      }
                      return Promise.resolve();
                    },
                  },
                ]}
              >
828
                <Input placeholder="请输入用户名称" autoComplete="off" />
829
              </Form.Item>
皮倩雯's avatar
皮倩雯 committed
830
              <Form.Item
831
                label="数据库用户密码"
皮倩雯's avatar
皮倩雯 committed
832 833 834 835 836 837 838 839 840 841 842 843 844
                name="password"
                rules={[
                  {
                    validator: (rule, value) => {
                      console.log(form.getFieldValue().password);
                      if (form.getFieldValue().password == '') {
                        return Promise.reject('用户密码必填');
                      }
                      return Promise.resolve();
                    },
                  },
                ]}
              >
845 846 847 848 849 850
                <Input
                  placeholder="请输入用户密码"
                  type="password"
                  autoComplete="new-password"
                  id="pwd"
                />
851
              </Form.Item>
皮倩雯's avatar
皮倩雯 committed
852
              <Form.Item
853
                label="数据库名称"
皮倩雯's avatar
皮倩雯 committed
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
                name="dbName"
                rules={[
                  {
                    validator: (rule, value) => {
                      console.log(form.getFieldValue().dbName);
                      if (
                        form.getFieldValue().dbName == '' ||
                        form.getFieldValue().dbName == undefined
                      ) {
                        return Promise.reject('数据库名称必填');
                      }
                      return Promise.resolve();
                    },
                  },
                ]}
              >
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
                <Select
                  showSearch
                  mode="tags"
                  placeholder="请选择数据库"
                  optionFilterProp="children"
                  onFocus={() => {
                    selectFocus();
                  }}
                  onChange={e => {
                    onChange(e);
                  }}
                  // eslint-disable-next-line no-shadow
                  filterOption={(input, option) => {
                    console.log(option);
                    return (
                      option.children &&
886
                      option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
887 888 889 890 891 892 893 894 895 896 897 898
                    );
                  }}
                >
                  {option &&
                    option.length > 0 &&
                    option.map((item, index) => (
                      <Option value={item.value} key={item.value + index}>
                        {item.value}
                      </Option>
                    ))}
                </Select>
              </Form.Item>
Maofei94's avatar
Maofei94 committed
899
              <Form.Item wrapperCol={{ offset: 3 }}>
900 901 902 903
                <div className={styles.tCenter}>
                  <Space size="large" className={styles.btnBox}>
                    <Space>
                      <Button onClick={onCheck}>测试连接</Button>
904
                      <Button type="primary" htmlType="submit" loading={tableLoading}>
905 906 907 908
                        保存连接
                      </Button>
                    </Space>
                    <Space>
909
                      {/* <Button
910
                        type="primary"
皮倩雯's avatar
皮倩雯 committed
911 912 913 914
                        // onClick={() => {
                        //   getInitList();
                        //   deleteInitDBLogNew();
                        // }}
915
                        onClick={() => {
皮倩雯's avatar
皮倩雯 committed
916
                          flagChange();
Maofei94's avatar
Maofei94 committed
917
                        }}
918
                      >
919
                        数据库初始化
920
                      </Button> */}
921
                    </Space>
Maofei94's avatar
Maofei94 committed
922
                  </Space>
923 924 925 926
                </div>
              </Form.Item>
            </Form>
          </Spin>
张烨's avatar
张烨 committed
927 928
        </Card>

929
        <Card className={styles.mgTop20}>
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
          <div className={styles.recordBox}>
            <div className={styles.tableTitle}>近期保存的数据库连接</div>
            <Table
              style={{ marginTop: '20px', marginBottom: '20px' }}
              scroll={{ x: 'max-content', y: 'calc(100vh - 550px)' }}
              columns={columns}
              dataSource={data}
              pagination={false}
              bordered
              loading={tableLoading}
              size="small"
              onRow={record => ({
                onClick: () => {
                  tableClick(record);
                  setFlag(0);
                }, // 点击行
              })}
            />
          </div>
张烨's avatar
张烨 committed
949
        </Card>
Maofei94's avatar
Maofei94 committed
950 951 952 953 954 955
        <Modal
          title="初始化数据库"
          visible={initVisible}
          onCancel={() => {
            setInitVisible(false);
            setInitContent('');
956 957 958 959
            // deleteInitDBLog({
            //   _version: 9999,
            //   _dc: Date.now(),
            // });
960
            deleteInitDBLogNew();
Maofei94's avatar
Maofei94 committed
961
          }}
962
          width={800}
Maofei94's avatar
Maofei94 committed
963
          maskClosable={false}
964
          bodyStyle={{
965
            height: '500px',
966
            // overflowY: 'auto',
967
          }}
Maofei94's avatar
Maofei94 committed
968 969 970
          footer={[
            <Button
              onClick={() => {
Maofei94's avatar
Maofei94 committed
971
                handleClick();
Maofei94's avatar
Maofei94 committed
972
              }}
973
              key="back"
Maofei94's avatar
Maofei94 committed
974 975 976 977 978 979
              type="primary"
            >
              关闭窗口
            </Button>,
          ]}
        >
tianfen's avatar
tianfen committed
980 981 982
          <div
            ref={scroll}
            style={{
983
              maxHeight: '470px',
tianfen's avatar
tianfen committed
984 985 986 987
              overflowY: 'auto',
              marginRight: ' -24px',
            }}
          >
Maofei94's avatar
Maofei94 committed
988
            {initContent || (
Maofei94's avatar
Maofei94 committed
989
              <Spin
990
                spinning={initLoading}
Maofei94's avatar
Maofei94 committed
991 992 993
                tip="loading..."
                style={{ width: '100%', marginTop: '40px' }}
              />
Maofei94's avatar
Maofei94 committed
994
            )}
995
          </div>
Maofei94's avatar
Maofei94 committed
996 997
        </Modal>

998
        <Modal
张烨's avatar
张烨 committed
999
          title="修改链接描述"
1000
          visible={modalVisible.describeVisible}
张烨's avatar
张烨 committed
1001
          onOk={() => modalOkCallback()}
1002
          onCancel={() => handleShowModal('describeVisible', false)}
1003
          width="800px"
1004
          bodyStyle={{
张烨's avatar
张烨 committed
1005
            minHeight: '100px',
1006
          }}
张烨's avatar
张烨 committed
1007
          cancelText="取消"
Maofei94's avatar
Maofei94 committed
1008
          okText="确认"
Maofei94's avatar
Maofei94 committed
1009
          destroyOnClose
1010
        >
张烨's avatar
张烨 committed
1011
          <Row>
1012
            <Col span={2} className={styles.decsBox}>
Maofei94's avatar
Maofei94 committed
1013
              描述:
张烨's avatar
张烨 committed
1014
            </Col>
Maofei94's avatar
Maofei94 committed
1015

1016
            <Col span={22}>
张烨's avatar
张烨 committed
1017 1018
              <Input
                placeholder="请输入描述"
Maofei94's avatar
Maofei94 committed
1019 1020
                allowClear
                defaultValue={desc}
张烨's avatar
张烨 committed
1021 1022 1023 1024 1025
                onChange={value => {
                  descChange(value);
                }}
              />
            </Col>
1026
          </Row>
1027
        </Modal>
皮倩雯's avatar
皮倩雯 committed
1028 1029 1030 1031 1032 1033 1034 1035
        <InitModal
          visible={visibleModel}
          onCancel={() => setVisibleModel(false)}
          keepDb={keepDb}
          callBackSubmit={onOK}
          keepProduct={keepProduct}
          keep={keep}
        />
1036 1037 1038 1039 1040 1041 1042
        {/* 初始化选择产品弹窗 */}
        <Modal
          title="初始化"
          maskClosable={false}
          visible={modalVisible.initVisible}
          onCancel={() => handleShowModal('initVisible', false)}
          footer={[
1043
            <Button key="back" onClick={() => handleShowModal('initVisible', false)}>
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
              取消
            </Button>,
            <Popconfirm
              placement="topLeft"
              title="是否确认初始化"
              onConfirm={initDatabasePro}
              okText="确认"
              key="submit"
              cancelText="取消"
            >
              <Button type="primary" loading={initLoading}>
                确认
              </Button>
            </Popconfirm>,
          ]}
          width={900}
        >
          <Spin spinning={initLoading}>
            <div className={styles.cardContainer}>
              <Tabs defaultActiveKey="1" type="card" tabBarGutter={-1}>
                {initList.map((item, index) => renderInitListItem(item, index))}
              </Tabs>
            </div>
          </Spin>
        </Modal>
张烨's avatar
张烨 committed
1069 1070 1071 1072
      </PageContainer>
    </>
  );
};
1073

张烨's avatar
张烨 committed
1074
export default connect()(InitDataBase);