Skip to content

Commit 9d60fbd

Browse files
committed
feat: 添加批量操作
1 parent 19a47a1 commit 9d60fbd

File tree

6 files changed

+102
-7
lines changed

6 files changed

+102
-7
lines changed

src/api/login/login.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export class LoginService {
8282
.createQueryBuilder('account')
8383
.select('account.id', 'id')
8484
.addSelect('account.username', 'username')
85+
.addSelect('account.tenantId', 'tenantId')
8586
.addSelect('account.status', 'status')
8687
.addSelect('account.accountType', 'accountType')
8788
.addSelect('account.password', 'password')

src/api/login/vo/login.vo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class LoginVo {
1010
export class LoginAccountVo {
1111
readonly id!: number; // 账号id
1212
readonly username!: string; // 用户名
13+
readonly tenantId!: number; // 商户id
1314
readonly accountType!: number; // 账号类型:0普通账号,1是主账号,2是超管
1415
readonly status!: number; // 状态0是正常,1是禁用
1516
readonly password!: string; // 密码

src/api/tenant/dto/tenant.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class TenantDto {
1111
@IsOptional({ message: '商户联系人' })
1212
username!: string;
1313

14-
@IsMobilePhone('zh-CN', { strictMode: true }, { message: '手机号码格式错误' })
14+
@IsMobilePhone('zh-CN', { strictMode: false }, { message: '手机号码格式错误' })
1515
@IsOptional({ message: '手机号码' })
1616
mobile!: string;
1717

src/api/tenant/tenant.controller.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Query,
1111
UseGuards,
1212
} from '@nestjs/common';
13+
import { CurrentUser, ICurrentUserType } from '@src/decorators';
1314
import { AuthGuard } from '@src/guard/auth.guard';
1415
import { TenantDto } from './dto/tenant.dto';
1516
import { QueryTenantDto } from './dto/tenant.query';
@@ -25,14 +26,36 @@ export class TenantController {
2526
return await this.tenantService.createTenantApi(req);
2627
}
2728

29+
@Post('delete')
30+
async batchDeleteTenantByIdListApi(
31+
@Body() idList: number[],
32+
@CurrentUser('userInfo') currentUser: ICurrentUserType
33+
): Promise<string> {
34+
return await this.tenantService.batchDeleteTenantByIdListApi(idList, currentUser);
35+
}
36+
2837
@Delete(':id')
29-
async deleteTenantByIdApi(@Param('id', new ParseIntPipe()) id: number): Promise<string> {
30-
return await this.tenantService.deleteTenantByIdApi(id);
38+
async deleteTenantByIdApi(
39+
@Param('id', new ParseIntPipe()) id: number,
40+
@CurrentUser('userInfo') currentUser: ICurrentUserType
41+
): Promise<string> {
42+
return await this.tenantService.deleteTenantByIdApi(id, currentUser);
43+
}
44+
45+
@Post('/batchStatus')
46+
async batchModifyTenantStatusByIdApi(
47+
@Body() idList: number[],
48+
@CurrentUser('userInfo') currentUser: ICurrentUserType
49+
): Promise<string> {
50+
return await this.tenantService.batchModifyTenantStatusByIdApi(idList, currentUser);
3151
}
3252

3353
@Put('/status/:id')
34-
async modifyTenantStatusByIdApi(@Param('id', new ParseIntPipe()) id: number): Promise<string> {
35-
return await this.tenantService.modifyTenantStatusByIdApi(id);
54+
async modifyTenantStatusByIdApi(
55+
@Param('id', new ParseIntPipe()) id: number,
56+
@CurrentUser('userInfo') currentUser: ICurrentUserType
57+
): Promise<string> {
58+
return await this.tenantService.modifyTenantStatusByIdApi(id, currentUser);
3659
}
3760

3861
@Put(':id')

src/api/tenant/tenant.service.ts

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { TenantPageVo, TenantVo } from './vo/tenant.vo';
1010
import { AreaEntity } from '../area/entities/area.entity';
1111
import { mapToObj } from '@src/utils';
1212
import { AccountEntity } from '../account/entities/account.entity';
13+
import { ICurrentUserType } from '@src/decorators';
1314

1415
@Injectable()
1516
export class TenantService {
@@ -42,6 +43,32 @@ export class TenantService {
4243
return '创建成功';
4344
}
4445

46+
/**
47+
* @Author: 水痕
48+
* @Date: 2023-10-09 20:58:51
49+
* @LastEditors: 水痕
50+
* @Description: 根据id列表批量删除
51+
* @param {number} idList
52+
* @return {*}
53+
*/
54+
async batchDeleteTenantByIdListApi(
55+
idList: number[],
56+
currentUser: ICurrentUserType
57+
): Promise<string> {
58+
console.log(idList, '获取到的数据', currentUser);
59+
const { tenantId } = currentUser;
60+
console.log(tenantId, idList.includes(tenantId), '???');
61+
if (idList.includes(tenantId)) {
62+
throw new HttpException('自己不能删除自己', HttpStatus.OK);
63+
}
64+
const { affected } = await this.tenantRepository.softDelete(idList);
65+
if (affected) {
66+
return '删除成功';
67+
} else {
68+
return '删除成功';
69+
}
70+
}
71+
4572
/**
4673
* @Author: 水痕
4774
* @Date: 2023-10-07 10:50:10
@@ -50,7 +77,11 @@ export class TenantService {
5077
* @param {number} id
5178
* @return {*}
5279
*/
53-
async deleteTenantByIdApi(id: number): Promise<string> {
80+
async deleteTenantByIdApi(id: number, currentUser: ICurrentUserType): Promise<string> {
81+
const { tenantId } = currentUser;
82+
if (tenantId == id) {
83+
throw new HttpException('自己不能删除自己', HttpStatus.OK);
84+
}
5485
const { affected } = await this.tenantRepository.softDelete(id);
5586
if (affected) {
5687
return '删除成功';
@@ -59,6 +90,40 @@ export class TenantService {
5990
}
6091
}
6192

93+
/**
94+
* @Author: 水痕
95+
* @Date: 2023-10-09 22:06:30
96+
* @LastEditors: 水痕
97+
* @Description: 批量修改状态
98+
* @return {*}
99+
*/
100+
async batchModifyTenantStatusByIdApi(
101+
idList: number[],
102+
currentUser: ICurrentUserType
103+
): Promise<string> {
104+
const { tenantId } = currentUser;
105+
if (idList.includes(tenantId)) {
106+
throw new HttpException('自己不能修改自己', HttpStatus.OK);
107+
}
108+
const tenantEntityList: Pick<TenantEntity, 'status'>[] = await this.tenantRepository.find({
109+
where: { id: In(idList) },
110+
select: ['status'],
111+
});
112+
if ([...new Set(tenantEntityList.map((item) => item.status))].length > 1) {
113+
throw new HttpException('当前状态不统一,不能批量修改', HttpStatus.OK);
114+
}
115+
const { affected } = await this.tenantRepository.update(idList, {
116+
status:
117+
tenantEntityList[0]?.status == StatusEnum.FORBIDDEN
118+
? StatusEnum.NORMAL
119+
: StatusEnum.FORBIDDEN,
120+
});
121+
if (affected) {
122+
return '修改成功';
123+
} else {
124+
return '修改失败';
125+
}
126+
}
62127
/**
63128
* @Author: 水痕
64129
* @Date: 2023-10-07 20:51:30
@@ -67,7 +132,11 @@ export class TenantService {
67132
* @param {number} id
68133
* @return {*}
69134
*/
70-
async modifyTenantStatusByIdApi(id: number): Promise<string> {
135+
async modifyTenantStatusByIdApi(id: number, currentUser: ICurrentUserType): Promise<string> {
136+
const { tenantId } = currentUser;
137+
if (tenantId == id) {
138+
throw new HttpException('自己不能修改自己', HttpStatus.OK);
139+
}
71140
const tenantEntity: Pick<TenantEntity, 'status'> | null = await this.tenantRepository.findOne({
72141
where: { id },
73142
select: ['status'],

src/guard/auth.guard.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class AuthGuard implements CanActivate {
2121
if (token) {
2222
// 如果传递了token的话就要从redis中查询是否有该token
2323
const result = await this.redisService.get(token);
24+
console.log(result, '当前数据--->');
2425
if (result) {
2526
// 这里我们知道result数据类型就是我们定义的直接断言
2627
request.user = result;

0 commit comments

Comments
 (0)