From 3e4b35d9e7a6de4437670c3e509c33d664853d70 Mon Sep 17 00:00:00 2001 From: renshen <1005457301@qq.com> Date: Mon, 26 Oct 2026 11:22:23 +0800 Subject: [PATCH] perf: index after-sales order number filters --- .../AfterSalesCostRowRepository.php | 103 ++++++++++++++++-- .../Business/AfterSalesCostImportTest.php | 35 ++++++ 2 files changed, 128 insertions(+), 10 deletions(-) diff --git a/modules/EcommerceFinance/Repositories/AfterSalesCostRowRepository.php b/modules/EcommerceFinance/Repositories/AfterSalesCostRowRepository.php index 3c999cfa..2c64e266 100644 --- a/modules/EcommerceFinance/Repositories/AfterSalesCostRowRepository.php +++ b/modules/EcommerceFinance/Repositories/AfterSalesCostRowRepository.php @@ -46,7 +46,7 @@ class AfterSalesCostRowRepository // 指定单号时,导入表里命中该单就完整(同单的所有明细行都在导入数据里)。 if (trim((string) ($filters['order_no'] ?? '')) !== '') { - return $this->filtered($filters)->limit(1)->exists(); + return $this->orderNumberExists($filters); } $from = trim((string) ($filters['from'] ?? '')); @@ -85,6 +85,10 @@ class AfterSalesCostRowRepository $page = max($page, 1); $limit = min(max($limit, 1), 100); + if (trim((string) ($filters['order_no'] ?? '')) !== '') { + return $this->paginateByOrderNumbers($filters, $page, $limit); + } + $records = $this->filtered($filters) ->orderByDesc('order_time') ->orderByDesc('id') @@ -247,7 +251,6 @@ class AfterSalesCostRowRepository $query = DB::table(self::TABLE); $from = trim((string) ($filters['from'] ?? '')); $to = trim((string) ($filters['to'] ?? '')); - $orderNo = trim((string) ($filters['order_no'] ?? '')); $sku = trim((string) ($filters['sku_id'] ?? '')); $period = self::fullMonthPeriod($from, $to); @@ -277,14 +280,6 @@ class AfterSalesCostRowRepository $this->applyShopFilters($query, $filters); - if ($orderNo !== '') { - $query->where(function (Builder $inner) use ($orderNo): void { - $inner->where('online_order_no', 'like', $orderNo . '%') - ->orWhere('internal_order_no', 'like', $orderNo . '%') - ->orWhere('internal_order_no', '=', $orderNo); - }); - } - $skuValues = self::batchValues($sku); if ($skuValues !== []) { $query->where(function (Builder $codes) use ($skuValues): void { @@ -303,6 +298,89 @@ class AfterSalesCostRowRepository return $query; } + /** + * 单号查询拆成线上/内部两个独立索引查询,避免 OR 让数据库扫描整个月份。 + * + * @param array $filters + * @return array{rows:list>,has_more:bool} + */ + private function paginateByOrderNumbers(array $filters, int $page, int $limit): array + { + $values = self::batchValues((string) ($filters['order_no'] ?? '')); + if ($values === []) { + return ['rows' => [], 'has_more' => false]; + } + + $baseFilters = array_replace($filters, ['order_no' => '']); + $cap = max($limit * 20, 1000); + $ids = []; + + foreach (['online_order_no', 'internal_order_no'] as $field) { + $query = $this->orderNumberBranch($baseFilters, $field, $values) + ->select('id') + ->limit($cap); + + foreach ($query->pluck('id') as $id) { + $ids[(int) $id] = true; + } + } + + if ($ids === []) { + return ['rows' => [], 'has_more' => false]; + } + + $records = DB::table(self::TABLE) + ->whereIn('id', array_keys($ids)) + ->orderByDesc('order_time') + ->orderByDesc('id') + ->forPage($page, $limit + 1) + ->get(); + $rows = []; + + foreach ($records as $record) { + $payload = json_decode((string) $record->payload, true); + $rows[] = is_array($payload) ? $payload : []; + } + + return [ + 'rows' => array_slice($rows, 0, $limit), + 'has_more' => count($rows) > $limit, + ]; + } + + /** @param array $filters */ + private function orderNumberExists(array $filters): bool + { + $values = self::batchValues((string) ($filters['order_no'] ?? '')); + if ($values === []) { + return false; + } + + $baseFilters = array_replace($filters, ['order_no' => '']); + foreach (['online_order_no', 'internal_order_no'] as $field) { + if ($this->orderNumberBranch($baseFilters, $field, $values)->limit(1)->exists()) { + return true; + } + } + + return false; + } + + /** + * @param array $filters + * @param list $values + */ + private function orderNumberBranch(array $filters, string $field, array $values): Builder + { + $query = $this->filtered($filters); + + if (count($values) === 1) { + return $query->where($field, 'like', self::likePrefix($values[0])); + } + + return $query->whereIn($field, $values); + } + /** @param array $filters */ private function legacyFilteredRange(array $filters): Builder { @@ -413,6 +491,11 @@ class AfterSalesCostRowRepository return '%' . str_replace(['!', '%', '_'], ['!!', '!%', '!_'], trim($value)) . '%'; } + private static function likePrefix(string $value): string + { + return str_replace(['!', '%', '_'], ['!!', '!%', '!_'], trim($value)) . '%'; + } + /** @return list */ private static function batchValues(string $value): array { diff --git a/tests/Feature/Business/AfterSalesCostImportTest.php b/tests/Feature/Business/AfterSalesCostImportTest.php index ea43ef08..cf77b761 100644 --- a/tests/Feature/Business/AfterSalesCostImportTest.php +++ b/tests/Feature/Business/AfterSalesCostImportTest.php @@ -126,6 +126,41 @@ class AfterSalesCostImportTest extends FeatureTestCase $this->assertFalse($partial); } + public function test_period_import_matches_online_and_internal_order_numbers(): void + { + DB::table('ops_after_sales_cost_rows')->insert(AfterSalesCostRowRepository::toRecord([ + 'shop_name' => '天猫-亿家达旗舰店', + 'online_order_no' => '5120748758529015001', + 'internal_order_no' => '8064501', + 'product_code' => 'SKU-ONLINE', + 'order_time' => '2025-03-20 10:00:00', + ], null, '2026-06')); + + $filters = [ + 'from' => '2026-06-01', + 'to' => '2026-06-30', + 'shop_names' => [], + 'shop_names_exact' => false, + 'sku_id' => '', + ]; + + $online = app(OrderDataPageData::class)->afterSalesRows( + array_replace($filters, ['order_no' => '5120748758529015001']), + 1, + 20, + ); + $internal = app(OrderDataPageData::class)->afterSalesRows( + array_replace($filters, ['order_no' => '8064501']), + 1, + 20, + ); + + $this->assertSame('imported', $online['status']['X-Caiwu-Data-Status']); + $this->assertSame('SKU-ONLINE', $online['rows'][0]['product_code']); + $this->assertSame('imported', $internal['status']['X-Caiwu-Data-Status']); + $this->assertSame('SKU-ONLINE', $internal['rows'][0]['product_code']); + } + public function test_component_rows_for_orders_reads_split_components_from_payload(): void { $repository = app(AfterSalesCostRowRepository::class); -- 2.47.2