import * as assert from 'assert' ; import { Provide } from '@midwayjs/decorator' ; import { InjectEntityModel } from '@midwayjs/orm' ; import { Repository , Like , In } from 'typeorm' ; import { AdminPermissionModel } from '../../model/admin-permission' ; import { QueryDTO , CreateDTO , UpdateDTO } from '../../dto/admin/permission' ; import MyError from '../../util/my-error' ; @ Provide ( ) export class AdminPermissionService { @ InjectEntityModel ( AdminPermissionModel ) adminPermissionModel : Repository < AdminPermissionModel > ; param { QueryDTO } params async queryAdminPermission ( 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 }%` ) ; } if ( filter . httpPath ) { where . httpPath = Like ( `${ filter . httpPath }%` ) ; } if ( filter . httpMethod ) { where . httpMethod = Like ( `${ filter . httpMethod }%` ) ; } const [ list , total ] = await this . adminPermissionModel . findAndCount ( { where , order , take : pageSize , skip : pageSize * ( current - 1 ) , } ) ; return { current , pageSize , total , list , } ; } param { String } id async getAdminPermissionById ( id ) { const row = await this . adminPermissionModel . createQueryBuilder ( ) . select ( ) . leftJoinAndSelect ( 'AdminPermissionModel.roles' , 'role' ) . leftJoinAndSelect ( 'AdminPermissionModel.menu' , 'menu' ) . where ( { id : id } ) . getOne ( ) ; return row ; } param { CreateDTO } params async createAdminPermission ( params ) { let permission = new AdminPermissionModel ( ) ; permission = this . adminPermissionModel . merge ( permission , params ) ; const created = await this . adminPermissionModel . save ( permission ) ; return created ; } param { UpdateDTO } params async updateAdminPermission ( params ) { const { id , ... columns } = params ; return this . adminPermissionModel . createQueryBuilder ( ) . update ( ) . set ( columns ) . where ( 'id = :id' , { id } ) . execute ( ) ; } param { string [ ] } ids async removeAdminPermissionByIds ( ids ) { return ( this . adminPermissionModel . createQueryBuilder ( ) . delete ( ) . where ( { id : In ( ids ) , } ) . execute ( ) ) ; } param { string [ ] } ids async checkPermissionExists ( ids = [ ] ) { const count = await this . adminPermissionModel . count ( { where : { id : In ( ids ) , } , } ) ; assert . deepStrictEqual ( count , ids . length , new MyError ( '权限不存在，请检查' , 400 ) ) ; } }