fish-friends.controller.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
  2. import { FishFriendsService } from '../services/fish-friends.service'
  3. import {
  4. ListFishFriendsQuery,
  5. CreateFishFriendsBody,
  6. BatchCreateFishFriendsBody,
  7. UpdateFishFriendsBody,
  8. DeleteFishFriendsBody
  9. } from '../dto/fish-friends.dto'
  10. export class FishFriendsController {
  11. private fishFriendsService: FishFriendsService
  12. constructor(app: FastifyInstance) {
  13. this.fishFriendsService = new FishFriendsService(app)
  14. }
  15. async create(request: FastifyRequest<{ Body: CreateFishFriendsBody }>, reply: FastifyReply) {
  16. try {
  17. const fishFriendsData = request.body
  18. // 检查是否已存在相同 ID 的记录(包括已删除的记录)
  19. try {
  20. await this.fishFriendsService.findByIdIncludeDeleted(fishFriendsData.id)
  21. return reply.code(400).send({ message: '记录已存在' })
  22. } catch (error) {
  23. // 记录不存在,可以继续创建
  24. }
  25. const fishFriends = await this.fishFriendsService.create(fishFriendsData)
  26. return reply.code(201).send({
  27. message: '创建成功',
  28. fishFriends: {
  29. id: fishFriends.id,
  30. fishId: fishFriends.fishId,
  31. ownerId: fishFriends.ownerId,
  32. tgName: fishFriends.tgName,
  33. tgUsername: fishFriends.tgUsername,
  34. tgRemarkName: fishFriends.tgRemarkName,
  35. tgPhone: fishFriends.tgPhone,
  36. remark: fishFriends.remark,
  37. createdAt: fishFriends.createdAt,
  38. updatedAt: fishFriends.updatedAt
  39. }
  40. })
  41. } catch (error) {
  42. console.error('创建记录失败:', error)
  43. return reply.code(500).send({ message: '创建失败' })
  44. }
  45. }
  46. async batchUpsert(request: FastifyRequest<{ Body: BatchCreateFishFriendsBody }>, reply: FastifyReply) {
  47. try {
  48. const { fishFriends } = request.body
  49. if (!fishFriends || !Array.isArray(fishFriends) || fishFriends.length === 0) {
  50. return reply.code(400).send({ message: 'error' })
  51. }
  52. // 使用批量 upsert 操作
  53. await this.fishFriendsService.batchUpsert(fishFriends)
  54. return reply.code(201).send({
  55. message: `ok`
  56. })
  57. } catch (error) {
  58. return reply.code(500).send({ message: 'error' })
  59. }
  60. }
  61. async getById(request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) {
  62. try {
  63. const { id } = request.params
  64. const fishFriends = await this.fishFriendsService.findById(id)
  65. return reply.send({
  66. fishFriends: {
  67. id: fishFriends.id,
  68. fishId: fishFriends.fishId,
  69. ownerId: fishFriends.ownerId,
  70. tgName: fishFriends.tgName,
  71. tgUsername: fishFriends.tgUsername,
  72. tgRemarkName: fishFriends.tgRemarkName,
  73. tgPhone: fishFriends.tgPhone,
  74. remark: fishFriends.remark,
  75. createdAt: fishFriends.createdAt,
  76. updatedAt: fishFriends.updatedAt
  77. }
  78. })
  79. } catch (error) {
  80. return reply.code(404).send({ message: '记录不存在' })
  81. }
  82. }
  83. async list(request: FastifyRequest<{ Querystring: ListFishFriendsQuery }>, reply: FastifyReply) {
  84. try {
  85. const query = request.query
  86. if (request.user.role !== 'admin') {
  87. query.ownerId = request.user.id
  88. }
  89. const result = await this.fishFriendsService.list(query)
  90. return reply.send(result)
  91. } catch (error) {
  92. console.error('查询记录失败:', error)
  93. return reply.code(500).send({ message: '查询失败' })
  94. }
  95. }
  96. async update(request: FastifyRequest<{ Body: UpdateFishFriendsBody }>, reply: FastifyReply) {
  97. try {
  98. const { id, ...updateData } = request.body
  99. // 检查记录是否存在
  100. try {
  101. await this.fishFriendsService.findById(id)
  102. } catch (error) {
  103. return reply.code(404).send({ message: '记录不存在' })
  104. }
  105. const updatedFishFriends = await this.fishFriendsService.update(id, updateData)
  106. return reply.send({
  107. message: '更新成功',
  108. fishFriends: {
  109. id: updatedFishFriends.id,
  110. fishId: updatedFishFriends.fishId,
  111. ownerId: updatedFishFriends.ownerId,
  112. tgName: updatedFishFriends.tgName,
  113. tgUsername: updatedFishFriends.tgUsername,
  114. tgRemarkName: updatedFishFriends.tgRemarkName,
  115. tgPhone: updatedFishFriends.tgPhone,
  116. remark: updatedFishFriends.remark,
  117. createdAt: updatedFishFriends.createdAt,
  118. updatedAt: updatedFishFriends.updatedAt
  119. }
  120. })
  121. } catch (error) {
  122. console.error('更新记录失败:', error)
  123. return reply.code(500).send({ message: '更新失败' })
  124. }
  125. }
  126. async delete(request: FastifyRequest<{ Body: DeleteFishFriendsBody }>, reply: FastifyReply) {
  127. try {
  128. const { id } = request.body
  129. // 检查记录是否存在
  130. try {
  131. await this.fishFriendsService.findById(id)
  132. } catch (error) {
  133. return reply.code(404).send({ message: '记录不存在' })
  134. }
  135. await this.fishFriendsService.delete(id)
  136. return reply.send({ message: '删除成功' })
  137. } catch (error) {
  138. console.error('删除记录失败:', error)
  139. return reply.code(500).send({ message: '删除失败' })
  140. }
  141. }
  142. async batchDelete(request: FastifyRequest<{ Body: { ids: string[] } }>, reply: FastifyReply) {
  143. try {
  144. const { ids } = request.body
  145. if (!ids || ids.length === 0) {
  146. return reply.code(400).send({ message: '请提供要删除的记录ID' })
  147. }
  148. await this.fishFriendsService.batchDelete(ids)
  149. return reply.send({ message: `成功删除 ${ids.length} 条记录` })
  150. } catch (error) {
  151. console.error('批量删除记录失败:', error)
  152. return reply.code(500).send({ message: '批量删除失败' })
  153. }
  154. }
  155. async getStatistics(request: FastifyRequest, reply: FastifyReply) {
  156. try {
  157. const statistics = await this.fishFriendsService.getStatistics()
  158. return reply.send({
  159. statistics
  160. })
  161. } catch (error) {
  162. console.error('获取统计信息失败:', error)
  163. return reply.code(500).send({ message: '获取统计信息失败' })
  164. }
  165. }
  166. async findByFishId(request: FastifyRequest<{ Querystring: { fishId: string } }>, reply: FastifyReply) {
  167. try {
  168. const { fishId } = request.query
  169. const fishFriendsList = await this.fishFriendsService.findByFishId(fishId)
  170. return reply.send({
  171. fishFriendsList
  172. })
  173. } catch (error) {
  174. console.error('按Fish ID查询失败:', error)
  175. return reply.code(500).send({ message: '查询失败' })
  176. }
  177. }
  178. async findByOwnerId(request: FastifyRequest<{ Querystring: { ownerId: number } }>, reply: FastifyReply) {
  179. try {
  180. const { ownerId } = request.query
  181. const fishFriendsList = await this.fishFriendsService.findByOwnerId(ownerId)
  182. return reply.send({
  183. fishFriendsList
  184. })
  185. } catch (error) {
  186. console.error('按所有者ID查询失败:', error)
  187. return reply.code(500).send({ message: '查询失败' })
  188. }
  189. }
  190. async findByTgUsername(request: FastifyRequest<{ Querystring: { tgUsername: string } }>, reply: FastifyReply) {
  191. try {
  192. const { tgUsername } = request.query
  193. const fishFriendsList = await this.fishFriendsService.findByTgUsername(tgUsername)
  194. return reply.send({
  195. fishFriendsList
  196. })
  197. } catch (error) {
  198. console.error('按Telegram用户名查询失败:', error)
  199. return reply.code(500).send({ message: '查询失败' })
  200. }
  201. }
  202. async exportToExcel(request: FastifyRequest<{ Querystring: ListFishFriendsQuery }>, reply: FastifyReply) {
  203. try {
  204. const query = request.query
  205. const excelBuffer = await this.fishFriendsService.exportToExcel(query)
  206. // 设置响应头
  207. reply.header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
  208. reply.header('Content-Disposition', 'attachment; filename="fish_friends_data.xlsx"')
  209. reply.header('Content-Length', excelBuffer.length.toString())
  210. return reply.send(excelBuffer)
  211. } catch (error) {
  212. console.error('导出Excel失败:', error)
  213. return reply.code(500).send({ message: '导出失败' })
  214. }
  215. }
  216. }