refactor(batch): 去重阻断warning组装提炼BatchDispatchWarning并加单测
This commit is contained in:
@@ -1632,22 +1632,14 @@ class PlatformOrderController extends Controller
|
|||||||
$existing = (string) (\App\Support\BatchDispatchLock::getExistingValue($lockKey) ?? '');
|
$existing = (string) (\App\Support\BatchDispatchLock::getExistingValue($lockKey) ?? '');
|
||||||
$existing = trim($existing);
|
$existing = trim($existing);
|
||||||
|
|
||||||
$warningMsg = '检测到刚刚已提交过同一批次的 BAS 任务(1 分钟内)。为避免重复投递,本次未再次提交。';
|
$flash = \App\Support\BatchDispatchWarning::build('BAS ', $existing, 'bas');
|
||||||
|
|
||||||
// 若锁内已有 run_id(作为 value 存储),则在提示中带出 run_id(短展示)并给运营一个直达复盘入口。
|
$res = redirect()->back()->with('warning', (string) ($flash['warning'] ?? ''));
|
||||||
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', $warningMsg);
|
foreach (['warning_link_href', 'warning_link_label', 'warning_copy_text', 'warning_copy_label'] as $k) {
|
||||||
|
if (isset($flash[$k]) && (string) $flash[$k] !== '') {
|
||||||
if ($existing !== '' && str_starts_with($existing, 'BAS')) {
|
$res = $res->with($k, (string) $flash[$k]);
|
||||||
$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');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $res;
|
return $res;
|
||||||
@@ -1793,21 +1785,14 @@ class PlatformOrderController extends Controller
|
|||||||
$existing = (string) (\App\Support\BatchDispatchLock::getExistingValue($lockKey) ?? '');
|
$existing = (string) (\App\Support\BatchDispatchLock::getExistingValue($lockKey) ?? '');
|
||||||
$existing = trim($existing);
|
$existing = trim($existing);
|
||||||
|
|
||||||
$warningMsg = '检测到刚刚已提交过同一批次的 BMPA 任务(1 分钟内)。为避免重复投递,本次未再次提交。';
|
$flash = \App\Support\BatchDispatchWarning::build('BMPA ', $existing, 'bmpa');
|
||||||
|
|
||||||
if ($existing !== '' && str_starts_with($existing, 'BMPA')) {
|
$res = redirect()->back()->with('warning', (string) ($flash['warning'] ?? ''));
|
||||||
$display = \App\Support\RunId::short((string) $existing, 7, 4);
|
|
||||||
$warningMsg = '检测到刚刚已提交过同一批次的 BMPA 任务(1 分钟内,run_id=' . $display . ')。为避免重复投递,本次未再次提交。';
|
|
||||||
}
|
|
||||||
|
|
||||||
$res = redirect()->back()->with('warning', $warningMsg);
|
foreach (['warning_link_href', 'warning_link_label', 'warning_copy_text', 'warning_copy_label'] as $k) {
|
||||||
|
if (isset($flash[$k]) && (string) $flash[$k] !== '') {
|
||||||
if ($existing !== '' && str_starts_with($existing, 'BMPA')) {
|
$res = $res->with($k, (string) $flash[$k]);
|
||||||
$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');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $res;
|
return $res;
|
||||||
|
|||||||
39
app/Support/BatchDispatchWarning.php
Normal file
39
app/Support/BatchDispatchWarning.php
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Support;
|
||||||
|
|
||||||
|
class BatchDispatchWarning
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array{warning:string, warning_link_href?:string, warning_link_label?:string, warning_copy_text?:string, warning_copy_label?:string}
|
||||||
|
*/
|
||||||
|
public static function build(
|
||||||
|
string $actionLabel,
|
||||||
|
string $runId,
|
||||||
|
string $batchType,
|
||||||
|
): array {
|
||||||
|
$actionLabel = trim((string) $actionLabel);
|
||||||
|
$runId = trim((string) $runId);
|
||||||
|
$batchType = trim((string) $batchType);
|
||||||
|
|
||||||
|
$msg = '检测到刚刚已提交过同一批次的' . $actionLabel . '任务(1 分钟内)。为避免重复投递,本次未再次提交。';
|
||||||
|
|
||||||
|
$payload = [
|
||||||
|
'warning' => $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;
|
||||||
|
}
|
||||||
|
}
|
||||||
36
tests/Unit/BatchDispatchWarningBuildTest.php
Normal file
36
tests/Unit/BatchDispatchWarningBuildTest.php
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit;
|
||||||
|
|
||||||
|
use App\Support\BatchDispatchWarning;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class BatchDispatchWarningBuildTest extends TestCase
|
||||||
|
{
|
||||||
|
public function test_build_should_include_run_id_in_warning_and_actions_when_run_id_present(): void
|
||||||
|
{
|
||||||
|
$flash = BatchDispatchWarning::build('BAS ', 'BAS202603171234560001', 'bas');
|
||||||
|
|
||||||
|
$this->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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user