瞪眼法测试通过

This commit is contained in:
Kyaru
2026-06-25 21:18:13 +08:00
parent b9266dfc1f
commit 67c9330659
11 changed files with 919 additions and 49 deletions

View File

@@ -14,7 +14,7 @@ function pagination(query) {
async function list(req, res) {
try {
const { page, pageSize, offset } = pagination(req.query)
const { name, phone, province, city } = req.query
const { name, phone, province, city, id } = req.query
let where = 'WHERE 1=1'
const params = []
@@ -29,32 +29,40 @@ async function list(req, res) {
params.push(...scope.values)
}
if (id) {
where += ' AND c.id = ?'
params.push(id)
}
if (name) {
where += ' AND name LIKE ?'
where += ' AND c.name LIKE ?'
params.push(`%${name}%`)
}
if (phone) {
where += ' AND phone LIKE ?'
where += ' AND c.phone LIKE ?'
params.push(`%${phone}%`)
}
if (province) {
where += ' AND province = ?'
where += ' AND c.province = ?'
params.push(province)
}
if (city) {
where += ' AND city = ?'
where += ' AND c.city = ?'
params.push(city)
}
// 查总数
const [[{ total }]] = await pool.query(
`SELECT COUNT(*) AS total FROM customers ${where}`,
`SELECT COUNT(*) AS total FROM customers c ${where}`,
params
)
// 查分页数据
// 查分页数据JOIN users+employees 获取负责人姓名)
const [rows] = await pool.query(
`SELECT * FROM customers ${where} ORDER BY id DESC LIMIT ? OFFSET ?`,
`SELECT c.*, e.name AS responsible_user_name
FROM customers c
LEFT JOIN users u ON c.responsible_user_id = u.id
LEFT JOIN employees e ON u.employee_id = e.id
${where} ORDER BY c.id DESC LIMIT ? OFFSET ?`,
[...params, pageSize, offset]
)
@@ -201,6 +209,9 @@ async function remove(req, res) {
res.json({ code: 0, message: 'ok' })
} catch (e) {
console.error('[customers delete] error:', e)
if (e.code === 'ER_ROW_IS_REFERENCED_2') {
return res.status(400).json({ code: 400, message: '该客户被合同或售后记录引用,无法删除' })
}
res.status(500).json({ code: 500, message: e.message })
}
}