user-consumption.mt.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
  2. import { z } from '@hono/zod-openapi';
  3. import { authMiddleware } from '@d8d/core-module-mt/auth-module-mt/middleware';
  4. import { AppDataSource, ErrorSchema, parseWithAwait } from '@d8d/shared-utils';
  5. import { AuthContext } from '@d8d/shared-types';
  6. import { DataOverviewServiceMt } from '../services';
  7. import { UserConsumptionQuerySchema, UserConsumptionApiResponseSchema } from '../schemas';
  8. const userConsumptionRoute = createRoute({
  9. method: 'get',
  10. path: '/user-consumption',
  11. middleware: [authMiddleware],
  12. request: {
  13. query: UserConsumptionQuerySchema
  14. },
  15. responses: {
  16. 200: {
  17. description: '获取用户消费统计成功',
  18. content: {
  19. 'application/json': {
  20. schema: UserConsumptionApiResponseSchema
  21. }
  22. }
  23. },
  24. 400: {
  25. description: '请求参数错误',
  26. content: {
  27. 'application/json': {
  28. schema: ErrorSchema
  29. }
  30. }
  31. },
  32. 401: {
  33. description: '认证失败',
  34. content: {
  35. 'application/json': {
  36. schema: ErrorSchema
  37. }
  38. }
  39. },
  40. 403: {
  41. description: '权限不足',
  42. content: {
  43. 'application/json': {
  44. schema: ErrorSchema
  45. }
  46. }
  47. },
  48. 500: {
  49. description: '服务器内部错误',
  50. content: {
  51. 'application/json': {
  52. schema: ErrorSchema
  53. }
  54. }
  55. }
  56. }
  57. });
  58. const userConsumptionRoutes = new OpenAPIHono<AuthContext>()
  59. .openapi(userConsumptionRoute, async (c) => {
  60. const user = c.get('user');
  61. // 调试:记录原始查询参数
  62. const rawQuery = c.req.query();
  63. console.debug('用户消费统计API调试 - 原始查询参数:', rawQuery);
  64. console.debug('用户消费统计API调试 - 原始参数类型检查:', {
  65. page: { value: rawQuery.page, type: typeof rawQuery.page },
  66. limit: { value: rawQuery.limit, type: typeof rawQuery.limit },
  67. year: { value: rawQuery.year, type: typeof rawQuery.year }
  68. });
  69. const queryParams = c.req.valid('query');
  70. // 调试:记录验证后的查询参数
  71. console.debug('用户消费统计API调试 - 验证后的查询参数:', queryParams);
  72. console.debug('用户消费统计API调试 - 验证后参数类型检查:', {
  73. page: { value: queryParams.page, type: typeof queryParams.page },
  74. limit: { value: queryParams.limit, type: typeof queryParams.limit },
  75. year: { value: queryParams.year, type: typeof queryParams.year }
  76. });
  77. try {
  78. const service = new DataOverviewServiceMt(AppDataSource);
  79. // 分离时间筛选参数和分页参数
  80. const { startDate, endDate, timeRange, ...paginationParams } = queryParams;
  81. const timeFilterParams = { startDate, endDate, timeRange };
  82. const statistics = await service.getUserConsumptionStatistics(
  83. user.tenantId,
  84. timeFilterParams,
  85. paginationParams
  86. );
  87. const responseData = await parseWithAwait(UserConsumptionApiResponseSchema, {
  88. data: statistics,
  89. success: true,
  90. message: '获取用户消费统计成功'
  91. });
  92. return c.json(responseData, 200);
  93. } catch (error) {
  94. console.error('获取用户消费统计失败:', error);
  95. return c.json(
  96. { code: 500, message: error instanceof Error ? error.message : '获取用户消费统计失败' },
  97. 500
  98. );
  99. }
  100. });
  101. export default userConsumptionRoutes;