fix: 避免OA付款补查被接口额度饿死

This commit is contained in:
yueyang 2026-10-28 17:41:07 +08:00
parent dba1eb3528
commit 7048910e01
2 changed files with 256 additions and 32 deletions

View File

@ -25,6 +25,8 @@ class DingProcessDetailMirrorSynchronizer
private const APPROVAL_API_FALLBACK_LIMIT = 50;
private const APPROVAL_API_BACKLOG_BATCH_LIMIT = 25;
private readonly ConnectionInterface $connection;
private int $approvalApiAttempts = 0;
@ -367,13 +369,30 @@ class DingProcessDetailMirrorSynchronizer
$cursor = $this->stateCursor($state);
$lastSchemeId = max(0, (int) ($cursor['scheme_high_watermark'] ?? 0));
$result = $this->extractWorkflows(
$apiCursor = (string) ($cursor['approval_api_cursor'] ?? '');
// 先消化历史待核实流程,避免本轮变更流程耗尽接口额度后补查游标永远不前进。
$backlogIds = $this->approvalApiFallbackCandidates(
$apiCursor,
[],
self::APPROVAL_API_BACKLOG_BATCH_LIMIT,
);
$result = $backlogIds === []
? null
: $this->extractApprovalApiWorkflows($backlogIds, $now);
if ($backlogIds !== []) {
$apiCursor = (string) $backlogIds[array_key_last($backlogIds)];
}
$incremental = $this->extractWorkflows(
$where,
$bindings,
$cursorExpression,
'sync_cursor',
$now,
);
$result = $result === null
? $incremental
: $this->mergeExtracted($result, $incremental);
if ($lastSchemeId > 0) {
$changedProcessIds = $this->schemeChangedProcessIds(
@ -395,23 +414,15 @@ class DingProcessDetailMirrorSynchronizer
}
}
$apiCursor = (string) ($cursor['approval_api_cursor'] ?? '');
$fallbackIds = $this->approvalApiFallbackCandidates(
$apiCursor,
$result['process_instance_ids'],
);
if ($fallbackIds !== []) {
foreach (array_chunk($fallbackIds, 25) as $chunk) {
$placeholders = implode(',', array_fill(0, count($chunk), '?'));
$fallbackResult = $this->extractWorkflows(
"w.processInstanceId IN ({$placeholders})",
$chunk,
'w.processInstanceId',
'processInstanceId',
$now,
);
$result = $this->mergeExtracted($result, $fallbackResult);
}
$result = $this->mergeExtracted(
$result,
$this->extractApprovalApiWorkflows($fallbackIds, $now),
);
$apiCursor = (string) $fallbackIds[array_key_last($fallbackIds)];
}
@ -421,17 +432,58 @@ class DingProcessDetailMirrorSynchronizer
return $result;
}
/**
* @param list<string> $processInstanceIds
* @return array{
* details:list<array<string,mixed>>,
* payments:list<array<string,mixed>>,
* process_instance_ids:list<string>,
* metrics:array<string,mixed>
* }
*/
private function extractApprovalApiWorkflows(array $processInstanceIds, Carbon $now): array
{
$result = [
'details' => [],
'payments' => [],
'process_instance_ids' => [],
'metrics' => [
'workflows' => 0,
'details' => 0,
'payments' => 0,
'amount_total' => 0.0,
'bank_amount_total' => 0.0,
],
];
foreach (array_chunk($processInstanceIds, 25) as $chunk) {
$placeholders = implode(',', array_fill(0, count($chunk), '?'));
$batch = $this->extractWorkflows(
"w.processInstanceId IN ({$placeholders})",
$chunk,
'w.processInstanceId',
'processInstanceId',
$now,
);
$result = $this->mergeExtracted($result, $batch);
}
return $result;
}
/**
* Rotate through approvals whose payment component is absent from the
* replica. The cursor prevents one repeatedly failing approval from
* blocking the rest of the backlog.
*
* @param list<string> $excludedProcessInstanceIds
* @param int|null $maxCandidates
* @return list<string>
*/
private function approvalApiFallbackCandidates(
string $afterProcessInstanceId,
array $excludedProcessInstanceIds,
?int $maxCandidates = null,
): array {
if (! $this->approvalClient->isConfigured()
|| ! Schema::connection($this->connection->getName())->hasColumn(
@ -448,18 +500,25 @@ class DingProcessDetailMirrorSynchronizer
if (! $this->approvalApiAvailable || $remainingAttempts <= 0) {
return [];
}
$candidateLimit = $maxCandidates === null
? $remainingAttempts
: min($remainingAttempts, max(0, $maxCandidates));
if ($candidateLimit <= 0) {
return [];
}
$cashierStaleBefore = now()->subMinutes(15);
$base = $this->connection->table('ding_process_detail_mirror')
->where('source_scope', $this->scope())
->whereIn('status', ['审批中', '已完成'])
->where(function ($query) use ($cashierRefreshEnabled): void {
->where(function ($query) use ($cashierRefreshEnabled, $cashierStaleBefore): void {
$query->where('payment_source_state', 'missing');
if ($cashierRefreshEnabled) {
$query->orWhere(function ($cashier): void {
$query->orWhere(function ($cashier) use ($cashierStaleBefore): void {
$cashier->where('status', '审批中')
->where(function ($checked): void {
->where(function ($checked) use ($cashierStaleBefore): void {
$checked->whereNull('cashier_checked_at')
->orWhere('cashier_checked_at', '<', now()->subMinutes(15));
->orWhere('cashier_checked_at', '<', $cashierStaleBefore);
});
});
}
@ -475,7 +534,7 @@ class DingProcessDetailMirrorSynchronizer
)
->distinct()
->orderBy('process_instance_id')
->limit($remainingAttempts * 2)
->limit($candidateLimit * 2)
->pluck('process_instance_id')
->map(static fn (mixed $value): string => trim((string) $value))
->filter()
@ -490,7 +549,7 @@ class DingProcessDetailMirrorSynchronizer
$ids = (clone $base)
->distinct()
->orderBy('process_instance_id')
->limit($remainingAttempts * 2)
->limit($candidateLimit * 2)
->pluck('process_instance_id')
->map(static fn (mixed $value): string => trim((string) $value))
->filter()
@ -502,7 +561,7 @@ class DingProcessDetailMirrorSynchronizer
));
}
return array_slice($ids, 0, $remainingAttempts);
return array_slice($ids, 0, $candidateLimit);
}
/**

View File

@ -488,6 +488,115 @@ class DingProcessDetailMirrorSynchronizerTest extends TestCase
);
}
public function test_incremental_sync_reserves_api_quota_for_cashier_backlog(): void
{
$connection = DB::connection('business_mirror');
$now = now();
$backlog = [];
$bulk = [];
for ($index = 0; $index < 30; $index++) {
$processInstanceId = sprintf('backlog-%03d', $index);
$businessId = sprintf('20261022%09d', 100000000 + $index);
$backlog[] = $this->runningWorkflow($processInstanceId, $businessId);
$this->approvalClient->cashierStatusByInstance[$processInstanceId] = 'completed';
$connection->table('ding_process_detail_mirror')->insert([
'business_id' => $businessId,
'process_instance_id' => $processInstanceId,
'line_no' => 1,
'business_month' => '202610',
'originator_name' => '李斯',
'status' => '审批中',
'payment_source_state' => 'present',
'cashier_status' => 'pending_sync',
'cashier_checked_at' => null,
'document_type' => '公摊借支-销账',
'purpose' => 'AI TOKEN费用',
'account_code' => '1221.04',
'account_name' => '其他应收款_借支销账',
'amount' => 100,
'source_scope' => 'personal_permission',
'synced_at' => $now->copy()->subHour(),
'created_at' => $now->copy()->subDay(),
'updated_at' => $now->copy()->subHour(),
]);
}
for ($index = 0; $index < 60; $index++) {
$processInstanceId = sprintf('bulk-%03d', $index);
$businessId = sprintf('202611%015d', $index);
$bulk[] = $this->runningWorkflow($processInstanceId, $businessId);
}
$this->query->workflows = $bulk;
$this->query->workflowsById = array_column(
[...$backlog, ...$bulk],
null,
'processInstanceId',
);
$this->query->schemeHighWatermark = 100;
$this->query->scheme = [
'收款费用明细' => json_encode([[
'rowValue' => [
['label' => '代发订单金额', 'value' => '100.00'],
['label' => '科目编码', 'value' => '1221.04'],
['label' => '科目名称', 'value' => '其他应收款_借支销账'],
],
]], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
'收款合计' => 100,
'付款明细' => json_encode([[
'rowValue' => [
['label' => '对应序号', 'value' => '1'],
['label' => '付款金额', 'value' => '100.00'],
['label' => '付款方式', 'value' => '银行卡'],
['label' => '打款时间', 'value' => '2026-10-23'],
],
]], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
];
$connection->table('business_mirror_table_states')->insert([
'dataset_key' => 'ding.process_details.personal_permission',
'source_key' => 'operations_replica',
'source_object' => 'test',
'target_table' => 'ding_process_detail_mirror',
'status' => 'succeeded',
'run_mode' => 'incremental',
'generation' => 'test',
'cursor' => json_encode([
'source' => 'structured',
'scheme_high_watermark' => 100,
'approval_api_cursor' => '',
], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR),
'initial_completed_at' => $now->copy()->subDay(),
'last_full_succeeded_at' => $now->copy()->subDay(),
'last_attempted_at' => $now->copy()->subMinutes(10),
'last_succeeded_at' => $now->copy()->subMinutes(10),
'last_upserted_rows' => 0,
'mirror_row_count' => 30,
'created_at' => $now->copy()->subDay(),
'updated_at' => $now->copy()->subMinutes(10),
]);
$this->synchronizer->sync(false);
$expectedBacklog = array_map(
static fn (array $workflow): string => (string) $workflow['processInstanceId'],
array_slice($backlog, 0, 25),
);
$this->assertCount(50, $this->approvalClient->calls);
$this->assertSame($expectedBacklog, array_slice($this->approvalClient->calls, 0, 25));
$this->assertSame(
'backlog-024',
json_decode((string) $connection->table('business_mirror_table_states')
->value('cursor'), true)['approval_api_cursor'],
);
$this->assertSame('completed', $connection->table('ding_process_detail_mirror')
->where('process_instance_id', 'backlog-024')
->value('cashier_status'));
$this->assertNotNull($connection->table('ding_process_detail_mirror')
->where('process_instance_id', 'backlog-024')
->value('cashier_checked_at'));
}
public function test_business_id_dry_run_uses_official_api_without_publishing(): void
{
$workflow = [
@ -608,6 +717,21 @@ class DingProcessDetailMirrorSynchronizerTest extends TestCase
];
}
/** @return array<string,mixed> */
private function runningWorkflow(string $processInstanceId, string $businessId): array
{
return [
'processInstanceId' => $processInstanceId,
'processCode' => 'PROC-ACF09A14-B094-46B1-A9E2-8BB1F4E1FDCF',
'businessId' => $businessId,
'title' => '李斯提交的公摊借支-销账',
'status' => 'RUNNING',
'originatorUserId' => 'user-1',
'joinAt' => '2026-10-22 17:33:00',
'updateAt' => '2026-10-28 17:00:00',
];
}
private function createMirrorTables(): void
{
Schema::connection('business_mirror')->create(
@ -697,6 +821,9 @@ class FakeDingProcessDetailQuery extends UpstreamReadOnlyQuery
public array $schemeChangedWorkflows = [];
/** @var array<string,array<string,mixed>> */
public array $workflowsById = [];
/** @var list<string> */
public array $schemeChangedProcessIds = [];
@ -741,24 +868,62 @@ class FakeDingProcessDetailQuery extends UpstreamReadOnlyQuery
}
if (str_contains($sql, 'FROM rpt.ding_Workflow w')
&& str_contains($sql, 'w.processInstanceId IN')) {
return $this->schemeChangedWorkflows;
$wanted = array_fill_keys(array_map('strval', $bindings), true);
$rows = [];
$candidates = [
...$this->workflows,
...$this->schemeChangedWorkflows,
...array_values($this->workflowsById),
];
foreach ($candidates as $workflow) {
$processInstanceId = (string) ($workflow['processInstanceId'] ?? '');
if ($processInstanceId !== '' && isset($wanted[$processInstanceId])) {
$rows[$processInstanceId] = $workflow;
}
}
return array_values($rows);
}
if (str_contains($sql, 'FROM rpt.ding_Workflow w')) {
return $this->workflows;
}
if (str_contains($sql, 'FROM rpt.ding_WorkflowScheme')) {
$rows = [];
$processInstanceId = (string) (
$this->workflows[0]['processInstanceId']
?? $this->schemeChangedWorkflows[0]['processInstanceId']
?? ''
);
foreach ($this->scheme as $name => $value) {
$rows[] = [
'processInstanceId' => $processInstanceId,
'name' => $name,
'value' => $value,
];
$requested = array_fill_keys(array_map('strval', $bindings), true);
$known = [];
$candidates = [
...$this->workflows,
...$this->schemeChangedWorkflows,
...array_values($this->workflowsById),
];
foreach ($candidates as $workflow) {
$processInstanceId = (string) ($workflow['processInstanceId'] ?? '');
if ($processInstanceId !== '' && isset($requested[$processInstanceId])) {
$known[$processInstanceId] = true;
}
}
if ($known === []) {
$fallback = '';
if ($this->workflows !== []) {
$fallback = (string) ($this->workflows[0]['processInstanceId'] ?? '');
} elseif ($this->schemeChangedWorkflows !== []) {
$fallback = (string) ($this->schemeChangedWorkflows[0]['processInstanceId'] ?? '');
} elseif ($this->workflowsById !== []) {
$fallbackWorkflow = $this->workflowsById[array_key_first($this->workflowsById)];
$fallback = (string) ($fallbackWorkflow['processInstanceId'] ?? '');
}
if ($fallback !== '') {
$known[$fallback] = true;
}
}
foreach (array_keys($known) as $processInstanceId) {
foreach ($this->scheme as $name => $value) {
$rows[] = [
'processInstanceId' => $processInstanceId,
'name' => $name,
'value' => $value,
];
}
}
return $rows;