|
@@ -1,146 +0,0 @@
|
|
|
-import { OpenAPIHono, createRoute } from '@hono/zod-openapi';
|
|
|
|
|
-import { z } from '@hono/zod-openapi';
|
|
|
|
|
-import { ClientSchema } from '@/server/modules/clients/client.entity';
|
|
|
|
|
-import { ClientService } from '@/server/modules/clients/client.service';
|
|
|
|
|
-import { authMiddleware } from '@/server/middleware/auth.middleware';
|
|
|
|
|
-import { AppDataSource } from '@/server/data-source';
|
|
|
|
|
-import { AuthContext } from '@/server/types/context';
|
|
|
|
|
-
|
|
|
|
|
-const app = new OpenAPIHono<AuthContext>();
|
|
|
|
|
-
|
|
|
|
|
-// 查询参数Schema
|
|
|
|
|
-const ListQuery = z.object({
|
|
|
|
|
- page: z.coerce.number().int().positive().default(1),
|
|
|
|
|
- pageSize: z.coerce.number().int().positive().default(10),
|
|
|
|
|
- keyword: z.string().optional(),
|
|
|
|
|
- auditStatus: z.coerce.number().min(0).max(2).optional(),
|
|
|
|
|
- status: z.coerce.number().min(0).max(1).optional(),
|
|
|
|
|
- responsibleUserId: z.coerce.number().int().positive().optional(),
|
|
|
|
|
- salesPersonId: z.coerce.number().int().positive().optional(),
|
|
|
|
|
- operatorId: z.coerce.number().int().positive().optional(),
|
|
|
|
|
- customerType: z.string().optional(),
|
|
|
|
|
- industry: z.string().optional(),
|
|
|
|
|
- source: z.string().optional(),
|
|
|
|
|
- startDateFrom: z.coerce.date().optional(),
|
|
|
|
|
- startDateTo: z.coerce.date().optional(),
|
|
|
|
|
- createdAtFrom: z.coerce.date().optional(),
|
|
|
|
|
- createdAtTo: z.coerce.date().optional(),
|
|
|
|
|
- sortBy: z.enum([
|
|
|
|
|
- 'id', 'companyName', 'contactPerson', 'createdAt', 'updatedAt',
|
|
|
|
|
- 'startDate', 'nextContactTime', 'auditStatus', 'status'
|
|
|
|
|
- ]).default('createdAt'),
|
|
|
|
|
- sortOrder: z.enum(['ASC', 'DESC']).default('DESC')
|
|
|
|
|
-});
|
|
|
|
|
-
|
|
|
|
|
-// 响应Schema
|
|
|
|
|
-const ListResponse = z.object({
|
|
|
|
|
- data: z.array(ClientSchema),
|
|
|
|
|
- pagination: z.object({
|
|
|
|
|
- total: z.number(),
|
|
|
|
|
- current: z.number(),
|
|
|
|
|
- pageSize: z.number()
|
|
|
|
|
- })
|
|
|
|
|
-});
|
|
|
|
|
-
|
|
|
|
|
-// 路由定义
|
|
|
|
|
-const route = createRoute({
|
|
|
|
|
- method: 'get',
|
|
|
|
|
- path: '/',
|
|
|
|
|
- middleware: [authMiddleware],
|
|
|
|
|
- request: {
|
|
|
|
|
- query: ListQuery
|
|
|
|
|
- },
|
|
|
|
|
- responses: {
|
|
|
|
|
- 200: {
|
|
|
|
|
- description: '成功获取客户列表',
|
|
|
|
|
- content: {
|
|
|
|
|
- 'application/json': {
|
|
|
|
|
- schema: ListResponse
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-});
|
|
|
|
|
-
|
|
|
|
|
-// 路由实现
|
|
|
|
|
-app.openapi(route, async (c) => {
|
|
|
|
|
- try {
|
|
|
|
|
- const query = c.req.valid('query');
|
|
|
|
|
- const service = new ClientService(AppDataSource);
|
|
|
|
|
-
|
|
|
|
|
- // 构建where条件
|
|
|
|
|
- const where: any = {};
|
|
|
|
|
-
|
|
|
|
|
- if (query.auditStatus !== undefined) {
|
|
|
|
|
- where.auditStatus = query.auditStatus;
|
|
|
|
|
- }
|
|
|
|
|
- if (query.status !== undefined) {
|
|
|
|
|
- where.status = query.status;
|
|
|
|
|
- }
|
|
|
|
|
- if (query.responsibleUserId !== undefined) {
|
|
|
|
|
- where.responsibleUserId = query.responsibleUserId;
|
|
|
|
|
- }
|
|
|
|
|
- if (query.salesPersonId !== undefined) {
|
|
|
|
|
- where.salesPersonId = query.salesPersonId;
|
|
|
|
|
- }
|
|
|
|
|
- if (query.operatorId !== undefined) {
|
|
|
|
|
- where.operatorId = query.operatorId;
|
|
|
|
|
- }
|
|
|
|
|
- if (query.customerType) {
|
|
|
|
|
- where.customerType = query.customerType;
|
|
|
|
|
- }
|
|
|
|
|
- if (query.industry) {
|
|
|
|
|
- where.industry = query.industry;
|
|
|
|
|
- }
|
|
|
|
|
- if (query.source) {
|
|
|
|
|
- where.source = query.source;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 构建日期范围
|
|
|
|
|
- if (query.startDateFrom || query.startDateTo) {
|
|
|
|
|
- where.startDateRange = {
|
|
|
|
|
- start: query.startDateFrom,
|
|
|
|
|
- end: query.startDateTo
|
|
|
|
|
- };
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (query.createdAtFrom || query.createdAtTo) {
|
|
|
|
|
- where.createdAtRange = {
|
|
|
|
|
- start: query.createdAtFrom,
|
|
|
|
|
- end: query.createdAtTo
|
|
|
|
|
- };
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 构建排序对象
|
|
|
|
|
- const order: any = {};
|
|
|
|
|
- order[query.sortBy] = query.sortOrder;
|
|
|
|
|
-
|
|
|
|
|
- // 调用自定义的getList方法
|
|
|
|
|
- const [data, total] = await service.getList(
|
|
|
|
|
- query.page,
|
|
|
|
|
- query.pageSize,
|
|
|
|
|
- query.keyword,
|
|
|
|
|
- ['companyName', 'contactPerson', 'mobile'],
|
|
|
|
|
- where,
|
|
|
|
|
- ['responsibleUser', 'salesPerson', 'operator'],
|
|
|
|
|
- order
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- return c.json({
|
|
|
|
|
- data,
|
|
|
|
|
- pagination: {
|
|
|
|
|
- total,
|
|
|
|
|
- current: query.page,
|
|
|
|
|
- pageSize: query.pageSize
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- } catch (error) {
|
|
|
|
|
- console.error('获取客户列表失败:', error);
|
|
|
|
|
- return c.json({
|
|
|
|
|
- code: 500,
|
|
|
|
|
- message: error instanceof Error ? error.message : '获取列表失败'
|
|
|
|
|
- }, 500);
|
|
|
|
|
- }
|
|
|
|
|
-});
|
|
|
|
|
-
|
|
|
|
|
-export default app;
|
|
|