Skip to content

Commit 8c16c58

Browse files
committed
feat: 部门
1 parent a832611 commit 8c16c58

File tree

5 files changed

+62
-19
lines changed

5 files changed

+62
-19
lines changed

src/api/department/department.controller.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import { DepartmentService } from './department.service';
1414
import { DepartmentDto } from './dto/department.dto';
1515
import { CurrentUser, ICurrentUserType } from '@src/decorators';
16-
import { DepartmentPageVo, DepartmentVo } from './vo/department.vo';
16+
import { DepartmentPageVo, DepartmentVo, SimplenessDepartmentVo } from './vo/department.vo';
1717
import { QueryDepartmentDto } from './dto/department.query';
1818
import { AuthGuard } from '@src/guard/auth.guard';
1919
@UseGuards(AuthGuard)
@@ -58,6 +58,13 @@ export class DepartmentController {
5858
return await this.departmentService.getDepartmentPageApi(queryOption, currentUser);
5959
}
6060

61+
@Get('list')
62+
async getDepartmentListApi(
63+
@CurrentUser('userInfo') currentUser: ICurrentUserType
64+
): Promise<SimplenessDepartmentVo[]> {
65+
return await this.departmentService.getDepartmentListApi(currentUser);
66+
}
67+
6168
@Get(':id')
6269
async getDepartmentByIdApi(
6370
@Param('id', new ParseIntPipe()) id: number

src/api/department/department.service.ts

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Equal, FindOperator, ILike, In, Repository, SelectQueryBuilder } from '
66
import { DepartmentDto } from './dto/department.dto';
77
import { ICurrentUserType } from '@src/decorators';
88
import { HttpStatusCode } from 'axios';
9-
import { DepartmentPageVo, DepartmentVo } from './vo/department.vo';
9+
import { DepartmentPageVo, DepartmentVo, SimplenessDepartmentVo } from './vo/department.vo';
1010
import { TenantEntity } from '../tenant/entities/tenant.entity';
1111
import { QueryDepartmentDto } from './dto/department.query';
1212
import { PageEnum, StatusEnum } from '@src/enums';
@@ -182,6 +182,7 @@ export class DepartmentService {
182182
.offset((pageNumber - 1) * pageSize)
183183
.limit(pageSize)
184184
.getRawMany();
185+
console.log(data, '????????????????');
185186
return {
186187
data,
187188
total,
@@ -190,6 +191,26 @@ export class DepartmentService {
190191
};
191192
}
192193

194+
/**
195+
* @Author: 水痕
196+
* @Date: 2023-10-18 20:27:28
197+
* @LastEditors: 水痕
198+
* @Description: 获取当前商户下的部门
199+
* @param {ICurrentUserType} currentUser
200+
* @return {*}
201+
*/
202+
async getDepartmentListApi(currentUser: ICurrentUserType): Promise<SimplenessDepartmentVo[]> {
203+
const { id, tenantId, accountType } = currentUser;
204+
const query = new Map<string, FindOperator<string>>();
205+
query.set('tenantId', Equal(tenantId + ''));
206+
if (accountType != AccountTypeEnum.SUPER_ACCOUNT) {
207+
query.set('parentId', Equal(id + ''));
208+
}
209+
return await this.departmentRepository.find({
210+
where: mapToObj(query),
211+
select: ['id', 'title', 'parentId'],
212+
});
213+
}
193214
/**
194215
* @Author: 水痕
195216
* @Date: 2023-10-18 15:21:30
@@ -206,30 +227,39 @@ export class DepartmentService {
206227
* @Author: 水痕
207228
* @Date: 2023-10-18 15:52:37
208229
* @LastEditors: 水痕
209-
* @Description: 根据id列表修改状态
230+
* @Description: 批量删除部门
210231
* @param {number} idList
211232
* @return {*}
212233
*/
213234
async batchDeleteDepartmentByIdListApi(idList: number[]): Promise<string> {
214-
const departmentEntityList: Pick<DepartmentEntity, 'status'>[] =
215-
await this.departmentRepository.find({ where: { id: In(idList) }, select: ['status'] });
216-
const statusList = departmentEntityList.map((item) => item.status);
217-
if ([...new Set(statusList)].length > 1) {
218-
throw new HttpException('当前部门多个状态,不能批量操作', HttpStatusCode.Ok);
235+
const departmentEntityList: Pick<DepartmentEntity, 'parentId'>[] =
236+
await this.departmentRepository.find({
237+
where: { parentId: In(idList) },
238+
select: ['parentId', 'id'],
239+
});
240+
if (departmentEntityList.length > 0) {
241+
throw new HttpException('当前部门有子部门,不能直接删除', HttpStatusCode.Ok);
219242
}
220-
const status = statusList[0] == StatusEnum.FORBIDDEN ? StatusEnum.NORMAL : StatusEnum.FORBIDDEN;
221-
const { affected } = await this.departmentRepository.update({ id: In(idList) }, { status });
243+
const departmentEntityList1: Pick<DepartmentEntity, 'parentId'>[] =
244+
await this.departmentRepository.find({
245+
where: { id: In(idList) },
246+
select: ['parentId'],
247+
});
248+
if (departmentEntityList1.map((item) => item.parentId).includes(-1)) {
249+
throw new HttpException('当前部门有子部门,不能直接删除', HttpStatusCode.Ok);
250+
}
251+
const { affected } = await this.departmentRepository.softDelete({ id: In(idList) });
222252
if (affected) {
223-
return '修改成功';
253+
return '删除成功';
224254
} else {
225-
return '修改失败';
255+
return '删除失败';
226256
}
227257
}
228258
/**
229259
* @Author: 水痕
230260
* @Date: 2023-10-18 15:58:14
231261
* @LastEditors: 水痕
232-
* @Description: 批量删除部门
262+
* @Description:根据id列表修改状态
233263
* @param {number} idList
234264
* @return {*}
235265
*/
@@ -240,11 +270,12 @@ export class DepartmentService {
240270
if ([...new Set(statusList)].length > 1) {
241271
throw new HttpException('当前部门多个状态,不能批量操作', HttpStatusCode.Ok);
242272
}
243-
const { affected } = await this.departmentRepository.softDelete({ id: In(idList) });
273+
const status = statusList[0] == StatusEnum.FORBIDDEN ? StatusEnum.NORMAL : StatusEnum.FORBIDDEN;
274+
const { affected } = await this.departmentRepository.update({ id: In(idList) }, { status });
244275
if (affected) {
245-
return '删除成功';
276+
return '修改成功';
246277
} else {
247-
return '删除失败';
278+
return '修改失败';
248279
}
249280
}
250281
/**

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export class DepartmentDto {
2727
@IsOptional({ message: '排序' })
2828
sort!: number;
2929

30-
@Min(1, { message: '自己关联主键id最小值为1' })
3130
@IsInt({ message: '自己关联主键id必须是整数' })
3231
@Type(() => Number)
3332
parentId!: number;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class DepartmentEntity extends SharedEntity {
7979
type: 'int',
8080
name: 'parent_id',
8181
nullable: true,
82-
default: '-1',
82+
default: -1,
8383
comment: '自己关联主键id',
8484
})
8585
parentId!: number;

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ export class DepartmentVo extends QueryVo {
1111
readonly sort!: number; // 排序
1212
readonly tenantId!: number; // 关联到tenant表主键id
1313
readonly tenantName!: string; // 关联到tenant表主键id
14-
readonly parentId!: number; // 自己关联主键id
14+
readonly parentId!: number | string; // 自己关联主键id
1515
readonly parentTitle!: string; // 自己关联主键id
1616
}
1717

1818
export class DepartmentPageVo extends QueryListVo {
1919
readonly data!: DepartmentVo[];
2020
}
21+
22+
export class SimplenessDepartmentVo {
23+
readonly id!: number; // 部门id
24+
readonly title!: string; // 部门名称
25+
readonly parentId!: number; // 自己关联主键id
26+
}

0 commit comments

Comments
 (0)