book_manage/apps/server/src/models/department/department.router.ts

84 lines
3.2 KiB
TypeScript
Raw Normal View History

2024-09-03 20:19:33 +08:00
import { Injectable } from '@nestjs/common';
import { TrpcService } from '@server/trpc/trpc.service';
import { DepartmentService } from './department.service'; // assuming it's in the same directory
2025-03-05 21:21:03 +08:00
import {
DepartmentMethodSchema,
Prisma,
UpdateOrderSchema,
} from '@nice/common';
2024-12-30 08:26:40 +08:00
import { z, ZodType } from 'zod';
import { DepartmentRowService } from './department.row.service';
2024-09-03 20:19:33 +08:00
2025-03-05 21:21:03 +08:00
const DepartmentCreateArgsSchema: ZodType<Prisma.DepartmentCreateArgs> =
z.any();
const DepartmentUpdateArgsSchema: ZodType<Prisma.DepartmentUpdateArgs> =
z.any();
const DepartmentFindFirstArgsSchema: ZodType<Prisma.DepartmentFindFirstArgs> =
z.any();
const DepartmentFindManyArgsSchema: ZodType<Prisma.DepartmentFindManyArgs> =
z.any();
2024-09-03 20:19:33 +08:00
@Injectable()
export class DepartmentRouter {
constructor(
private readonly trpc: TrpcService,
2024-12-30 08:26:40 +08:00
private readonly departmentService: DepartmentService, // 注入 DepartmentService
2025-03-05 21:21:03 +08:00
private readonly departmentRowService: DepartmentRowService,
) {}
2024-09-03 20:19:33 +08:00
router = this.trpc.router({
2024-12-30 08:26:40 +08:00
// 创建部门
2024-09-03 20:19:33 +08:00
create: this.trpc.protectProcedure
2024-12-30 08:26:40 +08:00
.input(DepartmentCreateArgsSchema) // 根据 schema 期望输入
2024-09-03 20:19:33 +08:00
.mutation(async ({ input }) => {
return this.departmentService.create(input);
}),
2024-12-30 08:26:40 +08:00
// 更新部门
2024-09-03 20:19:33 +08:00
update: this.trpc.protectProcedure
2024-12-30 08:26:40 +08:00
.input(DepartmentUpdateArgsSchema) // 根据 schema 期望输入
2024-09-03 20:19:33 +08:00
.mutation(async ({ input }) => {
return this.departmentService.update(input);
}),
2024-12-30 08:26:40 +08:00
// 根据 ID 列表软删除部门
softDeleteByIds: this.trpc.protectProcedure
.input(z.object({ ids: z.array(z.string()) })) // 根据 schema 期望输入
2024-09-03 20:19:33 +08:00
.mutation(async ({ input }) => {
2024-12-30 08:26:40 +08:00
return this.departmentService.softDeleteByIds(input.ids);
2024-09-03 20:19:33 +08:00
}),
2024-12-30 08:26:40 +08:00
// 更新部门顺序
2025-03-05 21:21:03 +08:00
updateOrder: this.trpc.protectProcedure
.input(UpdateOrderSchema)
.mutation(async ({ input }) => {
return this.departmentService.updateOrder(input);
}),
2024-12-30 08:26:40 +08:00
// 查询多个部门
findMany: this.trpc.procedure
.input(DepartmentFindManyArgsSchema) // 假设 StaffMethodSchema.findMany 是根据关键字查找员工的 Zod schema
2024-09-03 20:19:33 +08:00
.query(async ({ input }) => {
2024-12-30 08:26:40 +08:00
return await this.departmentService.findMany(input);
2024-09-03 20:19:33 +08:00
}),
2024-12-30 08:26:40 +08:00
// 查询第一个部门
findFirst: this.trpc.procedure
.input(DepartmentFindFirstArgsSchema) // 假设 StaffMethodSchema.findMany 是根据关键字查找员工的 Zod schema
2024-09-03 20:19:33 +08:00
.query(async ({ input }) => {
2024-12-30 08:26:40 +08:00
return await this.departmentService.findFirst(input);
2024-09-03 20:19:33 +08:00
}),
2024-12-30 08:26:40 +08:00
// 获取子部门的简单树结构
getChildSimpleTree: this.trpc.procedure
2025-03-05 21:21:03 +08:00
.input(DepartmentMethodSchema.getSimpleTree)
.query(async ({ input }) => {
return await this.departmentService.getChildSimpleTree(input);
2024-09-03 20:19:33 +08:00
}),
2024-12-30 08:26:40 +08:00
// 获取父部门的简单树结构
getParentSimpleTree: this.trpc.procedure
2025-03-05 21:21:03 +08:00
.input(DepartmentMethodSchema.getSimpleTree)
.query(async ({ input }) => {
return await this.departmentService.getParentSimpleTree(input);
2024-09-03 20:19:33 +08:00
}),
2024-12-30 08:26:40 +08:00
// 获取部门行数据
getRows: this.trpc.protectProcedure
.input(DepartmentMethodSchema.getRows)
.query(async ({ input, ctx }) => {
return await this.departmentRowService.getRows(input, ctx.staff);
2024-09-03 20:19:33 +08:00
}),
});
}