159 lines
3.4 KiB
JavaScript
159 lines
3.4 KiB
JavaScript
import { getDb } from '../db/client.js'
|
|
|
|
export function findOrderByPlatformOrderId({ provider = 'agiso', platform, shopId = '', platformOrderId }) {
|
|
return getDb().prepare(`
|
|
SELECT *
|
|
FROM orders
|
|
WHERE provider = ? AND platform = ? AND shop_id = ? AND platform_order_id = ?
|
|
LIMIT 1
|
|
`).get(provider, platform, shopId, platformOrderId) || null
|
|
}
|
|
|
|
export function createOrder(input) {
|
|
const result = getDb().prepare(`
|
|
INSERT INTO orders (
|
|
provider,
|
|
platform,
|
|
shop_id,
|
|
shop_name,
|
|
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.provider,
|
|
input.platform,
|
|
input.shopId,
|
|
input.shopName,
|
|
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
|
|
provider = ?,
|
|
platform = ?,
|
|
shop_id = ?,
|
|
shop_name = ?,
|
|
order_status = ?,
|
|
pay_status = ?,
|
|
buyer_id = ?,
|
|
buyer_name = ?,
|
|
receiver_contact = ?,
|
|
total_amount = ?,
|
|
currency = ?,
|
|
raw_payload_json = ?,
|
|
paid_at = ?,
|
|
updated_at = ?
|
|
WHERE id = ?
|
|
`).run(
|
|
input.provider,
|
|
input.platform,
|
|
input.shopId,
|
|
input.shopName,
|
|
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),
|
|
}
|
|
}
|