import { Input, Tree } from 'antd'; import { DeleteOutlined, EditOutlined, LoadingOutlined, PlusCircleOutlined, SearchOutlined, } from '@ant-design/icons'; import { useEffect, useRef, useState } from 'react'; import { service } from '@/pages/system/Department'; import { Ellipsis, Empty, PermissionButton } from '@/components'; import { useIntl, useLocation } from 'umi'; import { cloneDeep, debounce, omit } from 'lodash'; import Save from '../save'; import { ISchema } from '@formily/json-schema'; import { DepartmentItem } from '@/pages/system/Department/typings'; import { ArrayToTree, onlyMessage } from '@/utils/util'; import classnames from 'classnames'; import _ from 'lodash'; interface TreeProps { onSelect: (id: string) => void; } export const getSortIndex = (data: DepartmentItem[], pId?: string): number => { let sortIndex = 0; if (data.length) { if (!pId) { return data.sort((a, b) => b.sortIndex - a.sortIndex)[0].sortIndex + 1; } data.some((department) => { if (department.id === pId && department.children) { const sortArray = department.children.sort((a, b) => b.sortIndex - a.sortIndex); sortIndex = sortArray[0].sortIndex + 1; return true; } else if (department.children) { sortIndex = getSortIndex(department.children, pId); return !!sortIndex; } return false; }); } return sortIndex; }; const TreeMap = new Map(); export default (props: TreeProps) => { const intl = useIntl(); const [treeData, setTreeData] = useState(undefined); const [treeDataList, setTreeDataList] = useState(undefined); const [loading, setLoading] = useState(false); const [keys, setKeys] = useState([]); const [visible, setVisible] = useState(false); const [data, setData] = useState(); const [expandedKeys, setExpandedKeys] = useState([]); const [showToolIndex, setShowToolIndex] = useState(''); const searchKey = useRef(''); const location = useLocation(); const { permission } = PermissionButton.usePermission('system/Department'); const handleTreeMap = (_data: any[]) => { if (_data) { _data.map((item) => { TreeMap.set(item.id, omit(cloneDeep(item), ['children'])); if (item.children) { handleTreeMap(item.children); } }); } }; const queryTreeData = async () => { setKeys([]); const terms: Record = {}; if (searchKey.current) { terms.terms = [{ column: 'name$LIKE', value: `%${searchKey.current}%` }]; } setLoading(true); const resp = await service.queryOrgThree({ paging: false, sorts: [{ name: 'sortIndex', order: 'asc' }], ...terms, }); setLoading(false); if (resp.status === 200) { handleTreeMap(resp.result); setTreeData(resp.result); if (resp.result && resp.result.length) { setKeys([resp.result[0].id]); } } }; const queryList = (list: any, id: string, flag?: boolean) => { if (list && Array.isArray(list) && list.length) { return list.map((item) => { if (item.id === id || flag) { item.disabled = true; } if (item.children && Array.isArray(item.children) && item.children.length) { item.children = queryList(item.children, id, item.id === id || flag); } return item; }); } else { return []; } }; const updateOrg = (id: string) => { const list = _.cloneDeep(treeData); setTreeDataList(queryList(list, id)); }; const deleteItem = async (id: string) => { const response: any = await service.remove(id); if (response.status === 200) { onlyMessage( intl.formatMessage({ id: 'pages.data.option.success', defaultMessage: '操作成功!', }), ); queryTreeData(); } }; const searchByTreeMap = (key: string) => { const searchTree: string[] = []; const treeArray = new Map(); if (key) { TreeMap.forEach((item) => { if (item.name.includes(key)) { searchTree.push(item.parentId); treeArray.set(item.id, item); } }); function dig(_data: any[]): any { const pIds: string[] = []; if (!_data.length) return; _data.forEach((item) => { if (TreeMap.has(item)) { const _item = TreeMap.get(item); pIds.push(_item.parentId); treeArray.set(item, _item); } }); } dig(searchTree); const arr = ArrayToTree(cloneDeep([...treeArray.values()])); setTreeData(arr); } else { setTreeData(ArrayToTree(cloneDeep([...TreeMap.values()]))); } }; const onSearchChange = (e: any) => { searchKey.current = e.target.value; // queryTreeData(); searchByTreeMap(e.target.value); }; const schema: ISchema = { type: 'object', properties: { parentId: { type: 'string', title: '上级组织', 'x-decorator': 'FormItem', 'x-component': 'TreeSelect', 'x-component-props': { fieldNames: { label: 'name', value: 'id', }, placeholder: '请选择上级组织', }, enum: treeDataList, }, name: { type: 'string', title: intl.formatMessage({ id: 'pages.table.name', defaultMessage: '名称', }), required: true, 'x-decorator': 'FormItem', 'x-component': 'Input', 'x-component-props': { placeholder: '请输入名称', }, 'x-validator': [ { max: 64, message: '最多可输入64个字符', }, { required: true, message: '请输入名称', }, ], }, sortIndex: { type: 'string', title: intl.formatMessage({ id: 'pages.device.instanceDetail.detail.sort', defaultMessage: '排序', }), required: true, 'x-decorator': 'FormItem', 'x-component': 'NumberPicker', 'x-component-props': { placeholder: '请输入排序', }, 'x-validator': [ { required: true, message: '请输入排序', }, { pattern: /^[0-9]*[1-9][0-9]*$/, message: '请输入大于0的整数', }, ], }, }, }; useEffect(() => { if ((location as any).query?.save === 'true') { setData({ sortIndex: treeData && treeData.length + 1 }); setVisible(true); setTreeDataList(treeData); } }, [location, treeData]); useEffect(() => { queryTreeData(); TreeMap.clear(); }, []); useEffect(() => { if (keys.length) { props.onSelect(keys[0]); } }, [keys]); return (
{loading && (
)} } onChange={debounce(onSearchChange, 500)} /> { const sortIndex = treeData && treeData.length ? treeData[treeData.length - 1].sortIndex + 1 : 1; setData({ sortIndex }); setVisible(true); setTreeDataList(treeData); }} > 新增 {/* */} {treeData ? (
{ if (_keys && _keys.length) { setKeys(_keys); } }} expandedKeys={expandedKeys} onExpand={(_keys: any[]) => { setExpandedKeys(_keys); }} titleRender={(nodeData: any) => { return (
{ setShowToolIndex(nodeData.id); }} onMouseLeave={() => { setShowToolIndex(''); }} > { e.stopPropagation(); updateOrg(nodeData.id); setData({ ...nodeData, }); setVisible(true); }} > { e.stopPropagation(); setData({ parentId: nodeData.id, sortIndex: nodeData.children ? nodeData.children.length + 1 : 1, }); setVisible(true); setTreeDataList(treeData); }} > { e.stopPropagation(); }} tooltip={{ title: intl.formatMessage({ id: 'pages.data.option.delete', defaultMessage: '删除', }), }} isPermission={permission.delete} >
); }} />
) : (
)} { setVisible(false); setData(undefined); }} reload={async (pId) => { await queryTreeData(); if (pId && !expandedKeys.includes(pId)) { setExpandedKeys([...expandedKeys, pId]); } }} data={data} schema={schema} />
); };