彻底重构-4
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
import { query } from '../db/client.js'
|
||||
|
||||
export async function listTaskInventoryBindingsByTaskId(taskId) {
|
||||
const result = await query(
|
||||
`
|
||||
SELECT
|
||||
tib.id,
|
||||
tib.task_id,
|
||||
tib.inventory_item_id,
|
||||
tib.role_key,
|
||||
tib.quantity,
|
||||
tib.binding_status,
|
||||
tib.consumed_at,
|
||||
tib.released_at,
|
||||
tib.metadata_json,
|
||||
tib.created_at,
|
||||
tib.updated_at,
|
||||
ii.sku_code,
|
||||
ii.batch_no,
|
||||
ii.credential_type,
|
||||
ii.display_value,
|
||||
ii.status AS inventory_item_status,
|
||||
ii.invalid_reason
|
||||
FROM task_inventory_bindings tib
|
||||
JOIN inventory_items ii ON ii.id = tib.inventory_item_id
|
||||
WHERE tib.task_id = $1
|
||||
ORDER BY tib.id ASC
|
||||
`,
|
||||
[Number(taskId)],
|
||||
)
|
||||
|
||||
return result.rows
|
||||
}
|
||||
|
||||
export async function getTaskInventoryBindingById(bindingId) {
|
||||
const result = await query(
|
||||
`
|
||||
SELECT
|
||||
tib.id,
|
||||
tib.task_id,
|
||||
tib.inventory_item_id,
|
||||
tib.role_key,
|
||||
tib.quantity,
|
||||
tib.binding_status,
|
||||
tib.consumed_at,
|
||||
tib.released_at,
|
||||
tib.metadata_json,
|
||||
tib.created_at,
|
||||
tib.updated_at,
|
||||
ii.sku_code,
|
||||
ii.batch_no,
|
||||
ii.credential_type,
|
||||
ii.display_value,
|
||||
ii.status AS inventory_item_status,
|
||||
ii.invalid_reason
|
||||
FROM task_inventory_bindings tib
|
||||
JOIN inventory_items ii ON ii.id = tib.inventory_item_id
|
||||
WHERE tib.id = $1
|
||||
LIMIT 1
|
||||
`,
|
||||
[Number(bindingId)],
|
||||
)
|
||||
|
||||
return result.rows[0] || null
|
||||
}
|
||||
|
||||
export async function listTaskInventoryBindingSummariesByTaskIds(taskIds = []) {
|
||||
const normalizedTaskIds = Array.from(new Set((Array.isArray(taskIds) ? taskIds : [])
|
||||
.map((value) => Number(value))
|
||||
.filter((value) => Number.isFinite(value) && value > 0)))
|
||||
|
||||
if (normalizedTaskIds.length === 0) {
|
||||
return []
|
||||
}
|
||||
|
||||
const result = await query(
|
||||
`
|
||||
SELECT
|
||||
tib.task_id,
|
||||
COUNT(*)::int AS total_binding_count,
|
||||
COUNT(*) FILTER (WHERE tib.binding_status = 'reserved')::int AS reserved_binding_count,
|
||||
COUNT(*) FILTER (WHERE tib.binding_status = 'consumed')::int AS consumed_binding_count,
|
||||
COUNT(*) FILTER (WHERE tib.binding_status = 'released')::int AS released_binding_count,
|
||||
ARRAY_AGG(DISTINCT tib.role_key ORDER BY tib.role_key) AS role_keys
|
||||
FROM task_inventory_bindings tib
|
||||
WHERE tib.task_id = ANY($1::bigint[])
|
||||
GROUP BY tib.task_id
|
||||
`,
|
||||
[normalizedTaskIds],
|
||||
)
|
||||
|
||||
return result.rows
|
||||
}
|
||||
Reference in New Issue
Block a user