| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
- import { z } from '@hono/zod-openapi';
- import { authMiddleware } from '@d8d/core-module-mt/auth-module-mt/middleware';
- import { AppDataSource, ErrorSchema, parseWithAwait } from '@d8d/shared-utils';
- import { AuthContext } from '@d8d/shared-types';
- import { DataOverviewServiceMt } from '../services';
- import { UserConsumptionQuerySchema, UserConsumptionApiResponseSchema } from '../schemas';
- const userConsumptionRoute = createRoute({
- method: 'get',
- path: '/user-consumption',
- middleware: [authMiddleware],
- request: {
- query: UserConsumptionQuerySchema
- },
- responses: {
- 200: {
- description: '获取用户消费统计成功',
- content: {
- 'application/json': {
- schema: UserConsumptionApiResponseSchema
- }
- }
- },
- 400: {
- description: '请求参数错误',
- content: {
- 'application/json': {
- schema: ErrorSchema
- }
- }
- },
- 401: {
- description: '认证失败',
- content: {
- 'application/json': {
- schema: ErrorSchema
- }
- }
- },
- 403: {
- description: '权限不足',
- content: {
- 'application/json': {
- schema: ErrorSchema
- }
- }
- },
- 500: {
- description: '服务器内部错误',
- content: {
- 'application/json': {
- schema: ErrorSchema
- }
- }
- }
- }
- });
- const userConsumptionRoutes = new OpenAPIHono<AuthContext>()
- .openapi(userConsumptionRoute, async (c) => {
- const user = c.get('user');
- // 调试:记录原始查询参数
- const rawQuery = c.req.query();
- console.debug('用户消费统计API调试 - 原始查询参数:', rawQuery);
- console.debug('用户消费统计API调试 - 原始参数类型检查:', {
- page: { value: rawQuery.page, type: typeof rawQuery.page },
- limit: { value: rawQuery.limit, type: typeof rawQuery.limit },
- year: { value: rawQuery.year, type: typeof rawQuery.year }
- });
- const queryParams = c.req.valid('query');
- // 调试:记录验证后的查询参数
- console.debug('用户消费统计API调试 - 验证后的查询参数:', queryParams);
- console.debug('用户消费统计API调试 - 验证后参数类型检查:', {
- page: { value: queryParams.page, type: typeof queryParams.page },
- limit: { value: queryParams.limit, type: typeof queryParams.limit },
- year: { value: queryParams.year, type: typeof queryParams.year }
- });
- try {
- const service = new DataOverviewServiceMt(AppDataSource);
- // 分离时间筛选参数和分页参数
- const { startDate, endDate, timeRange, ...paginationParams } = queryParams;
- const timeFilterParams = { startDate, endDate, timeRange };
- const statistics = await service.getUserConsumptionStatistics(
- user.tenantId,
- timeFilterParams,
- paginationParams
- );
- const responseData = await parseWithAwait(UserConsumptionApiResponseSchema, {
- data: statistics,
- success: true,
- message: '获取用户消费统计成功'
- });
- return c.json(responseData, 200);
- } catch (error) {
- console.error('获取用户消费统计失败:', error);
- return c.json(
- { code: 500, message: error instanceof Error ? error.message : '获取用户消费统计失败' },
- 500
- );
- }
- });
- export default userConsumptionRoutes;
|