63 lines
2.1 KiB
TypeScript
63 lines
2.1 KiB
TypeScript
import { Request, Response } from 'express'
|
|
import pool from '../utils/db'
|
|
import { RowDataPacket } from 'mysql2'
|
|
|
|
// GET /api/molds - 版房列表(支持搜索)
|
|
export async function getMolds(req: Request, res: Response): Promise<void> {
|
|
try {
|
|
const keyword = req.query.keyword as string | undefined
|
|
const page = Math.max(1, Number(req.query.page) || 1)
|
|
const pageSize = Math.min(50, Math.max(1, Number(req.query.pageSize) || 10))
|
|
const offset = (page - 1) * pageSize
|
|
|
|
let where = ''
|
|
const params: any[] = []
|
|
|
|
if (keyword && keyword.trim()) {
|
|
const kw = `%${keyword.trim()}%`
|
|
where = 'WHERE name LIKE ? OR style_no LIKE ? OR barcode_no LIKE ? OR style LIKE ?'
|
|
params.push(kw, kw, kw, kw)
|
|
}
|
|
|
|
const [countRows] = await pool.execute<RowDataPacket[]>(
|
|
`SELECT COUNT(*) as total FROM mold_infos ${where}`,
|
|
params
|
|
)
|
|
const total = countRows[0].total
|
|
|
|
const [rows] = await pool.execute<RowDataPacket[]>(
|
|
`SELECT id, name, style_no AS styleNo, barcode_no AS barcodeNo, style, images,
|
|
created_at AS createdAt, updated_at AS updatedAt
|
|
FROM mold_infos ${where}
|
|
ORDER BY id DESC LIMIT ? OFFSET ?`,
|
|
[...params, String(pageSize), String(offset)]
|
|
)
|
|
|
|
res.json({ code: 0, data: { list: rows, total, page, pageSize } })
|
|
} catch (err) {
|
|
console.error('getMolds error:', err)
|
|
res.status(500).json({ code: 500, message: '获取版房列表失败' })
|
|
}
|
|
}
|
|
|
|
// GET /api/molds/:id - 版房详情
|
|
export async function getMoldDetail(req: Request, res: Response): Promise<void> {
|
|
try {
|
|
const { id } = req.params
|
|
const [rows] = await pool.execute<RowDataPacket[]>(
|
|
'SELECT id, name, style_no AS styleNo, barcode_no AS barcodeNo, style, images, created_at AS createdAt, updated_at AS updatedAt FROM mold_infos WHERE id = ?',
|
|
[id]
|
|
)
|
|
|
|
if (rows.length === 0) {
|
|
res.status(404).json({ code: 404, message: '版房信息不存在' })
|
|
return
|
|
}
|
|
|
|
res.json({ code: 0, data: rows[0] })
|
|
} catch (err) {
|
|
console.error('getMoldDetail error:', err)
|
|
res.status(500).json({ code: 500, message: '获取版房详情失败' })
|
|
}
|
|
}
|