init
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
import { getDb } from '../db/client.js'
|
||||
|
||||
export function findOrderByPlatformOrderId(platform, platformOrderId) {
|
||||
return getDb().prepare(`
|
||||
SELECT *
|
||||
FROM orders
|
||||
WHERE platform = ? AND platform_order_id = ?
|
||||
LIMIT 1
|
||||
`).get(platform, platformOrderId) || null
|
||||
}
|
||||
|
||||
export function createOrder(input) {
|
||||
const result = getDb().prepare(`
|
||||
INSERT INTO orders (
|
||||
platform,
|
||||
platform_order_id,
|
||||
order_status,
|
||||
pay_status,
|
||||
buyer_id,
|
||||
buyer_name,
|
||||
receiver_contact,
|
||||
total_amount,
|
||||
currency,
|
||||
raw_payload_json,
|
||||
paid_at,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`).run(
|
||||
input.platform,
|
||||
input.platformOrderId,
|
||||
input.orderStatus,
|
||||
input.payStatus,
|
||||
input.buyerId,
|
||||
input.buyerName,
|
||||
input.receiverContact,
|
||||
input.totalAmount,
|
||||
input.currency,
|
||||
input.rawPayloadJson,
|
||||
input.paidAt,
|
||||
input.createdAt,
|
||||
input.updatedAt,
|
||||
)
|
||||
|
||||
return getOrderById(Number(result.lastInsertRowid))
|
||||
}
|
||||
|
||||
export function updateOrder(orderId, input) {
|
||||
getDb().prepare(`
|
||||
UPDATE orders
|
||||
SET
|
||||
order_status = ?,
|
||||
pay_status = ?,
|
||||
buyer_id = ?,
|
||||
buyer_name = ?,
|
||||
receiver_contact = ?,
|
||||
total_amount = ?,
|
||||
currency = ?,
|
||||
raw_payload_json = ?,
|
||||
paid_at = ?,
|
||||
updated_at = ?
|
||||
WHERE id = ?
|
||||
`).run(
|
||||
input.orderStatus,
|
||||
input.payStatus,
|
||||
input.buyerId,
|
||||
input.buyerName,
|
||||
input.receiverContact,
|
||||
input.totalAmount,
|
||||
input.currency,
|
||||
input.rawPayloadJson,
|
||||
input.paidAt,
|
||||
input.updatedAt,
|
||||
orderId,
|
||||
)
|
||||
|
||||
return getOrderById(orderId)
|
||||
}
|
||||
|
||||
export function getOrderById(orderId) {
|
||||
return getDb().prepare('SELECT * FROM orders WHERE id = ? LIMIT 1').get(orderId) || null
|
||||
}
|
||||
|
||||
export function listOrders({
|
||||
page = 1,
|
||||
pageSize = 20,
|
||||
platformOrderId = '',
|
||||
payStatus = '',
|
||||
skuCode = '',
|
||||
dateFrom = '',
|
||||
dateTo = '',
|
||||
} = {}) {
|
||||
const offset = (page - 1) * pageSize
|
||||
const filters = []
|
||||
const params = []
|
||||
|
||||
if (platformOrderId) {
|
||||
filters.push('o.platform_order_id LIKE ?')
|
||||
params.push(`%${platformOrderId}%`)
|
||||
}
|
||||
|
||||
if (payStatus) {
|
||||
filters.push('o.pay_status = ?')
|
||||
params.push(payStatus)
|
||||
}
|
||||
|
||||
if (skuCode) {
|
||||
filters.push('EXISTS (SELECT 1 FROM order_items oi WHERE oi.order_id = o.id AND oi.sku_code = ?)')
|
||||
params.push(skuCode)
|
||||
}
|
||||
|
||||
if (dateFrom) {
|
||||
filters.push('o.created_at >= ?')
|
||||
params.push(dateFrom)
|
||||
}
|
||||
|
||||
if (dateTo) {
|
||||
filters.push('o.created_at <= ?')
|
||||
params.push(dateTo)
|
||||
}
|
||||
|
||||
const whereClause = filters.length > 0 ? `WHERE ${filters.join(' AND ')}` : ''
|
||||
const db = getDb()
|
||||
const totalRow = db.prepare(`
|
||||
SELECT COUNT(*) AS total
|
||||
FROM orders o
|
||||
${whereClause}
|
||||
`).get(...params)
|
||||
|
||||
const items = db.prepare(`
|
||||
SELECT
|
||||
o.*,
|
||||
(SELECT COUNT(*) FROM delivery_tasks dt WHERE dt.order_id = o.id) AS task_count
|
||||
FROM orders o
|
||||
${whereClause}
|
||||
ORDER BY o.id DESC
|
||||
LIMIT ? OFFSET ?
|
||||
`).all(...params, pageSize, offset)
|
||||
|
||||
return {
|
||||
items,
|
||||
total: Number(totalRow?.total || 0),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user