fix: 用钉钉官方状态纠正OA副本滞后 #820
@ -1022,6 +1022,8 @@ class DingProcessDetailMirrorSynchronizer
|
||||
: $this->paymentSourceState($process, $fields);
|
||||
}
|
||||
}
|
||||
$workflow = $this->reconcileOfficialWorkflowState($workflow, $approvalSnapshot);
|
||||
$status = $this->status($workflow);
|
||||
$cashierStatus = $this->cashierStatus($process, $workflow, $approvalSnapshot);
|
||||
$cashierCheckedAt = $cashierStatus === 'pending_sync' ? null : $now;
|
||||
$taskState = $this->taskState($approvalSnapshot, $employees);
|
||||
@ -1456,6 +1458,77 @@ class DingProcessDetailMirrorSynchronizer
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The replica can keep a process as RUNNING after DingTalk has completed it.
|
||||
* A fresh official snapshot takes precedence while the source is still RUNNING.
|
||||
*
|
||||
* @param array<string,mixed> $workflow
|
||||
* @param array<string,mixed>|null $snapshot
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
private function reconcileOfficialWorkflowState(array $workflow, ?array $snapshot): array
|
||||
{
|
||||
if ($snapshot === null || DingPayableWorkflowStatus::key($workflow) !== 'RUNNING') {
|
||||
return $workflow;
|
||||
}
|
||||
|
||||
$officialState = [
|
||||
'status' => (string) ($snapshot['status'] ?? ''),
|
||||
'result' => (string) ($snapshot['result'] ?? ''),
|
||||
];
|
||||
$officialKey = DingPayableWorkflowStatus::key($officialState);
|
||||
if (! in_array($officialKey, [
|
||||
'COMPLETED',
|
||||
'REJECTED',
|
||||
'WITHDRAWN',
|
||||
'FAILED',
|
||||
'EXPIRED',
|
||||
'DELETED',
|
||||
], true)) {
|
||||
return $workflow;
|
||||
}
|
||||
|
||||
$workflow['status'] = $officialState['status'];
|
||||
$workflow['result'] = $officialState['result'];
|
||||
$finishedAt = $this->officialWorkflowFinishedAt($snapshot);
|
||||
if ($finishedAt !== null) {
|
||||
$workflow['finishTime'] = $finishedAt->format('Y-m-d H:i:s');
|
||||
}
|
||||
|
||||
return $workflow;
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $snapshot */
|
||||
private function officialWorkflowFinishedAt(array $snapshot): ?Carbon
|
||||
{
|
||||
$latest = null;
|
||||
foreach ((array) ($snapshot['tasks'] ?? []) as $task) {
|
||||
if (! is_array($task)) {
|
||||
continue;
|
||||
}
|
||||
$finishedAt = OaProcessTime::parseDingTalkTask($task['finishTime'] ?? null);
|
||||
if ($finishedAt !== null && ($latest === null || $finishedAt->gt($latest))) {
|
||||
$latest = $finishedAt;
|
||||
}
|
||||
}
|
||||
|
||||
if ($latest === null) {
|
||||
foreach ((array) ($snapshot['operation_records'] ?? []) as $operation) {
|
||||
if (! is_array($operation)
|
||||
|| strtoupper(trim((string) ($operation['type'] ?? '')))
|
||||
!== 'EXECUTE_TASK_NORMAL') {
|
||||
continue;
|
||||
}
|
||||
$finishedAt = OaProcessTime::parseDingTalkTask($operation['date'] ?? null);
|
||||
if ($finishedAt !== null && ($latest === null || $finishedAt->gt($latest))) {
|
||||
$latest = $finishedAt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $latest === null ? null : Carbon::instance($latest);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $process
|
||||
* @param array<string,mixed> $workflow
|
||||
|
||||
@ -206,6 +206,56 @@ class DingProcessDetailMirrorSynchronizerTest extends TestCase
|
||||
$this->assertEqualsWithDelta(5500.0, (float) $row->bank_amount, 0.001);
|
||||
}
|
||||
|
||||
public function test_official_completed_status_overrides_stale_running_source(): void
|
||||
{
|
||||
$this->query->workflows = [[
|
||||
'processInstanceId' => 'process-official-completed',
|
||||
'processCode' => 'PROC-ACF09A14-B094-46B1-A9E2-8BB1F4E1FDCF',
|
||||
'businessId' => '202610301116000560927',
|
||||
'title' => '周生辰提交的种菜核销种菜报销流程',
|
||||
'status' => 'RUNNING',
|
||||
'result' => '',
|
||||
'originatorUserId' => 'user-1',
|
||||
'originatorDeptName' => '财务中心',
|
||||
'joinAt' => '2026-10-30 11:17:49',
|
||||
'updateAt' => '2026-10-30 11:17:49',
|
||||
'finishTime' => '',
|
||||
]];
|
||||
$this->query->scheme = $this->daifaScheme();
|
||||
$this->approvalClient->cashierStatusByInstance['process-official-completed'] = 'completed';
|
||||
$this->approvalClient->statusByInstance['process-official-completed'] = 'COMPLETED';
|
||||
$this->approvalClient->resultByInstance['process-official-completed'] = 'agree';
|
||||
$this->approvalClient->tasksByInstance['process-official-completed'] = [
|
||||
[
|
||||
'taskGroupName' => '财务助理',
|
||||
'status' => 'COMPLETED',
|
||||
'result' => 'AGREE',
|
||||
'createTime' => '2026-10-30T19:51Z',
|
||||
'finishTime' => '2026-11-02T11:05Z',
|
||||
],
|
||||
[
|
||||
'taskGroupName' => '董事长',
|
||||
'status' => 'COMPLETED',
|
||||
'result' => 'AGREE',
|
||||
'createTime' => '2026-11-02T13:03Z',
|
||||
'finishTime' => '2026-11-02T13:03Z',
|
||||
],
|
||||
];
|
||||
|
||||
$this->synchronizer->sync(true);
|
||||
|
||||
$row = DB::connection('business_mirror')
|
||||
->table('ding_process_detail_mirror')
|
||||
->where('business_id', '202610301116000560927')
|
||||
->first();
|
||||
$this->assertSame('已完成', $row->status);
|
||||
$this->assertSame('2026-11-02 13:03:00', $row->approval_finished_at);
|
||||
$this->assertNull($row->current_task_name);
|
||||
$this->assertNull($row->current_approver_name);
|
||||
$this->assertNull($row->current_task_started_at);
|
||||
$this->assertSame('completed', $row->cashier_status);
|
||||
}
|
||||
|
||||
public function test_completed_workflow_uses_cashier_status_and_keeps_difference_separate(): void
|
||||
{
|
||||
$this->query->workflows = [[
|
||||
@ -1273,6 +1323,12 @@ class FakeDingTalkProcessInstanceClient extends DingTalkProcessInstanceClient
|
||||
/** @var array<string,list<array<string,mixed>>> */
|
||||
public array $tasksByInstance = [];
|
||||
|
||||
/** @var array<string,string> */
|
||||
public array $statusByInstance = [];
|
||||
|
||||
/** @var array<string,string> */
|
||||
public array $resultByInstance = [];
|
||||
|
||||
/** @var list<string> */
|
||||
public array $calls = [];
|
||||
|
||||
@ -1327,8 +1383,8 @@ class FakeDingTalkProcessInstanceClient extends DingTalkProcessInstanceClient
|
||||
'form_values' => $this->valuesByInstance[$processInstanceId] ?? [],
|
||||
'tasks' => $tasks,
|
||||
'operation_records' => $operations,
|
||||
'status' => null,
|
||||
'result' => null,
|
||||
'status' => $this->statusByInstance[$processInstanceId] ?? null,
|
||||
'result' => $this->resultByInstance[$processInstanceId] ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user