| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
- import { IncomeRecordsService } from '../services/income-records.service'
- import {
- CreateIncomeRecordBody,
- UpdateIncomeRecordBody,
- ListIncomeRecordsQuery,
- IncomeRecordParams
- } from '../dto/income-records.dto'
- import { UserRole } from '../entities/user.entity'
- import { TeamService } from '../services/team.service'
- import { UserService } from '../services/user.service'
- export class IncomeRecordsController {
- private incomeRecordsService: IncomeRecordsService
- private teamService: TeamService
- private userService: UserService
- constructor(app: FastifyInstance) {
- this.incomeRecordsService = new IncomeRecordsService(app)
- this.teamService = new TeamService(app)
- this.userService = new UserService(app)
- }
- async create(request: FastifyRequest<{ Body: CreateIncomeRecordBody }>, reply: FastifyReply) {
- try {
- const { agentId, userId, incomeAmount, incomeType, orderType, orderPrice, orderNo, payChannel, payNo } =
- request.body
- // 验证必填字段
- if (
- !agentId ||
- !userId ||
- !incomeAmount ||
- !incomeType ||
- !orderType ||
- !orderPrice ||
- !orderNo ||
- !payChannel ||
- !payNo
- ) {
- return reply.code(400).send({
- message:
- 'agentId、userId、incomeAmount、incomeType、orderType、orderPrice、orderNo、payChannel、payNo 为必填字段'
- })
- }
- // 验证金额为正数
- if (incomeAmount <= 0 || orderPrice <= 0) {
- return reply.code(400).send({ message: 'incomeAmount 和 orderPrice 必须大于 0' })
- }
- const incomeRecord = await this.incomeRecordsService.create(request.body)
- return reply.code(201).send(incomeRecord)
- } catch (error) {
- return reply.code(500).send({ message: '创建收入记录失败' })
- }
- }
- async findById(request: FastifyRequest<{ Params: IncomeRecordParams }>, reply: FastifyReply) {
- try {
- const { id } = request.params
- const incomeRecord = await this.incomeRecordsService.findById(id)
-
- // 解析分润信息(如果存在)
- let commissionDetails = null
- if (incomeRecord.commissionDetails) {
- try {
- commissionDetails = JSON.parse(incomeRecord.commissionDetails)
- } catch (parseError) {
- // 解析失败时返回 null
- commissionDetails = null
- }
- }
-
- // 返回包含解析后的分润信息的记录
- return reply.send({
- ...incomeRecord,
- commissionDetails
- })
- } catch (error) {
- return reply.code(404).send({ message: '收入记录不存在' })
- }
- }
- async findAll(request: FastifyRequest<{ Querystring: ListIncomeRecordsQuery }>, reply: FastifyReply) {
- try {
- const user = request.user
- if (!user) {
- return reply.code(403).send({ message: '用户未登录' })
- }
- // 普通用户不能查看分销明细
- if (user.role === UserRole.USER) {
- return reply.code(403).send({ message: '普通用户无权查看分销明细' })
- }
- // 根据用户角色设置查询条件
- if (user.role === UserRole.PROMOTER) {
- // 推广用户(团队成员)只能查看自己相关的订单(通过personalAgentId过滤)
- request.query.personalAgentId = user.id
- } else if (user.role === UserRole.TEAM) {
- // 团队账号可以查看自己团队的订单
- const team = await this.teamService.findByUserId(user.id)
- if (team) {
- request.query.agentId = team.userId
- }
- }
- // ADMIN 可以查看所有订单,不需要设置过滤条件
- const result = await this.incomeRecordsService.findAll(request.query)
- return reply.send(result)
- } catch (error) {
- return reply.code(500).send({ message: '获取收入记录列表失败' })
- }
- }
- async update(
- request: FastifyRequest<{ Params: IncomeRecordParams; Body: UpdateIncomeRecordBody }>,
- reply: FastifyReply
- ) {
- try {
- const { id } = request.params
- const updateData = { ...request.body, id }
- try {
- await this.incomeRecordsService.findById(id)
- } catch (error) {
- return reply.code(404).send({ message: '收入记录不存在' })
- }
- const updatedRecord = await this.incomeRecordsService.update(updateData)
- return reply.send(updatedRecord)
- } catch (error) {
- return reply.code(500).send({ message: '更新收入记录失败' })
- }
- }
- async delete(request: FastifyRequest<{ Params: IncomeRecordParams }>, reply: FastifyReply) {
- try {
- const { id } = request.params
- try {
- await this.incomeRecordsService.findById(id)
- } catch (error) {
- return reply.code(404).send({ message: '收入记录不存在' })
- }
- await this.incomeRecordsService.delete(id)
- return reply.send({ message: '收入记录已删除' })
- } catch (error) {
- return reply.code(500).send({ message: '删除收入记录失败' })
- }
- }
- async hardDelete(request: FastifyRequest<{ Params: IncomeRecordParams }>, reply: FastifyReply) {
- try {
- const { id } = request.params
- try {
- await this.incomeRecordsService.findById(id)
- } catch (error) {
- return reply.code(404).send({ message: '收入记录不存在' })
- }
- await this.incomeRecordsService.hardDelete(id)
- return reply.send({ message: '收入记录已永久删除' })
- } catch (error) {
- return reply.code(500).send({ message: '永久删除收入记录失败' })
- }
- }
- async getStatistics(
- request: FastifyRequest<{ Querystring: { startDate?: string; endDate?: string; userId?: number } }>,
- reply: FastifyReply
- ) {
- try {
- const { startDate, endDate, userId } = request.query
- const user = request.user
- if (!user) {
- return reply.code(403).send({ message: '用户未登录' })
- }
- let finalUserId: number | undefined
- if (user.role === UserRole.ADMIN) {
- if (userId) {
- finalUserId = userId
- }
- } else if (user.role === UserRole.TEAM) {
- finalUserId = user.id
- } else if (user.role === UserRole.PROMOTER) {
- const promoter = await this.userService.findById(user.id)
- finalUserId = promoter.parentId
- }
- const statistics = await this.incomeRecordsService.getStatistics(startDate, endDate, finalUserId)
- return reply.send(statistics)
- } catch (error) {
- return reply.code(500).send({ message: '获取统计数据失败' })
- }
- }
- }
|