Files
services/controllers/orders.js
T

142 lines
3.9 KiB
JavaScript
Raw Normal View History

2026-05-26 13:37:55 +08:00
const { query, transaction } = require('../config/database')
const { paginate } = require('../utils/pagination')
const orderService = require('../services/orderService')
2026-05-23 14:15:45 +08:00
async function getOrders(ctx) {
2026-05-26 13:37:55 +08:00
const { page, pageSize, status } = ctx.query
let sql = 'SELECT * FROM orders WHERE 1=1'
const params = []
if (status) {
sql += ' AND status = ?'
params.push(status)
}
sql += ' ORDER BY created_at DESC'
const result = await paginate(query, sql, params, page, pageSize)
const rows = result.data || []
await orderService.attachOrderItems(rows)
2026-05-23 14:15:45 +08:00
ctx.body = {
code: 200,
2026-05-26 13:37:55 +08:00
...result
2026-05-23 14:15:45 +08:00
}
}
async function getOrderById(ctx) {
const orderId = ctx.params.id
const orders = await query('SELECT * FROM orders WHERE id = ?', [orderId])
2026-05-26 13:37:55 +08:00
2026-05-23 14:15:45 +08:00
if (orders.length > 0) {
2026-05-26 13:37:55 +08:00
const order = orders[0]
order.items = await orderService.getOrderItems(orderId)
2026-05-23 14:15:45 +08:00
ctx.body = {
code: 200,
2026-05-26 13:37:55 +08:00
data: order
2026-05-23 14:15:45 +08:00
}
} else {
ctx.body = {
code: 404,
message: '订单不存在'
}
}
}
async function createOrder(ctx) {
2026-05-26 13:37:55 +08:00
const { id, userId, totalPrice, cart, remark, customerName, customerPhone, orderType, status } = ctx.request.body
const orderId = id || `order_${Date.now()}_${Math.floor(Math.random() * 1000)}`
const userInfo = JSON.stringify({
remark: remark || '',
customerName: customerName || '',
customerPhone: customerPhone || '',
orderType: orderType || 'customer'
})
await transaction(async (conn) => {
await conn.execute(
'INSERT INTO orders (id, user_id, status, total_price, cart, user_info) VALUES (?, ?, ?, ?, ?, ?)',
[orderId, userId || null, status || 'pending', totalPrice, typeof cart === 'string' ? cart : JSON.stringify(cart), userInfo]
)
await orderService.insertOrderItems(conn, orderId, cart)
})
const orders = await query('SELECT * FROM orders WHERE id = ?', [orderId])
const created = orders[0]
created.items = await orderService.getOrderItems(orderId)
if (created.status === 'completed') {
setImmediate(() => orderService.processOrderComplete(created))
2026-05-23 14:15:45 +08:00
}
2026-05-26 13:37:55 +08:00
2026-05-23 14:15:45 +08:00
ctx.body = {
code: 200,
2026-05-26 13:37:55 +08:00
data: created
2026-05-23 14:15:45 +08:00
}
}
async function updateOrder(ctx) {
const orderId = ctx.params.id
const updates = ctx.request.body
2026-05-26 13:37:55 +08:00
2026-05-23 14:15:45 +08:00
const orders = await query('SELECT * FROM orders WHERE id = ?', [orderId])
2026-05-26 13:37:55 +08:00
2026-05-23 14:15:45 +08:00
if (orders.length > 0) {
2026-05-26 13:37:55 +08:00
const prevStatus = orders[0].status
2026-05-23 14:15:45 +08:00
const updateFields = []
const updateParams = []
2026-05-26 13:37:55 +08:00
2026-05-23 14:15:45 +08:00
if (updates.status !== undefined) {
updateFields.push('status = ?')
updateParams.push(updates.status)
}
2026-05-26 13:37:55 +08:00
const totalPrice = updates.total_price !== undefined ? updates.total_price : updates.totalPrice
if (totalPrice !== undefined) {
2026-05-23 14:15:45 +08:00
updateFields.push('total_price = ?')
2026-05-26 13:37:55 +08:00
updateParams.push(totalPrice)
2026-05-23 14:15:45 +08:00
}
if (updates.cart !== undefined) {
2026-05-26 13:37:55 +08:00
const cartStr = typeof updates.cart === 'string' ? updates.cart : JSON.stringify(updates.cart)
2026-05-23 14:15:45 +08:00
updateFields.push('cart = ?')
2026-05-26 13:37:55 +08:00
updateParams.push(cartStr)
}
if (updateFields.length > 0) {
updateParams.push(orderId)
await query(`UPDATE orders SET ${updateFields.join(', ')} WHERE id = ?`, updateParams)
}
if (updates.cart !== undefined) {
await transaction(async (conn) => {
await conn.execute('DELETE FROM order_items WHERE order_id = ?', [orderId])
await orderService.insertOrderItems(conn, orderId, updates.cart)
})
2026-05-23 14:15:45 +08:00
}
2026-05-26 13:37:55 +08:00
2026-05-23 14:15:45 +08:00
const updatedOrders = await query('SELECT * FROM orders WHERE id = ?', [orderId])
2026-05-26 13:37:55 +08:00
const completed = updatedOrders[0]
completed.items = await orderService.getOrderItems(orderId)
if (completed.status === 'completed' && prevStatus !== 'completed') {
setImmediate(() => orderService.processOrderComplete(completed))
}
2026-05-23 14:15:45 +08:00
ctx.body = {
code: 200,
2026-05-26 13:37:55 +08:00
data: completed
2026-05-23 14:15:45 +08:00
}
} else {
ctx.body = {
code: 404,
message: '订单不存在'
}
}
}
module.exports = {
getOrders,
getOrderById,
createOrder,
updateOrder
2026-05-26 13:37:55 +08:00
}