Skip to content

Commit 5f0c985

Browse files
committed
feat: 查询列表
1 parent f8e443d commit 5f0c985

File tree

2 files changed

+114
-4
lines changed

2 files changed

+114
-4
lines changed

src/api/account/account.controller.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ export class AccountController {
3333
}
3434

3535
@Delete(':id')
36-
async deleteAccountByIdApi(@Param('id', new ParseIntPipe()) id: number): Promise<string> {
37-
return await this.accountService.deleteAccountByIdApi(id);
36+
async deleteAccountByIdApi(
37+
@Param('id', new ParseIntPipe()) id: number,
38+
@CurrentUser('userInfo') currentUser: ICurrentUserType
39+
): Promise<string> {
40+
return await this.accountService.deleteAccountByIdApi(id, currentUser);
3841
}
3942

4043
@Put('/status/:id')
@@ -64,4 +67,20 @@ export class AccountController {
6467
): Promise<AccountVo | undefined> {
6568
return await this.accountService.getAccountByIdApi(id);
6669
}
70+
71+
@Post('delete')
72+
async batchDeleteAccountByIdListApi(
73+
@Body() idList: number[],
74+
@CurrentUser('userInfo') currentUser: ICurrentUserType
75+
): Promise<string> {
76+
return await this.accountService.batchDeleteAccountByIdListApi(idList, currentUser);
77+
}
78+
79+
@Post('/batchStatus')
80+
async batchModifyAccountStatusByIdApi(
81+
@Body() idList: number[],
82+
@CurrentUser('userInfo') currentUser: ICurrentUserType
83+
): Promise<string> {
84+
return await this.accountService.batchModifyAccountStatusByIdApi(idList, currentUser);
85+
}
6786
}

src/api/account/account.service.ts

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
22
import { AccountEntity } from './entities/account.entity';
33
import { Request } from 'express';
44
import { InjectRepository } from '@nestjs/typeorm';
5-
import { Equal, FindOperator, ILike, Repository, SelectQueryBuilder } from 'typeorm';
5+
import { Equal, FindOperator, ILike, In, Repository, SelectQueryBuilder } from 'typeorm';
66
import { AccountDto } from './dto/account.dto';
77
import { ICurrentUserType } from '@src/decorators';
88
import { 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

Comments
 (0)