const depts = { name:'平安大酒店', id:'PROPERTY', children:[ { name: '财务部', id:'FINANCE', children:[ { name:'应收会计', id:'RECEIVABLE', }, { name:'应付会计', id:'PAYABLE', }, { name:'出纳', id:'CASH', }, ] }, { name: '销售部', id:'SALES', children:[ { name: '客房销售', id:'SALES_ROOMS' }, { name: '宴会销售', id:'SALES_BQT' }, { name:'预订部', id:'RESERVE', children:[ ] }, ] }, { name: '收益管理', id:'REVENUE', children:[ ] }, { name: '工程部', id:'ENGINEER', children:[ ] }, { name: '防损部', id:'SECURITY', children:[ ] }, { name: '客房部', id:'ROOMS', children:[ { name:'前厅部', id:'FO', children:[ { name:'前台', id:'FD', }, { name:'礼宾部', id:'CONCIERGE', }, { name:'行李员', id:'BELL', }, ] }, { name:'管家部', id:'HSKP', children:[ { name:'管家部 - 公共区域', id:'HSKP_PUB', }, { name:'管家部 - 客房', id:'HSKP_ROOMS', }, ] }, { name:'总机', id:'CALL', children:[ ] }, ] }, { name: '餐饮部', id:'FB', children:[ { name:'全日餐', id:'ADD', }, { name:'西餐厅', id:'RESTAURANT_1', }, { name:'酒廊', id:'LOUNGE', }, { name:'宴会', id:'BQT', }, { name:'送餐部', id:'IRD', }, ] }, { name: '康养', id:'WELLNESS', children:[ { name:'健身中心', id:'FIT', }, { name:'泳池', id:'POOL', }, { name:'水疗中心', id:'SPA', }, ] }, { name: '人力资源部', id:'HR', children:[ ] }, ] } const flattenDept = (d:any) => { if(d.children){ return [d, ...d.children.map(flattenDept).flat()] }else{ return [d] } } const getDepartments = async () => { return depts } const getParentDepartments = async (target:string) => { const d = await getDepartments() const recurse = (dArray:any[]) => { const tail = dArray[dArray.length - 1] if(!tail){ return [] }else if(tail.name == target){ return dArray }else if(tail.children && tail.children.length > 0){ for(let i = 0; i < tail.children.length; i++){ const childRes = recurse([tail.children[i]]) if(childRes.length > 0){ return [...dArray, ...childRes] } } return [] }else{ return [] } } return recurse([d]) } const getFlatDepartments = async () => { const flatDepts = await flattenDept(depts) return flatDepts } const getDeptMappings = async () => { const flatDepts = await flattenDept(depts) console.log('flatDepts', flatDepts) const out = {} flatDepts.forEach(r=>{ out[r.id] = r.name }) return out } const getChildrenDepts = async (id) => { const flatDepts = await flattenDept(depts) const foundDept = flatDepts.find(d=>d.id == id) if(foundDept) { return await flattenDept(foundDept) }else{ return [] } } export { depts, getDepartments, getParentDepartments, getFlatDepartments, getDeptMappings, getChildrenDepts }