import * as assert from 'assert' ; import { Provide , Inject } from '@midwayjs/decorator' ; import { InjectEntityModel } from '@midwayjs/orm' ; import { Repository , In } from 'typeorm' ; import { Context } from '@/interface' ; import { AdminMenuModel } from '../../model/admin-menu' ; import { CreateDTO , QueryDTO , UpdateDTO } from '../../dto/admin/menu' ; import { AdminRoleModel } from '../../model/admin-role' ; import MyError from '../../util/my-error' ; @ Provide ( ) export class AdminMenuService { @ Inject ( ) ctx : Context ; @ InjectEntityModel ( AdminMenuModel ) adminMenuModel : Repository < AdminMenuModel > ; param { QueryDTO } params async queryAdminMenu ( params ) { const { pageSize , current } = params ; const order = { order : 'DESC' } ; const [ list , total ] = await this . adminMenuModel . findAndCount ( { order , take : pageSize , skip : pageSize * ( current - 1 ) , } ) ; return { current , pageSize , total , list , } ; } param id async getAdminMenuById ( id ) { const row = await this . adminMenuModel . createQueryBuilder ( ) . select ( ) . leftJoinAndSelect ( 'AdminMenuModel.roles' , 'role' ) . where ( { id : id } ) . getOne ( ) ; return row ; } param { CreateDTO } params async createAdminMenu ( params ) { let menu = new AdminMenuModel ( ) ; const roles = params . roles . map ( item => { const role = new AdminRoleModel ( ) ; role . id = item ; return role ; } ) ; menu = this . adminMenuModel . merge ( menu , { ... params , roles : roles } ) ; const created = await this . adminMenuModel . save ( menu ) ; return created ; } param { UpdateDTO } params async updateAdminMenu ( params ) { const { id , roles : newRoles , ... columns } = params ; const menu = await this . getAdminMenuById ( id ) ; if ( newRoles ) { const oldRoles = menu . roles . map ( item => item . id ) ; const [ increase , decrease ] = this . ctx . helper . arrayDiff ( newRoles , oldRoles ) ; await Promise . all ( [ this . adminMenuModel . createQueryBuilder ( ) . relation ( AdminMenuModel , 'roles' ) . of ( menu ) . add ( increase ) , this . adminMenuModel . createQueryBuilder ( ) . relation ( AdminMenuModel , 'roles' ) . of ( menu ) . remove ( decrease ) , ] ) ; } return this . adminMenuModel . createQueryBuilder ( ) . update ( menu ) . set ( columns ) . where ( { id : menu . id , } ) . execute ( ) ; } param { string [ ] } ids async removeAdminMenuByIds ( ids ) { return this . adminMenuModel . createQueryBuilder ( ) . delete ( ) . where ( { id : In ( ids ) , } ) . execute ( ) ; } param { string [ ] } ids async checkMenuExists ( ids ) { const count = await this . adminMenuModel . count ( { where : { id : In ( ids ) , } , } ) ; assert . deepStrictEqual ( count , ids . length , new MyError ( '菜单不存在，请检查' , 400 ) ) ; } param params async orderAdminMenu ( params ) { const queue = params . map ( item => { const { id , ... field } = item ; return this . adminMenuModel . createQueryBuilder ( ) . update ( field ) . where ( { id , } ) . execute ( ) ; } ) ; return Promise . all ( queue ) ; } }