income-records.controller.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
  2. import { IncomeRecordsService } from '../services/income-records.service'
  3. import {
  4. CreateIncomeRecordBody,
  5. UpdateIncomeRecordBody,
  6. ListIncomeRecordsQuery,
  7. IncomeRecordParams
  8. } from '../dto/income-records.dto'
  9. import { UserRole } from '../entities/user.entity'
  10. import { TeamService } from '../services/team.service'
  11. import { UserService } from '../services/user.service'
  12. export class IncomeRecordsController {
  13. private incomeRecordsService: IncomeRecordsService
  14. private teamService: TeamService
  15. private userService: UserService
  16. constructor(app: FastifyInstance) {
  17. this.incomeRecordsService = new IncomeRecordsService(app)
  18. this.teamService = new TeamService(app)
  19. this.userService = new UserService(app)
  20. }
  21. async create(request: FastifyRequest<{ Body: CreateIncomeRecordBody }>, reply: FastifyReply) {
  22. try {
  23. const { agentId, userId, incomeAmount, incomeType, orderType, orderPrice, orderNo, payChannel, payNo } =
  24. request.body
  25. // 验证必填字段
  26. if (
  27. !agentId ||
  28. !userId ||
  29. !incomeAmount ||
  30. !incomeType ||
  31. !orderType ||
  32. !orderPrice ||
  33. !orderNo ||
  34. !payChannel ||
  35. !payNo
  36. ) {
  37. return reply.code(400).send({
  38. message:
  39. 'agentId、userId、incomeAmount、incomeType、orderType、orderPrice、orderNo、payChannel、payNo 为必填字段'
  40. })
  41. }
  42. // 验证金额为正数
  43. if (incomeAmount <= 0 || orderPrice <= 0) {
  44. return reply.code(400).send({ message: 'incomeAmount 和 orderPrice 必须大于 0' })
  45. }
  46. const incomeRecord = await this.incomeRecordsService.create(request.body)
  47. return reply.code(201).send(incomeRecord)
  48. } catch (error) {
  49. return reply.code(500).send({ message: '创建收入记录失败' })
  50. }
  51. }
  52. async findById(request: FastifyRequest<{ Params: IncomeRecordParams }>, reply: FastifyReply) {
  53. try {
  54. const { id } = request.params
  55. const incomeRecord = await this.incomeRecordsService.findById(id)
  56. // 解析分润信息(如果存在)
  57. let commissionDetails = null
  58. if (incomeRecord.commissionDetails) {
  59. try {
  60. commissionDetails = JSON.parse(incomeRecord.commissionDetails)
  61. } catch (parseError) {
  62. // 解析失败时返回 null
  63. commissionDetails = null
  64. }
  65. }
  66. // 返回包含解析后的分润信息的记录
  67. return reply.send({
  68. ...incomeRecord,
  69. commissionDetails
  70. })
  71. } catch (error) {
  72. return reply.code(404).send({ message: '收入记录不存在' })
  73. }
  74. }
  75. async findAll(request: FastifyRequest<{ Querystring: ListIncomeRecordsQuery }>, reply: FastifyReply) {
  76. try {
  77. const user = request.user
  78. if (!user) {
  79. return reply.code(403).send({ message: '用户未登录' })
  80. }
  81. // 普通用户不能查看分销明细
  82. if (user.role === UserRole.USER) {
  83. return reply.code(403).send({ message: '普通用户无权查看分销明细' })
  84. }
  85. // 根据用户角色设置查询条件
  86. if (user.role === UserRole.PROMOTER) {
  87. // 推广用户(团队成员)只能查看自己相关的订单(通过personalAgentId过滤)
  88. request.query.personalAgentId = user.id
  89. } else if (user.role === UserRole.TEAM) {
  90. // 团队账号可以查看自己团队的订单
  91. const team = await this.teamService.findByUserId(user.id)
  92. if (team) {
  93. request.query.agentId = team.userId
  94. }
  95. }
  96. // ADMIN 可以查看所有订单,不需要设置过滤条件
  97. const result = await this.incomeRecordsService.findAll(request.query)
  98. return reply.send(result)
  99. } catch (error) {
  100. return reply.code(500).send({ message: '获取收入记录列表失败' })
  101. }
  102. }
  103. async update(
  104. request: FastifyRequest<{ Params: IncomeRecordParams; Body: UpdateIncomeRecordBody }>,
  105. reply: FastifyReply
  106. ) {
  107. try {
  108. const { id } = request.params
  109. const updateData = { ...request.body, id }
  110. try {
  111. await this.incomeRecordsService.findById(id)
  112. } catch (error) {
  113. return reply.code(404).send({ message: '收入记录不存在' })
  114. }
  115. const updatedRecord = await this.incomeRecordsService.update(updateData)
  116. return reply.send(updatedRecord)
  117. } catch (error) {
  118. return reply.code(500).send({ message: '更新收入记录失败' })
  119. }
  120. }
  121. async delete(request: FastifyRequest<{ Params: IncomeRecordParams }>, reply: FastifyReply) {
  122. try {
  123. const { id } = request.params
  124. try {
  125. await this.incomeRecordsService.findById(id)
  126. } catch (error) {
  127. return reply.code(404).send({ message: '收入记录不存在' })
  128. }
  129. await this.incomeRecordsService.delete(id)
  130. return reply.send({ message: '收入记录已删除' })
  131. } catch (error) {
  132. return reply.code(500).send({ message: '删除收入记录失败' })
  133. }
  134. }
  135. async hardDelete(request: FastifyRequest<{ Params: IncomeRecordParams }>, reply: FastifyReply) {
  136. try {
  137. const { id } = request.params
  138. try {
  139. await this.incomeRecordsService.findById(id)
  140. } catch (error) {
  141. return reply.code(404).send({ message: '收入记录不存在' })
  142. }
  143. await this.incomeRecordsService.hardDelete(id)
  144. return reply.send({ message: '收入记录已永久删除' })
  145. } catch (error) {
  146. return reply.code(500).send({ message: '永久删除收入记录失败' })
  147. }
  148. }
  149. async getStatistics(
  150. request: FastifyRequest<{ Querystring: { startDate?: string; endDate?: string; userId?: number } }>,
  151. reply: FastifyReply
  152. ) {
  153. try {
  154. const { startDate, endDate, userId } = request.query
  155. const user = request.user
  156. if (!user) {
  157. return reply.code(403).send({ message: '用户未登录' })
  158. }
  159. let finalUserId: number | undefined
  160. if (user.role === UserRole.ADMIN) {
  161. if (userId) {
  162. finalUserId = userId
  163. }
  164. } else if (user.role === UserRole.TEAM) {
  165. finalUserId = user.id
  166. } else if (user.role === UserRole.PROMOTER) {
  167. const promoter = await this.userService.findById(user.id)
  168. finalUserId = promoter.parentId
  169. }
  170. const statistics = await this.incomeRecordsService.getStatistics(startDate, endDate, finalUserId)
  171. return reply.send(statistics)
  172. } catch (error) {
  173. return reply.code(500).send({ message: '获取统计数据失败' })
  174. }
  175. }
  176. }