Skip to content

Commit 111337c

Browse files
committed
feat: 批量操作
1 parent 49a6b28 commit 111337c

File tree

4 files changed

+71
-9
lines changed

4 files changed

+71
-9
lines changed

nestjs-mysql-api.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ CREATE TABLE `tenant` (
2929
DROP TABLE IF EXISTS `role`;
3030
CREATE TABLE `role` (
3131
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY key COMMENT '主键id',
32-
`name` varchar(50) UNIQUE NOT NULL COMMENT '角色名称',
32+
`name` varchar(50) NOT NULL COMMENT '角色名称',
3333
`description` varchar(255) DEFAULT NULL COMMENT '描述',
3434
`status` tinyint(4) DEFAULT 0 COMMENT '状态0是正常,1是禁用',
3535
`sort` int(11) DEFAULT 1 COMMENT '排序',
@@ -38,7 +38,7 @@ CREATE TABLE `role` (
3838
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
3939
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
4040
`deleted_at` timestamp NULL DEFAULT NULL COMMENT '软删除时间',
41-
UNIQUE KEY `UK_name_deleted_at` (`name`,`deleted_at`)
41+
UNIQUE KEY `UK_name_account_id_deleted_at` (`name`,`account_id`,`deleted_at`)
4242
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='角色表(也可以当部门表)';
4343

4444

src/api/menus/menus.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class MenusController {
2222

2323
@Get('/btnList/:id')
2424
async getBtnByMenusIdApi(
25-
@CurrentUser() userInfo: ICurrentUserType,
25+
@CurrentUser('userInfo') userInfo: ICurrentUserType,
2626
@Param('id', new ParseIntPipe()) id: number
2727
): Promise<ResourcesEntity[]> {
2828
return await this.menusService.getBtnByMenusIdApi(id, userInfo);

src/api/role/role.controller.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class RoleController {
2525
@Post()
2626
async createRoleApi(
2727
@Body() req: RoleDto,
28-
@CurrentUser() currentInfo: ICurrentUserType
28+
@CurrentUser('userInfo') currentInfo: ICurrentUserType
2929
): Promise<string> {
3030
return await this.roleService.createRoleApi(req, currentInfo);
3131
}
@@ -51,7 +51,7 @@ export class RoleController {
5151
@Get()
5252
async getRolePageApi(
5353
@Query() queryOption: QueryRoleDto,
54-
@CurrentUser() currentInfo: ICurrentUserType
54+
@CurrentUser('userInfo') currentInfo: ICurrentUserType
5555
): Promise<RolePageVo> {
5656
return await this.roleService.getRolePageApi(queryOption, currentInfo);
5757
}
@@ -60,4 +60,14 @@ export class RoleController {
6060
async getRoleByIdApi(@Param('id', new ParseIntPipe()) id: number): Promise<RoleVo | undefined> {
6161
return await this.roleService.getRoleByIdApi(id);
6262
}
63+
64+
@Post('delete')
65+
async batchDeleteRoleByIdListApi(@Body() idList: number[]): Promise<string> {
66+
return await this.roleService.batchDeleteRoleByIdListApi(idList);
67+
}
68+
69+
@Post('/batchStatus')
70+
async batchModifyRoleStatusByIdApi(@Body() idList: number[]): Promise<string> {
71+
return await this.roleService.batchModifyRoleStatusByIdApi(idList);
72+
}
6373
}

src/api/role/role.service.ts

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
22
import { RoleEntity } from './entities/role.entity';
33

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 { ICurrentUserType } from '@src/decorators';
77
import { RoleDto } from './dto/role.dto';
88
import { 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

Comments
 (0)