import * as assert from 'assert' ; import { Provide , Inject } from '@midwayjs/decorator' ; import { InjectEntityModel } from '@midwayjs/orm' ; import { Repository , Like , In } from 'typeorm' ; import { Context } from '@/interface' ; import { AdminUserModel } from '../../model/admin-user' ; import { AdminRoleModel } from '../../model/admin-role' ; import { AdminPermissionModel } from '../../model/admin-permission' ; import { QueryDTO , CreateDTO , UpdateDTO } from '../../dto/admin/user' ; import MyError from '../../util/my-error' ; @ Provide ( ) export class AdminUserService { @ Inject ( ) ctx : Context ; @ InjectEntityModel ( AdminUserModel ) adminUserModel : Repository < AdminUserModel > ; param { QueryDTO } params async queryAdminUser ( params ) { const { pageSize , current , sorter , ... filter } = params ; const where = { } ; const order = { id : 'DESC' } ; if ( sorter ) { const [ column , sort ] = sorter . split ( '_' ) ; order [ column ] = sort . split ( 'end' ) [ 0 ] . toUpperCase ( ) ; } if ( filter . id ) { where . id = Like ( filter . id ) ; } if ( filter . name ) { where . name = Like ( `${ filter . name }%` ) ; } if ( filter . username ) { where . username = Like ( `${ filter . username }%` ) ; } const [ list , total ] = await this . adminUserModel . findAndCount ( { where , order , take : pageSize , skip : pageSize * ( current - 1 ) , } ) ; return { current , pageSize , total , list , } ; } param id async getAdminUserById ( id ) { const row = await this . adminUserModel . createQueryBuilder ( ) . select ( ) . leftJoinAndSelect ( 'AdminUserModel.roles' , 'role' ) . leftJoinAndSelect ( 'AdminUserModel.permissions' , 'permission' ) . where ( { id : id } ) . getOne ( ) ; return row ; } param { CreateDTO } params async createAdminUser ( params ) { let user = new AdminUserModel ( ) ; const roles = params . roles ? params . roles . map ( item => { const role = new AdminRoleModel ( ) ; role . id = item ; return role ; } ) : [ ] ; const permissions = params . permissions ? params . permissions . map ( item => { const role = new AdminPermissionModel ( ) ; role . id = item ; return role ; } ) : [ ] ; user = this . adminUserModel . merge ( user , { ... params , roles : roles , permissions : permissions , } ) ; const created = await this . adminUserModel . save ( user ) ; return created ; } param { UpdateDTO } params async updateAdminUser ( params ) { const { id , roles : newRoles , permissions : newPermissions , password , ... columns } = params ; const user = await this . getAdminUserById ( id ) ; let newPassword = user . password ; if ( password ) { password !== user . password && ( newPassword = this . ctx . helper . bhash ( password ) ) ; } if ( newRoles ) { const oldRoles = user . roles . map ( item => item . id ) ; const [ increase , decrease ] = this . ctx . helper . arrayDiff ( newRoles , oldRoles ) ; await Promise . all ( [ this . adminUserModel . createQueryBuilder ( ) . relation ( AdminUserModel , 'roles' ) . of ( user ) . add ( increase ) , this . adminUserModel . createQueryBuilder ( ) . relation ( AdminUserModel , 'roles' ) . of ( user ) . remove ( decrease ) , ] ) ; } if ( newPermissions ) { const oldPermissions = user . permissions . map ( item => item . id ) ; const [ increase , decrease ] = this . ctx . helper . arrayDiff ( newPermissions , oldPermissions ) ; await Promise . all ( [ this . adminUserModel . createQueryBuilder ( ) . relation ( AdminUserModel , 'permissions' ) . of ( user ) . add ( increase ) , this . adminUserModel . createQueryBuilder ( ) . relation ( AdminUserModel , 'permissions' ) . of ( user ) . remove ( decrease ) , ] ) ; } return this . adminUserModel . createQueryBuilder ( ) . update ( user ) . set ( { ... columns , password : newPassword } ) . where ( { id : id } ) . execute ( ) ; } param { string [ ] } ids async removeAdminUserByIds ( ids ) { return this . adminUserModel . createQueryBuilder ( ) . delete ( ) . where ( { id : In ( ids ) , } ) . execute ( ) ; } param { string [ ] } ids async checkUserExists ( ids ) { const count = await this . adminUserModel . count ( { where : { id : In ( ids ) , } , } ) ; assert . deepStrictEqual ( count , ids . length , new MyError ( '管理员不存在，请检查' , 400 ) ) ; } }