Skip to content

Commit cc3391c

Browse files
committed
feat: 修改部门
1 parent 8c16c58 commit cc3391c

File tree

6 files changed

+33
-21
lines changed

6 files changed

+33
-21
lines changed

nestjs-mysql-api.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ DROP TABLE IF EXISTS `department`;
4949
CREATE TABLE `department` (
5050
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY key COMMENT '主键id',
5151
`title` varchar(50) NOT NULL COMMENT '部门名称',
52-
`name` varchar(50) DEFAULT NULL COMMENT '部门负责人',
52+
`account_id` int(11) DEFAULT NULL COMMENT '部门负责人,关联到account表主键id',
5353
`mobile` varchar(50) DEFAULT NULL COMMENT '联系手机号码',
5454
`email` varchar(50) DEFAULT NULL COMMENT '电邮地址',
5555
`description` varchar(255) DEFAULT NULL COMMENT '描述',

src/api/department/department.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Module } from '@nestjs/common';
22
import { RouterModule } from '@nestjs/core';
33
import { TypeOrmModule } from '@nestjs/typeorm';
44
import { ADMIN_PREFIX } from '@src/constants';
5+
import { AccountEntity } from '../account/entities/account.entity';
56
import { TenantEntity } from '../tenant/entities/tenant.entity';
67
import { DepartmentController } from './department.controller';
78
import { DepartmentService } from './department.service';
@@ -15,7 +16,7 @@ import { DepartmentEntity } from './entities/department.entity';
1516
module: DepartmentModule,
1617
},
1718
]),
18-
TypeOrmModule.forFeature([DepartmentEntity, TenantEntity]),
19+
TypeOrmModule.forFeature([DepartmentEntity, TenantEntity, AccountEntity]),
1920
],
2021
controllers: [DepartmentController],
2122
providers: [DepartmentService],

src/api/department/department.service.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { QueryDepartmentDto } from './dto/department.query';
1212
import { PageEnum, StatusEnum } from '@src/enums';
1313
import { mapToObj } from '@src/utils';
1414
import { AccountTypeEnum } from '@src/enums/account.type.enum';
15+
import { AccountEntity } from '../account/entities/account.entity';
1516

1617
@Injectable()
1718
export class DepartmentService {
@@ -32,14 +33,14 @@ export class DepartmentService {
3233
async createDepartmentApi(req: DepartmentDto, currentUser: ICurrentUserType): Promise<string> {
3334
// 1.判断部门存在吗
3435
const { tenantId } = currentUser;
35-
const departmentEntity: Pick<DepartmentEntity, 'id'> | null =
36-
await this.departmentRepository.findOne({
37-
where: { tenantId, title: req.title },
38-
select: ['id'],
39-
});
40-
if (departmentEntity?.id) {
41-
throw new HttpException(`${req.title}已经存在`, HttpStatusCode.Ok);
42-
}
36+
// const departmentEntity: Pick<DepartmentEntity, 'id'> | null =
37+
// await this.departmentRepository.findOne({
38+
// where: { tenantId, title: req.title },
39+
// select: ['id'],
40+
// });
41+
// if (departmentEntity?.id) {
42+
// throw new HttpException(`${req.title}已经存在`, HttpStatusCode.Ok);
43+
// }
4344
const data = this.departmentRepository.create({ ...req, tenantId });
4445
await this.departmentRepository.save(data);
4546
return '创建成功';
@@ -290,9 +291,9 @@ export class DepartmentService {
290291
.createQueryBuilder('department')
291292
.select('department.id', 'id')
292293
.addSelect('department.title', 'title')
293-
.addSelect('department.name', 'name')
294294
.addSelect('department.mobile', 'mobile')
295295
.addSelect('department.email', 'email')
296+
.addSelect('department.accountId', 'accountId')
296297
.addSelect('department.tenantId', 'tenantId')
297298
.addSelect('department.parentId', 'parentId')
298299
.addSelect('department.sort', 'sort')
@@ -318,6 +319,16 @@ export class DepartmentService {
318319
.from(TenantEntity, 'tenant'),
319320
'tenant',
320321
'department.tenantId=tenant.tenantId'
322+
)
323+
.leftJoinAndMapOne(
324+
'xx',
325+
(qb) =>
326+
qb
327+
.select('account.id', 'accountId')
328+
.addSelect('account.username', 'name')
329+
.from(AccountEntity, 'account'),
330+
'account',
331+
'department.accountId=account.accountId'
321332
);
322333
}
323334
}

src/api/department/dto/department.dto.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ export class DepartmentDto {
66
@IsNotEmpty({ message: '部门名称不能为空' })
77
title!: string;
88

9-
@MaxLength(50, { message: '部门负责人最大长度为50' })
9+
@Min(1, { message: '部门负责人最小值为1' })
10+
@IsInt({ message: '部门负责人必须是整数' })
11+
@Type(() => Number)
1012
@IsOptional({ message: '部门负责人' })
11-
name!: string;
13+
accountId!: number;
1214

1315
@IsOptional({ message: '联系手机号码' })
1416
mobile!: string;

src/api/department/entities/department.entity.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SharedEntity } from '@src/shared/entities/base.entity';
2-
import { Entity, Column, Index } from 'typeorm';
2+
import { Entity, Column } from 'typeorm';
33

44
@Entity('department')
55
export class DepartmentEntity extends SharedEntity {
@@ -11,15 +11,12 @@ export class DepartmentEntity extends SharedEntity {
1111
})
1212
title!: string;
1313

14-
@Index()
1514
@Column({
16-
type: 'varchar',
17-
length: 50,
18-
name: 'name',
19-
nullable: true,
20-
comment: '部门负责人',
15+
type: 'int',
16+
name: 'account_id',
17+
comment: '部门负责人,关联到account表主键id',
2118
})
22-
name!: string;
19+
accountId!: number;
2320

2421
@Column({
2522
type: 'varchar',

src/api/department/vo/department.vo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { QueryVo } from '@src/shared/vo/query.vo';
33

44
export class DepartmentVo extends QueryVo {
55
readonly title!: string; // 部门名称
6+
readonly accountId!: number; // 部门负责人
67
readonly name!: string; // 部门负责人
78
readonly mobile!: string; // 联系手机号码
89
readonly email!: string; // 电邮地址

0 commit comments

Comments
 (0)