2026-06-26 09:44:54 +08:00
|
|
|
|
// routes/afterSales.js —— 售后管理 CRUD(纯数据库操作,权限由中间件层控制)
|
2026-06-22 20:48:29 +08:00
|
|
|
|
const { pool } = require('../db')
|
|
|
|
|
|
|
2026-06-25 21:18:13 +08:00
|
|
|
|
// 格式化日期为 YYYY-MM-DD
|
|
|
|
|
|
function formatDate(dateStr) {
|
|
|
|
|
|
if (!dateStr) return null
|
|
|
|
|
|
if (typeof dateStr === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(dateStr)) {
|
|
|
|
|
|
return dateStr
|
|
|
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
|
|
|
const date = new Date(dateStr)
|
|
|
|
|
|
if (isNaN(date.getTime())) return null
|
|
|
|
|
|
const year = date.getFullYear()
|
|
|
|
|
|
const month = String(date.getMonth() + 1).padStart(2, '0')
|
|
|
|
|
|
const day = String(date.getDate()).padStart(2, '0')
|
|
|
|
|
|
return `${year}-${month}-${day}`
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
return null
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-22 20:48:29 +08:00
|
|
|
|
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 }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const LIST_SELECT = `
|
|
|
|
|
|
SELECT
|
|
|
|
|
|
a.*,
|
|
|
|
|
|
cu.name AS customer_name,
|
|
|
|
|
|
e.name AS employee_name
|
|
|
|
|
|
FROM after_sales a
|
|
|
|
|
|
LEFT JOIN customers cu ON a.customer_id = cu.id
|
|
|
|
|
|
LEFT JOIN employees e ON a.employee_id = e.id
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
// GET /api/after-sales —— 列表
|
|
|
|
|
|
async function list(req, res) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { page, pageSize, offset } = pagination(req.query)
|
2026-06-25 21:18:13 +08:00
|
|
|
|
const { handle_status, customer_name, id, customer_id, feedback } = req.query
|
2026-06-22 20:48:29 +08:00
|
|
|
|
|
|
|
|
|
|
let where = 'WHERE 1=1'
|
|
|
|
|
|
const params = []
|
|
|
|
|
|
|
2026-06-26 09:44:54 +08:00
|
|
|
|
// 数据范围过滤(由中间件注入 req.scope,已含表别名前缀)
|
|
|
|
|
|
if (req.scope && req.scope.sql) {
|
|
|
|
|
|
where += ' ' + req.scope.sql
|
|
|
|
|
|
params.push(...req.scope.params)
|
2026-06-24 22:44:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-25 21:18:13 +08:00
|
|
|
|
if (id) {
|
|
|
|
|
|
where += ' AND a.id = ?'
|
|
|
|
|
|
params.push(id)
|
|
|
|
|
|
}
|
|
|
|
|
|
if (customer_id) {
|
|
|
|
|
|
where += ' AND a.customer_id = ?'
|
|
|
|
|
|
params.push(customer_id)
|
|
|
|
|
|
}
|
|
|
|
|
|
if (feedback) {
|
|
|
|
|
|
where += ' AND a.feedback LIKE ?'
|
|
|
|
|
|
params.push(`%${feedback}%`)
|
|
|
|
|
|
}
|
2026-06-22 20:48:29 +08:00
|
|
|
|
if (handle_status) {
|
|
|
|
|
|
where += ' AND a.handle_status = ?'
|
|
|
|
|
|
params.push(handle_status)
|
|
|
|
|
|
}
|
|
|
|
|
|
if (customer_name) {
|
|
|
|
|
|
where += ' AND cu.name LIKE ?'
|
|
|
|
|
|
params.push(`%${customer_name}%`)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const [[{ total }]] = await pool.query(
|
|
|
|
|
|
`SELECT COUNT(*) AS total FROM after_sales a
|
|
|
|
|
|
LEFT JOIN customers cu ON a.customer_id = cu.id
|
|
|
|
|
|
LEFT JOIN employees e ON a.employee_id = e.id
|
|
|
|
|
|
${where}`,
|
|
|
|
|
|
params
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const [rows] = await pool.query(
|
|
|
|
|
|
`${LIST_SELECT} ${where} ORDER BY a.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('[after-sales list] error:', e)
|
|
|
|
|
|
res.status(500).json({ code: 500, message: e.message })
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GET /api/after-sales/:id —— 详情
|
|
|
|
|
|
async function detail(req, res) {
|
|
|
|
|
|
try {
|
2026-06-24 22:44:14 +08:00
|
|
|
|
let sql = `${LIST_SELECT} WHERE a.id = ?`
|
|
|
|
|
|
const params = [req.params.id]
|
|
|
|
|
|
|
2026-06-26 09:44:54 +08:00
|
|
|
|
// 数据范围过滤(由中间件注入 req.scope,已含表别名前缀)
|
|
|
|
|
|
if (req.scope && req.scope.sql) {
|
|
|
|
|
|
sql += ' ' + req.scope.sql
|
|
|
|
|
|
params.push(...req.scope.params)
|
2026-06-24 22:44:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const [rows] = await pool.query(sql, params)
|
2026-06-22 20:48:29 +08:00
|
|
|
|
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('[after-sales detail] error:', e)
|
|
|
|
|
|
res.status(500).json({ code: 500, message: e.message })
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// POST /api/after-sales —— 新增
|
|
|
|
|
|
async function create(req, res) {
|
|
|
|
|
|
const {
|
|
|
|
|
|
customer_id, feedback, employee_id, handle_method,
|
2026-06-26 09:44:54 +08:00
|
|
|
|
handle_status, service_date, remark,
|
2026-06-22 20:48:29 +08:00
|
|
|
|
} = req.body || {}
|
|
|
|
|
|
|
|
|
|
|
|
if (!customer_id) {
|
|
|
|
|
|
return res.status(400).json({ code: 400, message: '客户ID必填' })
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!feedback) {
|
|
|
|
|
|
return res.status(400).json({ code: 400, message: '售后反馈内容必填' })
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 校验客户存在
|
|
|
|
|
|
const [cust] = await pool.query('SELECT id FROM customers WHERE id = ?', [customer_id])
|
|
|
|
|
|
if (cust.length === 0) {
|
|
|
|
|
|
return res.status(400).json({ code: 400, message: '关联客户不存在' })
|
|
|
|
|
|
}
|
2026-06-26 09:44:54 +08:00
|
|
|
|
// 校验业务员,并通过 employee_id 自动推导负责人
|
|
|
|
|
|
let ownerId = req.user.id
|
2026-06-22 20:48:29 +08:00
|
|
|
|
if (employee_id) {
|
|
|
|
|
|
const [emp] = await pool.query('SELECT id FROM employees WHERE id = ?', [employee_id])
|
|
|
|
|
|
if (emp.length === 0) {
|
|
|
|
|
|
return res.status(400).json({ code: 400, message: '关联业务员不存在' })
|
|
|
|
|
|
}
|
2026-06-26 09:44:54 +08:00
|
|
|
|
// 业务员即负责人:查找该员工对应的系统用户
|
|
|
|
|
|
const [userRows] = await pool.query('SELECT id FROM users WHERE employee_id = ? LIMIT 1', [employee_id])
|
|
|
|
|
|
if (userRows.length > 0) {
|
|
|
|
|
|
ownerId = userRows[0].id
|
|
|
|
|
|
}
|
2026-06-22 20:48:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const [result] = await pool.query(
|
|
|
|
|
|
`INSERT INTO after_sales
|
2026-06-24 22:44:14 +08:00
|
|
|
|
(customer_id, feedback, employee_id, handle_method, handle_status, service_date, remark, responsible_user_id)
|
|
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
2026-06-22 20:48:29 +08:00
|
|
|
|
[customer_id, feedback,
|
|
|
|
|
|
employee_id || null,
|
|
|
|
|
|
handle_method || null,
|
|
|
|
|
|
handle_status || '待处理',
|
2026-06-25 21:18:13 +08:00
|
|
|
|
formatDate(service_date),
|
2026-06-24 22:44:14 +08:00
|
|
|
|
remark || null,
|
|
|
|
|
|
ownerId]
|
2026-06-22 20:48:29 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const [rows] = await pool.query(`${LIST_SELECT} WHERE a.id = ?`, [result.insertId])
|
|
|
|
|
|
res.json({ code: 0, message: 'ok', data: rows[0] })
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error('[after-sales create] error:', e)
|
|
|
|
|
|
res.status(500).json({ code: 500, message: e.message })
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// PUT /api/after-sales/:id —— 更新
|
|
|
|
|
|
async function update(req, res) {
|
|
|
|
|
|
const { id } = req.params
|
|
|
|
|
|
const fields = [
|
|
|
|
|
|
'customer_id', 'feedback', 'employee_id', 'handle_method',
|
2026-06-24 22:44:14 +08:00
|
|
|
|
'handle_status', 'service_date', 'remark', 'responsible_user_id',
|
2026-06-22 20:48:29 +08:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
2026-06-24 22:44:14 +08:00
|
|
|
|
// 确认记录存在且在数据范围内
|
2026-06-26 09:44:54 +08:00
|
|
|
|
let checkSql = 'SELECT id, employee_id FROM after_sales WHERE id = ?'
|
|
|
|
|
|
let checkParams = [id]
|
|
|
|
|
|
if (req.scope && req.scope.sql) {
|
|
|
|
|
|
checkSql += ' ' + req.scope.sql
|
|
|
|
|
|
checkParams.push(...req.scope.params)
|
2026-06-24 22:44:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
const [existing] = await pool.query(checkSql, checkParams)
|
2026-06-22 20:48:29 +08:00
|
|
|
|
if (existing.length === 0) {
|
2026-06-24 22:44:14 +08:00
|
|
|
|
return res.status(404).json({ code: 404, message: '售后记录不存在或无权操作' })
|
2026-06-22 20:48:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (req.body.customer_id) {
|
|
|
|
|
|
const [cust] = await pool.query('SELECT id FROM customers WHERE id = ?', [req.body.customer_id])
|
|
|
|
|
|
if (cust.length === 0) {
|
|
|
|
|
|
return res.status(400).json({ code: 400, message: '关联客户不存在' })
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (req.body.employee_id) {
|
|
|
|
|
|
const [emp] = await pool.query('SELECT id FROM employees WHERE id = ?', [req.body.employee_id])
|
|
|
|
|
|
if (emp.length === 0) {
|
|
|
|
|
|
return res.status(400).json({ code: 400, message: '关联业务员不存在' })
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-26 09:44:54 +08:00
|
|
|
|
// 业务员即负责人:当 employee_id 变更时自动推导 responsible_user_id
|
|
|
|
|
|
if (req.body.employee_id !== undefined) {
|
|
|
|
|
|
if (req.body.employee_id) {
|
|
|
|
|
|
const [userRows] = await pool.query('SELECT id FROM users WHERE employee_id = ? LIMIT 1', [req.body.employee_id])
|
|
|
|
|
|
if (userRows.length > 0) {
|
|
|
|
|
|
req.body.responsible_user_id = userRows[0].id
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 该员工无对应系统用户时,设为当前登录用户
|
|
|
|
|
|
req.body.responsible_user_id = req.user.id
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 业务员被清空时,清空负责人
|
|
|
|
|
|
req.body.responsible_user_id = null
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-22 20:48:29 +08:00
|
|
|
|
const sets = []
|
|
|
|
|
|
const params = []
|
|
|
|
|
|
for (const f of fields) {
|
|
|
|
|
|
if (req.body[f] !== undefined) {
|
|
|
|
|
|
sets.push(`${f} = ?`)
|
2026-06-25 21:18:13 +08:00
|
|
|
|
// 日期字段需要格式化
|
|
|
|
|
|
if (f === 'service_date') {
|
|
|
|
|
|
params.push(formatDate(req.body[f]))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
params.push(req.body[f])
|
|
|
|
|
|
}
|
2026-06-22 20:48:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (sets.length === 0) {
|
|
|
|
|
|
return res.status(400).json({ code: 400, message: '没有需要更新的字段' })
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
params.push(id)
|
|
|
|
|
|
await pool.query(`UPDATE after_sales SET ${sets.join(', ')} WHERE id = ?`, params)
|
|
|
|
|
|
|
|
|
|
|
|
const [rows] = await pool.query(`${LIST_SELECT} WHERE a.id = ?`, [id])
|
|
|
|
|
|
res.json({ code: 0, message: 'ok', data: rows[0] })
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error('[after-sales update] error:', e)
|
|
|
|
|
|
res.status(500).json({ code: 500, message: e.message })
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DELETE /api/after-sales/:id —— 删除
|
|
|
|
|
|
async function remove(req, res) {
|
|
|
|
|
|
const { id } = req.params
|
|
|
|
|
|
try {
|
2026-06-24 22:44:14 +08:00
|
|
|
|
let checkSql = 'SELECT id FROM after_sales WHERE id = ?'
|
2026-06-26 09:44:54 +08:00
|
|
|
|
let checkParams = [id]
|
|
|
|
|
|
if (req.scope && req.scope.sql) {
|
|
|
|
|
|
checkSql += ' ' + req.scope.sql
|
|
|
|
|
|
checkParams.push(...req.scope.params)
|
2026-06-24 22:44:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
const [existing] = await pool.query(checkSql, checkParams)
|
2026-06-22 20:48:29 +08:00
|
|
|
|
if (existing.length === 0) {
|
2026-06-24 22:44:14 +08:00
|
|
|
|
return res.status(404).json({ code: 404, message: '售后记录不存在或无权操作' })
|
2026-06-22 20:48:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
await pool.query('DELETE FROM after_sales WHERE id = ?', [id])
|
|
|
|
|
|
res.json({ code: 0, message: 'ok' })
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error('[after-sales delete] error:', e)
|
|
|
|
|
|
res.status(500).json({ code: 500, message: e.message })
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = { list, detail, create, update, remove }
|