init
This commit is contained in:
@@ -0,0 +1,228 @@
|
||||
import { getDb } from '../db/client.js'
|
||||
|
||||
export function listTasksByOrderId(orderId) {
|
||||
return getDb().prepare(`
|
||||
SELECT *
|
||||
FROM delivery_tasks
|
||||
WHERE order_id = ?
|
||||
ORDER BY id ASC
|
||||
`).all(orderId)
|
||||
}
|
||||
|
||||
export function createTask(input) {
|
||||
const result = getDb().prepare(`
|
||||
INSERT INTO delivery_tasks (
|
||||
order_id,
|
||||
order_item_id,
|
||||
platform_order_id,
|
||||
task_no,
|
||||
task_status,
|
||||
login_type,
|
||||
claim_token_id,
|
||||
reserved_cdk_id,
|
||||
browser_session_id,
|
||||
nickname,
|
||||
role_id,
|
||||
role_name,
|
||||
area,
|
||||
partition_name,
|
||||
result_code,
|
||||
result_message,
|
||||
screenshot_path,
|
||||
artifacts_json,
|
||||
last_error,
|
||||
retry_count,
|
||||
expires_at,
|
||||
claimed_at,
|
||||
role_confirmed_at,
|
||||
redeemed_at,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`).run(
|
||||
input.orderId,
|
||||
input.orderItemId,
|
||||
input.platformOrderId,
|
||||
input.taskNo,
|
||||
input.taskStatus,
|
||||
input.loginType,
|
||||
input.claimTokenId,
|
||||
input.reservedCdkId,
|
||||
input.browserSessionId,
|
||||
input.nickname,
|
||||
input.roleId,
|
||||
input.roleName,
|
||||
input.area,
|
||||
input.partitionName,
|
||||
input.resultCode,
|
||||
input.resultMessage,
|
||||
input.screenshotPath,
|
||||
input.artifactsJson,
|
||||
input.lastError,
|
||||
input.retryCount,
|
||||
input.expiresAt,
|
||||
input.claimedAt,
|
||||
input.roleConfirmedAt,
|
||||
input.redeemedAt,
|
||||
input.createdAt,
|
||||
input.updatedAt,
|
||||
)
|
||||
|
||||
return getTaskById(Number(result.lastInsertRowid))
|
||||
}
|
||||
|
||||
export function updateTask(taskId, patch) {
|
||||
const current = getTaskById(taskId)
|
||||
|
||||
if (!current) {
|
||||
return null
|
||||
}
|
||||
|
||||
const next = { ...current, ...patch }
|
||||
|
||||
getDb().prepare(`
|
||||
UPDATE delivery_tasks
|
||||
SET
|
||||
task_status = ?,
|
||||
login_type = ?,
|
||||
claim_token_id = ?,
|
||||
reserved_cdk_id = ?,
|
||||
browser_session_id = ?,
|
||||
nickname = ?,
|
||||
role_id = ?,
|
||||
role_name = ?,
|
||||
area = ?,
|
||||
partition_name = ?,
|
||||
result_code = ?,
|
||||
result_message = ?,
|
||||
screenshot_path = ?,
|
||||
artifacts_json = ?,
|
||||
last_error = ?,
|
||||
retry_count = ?,
|
||||
expires_at = ?,
|
||||
claimed_at = ?,
|
||||
role_confirmed_at = ?,
|
||||
redeemed_at = ?,
|
||||
updated_at = ?
|
||||
WHERE id = ?
|
||||
`).run(
|
||||
next.task_status,
|
||||
next.login_type,
|
||||
next.claim_token_id,
|
||||
next.reserved_cdk_id,
|
||||
next.browser_session_id,
|
||||
next.nickname,
|
||||
next.role_id,
|
||||
next.role_name,
|
||||
next.area,
|
||||
next.partition_name,
|
||||
next.result_code,
|
||||
next.result_message,
|
||||
next.screenshot_path,
|
||||
next.artifacts_json,
|
||||
next.last_error,
|
||||
next.retry_count,
|
||||
next.expires_at,
|
||||
next.claimed_at,
|
||||
next.role_confirmed_at,
|
||||
next.redeemed_at,
|
||||
next.updated_at,
|
||||
taskId,
|
||||
)
|
||||
|
||||
return getTaskById(taskId)
|
||||
}
|
||||
|
||||
export function getTaskById(taskId) {
|
||||
return getDb().prepare('SELECT * FROM delivery_tasks WHERE id = ? LIMIT 1').get(taskId) || null
|
||||
}
|
||||
|
||||
export function findTaskByClaimTokenId(claimTokenId) {
|
||||
return getDb().prepare(`
|
||||
SELECT *
|
||||
FROM delivery_tasks
|
||||
WHERE claim_token_id = ?
|
||||
LIMIT 1
|
||||
`).get(claimTokenId) || null
|
||||
}
|
||||
|
||||
export function listTasks({
|
||||
page = 1,
|
||||
pageSize = 20,
|
||||
status = '',
|
||||
platformOrderId = '',
|
||||
taskNo = '',
|
||||
skuCode = '',
|
||||
roleId = '',
|
||||
dateFrom = '',
|
||||
dateTo = '',
|
||||
} = {}) {
|
||||
const offset = (page - 1) * pageSize
|
||||
const filters = []
|
||||
const params = []
|
||||
|
||||
if (status) {
|
||||
filters.push('dt.task_status = ?')
|
||||
params.push(status)
|
||||
}
|
||||
|
||||
if (platformOrderId) {
|
||||
filters.push('dt.platform_order_id LIKE ?')
|
||||
params.push(`%${platformOrderId}%`)
|
||||
}
|
||||
|
||||
if (taskNo) {
|
||||
filters.push('dt.task_no LIKE ?')
|
||||
params.push(`%${taskNo}%`)
|
||||
}
|
||||
|
||||
if (skuCode) {
|
||||
filters.push('oi.sku_code = ?')
|
||||
params.push(skuCode)
|
||||
}
|
||||
|
||||
if (roleId) {
|
||||
filters.push('dt.role_id LIKE ?')
|
||||
params.push(`%${roleId}%`)
|
||||
}
|
||||
|
||||
if (dateFrom) {
|
||||
filters.push('dt.created_at >= ?')
|
||||
params.push(dateFrom)
|
||||
}
|
||||
|
||||
if (dateTo) {
|
||||
filters.push('dt.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 delivery_tasks dt
|
||||
LEFT JOIN order_items oi ON oi.id = dt.order_item_id
|
||||
${whereClause}
|
||||
`).get(...params)
|
||||
|
||||
const items = db.prepare(`
|
||||
SELECT
|
||||
dt.*,
|
||||
oi.sku_code,
|
||||
oi.sku_name,
|
||||
ci.cdk_code,
|
||||
ct.token AS claim_token
|
||||
FROM delivery_tasks dt
|
||||
LEFT JOIN order_items oi ON oi.id = dt.order_item_id
|
||||
LEFT JOIN cdk_inventory ci ON ci.id = dt.reserved_cdk_id
|
||||
LEFT JOIN claim_tokens ct ON ct.id = dt.claim_token_id
|
||||
${whereClause}
|
||||
ORDER BY dt.id DESC
|
||||
LIMIT ? OFFSET ?
|
||||
`).all(...params, pageSize, offset)
|
||||
|
||||
return {
|
||||
items,
|
||||
total: Number(totalRow?.total || 0),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user