| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- 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 })
- }
- }
- }
|