import { FastifyRequest, FastifyReply, FastifyInstance } from 'fastify' import { GoodsInfoService } from '../services/goods-info.service' import { ScanRecordService } from '../services/scan-record.service' import { CreateGoodsInfoDto, UpdateGoodsInfoDto, QueryGoodsInfoDto, AdminUpdateGoodsInfoDto } from '../dto/goods-info.dto' export class GoodsInfoController { private goodsInfoService: GoodsInfoService private scanRecordService: ScanRecordService constructor(app: FastifyInstance) { this.goodsInfoService = new GoodsInfoService(app) this.scanRecordService = new ScanRecordService(app) } async create(request: FastifyRequest<{ Body: CreateGoodsInfoDto }>, reply: FastifyReply) { try { const { qrCode, ...data } = request.body if (data.photoUrl) { try { const urlObj = new URL(data.photoUrl) data.photoUrl = urlObj.pathname.substring(1) } catch (error) {} } const goodsInfo = await this.goodsInfoService.create(qrCode, data) return reply.code(201).send({ message: '物品信息创建成功', data: goodsInfo }) } catch (error) { const message = error instanceof Error ? error.message : '创建失败' return reply.code(400).send({ message }) } } async update(request: FastifyRequest<{ Body: UpdateGoodsInfoDto }>, reply: FastifyReply) { try { const { qrCode, maintenanceCode, ...data } = request.body if (data.photoUrl) { try { const urlObj = new URL(data.photoUrl) data.photoUrl = urlObj.pathname.substring(1) } catch (error) {} } const goodsInfo = await this.goodsInfoService.update(qrCode, maintenanceCode, data) return reply.send({ message: '物品信息更新成功', data: goodsInfo }) } catch (error) { const message = error instanceof Error ? error.message : '更新失败' return reply.code(400).send({ message }) } } async get(request: FastifyRequest<{ Querystring: { qrCode: string } }>, reply: FastifyReply) { try { const { qrCode } = request.query if (!qrCode) { return reply.code(400).send({ message: '请提供二维码参数' }) } const goodsInfo = await this.goodsInfoService.findByQrCode(qrCode) if (!goodsInfo) { return reply.code(404).send({ message: '物品信息不存在' }) } return reply.send(goodsInfo) } catch (error) { const message = error instanceof Error ? error.message : '获取失败' return reply.code(500).send({ message }) } } async adminUpdate(request: FastifyRequest<{ Body: AdminUpdateGoodsInfoDto }>, reply: FastifyReply) { try { const { qrCodeId, ...data } = request.body if (!qrCodeId) { return reply.code(400).send({ message: '请提供二维码ID' }) } if (data.photoUrl) { try { const urlObj = new URL(data.photoUrl) data.photoUrl = urlObj.pathname.substring(1) } catch (error) {} } const goodsInfo = await this.goodsInfoService.adminUpdate(qrCodeId, data) return reply.send({ message: '物品信息更新成功', data: goodsInfo }) } catch (error) { const message = error instanceof Error ? error.message : '更新失败' return reply.code(400).send({ message }) } } async list(request: FastifyRequest<{ Querystring: QueryGoodsInfoDto }>, reply: FastifyReply) { try { const { name, contactName, contactPhone, startDate, endDate, page, pageSize } = request.query const result = await this.goodsInfoService.query( name, contactName, contactPhone, startDate, endDate, page, pageSize ) return reply.send(result) } catch (error) { const message = error instanceof Error ? error.message : '查询失败' return reply.code(500).send({ message }) } } async adminGetDetail(request: FastifyRequest<{ Querystring: { qrCodeId: string } }>, reply: FastifyReply) { try { const { qrCodeId } = request.query if (!qrCodeId) { return reply.code(400).send({ message: '请提供二维码ID' }) } const goodsInfo = await this.goodsInfoService.adminGetDetail(Number(qrCodeId)) if (!goodsInfo) { return reply.code(404).send({ message: '物品信息不存在' }) } return reply.send(goodsInfo) } catch (error) { const message = error instanceof Error ? error.message : '获取失败' return reply.code(500).send({ message }) } } }