feat(bmpa): write last_result summary for batch tracing

This commit is contained in:
萝卜
2026-03-17 14:29:26 +08:00
parent 277d8fc69d
commit 40ae678e03
2 changed files with 154 additions and 3 deletions

View File

@@ -1868,12 +1868,53 @@ class PlatformOrderController extends Controller
}
}
$msg = '批量标记支付并生效完成:成功 ' . $success . ' 条,失败 ' . $failed . ' 条(命中 ' . $matchedTotal . ' 条,本次处理 ' . $processed . ' 条limit=' . $limit . 'run_id=' . $runId . '';
// 写入最小结果汇总(冗余写入到每条订单的 meta.batch_mark_paid_and_activate.last_result便于运营在列表页直接看到“本次批量 BMPA 的执行结果”。
// 注意:当前阶段不引入新表;后续若 BMPA Job 化,可复用 BAS 的 last_result 模式。
$topReasons = [];
if ($failed > 0 && count($failedReasonCounts) > 0) {
arsort($failedReasonCounts);
$top = array_slice($failedReasonCounts, 0, 3, true);
$topText = collect($top)->map(function ($cnt, $reason) {
foreach ($top as $reason => $cnt) {
$topReasons[] = [
'reason' => mb_substr((string) $reason, 0, 80),
'count' => (int) $cnt,
];
}
}
$summary = [
'run_id' => $runId,
'success' => $success,
'failed' => $failed,
'matched' => (int) $matchedTotal,
'processed' => (int) $processed,
'top_reasons' => $topReasons,
'at' => now()->toDateTimeString(),
];
foreach ($orders as $row) {
$order = PlatformOrder::query()->find($row->id);
if (! $order) {
continue;
}
$meta = (array) ($order->meta ?? []);
$bmpa = (array) (data_get($meta, 'batch_mark_paid_and_activate', []) ?? []);
if ((string) (data_get($bmpa, 'run_id') ?? '') !== $runId) {
continue;
}
data_set($bmpa, 'last_result', $summary);
data_set($meta, 'batch_mark_paid_and_activate', $bmpa);
$order->meta = $meta;
$order->save();
}
$msg = '批量标记支付并生效完成:成功 ' . $success . ' 条,失败 ' . $failed . ' 条(命中 ' . $matchedTotal . ' 条,本次处理 ' . $processed . ' 条limit=' . $limit . 'run_id=' . $runId . '';
if ($failed > 0 && count($failedReasonCounts) > 0) {
$topText = collect($failedReasonCounts)->sortDesc()->take(3)->map(function ($cnt, $reason) {
$reason = mb_substr((string) $reason, 0, 60);
return $reason . '' . (int) $cnt . '';
})->implode('');

View File

@@ -0,0 +1,110 @@
<?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 AdminPlatformOrderBatchMarkPaidAndActivateShouldWriteLastResultSummaryTest 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_last_result_summary_to_each_order_in_batch(): void
{
$this->loginAsPlatformAdmin();
$merchant = Merchant::query()->firstOrFail();
$plan = Plan::query()->create([
'code' => 'bmpa_last_result_summary_plan',
'name' => 'BMPA last_result 汇总写入测试套餐',
'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_LR_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_LR_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,
],
],
]);
$this->post('/admin/platform-orders/batch-mark-paid-and-activate', [
'scope' => 'filtered',
'status' => 'pending',
'payment_status' => 'unpaid',
'limit' => 50,
])->assertRedirect();
$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->assertSame($runIdA, $runIdB);
$lrA = (array) (data_get($a->meta, 'batch_mark_paid_and_activate.last_result') ?? []);
$lrB = (array) (data_get($b->meta, 'batch_mark_paid_and_activate.last_result') ?? []);
$this->assertSame($runIdA, (string) ($lrA['run_id'] ?? ''));
$this->assertSame($runIdA, (string) ($lrB['run_id'] ?? ''));
$this->assertSame(1, (int) ($lrA['success'] ?? 0));
$this->assertSame(1, (int) ($lrA['failed'] ?? 0));
$this->assertSame(2, (int) ($lrA['matched'] ?? 0));
$this->assertSame(2, (int) ($lrA['processed'] ?? 0));
$this->assertNotEmpty($lrA['top_reasons'] ?? []);
}
}