Index.js 13.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/**
 * 钟是志
 * 2022年10月12日
 * 禅道 http://scjoyedu.eicp.net:88/zentao/bug-view-28974.html
 * 选辅导员,教师,学生的通用组件
 * api 文档 https://console-docs.apipost.cn/preview/caa25e097e7386d0/a2d8620d944c56b5
 * */
import React, { useState, useEffect } from 'react';
import styles from './styles/styles.less';
import { Modal, Button, Input, Checkbox, message, Icon, Tree, Spin, Tooltip } from 'antd';
import { formatTreeData, colorTranslate, getApis } from './publicFunctions';
import lowCodeProps from './lowCodeProps';
13 14 15 16
import request from '@/webPublic/one_stop_public/utils/request';

function requestData(data, url){
  url = window.specialImportantSystemConfig.httpServer + url;
17 18 19
  return request(url, data, {
    method: 'POST',
  });
20 21
}

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

export default function SelectPerson(props) {
  const [show, setShow] = useState(false);
  const propsDiy = window.smartFormSelectPersonComponent?.lowCodeProps || lowCodeProps();
  const apis = getApis(propsDiy.searchType);
  const [authInstitutionInfo, setAuthInstitutionInfo] = useState(null); // 楼栋信息
  const [searchKey, setSearchKey] = useState(''); // 搜索框的内容
  const [searchResult, setSearchResult] = useState([]); // 搜索到的内容
  const [detailList, setDetailList] = useState([]); // 第三列显示的列表
  const [sgUsers, setSelectUsers] = useState([]); // 选中的item
  const [sgUserIds, setSgUserIds] = useState([]); // 选中的businessKey
  const [checkedList, setCheckedList] = useState([]); // 树节点中选中的复选框
  const [jqpp, setJqpp] = useState(false); // 是否精确匹配
  function changeShow() {
    setShow(!show);
37 38 39 40 41 42 43 44 45
  }

  function initShowData(stateList = {}) {
    setCheckedList(stateList.checkedList || []);
    setSgUserIds(stateList.sgUserIds || []);
    setSelectUsers(stateList.sgUsers || []);
    setSearchKey(stateList.searchKey || '');
    setSearchResult(stateList.searchResult || []);
    setDetailList(stateList.detailList || []);
46 47 48 49 50
  }

  useEffect(() => {
    window.smartFormSelectPersonComponent = {
      changeShow,
51
      initShowData,
52
      lowCodeProps: lowCodeProps(),
53 54 55 56 57
      callback: ({
                   value,
                   componentState,
                 }) => {
        console.log(value, componentState);
58 59 60 61 62 63
      },
    };
  }, []);

  // 第二栏树形数据
  const getTreeList = () => {
64 65 66
    requestData({
      ...propsDiy.apiParams,
    }, apis.findTree)
67 68 69 70 71 72
      .then(res => {
        if (res) {
          res = formatTreeData(res);
          setAuthInstitutionInfo(res);
        }
      });
73 74 75 76 77 78 79
  };

  //根据关键字搜索
  const search = () => {
    if (searchKey === '') {
      setSearchResult([]);
    } else {
80
      if (searchKey.length < 2) {
81 82 83 84 85 86 87
        message.warning('请至少输入2个字符');
        return false;
      }
      requestData(
        {
          keyword: searchKey,
          isExact: jqpp,
88
          ...propsDiy.apiParams,
89 90
        },
        apis.search,
91 92
      )
        .then(res => {
93
          if (res && Array.isArray(res)) {
94 95 96
            setSearchResult(res);
          }
        });
97 98 99 100 101 102 103 104 105 106 107
    }
  };

  //清空搜索条件
  const clear = () => {
    setSearchKey('');
    setSearchResult([]);
  };

  //根据id进行搜索
  const searchById = (selectedKeys, e) => {
108 109 110 111
    const {
      businessKey,
      selectType
    } = e.node.props;
112 113 114 115 116
    let bKey = businessKey;
    requestData(
      {
        businessKey: bKey,
        selectType,
117
        ...propsDiy.apiParams,
118 119
      },
      apis.findList,
120 121 122 123 124 125
    )
      .then(res => {
        if (res) {
          setDetailList(res);
        }
      });
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
  };

  //选中的人
  const selectUser = user => {
    setSelectUsers([...sgUsers, user]);
    setSgUserIds([...sgUserIds, user.businessKey]);
  };

  //删除选中的人
  const deleteSg = (x, index, e) => {
    // 阻止事件冒泡
    e.stopPropagation();
    let sgNew = [...sgUsers];
    sgNew = sgNew.filter(g => {
      return x.businessKey !== g.businessKey;
    });
    setSelectUsers(sgNew);
    setSgUserIds(
      sgUserIds.filter(g => {
        return x.businessKey !== g;
      }),
    );

    setCheckedList(checkedList.filter((g) => {
      return x.businessKey !== g.businessKey && !g.businessKey.startsWith(x.businessKey);
    }));
  };

  // 确认按钮
  const auth = () => {
    const data = {
      businessKey: [...sgUserIds, ...checkedList.map(g => g.businessKey)],
158
      ...propsDiy.apiParams,
159 160 161 162 163 164
    };
    if (!data.businessKey.length) {
      message.warning(propsDiy.getResultProps.emptySelectMessage);
      return false;
    }
    message.info(propsDiy.getResultProps.message);
165
    requestData(data, apis[propsDiy.getDataMethod]?apis[propsDiy.getDataMethod]:apis.getData)
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
      .then(res => {
        if (res) {
          message.success('操作成功');
          if (window.smartFormSelectPersonComponent.callback) {
            window.smartFormSelectPersonComponent.callback({
              value: res,
              componentState: {
                checkedList,
                sgUserIds,
                sgUsers,
                searchKey,
                searchResult,
                // detailList,
              },
            });
          }
          changeShow();
          // props.getData();
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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  };

  const changeJqpp = e => {
    setJqpp(e.target.checked);
  };


  useEffect(() => {
    if (show) {
      getTreeList();
    }
  }, [show]);

  const changeTreeCheck = (checkedKeys, e) => {
    const keys = e.checkedNodes.map(g => {
      const {
        businessKey,
        businessMemo,
        businessName,
        noCheckNum,
        preCheckInNum,
        selectType,
        level,
        bedNum,
        isSelect,
        checkInNum,
        womanNoCheckNum,
        manNoCheckNum,
      } = g.props;
      return {
        womanNoCheckNum,
        manNoCheckNum,
        businessKey,
        businessMemo,
        businessName,
        noCheckNum,
        preCheckInNum,
        selectType,
        level,
        bedNum,
        isSelect,
        checkInNum,
      };
    });
    setCheckedList(keys);
  };

  const distinctArr = () => {
    let arr = checkedList.concat(sgUsers);
    let map = new Map();
    for (let item of arr) {
      map.set(item.businessKey, item);
    }
    arr = [...map.values()];
    arr.sort((a, b) => { // 排序解决 多层子级的问题。
241
      if (a.businessKey && b.businessKey) {
242 243 244 245
        return a.businessKey.length - b.businessKey.length;
      }
      return 0;
    });
246
    const distnctBusinessKey = arr.reduce(function (response, current, index) {
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 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
      // 删除子级 key 保证数据唯一性
      let add = true;
      for (let item of response) {
        if (current.businessKey.startsWith(item.businessKey)) {
          add = false;
        }
      }
      if (add) {
        response.push(current);
        return response;
      }
      return response;
    }, []);
    return distnctBusinessKey;
  };

  const distinctA = distinctArr();

  return (
    <>
      {show && (
        <Modal
          className={styles.distribution}
          visible={true}
          width={1300}
          onOk={auth}
          maskClosable={false}
          onCancel={changeShow}
          {...propsDiy.ModalProps}
        >
          <div className={styles.modalContent}>
            <div className={styles.one} style={{ border: 'none' }}>
              <div>
                <Input
                  {...propsDiy.firstSearchProps.inputProps}
                  value={searchKey}
                  onChange={e => setSearchKey(e.target.value)}
                />
                <div className={styles.options}>
                  <Button className={styles.search} onClick={search}>
                    搜索
                  </Button>
                  <Button className={styles.clear} onClick={clear}>
                    清空
                  </Button>
                </div>
                <div>
                  <div className={styles.search_option}>搜索选项</div>
                  <Checkbox onChange={changeJqpp} checked={jqpp}>
                    精确匹配
                  </Checkbox>
                </div>
                <div className={styles.result}>
                  <div>{propsDiy.firstSearchProps.searchResultTitle}</div>
                  <div className={styles.search}>
                    {searchResult.length > 0 ? (
                      <div className={styles.search_result}>
                        {searchResult
                          ?.filter(g => {
                            return g.isSelect;
                          })
                          .map(x => {
                            return (
                              <div
                                key={x.businessKey}
                                onClick={
                                  sgUserIds.includes(x.businessKey) || !x.isSelect
                                    ? null
                                    : () => selectUser(x)
                                }
                              >
318
                                <span style={{ color: sgUserIds.includes(x.businessKey) && 'red' }}>
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
                                  {x.businessName}
                                </span>
                              </div>
                            );
                          })}
                      </div>
                    ) : (
                      <div className={styles.search_no_result}>无搜索结果</div>
                    )}
                  </div>
                </div>
              </div>
            </div>
            <div className={styles.one}>
              <div className={styles.one_title}>{propsDiy.secondTreeProps.title}</div>
              {authInstitutionInfo && Array.isArray(authInstitutionInfo) ? (
                <Tree
                  treeData={authInstitutionInfo}
                  checkable={true}
                  onSelect={searchById}
                  className={styles.treeDiv}
                  defaultExpandedKeys={['0000']}
                  checkedKeys={checkedList.map(g => g.businessKey)}
                  onCheck={changeTreeCheck}
                />
              ) : (
                <div className={styles.no_result}>
346 347
                  <Spin spinning={true} tip={propsDiy.secondTreeProps.loadingTreeDataMessage}
                        size="large"/>
348 349 350 351 352 353 354 355 356 357 358 359
                </div>
              )}
            </div>
            <div className={styles.one}>
              <div className={styles.one_title}>
                {propsDiy.thirdDetailListProps.title}
              </div>
              <div className={styles.list_content}>
                {detailList.length > 0 ? (
                  <div className={styles.search_result}>
                    {detailList?.filter(g => {
                      return g.isSelect;
360 361
                    })
                      .map(x => {
362 363 364 365 366 367 368 369 370 371 372 373 374
                        return (
                          <div
                            key={x.businessKey}
                            onClick={sgUserIds.includes(x.businessKey) ? null : () => selectUser(x)}
                          >
                            <span
                              style={
                                !x.isSelect
                                  ? {
                                    cursor: 'not-allowed',
                                    fontWeight: 'normal',
                                  }
                                  : {
375
                                    color: sgUserIds.includes(x.businessKey) && 'red',
376 377 378 379 380 381 382 383 384 385
                                  }
                              }
                            >
                              {x.businessName}
                            </span>
                          </div>
                        );
                      })}
                  </div>
                ) : (
386 387
                  <div
                    className={styles.no_result}>{propsDiy.thirdDetailListProps.emptyMessage}</div>
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
                )}
              </div>
            </div>
            <div className={styles.one}>
              <div className={styles.one_title}>{propsDiy.fourRangeProps.title}</div>
              <div className={styles.list_content}>
                {distinctA.length ? (
                  <div className={styles.search_result}>
                    {distinctA.map((x, index) => {
                      return (
                        <div
                          key={x.businessKey}
                          className={styles.selectSg}
                          style={{
                            backgroundColor: colorTranslate(x),
                          }}
                        >
                          <div className={styles.oneSelectedInfo}>
                            <Icon
                              type="delete"
                              style={{ marginRight: '5px' }}
                              onClick={e => deleteSg(x, index, e)}
                            />
411 412 413 414
                            {x.businessName}
                            {/*<Tooltip title={x.businessName}>*/}
                            {/*  {x.businessName}*/}
                            {/*</Tooltip>*/}
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
                          </div>
                        </div>
                      );
                    })}
                  </div>
                ) : (
                  <div className={styles.no_result}>{propsDiy.fourRangeProps.emptyMessage}</div>
                )}
              </div>
            </div>
          </div>
        </Modal>
      )}
    </>
  );
}