修复业绩明细导出:补平台筛选 + 改游标流式输出
导出补齐平台筛选,并把 chunk(1000) 的 OFFSET 轮询改为 cursor() 单查询流式输出。
This commit is contained in:
parent
2341a839f3
commit
d7582cdb46
@ -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']);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user