@@ -2,7 +2,7 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
22import { AccountEntity } from './entities/account.entity' ;
33import { Request } from 'express' ;
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 { AccountDto } from './dto/account.dto' ;
77import { ICurrentUserType } from '@src/decorators' ;
88import { ToolsService } from '@src/plugin/tools/tools.service' ;
@@ -70,8 +70,25 @@ export class AccountService {
7070 * @param {number } id
7171 * @return {* }
7272 */
73- async deleteAccountByIdApi ( id : number ) : Promise < string > {
73+ async deleteAccountByIdApi ( id : number , currentUser : ICurrentUserType ) : Promise < string > {
74+ const { accountId } = currentUser ;
7475 // TODO 判断不能删除自己及下面有账号的
76+ if ( Object . is ( id , accountId ) ) {
77+ throw new HttpException ( '自己不能删除自己' , HttpStatus . OK ) ;
78+ }
79+ const accountEntity : Pick < AccountEntity , 'id' > | null = await this . accountRepository . findOne ( {
80+ where : { parentId : id } ,
81+ select : [ 'id' ] ,
82+ } ) ;
83+ if ( accountEntity ?. id ) {
84+ throw new HttpException ( '下面有子账号不能直接删除' , HttpStatus . OK ) ;
85+ }
86+ // 超管不能被删除
87+ const accountEntity1 : Pick < AccountEntity , 'accountType' > | null =
88+ await this . accountRepository . findOne ( { where : { id } , select : [ 'accountType' ] } ) ;
89+ if ( accountEntity1 && accountEntity1 . accountType == AccountTypeEnum . SUPER_ACCOUNT ) {
90+ throw new HttpException ( '超管不能被删除' , HttpStatus . OK ) ;
91+ }
7592 const { affected } = await this . accountRepository . softDelete ( id ) ;
7693 if ( affected ) {
7794 return '删除成功' ;
@@ -194,6 +211,80 @@ export class AccountService {
194211 return await this . queryAccountBuilder . where ( 'account.id = :id' , { id } ) . getRawOne ( ) ;
195212 }
196213
214+ /**
215+ * @Author : 水痕
216+ * @Date : 2023-10-10 15:42:14
217+ * @LastEditors : 水痕
218+ * @Description : 批量删除
219+ * @return {* }
220+ */
221+ async batchDeleteAccountByIdListApi (
222+ idList : number [ ] ,
223+ currentUser : ICurrentUserType
224+ ) : Promise < string > {
225+ const { accountId } = currentUser ;
226+ // TODO 判断不能删除自己及下面有账号的
227+ if ( idList . includes ( accountId ) ) {
228+ throw new HttpException ( '自己不能删除自己' , HttpStatus . OK ) ;
229+ }
230+ const accountEntityList : Pick < AccountEntity , 'id' > [ ] = await this . accountRepository . find ( {
231+ where : { parentId : In ( idList ) } ,
232+ select : [ 'id' ] ,
233+ } ) ;
234+ if ( accountEntityList . length ) {
235+ throw new HttpException ( '下面有子账号不能直接删除' , HttpStatus . OK ) ;
236+ }
237+ // 超管不能被删除
238+ const accountEntityList1 : Pick < AccountEntity , 'accountType' > [ ] =
239+ await this . accountRepository . find ( { where : { id : In ( idList ) } , select : [ 'accountType' ] } ) ;
240+ if (
241+ accountEntityList1 . length &&
242+ accountEntityList1
243+ . map ( ( item ) => item . accountType )
244+ . some ( ( item ) => item == AccountTypeEnum . SUPER_ACCOUNT )
245+ ) {
246+ throw new HttpException ( '超管不能被删除' , HttpStatus . OK ) ;
247+ }
248+ const { affected } = await this . accountRepository . softDelete ( idList ) ;
249+ if ( affected ) {
250+ return '删除成功' ;
251+ } else {
252+ return '删除失败' ;
253+ }
254+ }
255+
256+ /**
257+ * @Author : 水痕
258+ * @Date : 2023-10-10 15:43:23
259+ * @LastEditors : 水痕
260+ * @Description : 批量修改状态
261+ * @return {* }
262+ */
263+ async batchModifyAccountStatusByIdApi (
264+ idList : number [ ] ,
265+ currentUser : ICurrentUserType
266+ ) : Promise < string > {
267+ const { accountId } = currentUser ;
268+ if ( idList . includes ( accountId ) ) {
269+ throw new HttpException ( '自己不能修改自己' , HttpStatus . OK ) ;
270+ }
271+ const accountEntityList : Pick < AccountEntity , 'status' > [ ] = await this . accountRepository . find ( {
272+ where : { id : In ( idList ) } ,
273+ select : [ 'status' ] ,
274+ } ) ;
275+ const statusList = accountEntityList . map ( ( item ) => item . status ) ;
276+ if ( [ ...new Set ( statusList ) ] . length > 1 ) {
277+ throw new HttpException ( '当前传递的数据状态不统一,不能批量操作' , HttpStatus . OK ) ;
278+ }
279+ const { affected } = await this . accountRepository . update ( idList , {
280+ status : statusList [ 0 ] == StatusEnum . FORBIDDEN ? StatusEnum . NORMAL : StatusEnum . FORBIDDEN ,
281+ } ) ;
282+ if ( affected ) {
283+ return '修改成功' ;
284+ } else {
285+ return '修改失败' ;
286+ }
287+ }
197288 /**
198289 * @Author : 水痕
199290 * @Date : 2023-10-07 20:28:53
0 commit comments