优化订单搜索和对账的批量取数与索引查询

This commit is contained in:
黄健铭 committed 2026-12-05 19:15:04 -08:00
1 parent 3aebb62252
commit cf0b0be83f
16 files changed
+671 -186

No files matched your search

+26 -1
View File
@@ -19,6 +19,9 @@ function target(ledger, kind, id) {
function projection(ledger, kind, id) {
const t = target(ledger, kind, id),
row = ledger.one('SELECT * FROM document_print_status WHERE kind=? AND id=?', t.kind, t.id);
return printStatus(t, row);
}
function printStatus(t, row) {
return {
...t,
printed: !!row?.printed,
@@ -27,6 +30,28 @@ function projection(ledger, kind, id) {
actor: row?.actor || null,
};
}
function displayRows(ledger, rows) {
const documentKind = row => (row.kind === 'recovery' ? 'recovery' : 'order');
const printable = rows.filter(row => !['afterSale', 'orderAfterSale'].includes(row.kind));
if (!printable.length) return rows;
// 按列表下标回填,让售后行留在原位,置换订单仍读取关联回收单的打印登记。
const records = ledger.rows(
`WITH inputs AS (
SELECT key AS position,value->>0 AS kind,value->>1 AS id FROM json_each(?)
), targets AS (
SELECT i.position,CASE WHEN r.id IS NULL THEN i.kind ELSE 'recovery' END AS kind,
COALESCE(r.id,i.id) AS id
FROM inputs i LEFT JOIN recoveries r ON i.kind='order' AND r.order_id=i.id
)
SELECT t.position,t.kind,t.id,p.printed,p.revision,p.updated,p.actor FROM targets t
LEFT JOIN document_print_status p ON p.kind=t.kind AND p.id=t.id`,
JSON.stringify(printable.map(row => [documentKind(row), row.id])),
);
const statuses = new Map(
records.map(({ position, kind, id, ...row }) => [printable[position], printStatus({ kind, id }, row)]),
);
return rows.map(row => (statuses.has(row) ? { ...row, printStatus: statuses.get(row) } : row));
}
function access(ledger, kind, id, actor) {
if (!actor || !['owner', 'assistant'].includes(actor.role)) fail('请先登录', 401);
if (!['order', 'recovery'].includes(kind)) fail('打印单据类型无效');
@@ -73,4 +98,4 @@ function decorate(html, ledger, kind, id, actor) {
const toolbar = `<span id="document-print-state" data-state="${escape(JSON.stringify(data))}" role="status">${state.printed ? '✓ 已打印' : '未打印'}</span><span id="document-print-error" role="alert"></span><button id="retry-document-print" type="button" hidden>重试保存打印记录</button>`;
return html.replace('<button id="print"', toolbar + '<button id="print"');
}
module.exports = { initialize, target, projection, access, read, update, decorate };
module.exports = { initialize, target, projection, displayRows, access, read, update, decorate };