goods-info.controller.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify'
  2. import { GoodsInfoService } from '../services/goods-info.service'
  3. import { ScanRecordService } from '../services/scan-record.service'
  4. import {
  5. CreateGoodsInfoDto,
  6. UpdateGoodsInfoDto,
  7. QueryGoodsInfoDto,
  8. AdminUpdateGoodsInfoDto
  9. } from '../dto/goods-info.dto'
  10. export class GoodsInfoController {
  11. private goodsInfoService: GoodsInfoService
  12. private scanRecordService: ScanRecordService
  13. constructor(app: FastifyInstance) {
  14. this.goodsInfoService = new GoodsInfoService(app)
  15. this.scanRecordService = new ScanRecordService(app)
  16. }
  17. async create(request: FastifyRequest<{ Body: CreateGoodsInfoDto }>, reply: FastifyReply) {
  18. try {
  19. const { qrCode, ...data } = request.body
  20. if (data.photoUrl) {
  21. try {
  22. const urlObj = new URL(data.photoUrl)
  23. data.photoUrl = urlObj.pathname.substring(1)
  24. } catch (error) {}
  25. }
  26. const goodsInfo = await this.goodsInfoService.create(qrCode, data)
  27. return reply.code(201).send({
  28. message: '物品信息创建成功',
  29. data: goodsInfo
  30. })
  31. } catch (error) {
  32. const message = error instanceof Error ? error.message : '创建失败'
  33. return reply.code(400).send({ message })
  34. }
  35. }
  36. async update(request: FastifyRequest<{ Body: UpdateGoodsInfoDto }>, reply: FastifyReply) {
  37. try {
  38. const { qrCode, maintenanceCode, ...data } = request.body
  39. if (data.photoUrl) {
  40. try {
  41. const urlObj = new URL(data.photoUrl)
  42. data.photoUrl = urlObj.pathname.substring(1)
  43. } catch (error) {}
  44. }
  45. const goodsInfo = await this.goodsInfoService.update(qrCode, maintenanceCode, data)
  46. return reply.send({
  47. message: '物品信息更新成功',
  48. data: goodsInfo
  49. })
  50. } catch (error) {
  51. const message = error instanceof Error ? error.message : '更新失败'
  52. return reply.code(400).send({ message })
  53. }
  54. }
  55. async get(request: FastifyRequest<{ Querystring: { qrCode: string } }>, reply: FastifyReply) {
  56. try {
  57. const { qrCode } = request.query
  58. if (!qrCode) {
  59. return reply.code(400).send({ message: '请提供二维码参数' })
  60. }
  61. const goodsInfo = await this.goodsInfoService.findByQrCode(qrCode)
  62. if (!goodsInfo) {
  63. return reply.code(404).send({ message: '物品信息不存在' })
  64. }
  65. return reply.send(goodsInfo)
  66. } catch (error) {
  67. const message = error instanceof Error ? error.message : '获取失败'
  68. return reply.code(500).send({ message })
  69. }
  70. }
  71. async adminUpdate(request: FastifyRequest<{ Body: AdminUpdateGoodsInfoDto }>, reply: FastifyReply) {
  72. try {
  73. const { qrCodeId, ...data } = request.body
  74. if (!qrCodeId) {
  75. return reply.code(400).send({ message: '请提供二维码ID' })
  76. }
  77. if (data.photoUrl) {
  78. try {
  79. const urlObj = new URL(data.photoUrl)
  80. data.photoUrl = urlObj.pathname.substring(1)
  81. } catch (error) {}
  82. }
  83. const goodsInfo = await this.goodsInfoService.adminUpdate(qrCodeId, data)
  84. return reply.send({
  85. message: '物品信息更新成功',
  86. data: goodsInfo
  87. })
  88. } catch (error) {
  89. const message = error instanceof Error ? error.message : '更新失败'
  90. return reply.code(400).send({ message })
  91. }
  92. }
  93. async list(request: FastifyRequest<{ Querystring: QueryGoodsInfoDto }>, reply: FastifyReply) {
  94. try {
  95. const { name, contactName, contactPhone, startDate, endDate, page, pageSize } = request.query
  96. const result = await this.goodsInfoService.query(
  97. name,
  98. contactName,
  99. contactPhone,
  100. startDate,
  101. endDate,
  102. page,
  103. pageSize
  104. )
  105. return reply.send(result)
  106. } catch (error) {
  107. const message = error instanceof Error ? error.message : '查询失败'
  108. return reply.code(500).send({ message })
  109. }
  110. }
  111. async adminGetDetail(request: FastifyRequest<{ Querystring: { qrCodeId: string } }>, reply: FastifyReply) {
  112. try {
  113. const { qrCodeId } = request.query
  114. if (!qrCodeId) {
  115. return reply.code(400).send({ message: '请提供二维码ID' })
  116. }
  117. const goodsInfo = await this.goodsInfoService.adminGetDetail(Number(qrCodeId))
  118. if (!goodsInfo) {
  119. return reply.code(404).send({ message: '物品信息不存在' })
  120. }
  121. return reply.send(goodsInfo)
  122. } catch (error) {
  123. const message = error instanceof Error ? error.message : '获取失败'
  124. return reply.code(500).send({ message })
  125. }
  126. }
  127. }