优化订单售后单号查询速度 #579
@ -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<string,mixed> $filters
|
||||
* @return array{rows:list<array<string,mixed>>,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<string,mixed> $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<string,mixed> $filters
|
||||
* @param list<string> $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<string,mixed> $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<string> */
|
||||
private static function batchValues(string $value): array
|
||||
{
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user