| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
- import { TeamService } from '../services/team.service'
- import { CreateTeamBody, UpdateTeamBody, ListTeamQuery, TeamParams, UpdateRevenueBody } from '../dto/team.dto'
- import { UserRole } from '../entities/user.entity'
- export class TeamController {
- private teamService: TeamService
- constructor(app: FastifyInstance) {
- this.teamService = new TeamService(app)
- }
- async create(request: FastifyRequest<{ Body: CreateTeamBody }>, reply: FastifyReply) {
- try {
- const team = await this.teamService.create(request.body, request.user.id)
- return reply.code(201).send(team)
- } catch (error) {
- return reply.code(500).send({ message: '创建团队失败', error })
- }
- }
- async findById(request: FastifyRequest<{ Params: TeamParams }>, reply: FastifyReply) {
- try {
- const { id } = request.params
- const team = await this.teamService.findById(id)
- return reply.send(team)
- } catch (error) {
- return reply.code(404).send({ message: '团队不存在' })
- }
- }
- async findAll(request: FastifyRequest<{ Querystring: ListTeamQuery }>, reply: FastifyReply) {
- try {
- const user = request.user
- if (!user) {
- return reply.code(403).send({ message: '用户未登录' })
- }
- if (user.role === UserRole.TEAM) {
- request.query.userId = user.id
- }
- const result = await this.teamService.findAll(request.query)
- return reply.send(result)
- } catch (error) {
- return reply.code(500).send({ message: '获取团队列表失败', error })
- }
- }
- async update(request: FastifyRequest<{ Params: TeamParams; Body: UpdateTeamBody }>, reply: FastifyReply) {
- try {
- const { id } = request.params
- const updateData = { ...request.body, id }
- try {
- await this.teamService.findById(id)
- } catch (error) {
- return reply.code(404).send({ message: '团队不存在' })
- }
- const updatedTeam = await this.teamService.update(updateData)
- return reply.send(updatedTeam)
- } catch (error) {
- return reply.code(500).send({ message: '更新团队失败', error })
- }
- }
- async delete(request: FastifyRequest<{ Params: TeamParams }>, reply: FastifyReply) {
- try {
- const { id } = request.params
- try {
- await this.teamService.findById(id)
- } catch (error) {
- return reply.code(404).send({ message: '团队不存在' })
- }
- await this.teamService.delete(id)
- return reply.send({ message: '团队已删除' })
- } catch (error) {
- return reply.code(500).send({ message: '删除团队失败', error })
- }
- }
- async updateRevenue(request: FastifyRequest<{ Body: UpdateRevenueBody }>, reply: FastifyReply) {
- try {
- const { id, amount, type } = request.body
- try {
- await this.teamService.findById(id)
- } catch (error) {
- return reply.code(404).send({ message: '团队不存在' })
- }
- const updatedTeam = await this.teamService.updateRevenue(id, amount, type)
- return reply.send(updatedTeam)
- } catch (error) {
- return reply.code(500).send({ message: '更新团队收入失败', error })
- }
- }
- async getStatistics(request: FastifyRequest, reply: FastifyReply) {
- try {
- const statistics = await this.teamService.getStatistics()
- return reply.send(statistics)
- } catch (error) {
- return reply.code(500).send({ message: '获取统计数据失败', error })
- }
- }
- async getTeamStatistics(request: FastifyRequest, reply: FastifyReply) {
- try {
- const user = request.user
- if (!user) {
- return reply.code(403).send({ message: '用户未登录' })
- }
- const statistics = await this.teamService.getTeamStatistics(user.id)
- return reply.send(statistics)
- } catch (error) {
- return reply.code(500).send({ message: '获取统计数据失败', error })
- }
- }
- async getTeams(request: FastifyRequest, reply: FastifyReply) {
- try {
- const teams = await this.teamService.getTeams()
- return reply.send(teams)
- } catch (error) {
- return reply.code(500).send({ message: '获取团队失败', error })
- }
- }
- async findByUserId(request: FastifyRequest, reply: FastifyReply) {
- try {
- const user = request.user
- if (!user) {
- return reply.code(403).send({ message: '用户未登录' })
- }
- const team = await this.teamService.findByUserId(user.id)
- return reply.send(team)
- } catch (error) {
- return reply.code(404).send({ message: '团队不存在' })
- }
- }
- }
|