Ai config
This commit is contained in:
+79
-35
@@ -1,22 +1,37 @@
|
||||
const { query } = require('../config/database')
|
||||
const { query, transaction } = require('../config/database')
|
||||
const { paginate } = require('../utils/pagination')
|
||||
const orderService = require('../services/orderService')
|
||||
|
||||
async function getOrders(ctx) {
|
||||
const orders = await query('SELECT * FROM orders ORDER BY created_at DESC')
|
||||
|
||||
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)
|
||||
|
||||
ctx.body = {
|
||||
code: 200,
|
||||
data: orders
|
||||
...result
|
||||
}
|
||||
}
|
||||
|
||||
async function getOrderById(ctx) {
|
||||
const orderId = ctx.params.id
|
||||
const orders = await query('SELECT * FROM orders WHERE id = ?', [orderId])
|
||||
|
||||
|
||||
if (orders.length > 0) {
|
||||
const order = orders[0]
|
||||
order.items = await orderService.getOrderItems(orderId)
|
||||
ctx.body = {
|
||||
code: 200,
|
||||
data: orders[0]
|
||||
data: order
|
||||
}
|
||||
} else {
|
||||
ctx.body = {
|
||||
@@ -27,59 +42,88 @@ async function getOrderById(ctx) {
|
||||
}
|
||||
|
||||
async function createOrder(ctx) {
|
||||
const { totalPrice, cart, userInfo } = ctx.request.body
|
||||
|
||||
const newOrder = {
|
||||
id: `order_${Date.now()}_${Math.floor(Math.random() * 1000)}`,
|
||||
status: 'pending',
|
||||
total_price: totalPrice,
|
||||
cart: typeof cart === 'string' ? cart : JSON.stringify(cart),
|
||||
user_info: typeof userInfo === 'string' ? userInfo : JSON.stringify(userInfo || {})
|
||||
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))
|
||||
}
|
||||
|
||||
await query(
|
||||
'INSERT INTO orders (id, status, total_price, cart, user_info) VALUES (?, ?, ?, ?, ?)',
|
||||
[newOrder.id, newOrder.status, newOrder.total_price, newOrder.cart, newOrder.user_info]
|
||||
)
|
||||
|
||||
|
||||
ctx.body = {
|
||||
code: 200,
|
||||
data: newOrder
|
||||
data: created
|
||||
}
|
||||
}
|
||||
|
||||
async function updateOrder(ctx) {
|
||||
const orderId = ctx.params.id
|
||||
const updates = ctx.request.body
|
||||
|
||||
|
||||
const orders = await query('SELECT * FROM orders WHERE id = ?', [orderId])
|
||||
|
||||
|
||||
if (orders.length > 0) {
|
||||
const prevStatus = orders[0].status
|
||||
const updateFields = []
|
||||
const updateParams = []
|
||||
|
||||
|
||||
if (updates.status !== undefined) {
|
||||
updateFields.push('status = ?')
|
||||
updateParams.push(updates.status)
|
||||
}
|
||||
if (updates.total_price !== undefined) {
|
||||
const totalPrice = updates.total_price !== undefined ? updates.total_price : updates.totalPrice
|
||||
if (totalPrice !== undefined) {
|
||||
updateFields.push('total_price = ?')
|
||||
updateParams.push(updates.total_price)
|
||||
updateParams.push(totalPrice)
|
||||
}
|
||||
if (updates.cart !== undefined) {
|
||||
const cartStr = typeof updates.cart === 'string' ? updates.cart : JSON.stringify(updates.cart)
|
||||
updateFields.push('cart = ?')
|
||||
updateParams.push(typeof updates.cart === 'string' ? updates.cart : JSON.stringify(updates.cart))
|
||||
updateParams.push(cartStr)
|
||||
}
|
||||
|
||||
updateParams.push(orderId)
|
||||
|
||||
await query(`UPDATE orders SET ${updateFields.join(', ')} WHERE id = ?`, updateParams)
|
||||
|
||||
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
||||
const updatedOrders = await query('SELECT * FROM orders WHERE id = ?', [orderId])
|
||||
|
||||
const completed = updatedOrders[0]
|
||||
completed.items = await orderService.getOrderItems(orderId)
|
||||
|
||||
if (completed.status === 'completed' && prevStatus !== 'completed') {
|
||||
setImmediate(() => orderService.processOrderComplete(completed))
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
code: 200,
|
||||
data: updatedOrders[0]
|
||||
data: completed
|
||||
}
|
||||
} else {
|
||||
ctx.body = {
|
||||
@@ -94,4 +138,4 @@ module.exports = {
|
||||
getOrderById,
|
||||
createOrder,
|
||||
updateOrder
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user