From 388c7b75d17d8c07b581d2d78ae14d698fc0a305 Mon Sep 17 00:00:00 2001 From: denghy Date: Mon, 16 Dec 2019 17:54:07 +0800 Subject: [PATCH] querySize --- app/Domains/Virtual/Exports/CardExport.php | 22 ++++++++++++++++++- .../Http/Controllers/CardController.php | 3 ++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/Domains/Virtual/Exports/CardExport.php b/app/Domains/Virtual/Exports/CardExport.php index 04811945..3aaa4410 100644 --- a/app/Domains/Virtual/Exports/CardExport.php +++ b/app/Domains/Virtual/Exports/CardExport.php @@ -9,12 +9,13 @@ use Dipper\Excel\Concerns\FromQuery; use Dipper\Excel\Concerns\WithHeadings; use Illuminate\Database\Eloquent\Collection; use App\Domains\Virtual\Services\CardService; +use Dipper\Excel\Concerns\WithCustomQuerySize; use Dipper\Excel\Concerns\WithColumnFormatting; use PhpOffice\PhpSpreadsheet\Style\NumberFormat; use App\Domains\Virtual\Repositories\PropertyRepository; use App\Domains\Virtual\Repositories\OrderCardPartitionRepository; -class CardExport extends AbstractExport implements FromQuery, WithHeadings, WithRows, WithColumnFormatting +class CardExport extends AbstractExport implements FromQuery, WithHeadings, WithRows, WithColumnFormatting, WithCustomQuerySize { public $conditions; @@ -25,6 +26,25 @@ class CardExport extends AbstractExport implements FromQuery, WithHeadings, With parent::__construct(); } + /** + * Queued exportables are processed in chunks; each chunk being a job pushed to the queue by the QueuedWriter. + * In case of exportables that implement the FromQuery concern, the number of jobs is calculated by dividing the $query->count() by the chunk size. + * Depending on the implementation of the query() method (eg. When using a groupBy clause), this calculation might not be correct. + * + * When this is the case, you should use this method to provide a custom calculation of the query size. + * + * @return int + */ + public function querySize(): int + { + $this->conditions['type'] = 0; + + // 添加卡属性匹配查找 + CardService::propConditions($this->conditions); + + return app(OrderCardPartitionRepository::class)->skipCache()->selectRaw("COUNT(*) AS count")->withConditions($this->conditions)->first()->count; + } + public function query() { $this->conditions['type'] = 0; diff --git a/app/Domains/Virtual/Http/Controllers/CardController.php b/app/Domains/Virtual/Http/Controllers/CardController.php index 9d7a8b3c..8ecdaac3 100644 --- a/app/Domains/Virtual/Http/Controllers/CardController.php +++ b/app/Domains/Virtual/Http/Controllers/CardController.php @@ -56,7 +56,8 @@ class CardController extends Controller } $conditions['type'] = 0; - $total = app(OrderCardPartitionRepository::class)->skipCache()->withConditions($conditions)->count(); + + $total = app(OrderCardPartitionRepository::class)->skipCache()->selectRaw("COUNT(*) AS count")->withConditions($conditions)->first()->count; if ($total > 250000) { throw new NotAllowedException('一次性导出数据超过25万条,请添加筛选条件后重试');