From 23ac8e3463d1661156ee3096c87d8c6b155973f7 Mon Sep 17 00:00:00 2001 From: yueyang <2495544247@qq.com> Date: Mon, 2 Nov 2026 15:50:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=94=A8=E9=92=89=E9=92=89=E5=AE=98?= =?UTF-8?q?=E6=96=B9=E7=8A=B6=E6=80=81=E7=BA=A0=E6=AD=A3OA=E5=89=AF?= =?UTF-8?q?=E6=9C=AC=E6=BB=9E=E5=90=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DingProcessDetailMirrorSynchronizer.php | 73 +++++++++++++++++++ ...ingProcessDetailMirrorSynchronizerTest.php | 60 ++++++++++++++- 2 files changed, 131 insertions(+), 2 deletions(-) diff --git a/modules/EcommerceFinance/Services/DingProcessDetailMirrorSynchronizer.php b/modules/EcommerceFinance/Services/DingProcessDetailMirrorSynchronizer.php index 6a14a9d7..bbb6e0ba 100644 --- a/modules/EcommerceFinance/Services/DingProcessDetailMirrorSynchronizer.php +++ b/modules/EcommerceFinance/Services/DingProcessDetailMirrorSynchronizer.php @@ -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 $workflow + * @param array|null $snapshot + * @return array + */ + 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 $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 $process * @param array $workflow diff --git a/tests/Unit/EcommerceFinance/DingProcessDetailMirrorSynchronizerTest.php b/tests/Unit/EcommerceFinance/DingProcessDetailMirrorSynchronizerTest.php index bc5093fc..7379cff2 100644 --- a/tests/Unit/EcommerceFinance/DingProcessDetailMirrorSynchronizerTest.php +++ b/tests/Unit/EcommerceFinance/DingProcessDetailMirrorSynchronizerTest.php @@ -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>> */ public array $tasksByInstance = []; + /** @var array */ + public array $statusByInstance = []; + + /** @var array */ + public array $resultByInstance = []; + /** @var list */ 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, ]; } } -- 2.47.2