team.controller.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
  2. import { TeamService } from '../services/team.service'
  3. import { CreateTeamBody, UpdateTeamBody, ListTeamQuery, TeamParams, UpdateRevenueBody } from '../dto/team.dto'
  4. import { UserRole } from '../entities/user.entity'
  5. export class TeamController {
  6. private teamService: TeamService
  7. constructor(app: FastifyInstance) {
  8. this.teamService = new TeamService(app)
  9. }
  10. async create(request: FastifyRequest<{ Body: CreateTeamBody }>, reply: FastifyReply) {
  11. try {
  12. const team = await this.teamService.create(request.body, request.user.id)
  13. return reply.code(201).send(team)
  14. } catch (error) {
  15. return reply.code(500).send({ message: '创建团队失败', error })
  16. }
  17. }
  18. async findById(request: FastifyRequest<{ Params: TeamParams }>, reply: FastifyReply) {
  19. try {
  20. const { id } = request.params
  21. const team = await this.teamService.findById(id)
  22. return reply.send(team)
  23. } catch (error) {
  24. return reply.code(404).send({ message: '团队不存在' })
  25. }
  26. }
  27. async findAll(request: FastifyRequest<{ Querystring: ListTeamQuery }>, reply: FastifyReply) {
  28. try {
  29. const user = request.user
  30. if (!user) {
  31. return reply.code(403).send({ message: '用户未登录' })
  32. }
  33. if (user.role === UserRole.TEAM) {
  34. request.query.userId = user.id
  35. }
  36. const result = await this.teamService.findAll(request.query)
  37. return reply.send(result)
  38. } catch (error) {
  39. return reply.code(500).send({ message: '获取团队列表失败', error })
  40. }
  41. }
  42. async update(request: FastifyRequest<{ Params: TeamParams; Body: UpdateTeamBody }>, reply: FastifyReply) {
  43. try {
  44. const { id } = request.params
  45. const updateData = { ...request.body, id }
  46. try {
  47. await this.teamService.findById(id)
  48. } catch (error) {
  49. return reply.code(404).send({ message: '团队不存在' })
  50. }
  51. const updatedTeam = await this.teamService.update(updateData)
  52. return reply.send(updatedTeam)
  53. } catch (error) {
  54. return reply.code(500).send({ message: '更新团队失败', error })
  55. }
  56. }
  57. async delete(request: FastifyRequest<{ Params: TeamParams }>, reply: FastifyReply) {
  58. try {
  59. const { id } = request.params
  60. try {
  61. await this.teamService.findById(id)
  62. } catch (error) {
  63. return reply.code(404).send({ message: '团队不存在' })
  64. }
  65. await this.teamService.delete(id)
  66. return reply.send({ message: '团队已删除' })
  67. } catch (error) {
  68. return reply.code(500).send({ message: '删除团队失败', error })
  69. }
  70. }
  71. async updateRevenue(request: FastifyRequest<{ Body: UpdateRevenueBody }>, reply: FastifyReply) {
  72. try {
  73. const { id, amount, type } = request.body
  74. try {
  75. await this.teamService.findById(id)
  76. } catch (error) {
  77. return reply.code(404).send({ message: '团队不存在' })
  78. }
  79. const updatedTeam = await this.teamService.updateRevenue(id, amount, type)
  80. return reply.send(updatedTeam)
  81. } catch (error) {
  82. return reply.code(500).send({ message: '更新团队收入失败', error })
  83. }
  84. }
  85. async getStatistics(request: FastifyRequest, reply: FastifyReply) {
  86. try {
  87. const statistics = await this.teamService.getStatistics()
  88. return reply.send(statistics)
  89. } catch (error) {
  90. return reply.code(500).send({ message: '获取统计数据失败', error })
  91. }
  92. }
  93. async getTeamStatistics(request: FastifyRequest, reply: FastifyReply) {
  94. try {
  95. const user = request.user
  96. if (!user) {
  97. return reply.code(403).send({ message: '用户未登录' })
  98. }
  99. const statistics = await this.teamService.getTeamStatistics(user.id)
  100. return reply.send(statistics)
  101. } catch (error) {
  102. return reply.code(500).send({ message: '获取统计数据失败', error })
  103. }
  104. }
  105. async getTeams(request: FastifyRequest, reply: FastifyReply) {
  106. try {
  107. const teams = await this.teamService.getTeams()
  108. return reply.send(teams)
  109. } catch (error) {
  110. return reply.code(500).send({ message: '获取团队失败', error })
  111. }
  112. }
  113. async findByUserId(request: FastifyRequest, reply: FastifyReply) {
  114. try {
  115. const user = request.user
  116. if (!user) {
  117. return reply.code(403).send({ message: '用户未登录' })
  118. }
  119. const team = await this.teamService.findByUserId(user.id)
  120. return reply.send(team)
  121. } catch (error) {
  122. return reply.code(404).send({ message: '团队不存在' })
  123. }
  124. }
  125. }