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 { AdminPermissionModel } from '../../model/admin-permission' ; import { AdminRoleModel } from '../../model/admin-role' ; import { QueryDTO , CreateDTO , UpdateDTO } from '../../dto/admin/role' ; import MyError from '../../util/my-error' ; @ Provide ( ) export class AdminRoleService { @ Inject ( ) ctx : Context ; @ InjectEntityModel ( AdminRoleModel ) adminRoleModel : Repository < AdminRoleModel > ; param { QueryDTO } params async queryAdminRole ( 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 . slug ) { where . slug = Like ( `${ filter . slug }%` ) ; } const [ list , total ] = await this . adminRoleModel . findAndCount ( { where , order , take : pageSize , skip : pageSize * ( current - 1 ) , } ) ; return { current , pageSize , total , list , } ; } param { String } id async getAdminRoleById ( id ) { const row = await this . adminRoleModel . createQueryBuilder ( ) . select ( ) . leftJoinAndSelect ( 'AdminRoleModel.permissions' , 'permission' ) . leftJoinAndSelect ( 'AdminRoleModel.menu' , 'menu' ) . where ( { id : id } ) . getOne ( ) ; return row ; } param { CreateDTO } params async createAdminRole ( params ) { let role = new AdminRoleModel ( ) ; const permissions = params . permissions ? params . permissions . map ( item => { const permission = new AdminPermissionModel ( ) ; permission . id = item ; return permission ; } ) : [ ] ; role = this . adminRoleModel . merge ( role , { ... params , permissions : permissions , } ) ; const created = await this . adminRoleModel . save ( role ) ; return created ; } param { UpdateDTO } params async updateAdminRole ( params ) { const { id , permissions : newPermissions , ... column } = params ; const role = await this . getAdminRoleById ( id ) ; if ( newPermissions ) { const oldPermissions = role . permissions . map ( item => item . id ) ; const [ increase , decrease ] = this . ctx . helper . arrayDiff ( newPermissions , oldPermissions ) ; await Promise . all ( [ this . adminRoleModel . createQueryBuilder ( ) . relation ( AdminRoleModel , 'permissions' ) . of ( role ) . add ( increase ) , this . adminRoleModel . createQueryBuilder ( ) . relation ( AdminRoleModel , 'permissions' ) . of ( role ) . remove ( decrease ) , ] ) ; } return this . adminRoleModel . createQueryBuilder ( ) . update ( role ) . set ( column ) . where ( { id : id } ) . execute ( ) ; } param { string [ ] } ids async removeAdminRoleByIds ( ids ) { return this . adminRoleModel . createQueryBuilder ( ) . delete ( ) . where ( { id : In ( ids ) , } ) . execute ( ) ; } param { string [ ] } ids async checkRoleExists ( ids = [ ] ) { const count = await this . adminRoleModel . count ( { where : { id : In ( ids ) , } , } ) ; assert . deepStrictEqual ( count , ids . length , new MyError ( '角色不存在，请检查' , 400 ) ) ; } }