fix: 退款镜像索引未就绪时保护页面查询

This commit is contained in:
苏木 2026-08-21 18:17:13 +08:00
parent eebf8b4c2e
commit 26ea2e1f0b
6 changed files with 119 additions and 2 deletions

View File

@ -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,
];
}

View File

@ -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<string,array<string,mixed>|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');
}

View File

@ -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);
}
</style>
<div class="refund-data-sync" role="status" data-sync-state="{{ $syncCode }}">
@ -84,6 +89,11 @@
<span class="material-icons-outlined" aria-hidden="true">error_outline</span>
<span>{{ $refundError }}</span>
</div>
@elseif ($refundIndexPending)
<div class="upstream-data-alert upstream-data-alert--warning" role="status">
<span class="material-icons-outlined" aria-hidden="true">hourglass_top</span>
<span>页面查询索引正在准备,生产同步轮转完成后将自动显示数据。</span>
</div>
@endif
<form
@ -162,6 +172,8 @@
<td colspan="{{ count($columns) }}">
@if ($refundPending)
首次同步尚未完成,完成后将自动显示数据。
@elseif ($refundIndexPending)
页面查询索引尚未就绪,当前不会执行高压力查询。
@elseif ($refundError)
数据暂时无法读取。
@else

View File

@ -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'

View File

@ -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);

View File

@ -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<string,array<string,mixed>> */
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();