refactor(batch): 去重阻断warning组装提炼BatchDispatchWarning并加单测

This commit is contained in:
萝卜
2026-03-17 18:52:08 +08:00
parent aefecd0cbe
commit ea70f24535
3 changed files with 87 additions and 27 deletions

View File

@@ -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;

View 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;
}
}