feat(bmpa): write run_id and richer meta for batch mark paid and activate

This commit is contained in:
萝卜
2026-03-17 14:03:06 +08:00
parent 9e40e73481
commit 0718090f49
2 changed files with 138 additions and 4 deletions

View File

@@ -1586,7 +1586,6 @@ class PlatformOrderController extends Controller
$success = 0;
$failed = 0;
$failedReasonCounts = [];
// 筛选摘要:用于审计记录(避免每条订单都手写拼接,且便于追溯本次批量处理口径)
$filterSummaryParts = [];
@@ -1699,8 +1698,12 @@ class PlatformOrderController extends Controller
$orders = $query->orderByDesc('id')->limit($limit)->get(['id']);
$processed = $orders->count();
// 批次号:用于把一次批量执行关联起来,便于后续追溯/筛选/可观测。
$runId = 'BMPA' . now()->format('YmdHis') . str_pad((string) random_int(1, 9999), 4, '0', STR_PAD_LEFT);
$success = 0;
$failed = 0;
$failedReasonCounts = [];
// 筛选摘要:用于审计记录(便于追溯本次批量处理口径)
$filterSummaryParts = [];
@@ -1816,11 +1819,13 @@ class PlatformOrderController extends Controller
];
data_set($meta2, 'audit', $audit);
// 便于追踪:记录最近一次批量推进信息
// 便于追踪:记录最近一次批量推进信息(扁平字段)
data_set($meta2, 'batch_mark_paid_and_activate', [
'at' => $nowStr,
'admin_id' => $admin->id,
'scope' => $scope,
'mode' => 'sync',
'run_id' => $runId,
]);
$order->meta = $meta2;
@@ -1834,17 +1839,43 @@ class PlatformOrderController extends Controller
$reason = $reason !== '' ? $reason : '未知错误';
$meta = (array) ($order->meta ?? []);
$failedReasonCounts[$reason] = ($failedReasonCounts[$reason] ?? 0) + 1;
data_set($meta, 'batch_mark_paid_and_activate_error', [
'message' => $reason,
'at' => $nowStr,
'admin_id' => $admin->id,
'scope' => $scope,
'run_id' => $runId,
'filters' => $filterSummary,
]);
// 即使失败也写入 batch_mark_paid_and_activate包含 run_id确保本批次可追溯。
data_set($meta, 'batch_mark_paid_and_activate', [
'at' => $nowStr,
'admin_id' => $admin->id,
'scope' => $scope,
'mode' => 'sync',
'run_id' => $runId,
]);
$order->meta = $meta;
$order->save();
}
}
$msg = '批量标记支付并生效完成:成功 ' . $success . ' 条,失败 ' . $failed . ' 条(命中 ' . $matchedTotal . ' 条,本次处理 ' . $processed . ' 条limit=' . $limit . '';
$msg = '批量标记支付并生效完成:成功 ' . $success . ' 条,失败 ' . $failed . ' 条(命中 ' . $matchedTotal . ' 条,本次处理 ' . $processed . ' 条limit=' . $limit . 'run_id=' . $runId . '';
if ($failed > 0 && count($failedReasonCounts) > 0) {
arsort($failedReasonCounts);
$top = array_slice($failedReasonCounts, 0, 3, true);
$topText = collect($top)->map(function ($cnt, $reason) {
$reason = mb_substr((string) $reason, 0, 60);
return $reason . '' . (int) $cnt . '';
})->implode('');
$msg .= '失败原因Top' . $topText;
}
return redirect()->back()->with('success', $msg);
}
@@ -1953,7 +1984,6 @@ class PlatformOrderController extends Controller
$success = 0;
$failed = 0;
$failedReasonCounts = [];
$nowStr = now()->toDateTimeString();
// 筛选摘要:用于审计记录(便于追溯本次批量处理口径)

View File

@@ -0,0 +1,104 @@
<?php
namespace Tests\Feature;
use App\Models\Merchant;
use App\Models\Plan;
use App\Models\PlatformOrder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AdminPlatformOrderBatchMarkPaidAndActivateShouldWriteRunIdTest extends TestCase
{
use RefreshDatabase;
protected function loginAsPlatformAdmin(): void
{
$this->seed();
$this->post('/admin/login', [
'email' => 'platform.admin@demo.local',
'password' => 'Platform@123456',
])->assertRedirect('/admin');
}
public function test_batch_mark_paid_and_activate_should_write_run_id_to_meta_on_success_and_failure(): void
{
$this->loginAsPlatformAdmin();
$merchant = Merchant::query()->firstOrFail();
$plan = Plan::query()->create([
'code' => 'bmpa_write_run_id_plan',
'name' => 'BMPA run_id 写入测试套餐',
'billing_cycle' => 'monthly',
'price' => 30,
'list_price' => 30,
'status' => 'active',
'sort' => 10,
'published_at' => now(),
]);
// A可处理
$a = PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'order_no' => 'PO_BMPA_RUN_ID_0001',
'order_type' => 'new_purchase',
'status' => 'pending',
'payment_status' => 'unpaid',
'plan_name' => $plan->name,
'billing_cycle' => $plan->billing_cycle,
'period_months' => 1,
'quantity' => 1,
'payable_amount' => 30,
'paid_amount' => 0,
'placed_at' => now()->subMinutes(10),
]);
// B制造失败已有回执证据但金额不一致
$b = PlatformOrder::query()->create([
'merchant_id' => $merchant->id,
'plan_id' => $plan->id,
'order_no' => 'PO_BMPA_RUN_ID_0002',
'order_type' => 'new_purchase',
'status' => 'pending',
'payment_status' => 'unpaid',
'plan_name' => $plan->name,
'billing_cycle' => $plan->billing_cycle,
'period_months' => 1,
'quantity' => 1,
'payable_amount' => 30,
'paid_amount' => 0,
'placed_at' => now()->subMinutes(9),
'meta' => [
'payment_summary' => [
'count' => 1,
'total_amount' => 1.00,
],
],
]);
$res = $this->post('/admin/platform-orders/batch-mark-paid-and-activate', [
'scope' => 'filtered',
'status' => 'pending',
'payment_status' => 'unpaid',
'limit' => 50,
]);
$res->assertRedirect();
$res->assertSessionHas('success');
$a->refresh();
$b->refresh();
$runIdA = (string) data_get($a->meta, 'batch_mark_paid_and_activate.run_id');
$runIdB = (string) data_get($b->meta, 'batch_mark_paid_and_activate.run_id');
$this->assertNotSame('', $runIdA);
$this->assertNotSame('', $runIdB);
$this->assertSame($runIdA, $runIdB);
// 失败订单应写入 error.run_id便于按批次追溯
$this->assertSame($runIdB, (string) data_get($b->meta, 'batch_mark_paid_and_activate_error.run_id'));
}
}