| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
- import { PromotionLinkService } from '../services/promotion-link.service'
- import {
- CreatePromotionLinkBody,
- UpdatePromotionLinkBody,
- ListPromotionLinkQuery,
- PromotionLinkParams
- } from '../dto/promotion-link.dto'
- import { LinkType } from '../entities/promotion-link.entity'
- import { UserRole } from '../entities/user.entity'
- import { TeamService } from '../services/team.service'
- import { TeamMembersService } from '../services/team-members.service'
- export class PromotionLinkController {
- private promotionLinkService: PromotionLinkService
- private teamService: TeamService
- private teamMembersService: TeamMembersService
- constructor(app: FastifyInstance) {
- this.promotionLinkService = new PromotionLinkService(app)
- this.teamService = new TeamService(app)
- this.teamMembersService = new TeamMembersService(app)
- }
- async create(request: FastifyRequest<{ Body: CreatePromotionLinkBody }>, reply: FastifyReply) {
- try {
- const user = request.user
- if (!user) {
- return reply.code(403).send({ message: '用户未登录' })
- }
- if (user.role === UserRole.TEAM) {
- const team = await this.teamService.findByUserId(user.id)
- request.body.teamId = team.id
- }
- const promotionLink = await this.promotionLinkService.create(request.body)
- return reply.code(201).send(promotionLink)
- } catch (error) {
- return reply.code(500).send({ message: '创建推广链接失败', error })
- }
- }
- async findById(request: FastifyRequest<{ Params: PromotionLinkParams }>, reply: FastifyReply) {
- try {
- const { id } = request.params
- const promotionLink = await this.promotionLinkService.findById(id)
- return reply.send(promotionLink)
- } catch (error) {
- return reply.code(404).send({ message: '推广链接不存在' })
- }
- }
- async findAll(request: FastifyRequest<{ Querystring: ListPromotionLinkQuery }>, reply: FastifyReply) {
- try {
- const user = request.user
- if (!user) {
- return reply.code(403).send({ message: '用户未登录' })
- }
- if (user.role === UserRole.PROMOTER) {
- const teamMembers = await this.teamMembersService.findByUserId(user.id)
- request.query.teamId = teamMembers.teamId
- } else if (user.role === UserRole.TEAM) {
- const team = await this.teamService.findByUserId(user.id)
- request.query.teamId = team.id
- }
- const result = await this.promotionLinkService.findAll(request.query)
- return reply.send(result)
- } catch (error) {
- return reply.code(500).send({ message: '获取推广链接列表失败', error })
- }
- }
- async update(
- request: FastifyRequest<{ Params: PromotionLinkParams; Body: UpdatePromotionLinkBody }>,
- reply: FastifyReply
- ) {
- try {
- const { id } = request.params
- const updateData = { ...request.body, id }
- try {
- await this.promotionLinkService.findById(id)
- } catch (error) {
- return reply.code(404).send({ message: '推广链接不存在' })
- }
- const updatedLink = await this.promotionLinkService.update(updateData)
- return reply.send(updatedLink)
- } catch (error) {
- return reply.code(500).send({ message: '更新推广链接失败', error })
- }
- }
- async delete(request: FastifyRequest<{ Params: PromotionLinkParams }>, reply: FastifyReply) {
- try {
- const { id } = request.params
- try {
- await this.promotionLinkService.findById(id)
- } catch (error) {
- return reply.code(404).send({ message: '推广链接不存在' })
- }
- await this.promotionLinkService.delete(id)
- return reply.send({ message: '推广链接已删除' })
- } catch (error) {
- return reply.code(500).send({ message: '删除推广链接失败', error })
- }
- }
- async findByType(request: FastifyRequest<{ Querystring: { type: LinkType } }>, reply: FastifyReply) {
- try {
- const { type } = request.query
- const links = await this.promotionLinkService.findByType(type)
- return reply.send(links)
- } catch (error) {
- return reply.code(500).send({ message: '获取指定类型推广链接失败', error })
- }
- }
- async getStatistics(request: FastifyRequest, reply: FastifyReply) {
- try {
- const statistics = await this.promotionLinkService.getStatistics()
- return reply.send(statistics)
- } catch (error) {
- return reply.code(500).send({ message: '获取统计数据失败', error })
- }
- }
- }
|