load(); \support\App::loadAllConfig(['route']); date_default_timezone_set('Asia/Shanghai'); use app\miniapi\auth\TestPhoneIdentityProvider; use plugin\saiadmin\app\model\water\WaterAgent; use plugin\saiadmin\app\model\water\WaterLedger; use plugin\saiadmin\app\model\water\WaterModel; if (!TestPhoneIdentityProvider::enabled()) { throw new RuntimeException('Requires local test environment'); } $result = WaterModel::transaction(function (): array { $agent = WaterAgent::query()->where('id', 7)->lockForUpdate()->first(); if (!$agent || $agent->name !== '测试商贸') { throw new RuntimeException('Expected named test agent 7'); } $base = 'TEST-MINI-COMMISSIONS-7-20261007'; $header = WaterLedger::query()->where('enterprise_id', 49)->where('kind', 'demo_seed')->where('reference', $base)->first(); if (!$header) { throw new RuntimeException('Initialize the original commission demo first'); } $summary = json_decode($header->note, true, 512, JSON_THROW_ON_ERROR); $created = 0; for ($index = 1; $index <= 20; $index++) { $id = 100 + $index; $key = ['enterprise_id' => 49, 'kind' => 'demo_seed', 'reference' => $base . '-ITEM-' . $id]; if (WaterLedger::query()->where($key)->exists()) { continue; } $status = $index % 5 === 0 ? 'adjustment' : ($index % 2 === 0 ? 'settled' : 'pending'); $amount = $status === 'adjustment' ? -(500 + $index * 25) : 2400 + $index * 160; $item = [ 'id' => $id, 'customer_name' => '【测试】分页企业' . str_pad((string) $index, 2, '0', STR_PAD_LEFT), 'title' => $status === 'adjustment' ? '售后退款调整' : '企业套餐佣金', 'status' => $status, 'status_text' => match ($status) { 'pending' => '待结算(观察期)', 'settled' => '已结算', default => '部分退款冲减' }, 'amount_cents' => $amount, 'created_at' => date('Y-m-d', strtotime('-' . ($index % 7) . ' days')), ]; WaterLedger::query()->create($key + [ 'sku_id' => 5, 'quantity' => 0, 'created_by' => 1, 'note' => json_encode($item, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR), 'created_at' => date('Y-m-d H:i:s'), ]); $summary['total_cents'] += $amount; if ($status === 'pending') { $summary['pending_cents'] += $amount; } else { $summary['withdrawable_cents'] += $amount; } $created++; } if ($created > 0) { $header->note = json_encode($summary, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR); $header->save(); } return ['created' => $created, 'total_records' => WaterLedger::query()->where('enterprise_id', 49)->where('kind', 'demo_seed')->where('reference', 'like', $base . '-ITEM-%')->count()]; }); echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . PHP_EOL;