优化订单搜索和对账的批量取数与索引查询
This commit is contained in:
1 parent
3aebb62252
commit
cf0b0be83f
16 files changed
+671
-186
No files matched your search
@@ -0,0 +1,117 @@
|
||||
'use strict';
|
||||
|
||||
// json_each 让批量读取只绑定一次 ID 集合,不受 SQLite 参数个数限制。
|
||||
const selected = 'SELECT value FROM json_each(?)';
|
||||
|
||||
function visibleOrders(ledger, ids) {
|
||||
if (!ledger.teamMode) return ledger.rows(`SELECT * FROM orders WHERE id IN (${selected})`, ids);
|
||||
const restrictActor = ledger.actor && ledger.actor.role !== 'owner',
|
||||
member = ledger.memberFilter;
|
||||
return ledger.rows(
|
||||
`SELECT o.*,m.member FROM orders o LEFT JOIN team_entities m ON m.kind='order' AND m.entity_id=o.id
|
||||
WHERE o.id IN (${selected})${restrictActor ? ' AND m.member IS ?' : ''}${member ? ' AND m.member=?' : ''}`,
|
||||
ids,
|
||||
...(restrictActor ? [ledger.memberFor()] : []),
|
||||
...(member ? [member] : []),
|
||||
);
|
||||
}
|
||||
|
||||
function forOrders(ledger, ids, orders, { includeAudit }) {
|
||||
const records = new Map(
|
||||
orders.map(order => [
|
||||
order.id,
|
||||
{
|
||||
payments: [],
|
||||
expenses: [],
|
||||
settlements: [],
|
||||
corrections: [],
|
||||
profit: 0,
|
||||
changes: {},
|
||||
cancellations: {},
|
||||
ownership: {},
|
||||
},
|
||||
]),
|
||||
);
|
||||
for (const table of ['payments', 'expenses', 'settlements']) {
|
||||
for (const row of ledger.rows(
|
||||
`SELECT * FROM ${table} WHERE order_id IN (${selected}) ORDER BY order_id,date,created,rowid`,
|
||||
ids,
|
||||
))
|
||||
records.get(row.order_id)[table].push(row);
|
||||
}
|
||||
for (const row of ledger.rows(
|
||||
`WITH selected_orders(id) AS (${selected}), targets AS (
|
||||
SELECT order_id,id FROM payments WHERE order_id IN selected_orders
|
||||
UNION SELECT order_id,id FROM expenses WHERE order_id IN selected_orders
|
||||
UNION SELECT order_id,id FROM settlements WHERE order_id IN selected_orders
|
||||
) SELECT c.*,t.order_id FROM targets t JOIN corrections c ON c.target=t.id ORDER BY c.target`,
|
||||
ids,
|
||||
)) {
|
||||
const details = records.get(row.order_id);
|
||||
delete row.order_id;
|
||||
details.corrections.push(row);
|
||||
}
|
||||
for (const row of ledger.rows(
|
||||
`SELECT order_id,SUM(profit) AS profit FROM journal WHERE order_id IN (${selected}) GROUP BY order_id`,
|
||||
ids,
|
||||
))
|
||||
records.get(row.order_id).profit = row.profit;
|
||||
for (const row of ledger.rows(`SELECT * FROM price_locks WHERE final_order_id IN (${selected})`, ids))
|
||||
records.get(row.final_order_id).priceLock = row;
|
||||
for (const row of ledger.rows(`SELECT * FROM order_after_sales WHERE order_id IN (${selected})`, ids))
|
||||
records.get(row.order_id).orderAfterSale = row;
|
||||
for (const row of ledger.rows(`SELECT id,data,status,order_id FROM recoveries WHERE order_id IN (${selected})`, ids))
|
||||
records.get(row.order_id).recovery = row;
|
||||
for (const row of ledger.rows(
|
||||
`SELECT entity,revision,done_revision,error,updated FROM sync WHERE entity IN (SELECT 'order:'||value FROM json_each(?))`,
|
||||
ids,
|
||||
)) {
|
||||
const details = records.get(row.entity.slice(6));
|
||||
delete row.entity;
|
||||
details.sync = row;
|
||||
}
|
||||
if (ledger.teamMode) {
|
||||
for (const row of ledger.rows(
|
||||
`SELECT source_id FROM team_fund_sources WHERE kind='order' AND source_id IN (${selected})`,
|
||||
ids,
|
||||
))
|
||||
records.get(row.source_id).teamFunds = true;
|
||||
}
|
||||
for (const row of ledger.rows(
|
||||
`SELECT entity_id,rate_permille FROM credit_card_rate_snapshots WHERE kind='order' AND entity_id IN (${selected})`,
|
||||
ids,
|
||||
))
|
||||
records.get(row.entity_id).cardRate = row.rate_permille;
|
||||
for (const [table, key] of [
|
||||
['order_changes', 'changes'],
|
||||
['order_cancellations', 'cancellations'],
|
||||
['order_ownership_changes', 'ownership'],
|
||||
]) {
|
||||
for (const row of ledger.rows(
|
||||
`SELECT * FROM ${table} WHERE status IN ('pending','approved') AND order_id IN (${selected})
|
||||
ORDER BY created DESC,rowid DESC`,
|
||||
ids,
|
||||
)) {
|
||||
const detail = records.get(row.order_id),
|
||||
requests = detail[key];
|
||||
if (!requests) continue;
|
||||
// 旧作废查询只按时间排序。同毫秒有多条历史批准时,交给原查询选择。
|
||||
if (key === 'cancellations' && row.status === 'approved' && requests.approved?.created === row.created)
|
||||
detail.cancellations = undefined;
|
||||
else requests[row.status] ??= row;
|
||||
}
|
||||
}
|
||||
if (includeAudit) {
|
||||
for (const { entity, ...row } of ledger.rows(
|
||||
`SELECT entity,action,actor,detail,created FROM (
|
||||
SELECT *,ROW_NUMBER() OVER (PARTITION BY entity ORDER BY id DESC) AS sequence
|
||||
FROM audit WHERE entity IN (${selected})
|
||||
) WHERE sequence<=100 ORDER BY entity,sequence`,
|
||||
ids,
|
||||
))
|
||||
(records.get(entity).audit ??= []).push(row);
|
||||
}
|
||||
return records;
|
||||
}
|
||||
|
||||
module.exports = { visibleOrders, forOrders };
|
||||
Reference in new issue
Block a user