From d7582cdb46519cbde8d712db7e16847dea576b16 Mon Sep 17 00:00:00 2001 From: chenxi Date: Wed, 7 Oct 2026 10:55:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=9A=E7=BB=A9=E6=98=8E?= =?UTF-8?q?=E7=BB=86=E5=AF=BC=E5=87=BA=EF=BC=9A=E8=A1=A5=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E7=AD=9B=E9=80=89=20+=20=E6=94=B9=E6=B8=B8=E6=A0=87=E6=B5=81?= =?UTF-8?q?=E5=BC=8F=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 导出补齐平台筛选,并把 chunk(1000) 的 OFFSET 轮询改为 cursor() 单查询流式输出。 --- app/Http/Controllers/Business/Pages.php | 44 ++++++++++++++----------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index c4d8b1ba2..d1e6c2790 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -708,6 +708,10 @@ class Pages extends Controller $query = DB::connection($connection)->table('store_receipt_order_match_rows') ->when($month !== null, static fn ($builder) => $builder->where('report_month', $month)); + $platform = trim((string) $request->query('platform', '')); + if ($platform !== '') { + $query->where('platform', $platform); + } $store = trim((string) $request->query('store_name', '')); if ($store !== '') { $query->where('store_name', 'like', '%' . addcslashes($store, '%_\\') . '%'); @@ -741,31 +745,33 @@ class Pages extends Controller $filename = '店铺收款匹配订单明细_' . ($month ?? 'all') . '.csv'; return response()->streamDownload(function () use ($query, $selected, $map): void { + // 单表几十万行:用游标一次查询顺序吐出行,避免 chunk 的 OFFSET 反复重跑 + // (29 万行时会退化成几百次排序查询,导出要几分钟)。 + @set_time_limit(0); $out = fopen('php://output', 'w'); fwrite($out, "\xEF\xBB\xBF"); fputcsv($out, array_map(static fn (string $key): string => $map[$key][1], $selected)); - $query->orderBy('occurred_date')->orderBy('store_name')->orderBy('online_order_no') - ->chunk(1000, function ($rows) use ($out, $selected, $map): void { - foreach ($rows as $row) { - $line = []; - foreach ($selected as $key) { - [$column, , $format] = $map[$key]; - $value = $row->{$map[$key][0]} ?? ''; - if ($format === 'money') { - $line[] = $value === null || $value === '' ? '' : number_format((float) $value, 2, '.', ''); - continue; - } - if ($format === 'int') { - $line[] = $value === null || $value === '' ? '' : (string) (int) $value; + $rows = $query->orderBy('occurred_date')->orderBy('store_name')->orderBy('online_order_no')->cursor(); + foreach ($rows as $row) { + $line = []; + foreach ($selected as $key) { + $format = $map[$key][2]; + $value = $row->{$map[$key][0]} ?? ''; + if ($format === 'money') { + $line[] = $value === null || $value === '' ? '' : number_format((float) $value, 2, '.', ''); - continue; - } - $line[] = is_string($value) ? $value : (string) $value; - } - fputcsv($out, $line); + continue; } - }); + if ($format === 'int') { + $line[] = $value === null || $value === '' ? '' : (string) (int) $value; + + continue; + } + $line[] = is_string($value) ? $value : (string) $value; + } + fputcsv($out, $line); + } fclose($out); }, $filename, ['Content-Type' => 'text/csv; charset=UTF-8']); }