diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index 5f15a937..26e6c3b1 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -268,13 +268,14 @@ class Pages extends Controller ]; $page = max((int) $request->query('page', 1), 1); $limit = min(max((int) setting('default.list_limit', 25), 1), 100); - $rows = new LengthAwarePaginator([], 0, $limit, $page, [ + $rows = new LengthAwarePaginator([], 0, $limit, 1, [ 'path' => $request->url(), 'query' => $request->query(), ]); $syncStatus = ['code' => 'unavailable', 'last_succeeded_at' => null]; $error = null; $pending = false; + $indexPending = false; try { if (! $this->nonJingxiRefunds->available()) { @@ -284,6 +285,8 @@ class Pages extends Controller $syncStatus = $this->nonJingxiRefunds->syncStatus($states); if (! $this->nonJingxiRefunds->ready()) { $pending = true; + } elseif (! $this->nonJingxiRefunds->queryReady()) { + $indexPending = true; } else { $result = $this->nonJingxiRefunds->paginate($filters, $page, $limit); $rows = new LengthAwarePaginator( @@ -307,6 +310,7 @@ class Pages extends Controller 'refundSyncStatus' => $syncStatus, 'refundError' => $error, 'refundPending' => $pending, + 'refundIndexPending' => $indexPending, ]; } diff --git a/modules/EcommerceFinance/Repositories/NonJingxiRefundMirrorRepository.php b/modules/EcommerceFinance/Repositories/NonJingxiRefundMirrorRepository.php index 4edc55f0..fe1ed510 100644 --- a/modules/EcommerceFinance/Repositories/NonJingxiRefundMirrorRepository.php +++ b/modules/EcommerceFinance/Repositories/NonJingxiRefundMirrorRepository.php @@ -24,6 +24,13 @@ class NonJingxiRefundMirrorRepository 'operations_replica.rpt.fys_shopInfo', ]; + private const REQUIRED_QUERY_INDEXES = [ + self::INFO_DATASET => [['as_date', 'Id'], ['o_id', 'Id'], ['as_id', 'Id']], + self::ITEM_DATASET => [['as_id', 'Id']], + 'operations_replica.rpt.jst_Orders' => [['o_id', 'Id']], + 'operations_replica.rpt.jst_OrderItem' => [['o_id', 'Id']], + ]; + private readonly ConnectionInterface $connection; public function __construct( @@ -48,6 +55,35 @@ class NonJingxiRefundMirrorRepository && $this->mirror->ready($this->dataset(self::ITEM_DATASET)); } + public function queryReady(): bool + { + $schema = Schema::connection($this->connection->getName()); + + try { + foreach (self::REQUIRED_QUERY_INDEXES as $datasetKey => $requiredIndexes) { + $table = $this->dataset($datasetKey)->targetTable(); + if (! $schema->hasTable($table)) { + return false; + } + + $indexes = collect($schema->getIndexes($table)) + ->map(static fn (array $index): array => array_map( + 'strtolower', + (array) ($index['columns'] ?? []), + )); + foreach ($requiredIndexes as $required) { + if (! $indexes->contains(array_map('strtolower', $required))) { + return false; + } + } + } + } catch (\Throwable) { + return false; + } + + return true; + } + /** @return array|null> */ public function states(): array { @@ -101,7 +137,7 @@ class NonJingxiRefundMirrorRepository */ public function paginate(array $filters, int $page, int $perPage): array { - if (! $this->ready()) { + if (! $this->ready() || ! $this->queryReady()) { throw new RuntimeException('business_mirror_dataset_not_ready'); } diff --git a/resources/views/business/pages/refunds/non-jingxi-refund-data.blade.php b/resources/views/business/pages/refunds/non-jingxi-refund-data.blade.php index 31402cc3..f704d874 100644 --- a/resources/views/business/pages/refunds/non-jingxi-refund-data.blade.php +++ b/resources/views/business/pages/refunds/non-jingxi-refund-data.blade.php @@ -69,6 +69,11 @@ color: var(--vip-content-accent-dark, #856632); font-size: 1.125rem; } + .upstream-data-alert--warning { + border-color: rgba(147, 95, 7, 0.35); + background: rgba(147, 95, 7, 0.06); + color: var(--vip-warning, #935f07); + }
@@ -84,6 +89,11 @@ {{ $refundError }}
+ @elseif ($refundIndexPending) +
+ + 页面查询索引正在准备,生产同步轮转完成后将自动显示数据。 +
@endif
@if ($refundPending) 首次同步尚未完成,完成后将自动显示数据。 + @elseif ($refundIndexPending) + 页面查询索引尚未就绪,当前不会执行高压力查询。 @elseif ($refundError) 数据暂时无法读取。 @else diff --git a/tests/Feature/Business/NonJingxiRefundPageTest.php b/tests/Feature/Business/NonJingxiRefundPageTest.php index 0a8a57ed..50495f43 100644 --- a/tests/Feature/Business/NonJingxiRefundPageTest.php +++ b/tests/Feature/Business/NonJingxiRefundPageTest.php @@ -32,6 +32,7 @@ class NonJingxiRefundPageTest extends FeatureTestCase 'last_succeeded_at' => '2026-08-21 12:00:00', ]); $repository->shouldReceive('ready')->once()->andReturnTrue(); + $repository->shouldReceive('queryReady')->once()->andReturnTrue(); $repository->shouldReceive('paginate')->once()->withArgs( static fn (array $filters, int $page, int $limit): bool => $filters === [ @@ -119,6 +120,7 @@ class NonJingxiRefundPageTest extends FeatureTestCase 'last_succeeded_at' => null, ]); $repository->shouldReceive('ready')->once()->andReturnFalse(); + $repository->shouldNotReceive('queryReady'); $repository->shouldNotReceive('paginate'); $this->app->instance(NonJingxiRefundMirrorRepository::class, $repository); @@ -145,6 +147,7 @@ class NonJingxiRefundPageTest extends FeatureTestCase $repository->shouldReceive('states')->once()->andReturn($states); $repository->shouldReceive('syncStatus')->once()->andReturn(['code' => 'healthy', 'last_succeeded_at' => '2026-08-21 12:00:00']); $repository->shouldReceive('ready')->once()->andReturnTrue(); + $repository->shouldReceive('queryReady')->once()->andReturnTrue(); $repository->shouldReceive('paginate')->once()->andReturn(['data' => [], 'total' => 0]); $this->app->instance(NonJingxiRefundMirrorRepository::class, $repository); @@ -158,6 +161,42 @@ class NonJingxiRefundPageTest extends FeatureTestCase ->assertSeeText('共 0 条记录'); } + public function test_page_does_not_run_the_query_before_required_indexes_exist(): void + { + $this->grantAdminPermission(); + + $states = [ + NonJingxiRefundMirrorRepository::INFO_DATASET => ['status' => 'running', 'initial_completed_at' => '2026-08-15 00:00:00'], + NonJingxiRefundMirrorRepository::ITEM_DATASET => ['status' => 'running', 'initial_completed_at' => '2026-08-15 00:00:00'], + ]; + $repository = \Mockery::mock(NonJingxiRefundMirrorRepository::class); + $repository->shouldReceive('available')->once()->andReturnTrue(); + $repository->shouldReceive('states')->once()->andReturn($states); + $repository->shouldReceive('syncStatus')->once()->with($states)->andReturn([ + 'code' => 'syncing', + 'last_succeeded_at' => '2026-08-17 10:05:07', + ]); + $repository->shouldReceive('ready')->once()->andReturnTrue(); + $repository->shouldReceive('queryReady')->once()->andReturnFalse(); + $repository->shouldNotReceive('paginate'); + $this->app->instance(NonJingxiRefundMirrorRepository::class, $repository); + + $this->loginAs(); + $url = route('business.pages.show', [ + 'section' => 'refunds', + 'page' => 'non-jingxi-refund-data', + ]).'?page=2'; + + $this->get($url) + ->assertOk() + ->assertSee('upstream-data-alert--warning', false) + ->assertSeeText('页面查询索引正在准备,生产同步轮转完成后将自动显示数据。') + ->assertSeeText('页面查询索引尚未就绪,当前不会执行高压力查询。') + ->assertSeeText('第 1 / 1 页') + ->assertDontSeeText('第 2 / 1 页') + ->assertDontSeeText('当前筛选范围暂无数据。'); + } + public function test_page_replaces_invalid_dates_with_the_default_range(): void { $this->grantAdminPermission(); @@ -175,6 +214,7 @@ class NonJingxiRefundPageTest extends FeatureTestCase 'last_succeeded_at' => '2026-08-21 12:00:00', ]); $repository->shouldReceive('ready')->once()->andReturnTrue(); + $repository->shouldReceive('queryReady')->once()->andReturnTrue(); $repository->shouldReceive('paginate')->once()->withArgs( static fn (array $filters, int $page, int $limit): bool => $filters['from'] === '2026-08-15' diff --git a/tests/Feature/Business/PagesTest.php b/tests/Feature/Business/PagesTest.php index a3aae668..38c50315 100644 --- a/tests/Feature/Business/PagesTest.php +++ b/tests/Feature/Business/PagesTest.php @@ -156,6 +156,7 @@ class PagesTest extends FeatureTestCase $repository->shouldNotReceive('states'); $repository->shouldNotReceive('syncStatus'); $repository->shouldNotReceive('ready'); + $repository->shouldNotReceive('queryReady'); $repository->shouldNotReceive('paginate'); $this->app->instance(NonJingxiRefundMirrorRepository::class, $repository); diff --git a/tests/Unit/EcommerceFinance/NonJingxiRefundMirrorRepositoryTest.php b/tests/Unit/EcommerceFinance/NonJingxiRefundMirrorRepositoryTest.php index 00a9951f..2ae7c23e 100644 --- a/tests/Unit/EcommerceFinance/NonJingxiRefundMirrorRepositoryTest.php +++ b/tests/Unit/EcommerceFinance/NonJingxiRefundMirrorRepositoryTest.php @@ -112,6 +112,24 @@ class NonJingxiRefundMirrorRepositoryTest extends TestCase $this->assertSame('initializing', $this->repository->syncStatus($states)['code']); } + public function test_query_is_blocked_when_a_required_lookup_index_is_missing(): void + { + Schema::connection('business_mirror')->table( + 'ops_jst_return_refund_info', + static fn (Blueprint $table) => $table->dropIndex(['as_date', 'Id']), + ); + + $this->assertFalse($this->repository->queryReady()); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('business_mirror_dataset_not_ready'); + $this->repository->paginate( + ['from' => '2026-08-18', 'to' => '2026-08-21', 'order_no' => '', 'shop_name' => '', 'sku_id' => ''], + 1, + 25, + ); + } + /** @return array> */ private function definitions(): array { @@ -148,20 +166,26 @@ class NonJingxiRefundMirrorRepositoryTest extends TestCase $table->decimal('buyer_apply_refund', 18, 2)->nullable(); $table->dateTime('createTime')->nullable(); $table->string('status')->nullable(); $table->string('shop_status')->nullable(); $table->dateTime('created')->nullable(); $table->dateTime('confirm_date')->nullable(); + $table->index(['as_date', 'Id']); + $table->index(['o_id', 'Id']); + $table->index(['as_id', 'Id']); }); $schema->create('ops_jst_return_refund_item', function (Blueprint $table): void { $table->integer('Id'); $table->integer('as_id'); $table->string('outer_oi_id')->nullable(); $table->string('shop_sku_id')->nullable(); $table->string('i_id')->nullable(); $table->string('sku_id')->nullable(); $table->integer('qty')->nullable(); $table->decimal('shop_amount', 18, 2)->nullable(); $table->string('type')->nullable(); $table->integer('r_qty')->nullable(); + $table->index(['as_id', 'Id']); }); $schema->create('ops_jst_orders', function (Blueprint $table): void { $table->integer('Id'); $table->integer('o_id'); $table->string('receiver_zip')->nullable(); + $table->index(['o_id', 'Id']); }); $schema->create('ops_jst_order_item', function (Blueprint $table): void { $table->integer('Id'); $table->integer('o_id'); $table->string('outer_oi_id'); $table->string('shop_i_id')->nullable(); $table->string('shop_sku_id')->nullable(); $table->string('i_id')->nullable(); $table->string('sku_id')->nullable(); $table->decimal('buyer_paid_amount', 18, 2)->nullable(); + $table->index(['o_id', 'Id']); }); $schema->create('ops_jst_sku', function (Blueprint $table): void { $table->integer('autoid'); $table->string('sku_id'); $table->string('sku_type'); $table->decimal('cost_price', 18, 2)->nullable();