fish.controller.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
  2. import { FishService } from '../services/fish.service'
  3. import { ResultEnum } from '../entities/fish.entity'
  4. import { UserRole } from '../entities/user.entity'
  5. import { ListFishQuery, CreateFishBody, UpdateFishBody, DeleteFishBody, StatisticsQuery } from '../dto/fish.dto'
  6. export class FishController {
  7. private fishService: FishService
  8. constructor(app: FastifyInstance) {
  9. this.fishService = new FishService(app)
  10. }
  11. async create(request: FastifyRequest<{ Body: CreateFishBody }>, reply: FastifyReply) {
  12. try {
  13. const fishData = request.body
  14. // 检查是否已存在相同 ID 的记录
  15. try {
  16. await this.fishService.findById(fishData.id)
  17. return reply.code(400).send({ message: '记录已存在' })
  18. } catch (error) {
  19. // 记录不存在,可以继续创建
  20. }
  21. const fish = await this.fishService.create(fishData)
  22. return reply.code(201).send({
  23. message: '创建成功',
  24. fish: {
  25. id: fish.id,
  26. name: fish.name,
  27. username: fish.username,
  28. phone: fish.phone,
  29. result: fish.result,
  30. ownerId: fish.ownerId,
  31. ownerName: fish.ownerName,
  32. ip: fish.ip,
  33. remark: fish.remark,
  34. createdAt: fish.createdAt,
  35. updatedAt: fish.updatedAt,
  36. loginTime: fish.loginTime
  37. }
  38. })
  39. } catch (error) {
  40. console.error('创建记录失败:', error)
  41. return reply.code(500).send({ message: '创建失败' })
  42. }
  43. }
  44. async getById(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) {
  45. try {
  46. const { id } = request.params
  47. const fish = await this.fishService.findById(id)
  48. return reply.send({
  49. fish: {
  50. id: fish.id,
  51. name: fish.name,
  52. username: fish.username,
  53. phone: fish.phone,
  54. password: fish.password,
  55. result: fish.result,
  56. ownerId: fish.ownerId,
  57. ownerName: fish.ownerName,
  58. ip: fish.ip,
  59. token: fish.token,
  60. session: fish.session,
  61. remark: fish.remark,
  62. createdAt: fish.createdAt,
  63. updatedAt: fish.updatedAt,
  64. loginTime: fish.loginTime
  65. }
  66. })
  67. } catch (error) {
  68. return reply.code(404).send({ message: '记录不存在' })
  69. }
  70. }
  71. async list(request: FastifyRequest<{ Querystring: ListFishQuery }>, reply: FastifyReply) {
  72. try {
  73. const query = request.query
  74. const result = await this.fishService.list(query)
  75. return reply.send(result)
  76. } catch (error) {
  77. console.error('查询记录失败:', error)
  78. return reply.code(500).send({ message: '查询失败' })
  79. }
  80. }
  81. async update(request: FastifyRequest<{ Body: UpdateFishBody }>, reply: FastifyReply) {
  82. try {
  83. const { id, ...updateData } = request.body
  84. // 检查记录是否存在
  85. try {
  86. await this.fishService.findById(id)
  87. } catch (error) {
  88. return reply.code(404).send({ message: '记录不存在' })
  89. }
  90. const updatedFish = await this.fishService.update(id, updateData)
  91. return reply.send({
  92. message: '更新成功',
  93. fish: {
  94. id: updatedFish.id,
  95. name: updatedFish.name,
  96. username: updatedFish.username,
  97. phone: updatedFish.phone,
  98. result: updatedFish.result,
  99. ownerId: updatedFish.ownerId,
  100. ownerName: updatedFish.ownerName,
  101. ip: updatedFish.ip,
  102. remark: updatedFish.remark,
  103. createdAt: updatedFish.createdAt,
  104. updatedAt: updatedFish.updatedAt,
  105. loginTime: updatedFish.loginTime
  106. }
  107. })
  108. } catch (error) {
  109. console.error('更新记录失败:', error)
  110. return reply.code(500).send({ message: '更新失败' })
  111. }
  112. }
  113. async delete(request: FastifyRequest<{ Body: DeleteFishBody }>, reply: FastifyReply) {
  114. try {
  115. const { id } = request.body
  116. // 检查记录是否存在
  117. try {
  118. await this.fishService.findById(id)
  119. } catch (error) {
  120. return reply.code(404).send({ message: '记录不存在' })
  121. }
  122. await this.fishService.delete(id)
  123. return reply.send({ message: '删除成功' })
  124. } catch (error) {
  125. console.error('删除记录失败:', error)
  126. return reply.code(500).send({ message: '删除失败' })
  127. }
  128. }
  129. async batchDelete(request: FastifyRequest<{ Body: { ids: string[] } }>, reply: FastifyReply) {
  130. try {
  131. const { ids } = request.body
  132. if (!ids || ids.length === 0) {
  133. return reply.code(400).send({ message: '请提供要删除的记录ID' })
  134. }
  135. await this.fishService.batchDelete(ids)
  136. return reply.send({ message: `成功删除 ${ids.length} 条记录` })
  137. } catch (error) {
  138. console.error('批量删除记录失败:', error)
  139. return reply.code(500).send({ message: '批量删除失败' })
  140. }
  141. }
  142. async getStatistics(request: FastifyRequest<{ Querystring: StatisticsQuery }>, reply: FastifyReply) {
  143. try {
  144. const { ownerId } = request.query
  145. let currentOwnerId = ownerId
  146. if (request.user.role !== UserRole.ADMIN) {
  147. currentOwnerId = request.user.id
  148. }
  149. const statistics = await this.fishService.getStatistics(currentOwnerId)
  150. return reply.send({
  151. statistics
  152. })
  153. } catch (error) {
  154. console.error('获取统计信息失败:', error)
  155. return reply.code(500).send({ message: '获取统计信息失败' })
  156. }
  157. }
  158. async findByOwnerId(request: FastifyRequest<{ Querystring: { ownerId: number } }>, reply: FastifyReply) {
  159. try {
  160. const { ownerId } = request.query
  161. const fishList = await this.fishService.findByOwnerId(ownerId)
  162. return reply.send({
  163. fishList
  164. })
  165. } catch (error) {
  166. console.error('按所有者ID查询失败:', error)
  167. return reply.code(500).send({ message: '查询失败' })
  168. }
  169. }
  170. async findByResult(request: FastifyRequest<{ Querystring: { result: string } }>, reply: FastifyReply) {
  171. try {
  172. const { result } = request.query
  173. // 验证 result 参数是否为有效的枚举值
  174. if (!Object.values(ResultEnum).includes(result as ResultEnum)) {
  175. return reply.code(400).send({ message: '无效的结果状态值' })
  176. }
  177. const fishList = await this.fishService.findByResult(result as ResultEnum)
  178. return reply.send({
  179. fishList
  180. })
  181. } catch (error) {
  182. console.error('按结果状态查询失败:', error)
  183. return reply.code(500).send({ message: '查询失败' })
  184. }
  185. }
  186. async exportToExcel(request: FastifyRequest<{ Querystring: ListFishQuery }>, reply: FastifyReply) {
  187. try {
  188. const query = request.query
  189. const excelBuffer = await this.fishService.exportToExcel(query)
  190. // 设置响应头
  191. reply.header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
  192. reply.header('Content-Disposition', 'attachment; filename="fish_data.xlsx"')
  193. reply.header('Content-Length', excelBuffer.length.toString())
  194. return reply.send(excelBuffer)
  195. } catch (error) {
  196. console.error('导出Excel失败:', error)
  197. return reply.code(500).send({ message: '导出失败' })
  198. }
  199. }
  200. }