import { BaseService } from '../../base' ; import { Repository , In } from 'typeorm' ; import SysDepartment from '../../../entity/admin/sys/department' ; import { InjectEntityModel } from '@midwayjs/orm' ; import { IInfoDeptResult } from '../interface' ; import { Config , Inject , Provide } from '@midwayjs/decorator' ; import { MoveDept , UpdateDeptDto } from '../../../dto/admin/sys/dept' ; import SysUser from '../../../entity/admin/sys/user' ; import SysRoleDepartment from '../../../entity/admin/sys/role_department' ; import { AdminSysRoleService } from './role' ; import { includes } from 'lodash' ; @ Provide ( ) export class AdminSysDeptService extends BaseService { @ InjectEntityModel ( SysDepartment ) dept : Repository < SysDepartment > ; @ InjectEntityModel ( SysRoleDepartment ) roleDept : Repository < SysRoleDepartment > ; @ Inject ( ) adminSysRoleService : AdminSysRoleService ; @ InjectEntityModel ( SysUser ) user : Repository < SysUser > ; @ Config ( 'rootRoleId' ) rootRoleId : number ; /**
     * 获取所有部门
     */ async list ( ) { return await this . dept . find ( ) ; } /**
     * 根据ID查找部门信息
     */ async info ( id ) { const department = await this . dept . findOne ( { id } ) ; let parentDepartment = null ; if ( department ! . parentId ) { parentDepartment = await this . dept . findOne ( { id : department ! . parentId } ) ; } return { department , parentDepartment } ; } /**
     * 更新部门信息
     */ async update ( param ) { await this . dept . update ( param . id , { parentId : param . parentId === - 1 ? undefined : param . parentId , name : param . name , orderNum : param . orderNum , } ) ; } /**
     * 转移部门
     */ async transfer ( userIds , deptId ) { await this . user . update ( { id : In ( userIds ) } , { departmentId : deptId } ) ; } /**
     * 新增部门
     */ async add ( deptName , parentDeptId ) { await this . dept . insert ( { name : deptName , parentId : parentDeptId === - 1 ? undefined : parentDeptId , } ) ; } /**
     * 移动排序
     */ async move ( depts ) { await this . getManager ( ) . transaction ( async ( manager ) => { for ( let i = 0 ; i < depts . length ; i ++ ) { await manager . update ( SysDepartment , { id : depts [ i ] . id } , { parentId : depts [ i ] . parentId } ) ; } } ) ; } /**
     * 根据ID删除部门
     */ async delete ( departmentId ) { await this . dept . delete ( departmentId ) ; } /**
     * 根据部门查询关联的用户数量
     */ async countUserByDeptId ( id ) { return await this . user . count ( { departmentId : id } ) ; } /**
     * 根据部门查询关联的角色数量
     */ async countRoleByDeptId ( id ) { return await this . roleDept . count ( { departmentId : id } ) ; } /**
     * 查找当前部门下的子部门数量
     */ async countChildDept ( id ) { return await this . dept . count ( { parentId : id } ) ; } /**
     * 根据当前角色id获取部门列表
     */ async getDepts ( uid ) { const roleIds = await this . adminSysRoleService . getRoleIdByUser ( uid ) ; let depts = [ ] ; if ( includes ( roleIds , this . rootRoleId ) ) { depts = await this . dept . find ( ) ; } else { depts = await this . dept . createQueryBuilder ( 'dept' ) . innerJoinAndSelect ( 'sys_role_department' , 'role_dept' , 'dept.id = role_dept.department_id' ) . andWhere ( 'role_dept.role_id IN (:...roldIds)' , { roldIds : roleIds } ) . orderBy ( 'dept.order_num' , 'ASC' ) . getMany ( ) ; } return depts ; } }