From ea70f245353e2ba7bc6171a8ac4b45320b2963b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=9D=E5=8D=9C?= Date: Tue, 17 Mar 2026 18:52:08 +0800 Subject: [PATCH] =?UTF-8?q?refactor(batch):=20=E5=8E=BB=E9=87=8D=E9=98=BB?= =?UTF-8?q?=E6=96=ADwarning=E7=BB=84=E8=A3=85=E6=8F=90=E7=82=BCBatchDispat?= =?UTF-8?q?chWarning=E5=B9=B6=E5=8A=A0=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Admin/PlatformOrderController.php | 39 ++++++------------- app/Support/BatchDispatchWarning.php | 39 +++++++++++++++++++ tests/Unit/BatchDispatchWarningBuildTest.php | 36 +++++++++++++++++ 3 files changed, 87 insertions(+), 27 deletions(-) create mode 100644 app/Support/BatchDispatchWarning.php create mode 100644 tests/Unit/BatchDispatchWarningBuildTest.php diff --git a/app/Http/Controllers/Admin/PlatformOrderController.php b/app/Http/Controllers/Admin/PlatformOrderController.php index 2725aa4..71fba78 100644 --- a/app/Http/Controllers/Admin/PlatformOrderController.php +++ b/app/Http/Controllers/Admin/PlatformOrderController.php @@ -1632,22 +1632,14 @@ class PlatformOrderController extends Controller $existing = (string) (\App\Support\BatchDispatchLock::getExistingValue($lockKey) ?? ''); $existing = trim($existing); - $warningMsg = '检测到刚刚已提交过同一批次的 BAS 任务(1 分钟内)。为避免重复投递,本次未再次提交。'; + $flash = \App\Support\BatchDispatchWarning::build('BAS ', $existing, 'bas'); - // 若锁内已有 run_id(作为 value 存储),则在提示中带出 run_id(短展示)并给运营一个直达复盘入口。 - if ($existing !== '' && str_starts_with($existing, 'BAS')) { - $display = \App\Support\RunId::short((string) $existing, 6, 4); - $warningMsg = '检测到刚刚已提交过同一批次的 BAS 任务(1 分钟内,run_id=' . $display . ')。为避免重复投递,本次未再次提交。'; - } + $res = redirect()->back()->with('warning', (string) ($flash['warning'] ?? '')); - $res = redirect()->back()->with('warning', $warningMsg); - - if ($existing !== '' && str_starts_with($existing, 'BAS')) { - $res = $res - ->with('warning_link_href', '/admin/platform-batches/show?type=bas&run_id=' . urlencode($existing)) - ->with('warning_link_label', '进入上次批次复盘') - ->with('warning_copy_text', $existing) - ->with('warning_copy_label', '复制run_id'); + foreach (['warning_link_href', 'warning_link_label', 'warning_copy_text', 'warning_copy_label'] as $k) { + if (isset($flash[$k]) && (string) $flash[$k] !== '') { + $res = $res->with($k, (string) $flash[$k]); + } } return $res; @@ -1793,21 +1785,14 @@ class PlatformOrderController extends Controller $existing = (string) (\App\Support\BatchDispatchLock::getExistingValue($lockKey) ?? ''); $existing = trim($existing); - $warningMsg = '检测到刚刚已提交过同一批次的 BMPA 任务(1 分钟内)。为避免重复投递,本次未再次提交。'; + $flash = \App\Support\BatchDispatchWarning::build('BMPA ', $existing, 'bmpa'); - if ($existing !== '' && str_starts_with($existing, 'BMPA')) { - $display = \App\Support\RunId::short((string) $existing, 7, 4); - $warningMsg = '检测到刚刚已提交过同一批次的 BMPA 任务(1 分钟内,run_id=' . $display . ')。为避免重复投递,本次未再次提交。'; - } + $res = redirect()->back()->with('warning', (string) ($flash['warning'] ?? '')); - $res = redirect()->back()->with('warning', $warningMsg); - - if ($existing !== '' && str_starts_with($existing, 'BMPA')) { - $res = $res - ->with('warning_link_href', '/admin/platform-batches/show?type=bmpa&run_id=' . urlencode($existing)) - ->with('warning_link_label', '进入上次批次复盘') - ->with('warning_copy_text', $existing) - ->with('warning_copy_label', '复制run_id'); + foreach (['warning_link_href', 'warning_link_label', 'warning_copy_text', 'warning_copy_label'] as $k) { + if (isset($flash[$k]) && (string) $flash[$k] !== '') { + $res = $res->with($k, (string) $flash[$k]); + } } return $res; diff --git a/app/Support/BatchDispatchWarning.php b/app/Support/BatchDispatchWarning.php new file mode 100644 index 0000000..1fa13e2 --- /dev/null +++ b/app/Support/BatchDispatchWarning.php @@ -0,0 +1,39 @@ + $msg, + ]; + + if ($runId === '') { + return $payload; + } + + $short = RunId::short($runId, $batchType === 'bmpa' ? 7 : 6, 4); + $payload['warning'] = '检测到刚刚已提交过同一批次的' . $actionLabel . '任务(1 分钟内,run_id=' . $short . ')。为避免重复投递,本次未再次提交。'; + + $payload['warning_link_href'] = '/admin/platform-batches/show?type=' . $batchType . '&run_id=' . urlencode($runId); + $payload['warning_link_label'] = '进入上次批次复盘'; + $payload['warning_copy_text'] = $runId; + $payload['warning_copy_label'] = '复制run_id'; + + return $payload; + } +} diff --git a/tests/Unit/BatchDispatchWarningBuildTest.php b/tests/Unit/BatchDispatchWarningBuildTest.php new file mode 100644 index 0000000..579c228 --- /dev/null +++ b/tests/Unit/BatchDispatchWarningBuildTest.php @@ -0,0 +1,36 @@ +assertIsArray($flash); + $this->assertArrayHasKey('warning', $flash); + $this->assertStringContainsString('run_id=', (string) $flash['warning']); + + $this->assertSame('进入上次批次复盘', (string) ($flash['warning_link_label'] ?? '')); + $this->assertStringContainsString('type=bas', (string) ($flash['warning_link_href'] ?? '')); + + $this->assertSame('BAS202603171234560001', (string) ($flash['warning_copy_text'] ?? '')); + $this->assertSame('复制run_id', (string) ($flash['warning_copy_label'] ?? '')); + } + + public function test_build_should_not_include_actions_when_run_id_missing(): void + { + $flash = BatchDispatchWarning::build('BMPA ', '', 'bmpa'); + + $this->assertIsArray($flash); + $this->assertArrayHasKey('warning', $flash); + $this->assertStringNotContainsString('run_id=', (string) $flash['warning']); + + $this->assertArrayNotHasKey('warning_link_href', $flash); + $this->assertArrayNotHasKey('warning_copy_text', $flash); + } +}