优化京东日账单深页查询 #202

Merged
qiuxiang merged 1 commits from fix/qiuxiang-jd-daily-deep-paging into main 2026-09-18 14:35:07 +08:00

View File

@ -18,9 +18,16 @@ class JdDailyBillRepository
$total = (int) $summary->count;
$page = max(1, $page);
$perPage = min(100, max(1, $perPage));
$rows = $this->ordered($query, $sort, $direction)->forPage($page, $perPage)->pluck('row_data');
// Skip deep pages using narrow indexed keys, then load JSON only for the visible rows.
$keys = $this->ordered($query, $sort, $direction)->forPage($page, $perPage)->get(['generation', 'record_code']);
$rows = [];
foreach ($keys->groupBy('generation') as $generation => $group) {
$records = $this->db()->table('jd_daily_bill_rows')->where('generation', $generation)
->whereIn('record_code', $group->pluck('record_code')->all())->get(['record_code', 'row_data']);
foreach ($records as $record) $rows[$generation . ':' . $record->record_code] = json_decode($record->row_data, true, 512, JSON_THROW_ON_ERROR);
}
return [
'data' => $rows->map(fn ($r) => json_decode($r, true, 512, JSON_THROW_ON_ERROR))->all(),
'data' => $keys->map(fn ($key) => $rows[$key->generation . ':' . $key->record_code])->all(),
'total' => $total, 'current_page' => $page, 'last_page' => max(1, (int) ceil($total / $perPage)), 'per_page' => $perPage,
'summary' => ['count' => $total, 'income' => round((float) $summary->income, 2), 'expense' => round((float) $summary->expense, 2),
'balance' => round((float) $summary->income - (float) $summary->expense, 2)],