// routes/products.js —— 产品管理 CRUD const { pool } = require('../db') function pagination(query) { const page = Math.max(Number(query.page) || 1, 1) const pageSize = Math.min(Math.max(Number(query.pageSize) || 10, 1), 100) const offset = (page - 1) * pageSize return { page, pageSize, offset } } // GET /api/products —— 列表 async function list(req, res) { try { const { page, pageSize, offset } = pagination(req.query) const { name, type, supplier } = req.query let where = 'WHERE 1=1' const params = [] if (name) { where += ' AND name LIKE ?' params.push(`%${name}%`) } if (type) { where += ' AND type LIKE ?' params.push(`%${type}%`) } if (supplier) { where += ' AND supplier LIKE ?' params.push(`%${supplier}%`) } const [[{ total }]] = await pool.query( `SELECT COUNT(*) AS total FROM products ${where}`, params ) const [rows] = await pool.query( `SELECT * FROM products ${where} ORDER BY id DESC LIMIT ? OFFSET ?`, [...params, pageSize, offset] ) res.json({ code: 0, message: 'ok', data: { list: rows, total, page, pageSize, totalPages: Math.ceil(total / pageSize), }, }) } catch (e) { console.error('[products list] error:', e) res.status(500).json({ code: 500, message: e.message }) } } // GET /api/products/:id —— 详情 async function detail(req, res) { try { const [rows] = await pool.query('SELECT * FROM products WHERE id = ?', [req.params.id]) if (rows.length === 0) { return res.status(404).json({ code: 404, message: '产品不存在' }) } res.json({ code: 0, message: 'ok', data: rows[0] }) } catch (e) { console.error('[products detail] error:', e) res.status(500).json({ code: 500, message: e.message }) } } // POST /api/products —— 新增 async function create(req, res) { const { name, type, quantity, price, unit, specification, supplier, remark, } = req.body || {} if (!name) { return res.status(400).json({ code: 400, message: '产品名称必填' }) } try { const [result] = await pool.query( `INSERT INTO products (name, type, quantity, price, unit, specification, supplier, remark) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, [name, type || null, quantity !== undefined ? quantity : 0, price !== undefined ? price : 0.00, unit || '件', specification || null, supplier || null, remark || null] ) const [rows] = await pool.query('SELECT * FROM products WHERE id = ?', [result.insertId]) res.json({ code: 0, message: 'ok', data: rows[0] }) } catch (e) { console.error('[products create] error:', e) res.status(500).json({ code: 500, message: e.message }) } } // PUT /api/products/:id —— 更新 async function update(req, res) { const { id } = req.params const fields = [ 'name', 'type', 'quantity', 'price', 'unit', 'specification', 'supplier', 'remark', ] try { const [existing] = await pool.query('SELECT id FROM products WHERE id = ?', [id]) if (existing.length === 0) { return res.status(404).json({ code: 404, message: '产品不存在' }) } const sets = [] const params = [] for (const f of fields) { if (req.body[f] !== undefined) { sets.push(`${f} = ?`) params.push(req.body[f]) } } if (sets.length === 0) { return res.status(400).json({ code: 400, message: '没有需要更新的字段' }) } params.push(id) await pool.query(`UPDATE products SET ${sets.join(', ')} WHERE id = ?`, params) const [rows] = await pool.query('SELECT * FROM products WHERE id = ?', [id]) res.json({ code: 0, message: 'ok', data: rows[0] }) } catch (e) { console.error('[products update] error:', e) res.status(500).json({ code: 500, message: e.message }) } } // DELETE /api/products/:id —— 删除 async function remove(req, res) { const { id } = req.params try { const [existing] = await pool.query('SELECT id FROM products WHERE id = ?', [id]) if (existing.length === 0) { return res.status(404).json({ code: 404, message: '产品不存在' }) } await pool.query('DELETE FROM products WHERE id = ?', [id]) res.json({ code: 0, message: 'ok' }) } catch (e) { console.error('[products delete] error:', e) res.status(500).json({ code: 500, message: e.message }) } } module.exports = { list, detail, create, update, remove }