'use strict'; async function getDepUsers(ctx) { try { const models = ctx.fs.dc.models; //所有部门 const departments = await models.Department.findAll({ attributes: ['id', 'name', 'dependence'], order: [['id', 'asc']], where: { delete: false, }, }) //所有用户 const allusers = await models.User.findAll({ attributes: ['id', 'name', 'username', 'department_id'], order: [['id', 'asc']], where: { delete: false, }, }) const employees = [...new Set(allusers)] function collectEmployees(departments, employees) { const result = []; const processDepartment = (department) => { const departmentData = { depId: department.id, depName: department.name, users: [], expanded: false, // 初始状态折叠 }; const departmentEmployees = employees.filter(employee => //console.log('employee.dataVaules.department_id', employee.department_id) employee.dataValues.department_id === department.id ); departmentData.users.push(...departmentEmployees); departments.forEach(subDepartment => { if (subDepartment.dependence === department.id) { const subDepartmentData = processDepartment(subDepartment); departmentData.users.push(...subDepartmentData.users); } }); return departmentData; }; departments.forEach(department => { if (department.dependence === null) { const departmentData = processDepartment(department); result.push(departmentData); } }); return result; } const result = collectEmployees(departments, employees); ctx.status = 200; ctx.body = result } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = {} } } module.exports = { getDepUsers };