@@ -2,7 +2,7 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
22import { RoleEntity } from './entities/role.entity' ;
33
44import { InjectRepository } from '@nestjs/typeorm' ;
5- import { Equal , FindOperator , ILike , Repository , SelectQueryBuilder } from 'typeorm' ;
5+ import { Equal , FindOperator , ILike , In , Repository , SelectQueryBuilder } from 'typeorm' ;
66import { ICurrentUserType } from '@src/decorators' ;
77import { RoleDto } from './dto/role.dto' ;
88import { PageEnum , StatusEnum } from '@src/enums' ;
@@ -33,7 +33,7 @@ export class RoleService {
3333 const { id, tenantId } = currentInfo ;
3434 // 1.判断当前账号下角色是否存在
3535 const roleEntity : Pick < RoleEntity , 'id' > | null = await this . roleRepository . findOne ( {
36- where : { name : req . name , tenantId : tenantId } ,
36+ where : { name : req . name , accountId : id } ,
3737 select : [ 'id' ] ,
3838 } ) ;
3939 if ( roleEntity ?. id ) {
@@ -103,6 +103,14 @@ export class RoleService {
103103 * @return {* }
104104 */
105105 async modifyRoleByIdApi ( id : number , req : RoleDto ) : Promise < string > {
106+ // 判断名字是否重复
107+ const roleEntity : Pick < RoleEntity , 'id' > | null = await this . roleRepository . findOne ( {
108+ where : { name : req . name } ,
109+ select : [ 'id' ] ,
110+ } ) ;
111+ if ( roleEntity && roleEntity . id != id ) {
112+ throw new HttpException ( `[${ req . name } ]可能重复` , HttpStatus . OK ) ;
113+ }
106114 const { affected } = await this . roleRepository . update ( id , req ) ;
107115 if ( affected ) {
108116 return '修改成功' ;
@@ -176,6 +184,50 @@ export class RoleService {
176184 return await this . queryRoleBuilder . where ( 'role.id = :id' , { id } ) . getRawOne ( ) ;
177185 }
178186
187+ /**
188+ * @Author : 水痕
189+ * @Date : 2023-10-10 20:39:04
190+ * @LastEditors : 水痕
191+ * @Description : 根据角色id批量删除
192+ * @param {number } idList
193+ * @return {* }
194+ */
195+ async batchDeleteRoleByIdListApi ( idList : number [ ] ) : Promise < string > {
196+ const { affected } = await this . roleRepository . softDelete ( idList ) ;
197+ if ( affected ) {
198+ return '删除成功' ;
199+ } else {
200+ return '删除失败' ;
201+ }
202+ }
203+
204+ /**
205+ * @Author : 水痕
206+ * @Date : 2023-10-10 20:41:14
207+ * @LastEditors : 水痕
208+ * @Description : 根据id批量修改状态
209+ * @param {number } idList
210+ * @return {* }
211+ */
212+ async batchModifyRoleStatusByIdApi ( idList : number [ ] ) : Promise < string > {
213+ const roleEntityList : Pick < RoleEntity , 'status' > [ ] = await this . roleRepository . find ( {
214+ where : { id : In ( idList ) } ,
215+ select : [ 'status' ] ,
216+ } ) ;
217+ const statusList = roleEntityList . map ( ( item ) => item . status ) ;
218+ if ( [ ...new Set ( statusList ) ] . length > 1 ) {
219+ throw new HttpException ( '当前传递的数据状态不统一,不能批量操作' , HttpStatus . OK ) ;
220+ }
221+ const { affected } = await this . roleRepository . update ( idList , {
222+ status : statusList [ 0 ] == StatusEnum . FORBIDDEN ? StatusEnum . NORMAL : StatusEnum . FORBIDDEN ,
223+ } ) ;
224+ if ( affected ) {
225+ return '修改成功' ;
226+ } else {
227+ return '修改失败' ;
228+ }
229+ }
230+
179231 get queryRoleBuilder ( ) : SelectQueryBuilder < RoleEntity > {
180232 return this . roleRepository
181233 . createQueryBuilder ( 'role' )
@@ -196,7 +248,7 @@ export class RoleService {
196248 . addSelect ( 'account.username' , 'accountUsername' )
197249 . from ( AccountEntity , 'account' ) ,
198250 'account' ,
199- 'role.accountId=account.id '
251+ 'role.accountId=account.accountId '
200252 )
201253 . leftJoinAndMapOne (
202254 'xx' ,
@@ -206,7 +258,7 @@ export class RoleService {
206258 . addSelect ( 'tenant.name' , 'tenantName' )
207259 . from ( TenantEntity , 'tenant' ) ,
208260 'tenant' ,
209- 'role.tenantId=tenant.id '
261+ 'role.tenantId=tenant.tenantId '
210262 ) ;
211263 }
212264}
0 commit comments