Compare commits
2 Commits
fc023242ce
...
7e96c01da1
| Author | SHA1 | Date | |
|---|---|---|---|
| 7e96c01da1 | |||
| 0cbd64a5af |
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Dashboard;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Models\AdminUser;
|
||||
use App\Services\Admin\AdminDashboardSnapshotBuilder;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* GET /api/v1/admin/dashboard — 首页仪表盘聚合数据(需登录;按权限返回子块)。
|
||||
*/
|
||||
final class AdminDashboardController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly AdminDashboardSnapshotBuilder $dashboard,
|
||||
) {}
|
||||
|
||||
public function __invoke(Request $request): JsonResponse
|
||||
{
|
||||
$admin = $request->lotteryAdmin();
|
||||
if (! $admin instanceof AdminUser) {
|
||||
return ApiResponse::error(
|
||||
trans('admin.unauthenticated', [], $request->lotteryLocale()),
|
||||
ErrorCode::AdminUnauthenticated->value,
|
||||
null,
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
||||
return ApiResponse::success($this->dashboard->build($admin));
|
||||
}
|
||||
}
|
||||
276
app/Services/Admin/AdminDashboardSnapshotBuilder.php
Normal file
276
app/Services/Admin/AdminDashboardSnapshotBuilder.php
Normal file
@@ -0,0 +1,276 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Admin;
|
||||
|
||||
use App\Lottery\DrawResultBatchStatus;
|
||||
use App\Models\AdminUser;
|
||||
use App\Models\Draw;
|
||||
use App\Models\RiskPool;
|
||||
use App\Models\SettlementBatch;
|
||||
use App\Models\TicketItem;
|
||||
use App\Models\TicketOrder;
|
||||
use App\Models\TransferOrder;
|
||||
use App\Services\Draw\DrawHallSnapshotBuilder;
|
||||
use Carbon\Carbon;
|
||||
|
||||
/**
|
||||
* 后台首页仪表盘:聚合大厅快照、当期财务、期号面板、风控摘要、异常转账计数。
|
||||
*
|
||||
* 权限与原先分散接口一致:开奖查看权负责 draw/finance/risk;钱包对账权负责异常转账总数。
|
||||
*/
|
||||
final class AdminDashboardSnapshotBuilder
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DrawHallSnapshotBuilder $hallSnapshot,
|
||||
) {}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function build(AdminUser $admin): array
|
||||
{
|
||||
$hall = $this->hallSnapshot->build();
|
||||
$canDraw = $this->canDrawFinanceAndRisk($admin);
|
||||
$canWallet = $this->canWalletReconcile($admin);
|
||||
|
||||
$out = [
|
||||
'hall' => $hall,
|
||||
'resolved_draw' => null,
|
||||
'finance' => null,
|
||||
'draw' => null,
|
||||
'risk' => null,
|
||||
'abnormal_transfer_total' => null,
|
||||
'warnings' => [],
|
||||
'capabilities' => [
|
||||
'draw_finance_risk' => $canDraw,
|
||||
'wallet_transfer_view' => $canWallet,
|
||||
],
|
||||
];
|
||||
|
||||
if ($hall === null) {
|
||||
return $out;
|
||||
}
|
||||
|
||||
$drawNo = (string) ($hall['draw_no'] ?? '');
|
||||
$draw = Draw::query()->where('draw_no', $drawNo)->first();
|
||||
|
||||
if ($draw === null) {
|
||||
$out['warnings'][] = [
|
||||
'code' => 'draw_row_missing',
|
||||
'message' => '大厅期号在 draws 表中未找到对应行。',
|
||||
];
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
$out['resolved_draw'] = [
|
||||
'id' => (int) $draw->id,
|
||||
'draw_no' => $draw->draw_no,
|
||||
];
|
||||
|
||||
if ($canDraw) {
|
||||
$out['finance'] = $this->financeSummary($draw);
|
||||
$out['draw'] = $this->drawPanel($draw);
|
||||
$out['risk'] = $this->riskPanel($draw);
|
||||
}
|
||||
|
||||
if ($canWallet) {
|
||||
$out['abnormal_transfer_total'] = $this->abnormalTransferTotal();
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
private function canDrawFinanceAndRisk(AdminUser $admin): bool
|
||||
{
|
||||
return $admin->hasAdminPermission('prd.draw_result.manage')
|
||||
|| $admin->hasAdminPermission('prd.draw_result.view');
|
||||
}
|
||||
|
||||
private function canWalletReconcile(AdminUser $admin): bool
|
||||
{
|
||||
return $admin->hasAdminPermission('prd.wallet_reconcile.manage')
|
||||
|| $admin->hasAdminPermission('prd.wallet_reconcile.view')
|
||||
|| $admin->hasAdminPermission('prd.wallet_reconcile.view_cs');
|
||||
}
|
||||
|
||||
private function abnormalTransferTotal(): int
|
||||
{
|
||||
return (int) TransferOrder::query()
|
||||
->whereIn('status', ['processing', 'failed', 'pending_reconcile'])
|
||||
->count();
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function financeSummary(Draw $draw): array
|
||||
{
|
||||
$drawId = (int) $draw->id;
|
||||
|
||||
$totalBetMinor = (int) TicketOrder::query()->where('draw_id', $drawId)->sum('total_actual_deduct');
|
||||
$orderCount = (int) TicketOrder::query()->where('draw_id', $drawId)->count();
|
||||
$itemCount = (int) TicketItem::query()->where('draw_id', $drawId)->count();
|
||||
|
||||
$currencyCode = (string) (TicketOrder::query()
|
||||
->where('draw_id', $drawId)
|
||||
->value('currency_code') ?? '');
|
||||
|
||||
$totalWinMinor = (int) TicketItem::query()->where('draw_id', $drawId)->sum('win_amount');
|
||||
$totalJackpotWinMinor = (int) TicketItem::query()->where('draw_id', $drawId)->sum('jackpot_win_amount');
|
||||
$totalPayoutMinor = $totalWinMinor + $totalJackpotWinMinor;
|
||||
$approxHouseGrossMinor = $totalBetMinor - $totalPayoutMinor;
|
||||
|
||||
$batches = SettlementBatch::query()
|
||||
->where('draw_id', $drawId)
|
||||
->orderByDesc('id')
|
||||
->limit(30)
|
||||
->get(['id', 'status', 'total_ticket_count', 'total_win_count', 'total_payout_amount', 'total_jackpot_payout_amount', 'finished_at']);
|
||||
|
||||
$batchRows = $batches->map(static function (SettlementBatch $b): array {
|
||||
return [
|
||||
'id' => (int) $b->id,
|
||||
'status' => $b->status,
|
||||
'total_ticket_count' => (int) $b->total_ticket_count,
|
||||
'total_win_count' => (int) $b->total_win_count,
|
||||
'total_payout_amount' => (int) $b->total_payout_amount,
|
||||
'total_jackpot_payout_amount' => (int) $b->total_jackpot_payout_amount,
|
||||
'finished_at' => $b->finished_at?->toIso8601String(),
|
||||
];
|
||||
})->values()->all();
|
||||
|
||||
return [
|
||||
'draw_id' => $drawId,
|
||||
'draw_no' => $draw->draw_no,
|
||||
'draw_status' => $draw->status,
|
||||
'currency_code' => $currencyCode !== '' ? $currencyCode : null,
|
||||
'order_count' => $orderCount,
|
||||
'ticket_item_count' => $itemCount,
|
||||
'total_bet_minor' => $totalBetMinor,
|
||||
'total_win_payout_minor' => $totalWinMinor,
|
||||
'total_jackpot_win_minor' => $totalJackpotWinMinor,
|
||||
'total_payout_minor' => $totalPayoutMinor,
|
||||
'approx_house_gross_minor' => $approxHouseGrossMinor,
|
||||
'settlement_batches' => $batchRows,
|
||||
];
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function drawPanel(Draw $draw): array
|
||||
{
|
||||
$nowUtc = now()->utc();
|
||||
$batchCounts = [
|
||||
'total' => $draw->resultBatches()->count(),
|
||||
'pending_review' => $draw->resultBatches()
|
||||
->where('status', DrawResultBatchStatus::PendingReview->value)
|
||||
->count(),
|
||||
'published' => $draw->resultBatches()
|
||||
->where('status', DrawResultBatchStatus::Published->value)
|
||||
->count(),
|
||||
];
|
||||
|
||||
return [
|
||||
'id' => (int) $draw->id,
|
||||
'draw_no' => $draw->draw_no,
|
||||
'business_date' => $draw->business_date instanceof Carbon
|
||||
? $draw->business_date->format('Y-m-d')
|
||||
: (string) $draw->business_date,
|
||||
'sequence_no' => (int) $draw->sequence_no,
|
||||
'status' => $draw->status,
|
||||
'hall_preview_status' => $this->hallSnapshot->effectiveHallDisplayStatus($draw, $nowUtc),
|
||||
'result_batch_counts' => $batchCounts,
|
||||
];
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function riskPanel(Draw $draw): array
|
||||
{
|
||||
$drawId = (int) $draw->id;
|
||||
|
||||
$sums = RiskPool::query()
|
||||
->where('draw_id', $drawId)
|
||||
->selectRaw('COALESCE(SUM(locked_amount), 0) as locked, COALESCE(SUM(total_cap_amount), 0) as cap')
|
||||
->first();
|
||||
|
||||
$locked = (int) (($sums?->locked) ?? 0);
|
||||
$cap = (int) (($sums?->cap) ?? 0);
|
||||
$usagePercent = $cap > 0 ? round(($locked / $cap) * 100, 4) : 0.0;
|
||||
|
||||
$hotPools = RiskPool::query()
|
||||
->where('draw_id', $drawId)
|
||||
->orderByRaw('(locked_amount * 1.0 / NULLIF(total_cap_amount, 0)) DESC')
|
||||
->orderByDesc('locked_amount')
|
||||
->orderBy('normalized_number')
|
||||
->limit(100)
|
||||
->get();
|
||||
|
||||
$hotRows = $hotPools->map(fn (RiskPool $pool) => $this->riskPoolRow($pool))->values()->all();
|
||||
|
||||
$buckets = ['d4' => 0, 'd3' => 0, 'd2' => 0, 'special' => 0, 'other' => 0];
|
||||
RiskPool::query()
|
||||
->where('draw_id', $drawId)
|
||||
->where('sold_out_status', 1)
|
||||
->select(['id', 'normalized_number'])
|
||||
->chunkById(500, function ($rows) use (&$buckets): void {
|
||||
foreach ($rows as $row) {
|
||||
$k = $this->soldOutBucketKey((string) $row->normalized_number);
|
||||
$buckets[$k]++;
|
||||
}
|
||||
});
|
||||
|
||||
return [
|
||||
'draw_id' => $drawId,
|
||||
'draw_no' => $draw->draw_no,
|
||||
'locked_amount' => $locked,
|
||||
'cap_amount' => $cap,
|
||||
'usage_percent' => $usagePercent,
|
||||
'hot_pool_rows' => $hotRows,
|
||||
'sold_out_buckets' => $buckets,
|
||||
];
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function riskPoolRow(RiskPool $pool): array
|
||||
{
|
||||
$cap = (int) $pool->total_cap_amount;
|
||||
$locked = (int) $pool->locked_amount;
|
||||
|
||||
return [
|
||||
'normalized_number' => $pool->normalized_number,
|
||||
'total_cap_amount' => $cap,
|
||||
'locked_amount' => $locked,
|
||||
'remaining_amount' => (int) $pool->remaining_amount,
|
||||
'sold_out_status' => (int) $pool->sold_out_status,
|
||||
'is_sold_out' => (int) $pool->sold_out_status === 1,
|
||||
'usage_ratio' => $cap > 0 ? round($locked / $cap, 6) : null,
|
||||
'version' => (int) $pool->version,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 与仪表盘前端维度一致:先按「提取数字位数」分 4D/3D/2D;含字母且不足 3 位有效数字的视为特别号。
|
||||
*
|
||||
* @return 'd4'|'d3'|'d2'|'special'|'other'
|
||||
*/
|
||||
private function soldOutBucketKey(string $normalizedNumber): string
|
||||
{
|
||||
$raw = trim($normalizedNumber);
|
||||
$digits = preg_replace('/\D/', '', $raw) ?? '';
|
||||
$digitLen = strlen($digits);
|
||||
$hasLetter = preg_match('/[A-Za-z]/', $raw) === 1;
|
||||
|
||||
if ($hasLetter && $digitLen < 3) {
|
||||
return 'special';
|
||||
}
|
||||
if ($digitLen >= 4) {
|
||||
return 'd4';
|
||||
}
|
||||
if ($digitLen === 3) {
|
||||
return 'd3';
|
||||
}
|
||||
if ($digitLen === 2) {
|
||||
return 'd2';
|
||||
}
|
||||
if ($hasLetter) {
|
||||
return 'special';
|
||||
}
|
||||
|
||||
return 'other';
|
||||
}
|
||||
}
|
||||
321
database/seeders/DashboardHallFixtureSeeder.php
Normal file
321
database/seeders/DashboardHallFixtureSeeder.php
Normal file
@@ -0,0 +1,321 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Lottery\DrawResultBatchStatus;
|
||||
use App\Lottery\DrawStatus;
|
||||
use App\Models\Draw;
|
||||
use App\Models\DrawResultBatch;
|
||||
use App\Models\Player;
|
||||
use App\Models\RiskPool;
|
||||
use App\Models\TicketItem;
|
||||
use App\Models\TicketOrder;
|
||||
use App\Models\TransferOrder;
|
||||
use App\Services\Draw\DrawHallSnapshotBuilder;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* 【仪表盘 / 大厅当期】向 **当前大厅 resolve 指向的期号** 写入丰富演示数据(财务、风控、开奖批次、异常转账)。
|
||||
*
|
||||
* 解决:手工往某一期灌数后,大厅仍指向另一期(`resolveHallTarget` 按 open→时间序),导致仪表盘全空。
|
||||
*
|
||||
* ```bash
|
||||
* php artisan db:seed --class="Database\\Seeders\\DashboardHallFixtureSeeder"
|
||||
* ```
|
||||
*/
|
||||
class DashboardHallFixtureSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
if (app()->environment('production')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$hall = app(DrawHallSnapshotBuilder::class);
|
||||
$draw = $hall->resolveHallTarget();
|
||||
if ($draw === null) {
|
||||
$draw = $this->createFallbackOpenDraw();
|
||||
}
|
||||
|
||||
$player = Player::query()
|
||||
->where('site_code', 'demo')
|
||||
->where('site_player_id', 'demo-player-001')
|
||||
->first()
|
||||
?? Player::query()->orderBy('id')->first();
|
||||
|
||||
if ($player === null) {
|
||||
$this->command?->warn('DashboardHallFixtureSeeder: 无 players 行,请先执行 DevPlayerAndWalletSeeder。');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($draw, $player): void {
|
||||
$this->seedRiskPools($draw);
|
||||
$this->seedTicketOrders($draw, $player);
|
||||
$this->seedPendingReviewBatch($draw);
|
||||
$this->seedAbnormalTransfers($player);
|
||||
});
|
||||
|
||||
$this->command?->info(sprintf(
|
||||
'DashboardHallFixtureSeeder: 已写入 hall 当期 draw_no=%s id=%d',
|
||||
$draw->draw_no,
|
||||
$draw->id,
|
||||
));
|
||||
}
|
||||
|
||||
private function createFallbackOpenDraw(): Draw
|
||||
{
|
||||
$now = Carbon::now()->utc();
|
||||
$biz = $now->format('Y-m-d');
|
||||
$ymd = str_replace('-', '', $biz);
|
||||
|
||||
return Draw::query()->updateOrCreate(
|
||||
['draw_no' => $ymd.'-997'],
|
||||
[
|
||||
'business_date' => $biz,
|
||||
'sequence_no' => 997,
|
||||
'status' => DrawStatus::Open->value,
|
||||
'start_time' => $now->copy()->subMinutes(5),
|
||||
'close_time' => $now->copy()->addHours(2),
|
||||
'draw_time' => $now->copy()->addHours(2)->addSeconds(30),
|
||||
'cooling_end_time' => null,
|
||||
'result_source' => null,
|
||||
'current_result_version' => 0,
|
||||
'settle_version' => 0,
|
||||
'is_reopened' => false,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
private function seedRiskPools(Draw $draw): void
|
||||
{
|
||||
$rows = [
|
||||
// 4D 纯数字
|
||||
['1234', 5_000_000, 3_200_000, 0],
|
||||
['2387', 2_000_000, 1_800_000, 0],
|
||||
['4752', 1_500_000, 1_200_000, 0],
|
||||
['8899', 800_000, 720_000, 0],
|
||||
['5678', 3_000_000, 900_000, 0],
|
||||
['3456', 1_200_000, 600_000, 0],
|
||||
['1111', 2_000_000, 400_000, 0],
|
||||
['2222', 1_000_000, 250_000, 0],
|
||||
['3333', 900_000, 450_000, 0],
|
||||
['4444', 700_000, 350_000, 0],
|
||||
['0999', 300_000, 180_000, 0],
|
||||
['0099', 200_000, 50_000, 0],
|
||||
// 3D:占位字母去掉后剩 3 位数字(如 909)
|
||||
['90X9', 1_200_000, 960_000, 0],
|
||||
['80Y0', 900_000, 720_000, 0],
|
||||
['Z999', 700_000, 560_000, 0],
|
||||
// 2D:剩 2 位数字
|
||||
['9X9X', 650_000, 520_000, 0],
|
||||
['8Y8Z', 550_000, 330_000, 0],
|
||||
// 特别号:含字母且有效数字不足 3 位
|
||||
['12AB', 600_000, 120_000, 0],
|
||||
['ABCD', 420_000, 210_000, 0],
|
||||
['1A2B', 380_000, 95_000, 0],
|
||||
// 售罄 —— 覆盖 4D / 3D / 2D / 特别
|
||||
['0001', 500_000, 500_000, 1],
|
||||
['9999', 400_000, 400_000, 1],
|
||||
['9Y90', 350_000, 350_000, 1],
|
||||
['9A9B', 280_000, 280_000, 1],
|
||||
['AB12', 220_000, 220_000, 1],
|
||||
];
|
||||
|
||||
foreach ($rows as [$num, $cap, $locked, $sold]) {
|
||||
$remaining = max(0, $cap - $locked);
|
||||
RiskPool::query()->updateOrCreate(
|
||||
['draw_id' => $draw->id, 'normalized_number' => $num],
|
||||
[
|
||||
'total_cap_amount' => $cap,
|
||||
'locked_amount' => $locked,
|
||||
'remaining_amount' => $remaining,
|
||||
'sold_out_status' => $sold,
|
||||
'version' => 1,
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function seedTicketOrders(Draw $draw, Player $player): void
|
||||
{
|
||||
$suffix = (string) $draw->id;
|
||||
|
||||
$o1 = TicketOrder::query()->firstOrCreate(
|
||||
['order_no' => 'ORD-HALL-'.$suffix.'-A'],
|
||||
[
|
||||
'player_id' => $player->id,
|
||||
'draw_id' => $draw->id,
|
||||
'currency_code' => strtoupper((string) ($player->default_currency ?? 'NPR')),
|
||||
'total_bet_amount' => 280_000,
|
||||
'total_rebate_amount' => 0,
|
||||
'total_actual_deduct' => 280_000,
|
||||
'total_estimated_payout' => 0,
|
||||
'status' => 'placed',
|
||||
'submit_source' => 'h5',
|
||||
'client_trace_id' => 'hall-fixture-a',
|
||||
],
|
||||
);
|
||||
|
||||
TicketItem::query()->firstOrCreate(
|
||||
['ticket_no' => 'TK-HALL-'.$suffix.'-001'],
|
||||
[
|
||||
'order_id' => $o1->id,
|
||||
'player_id' => $player->id,
|
||||
'draw_id' => $draw->id,
|
||||
'original_number' => '1234',
|
||||
'normalized_number' => '1234',
|
||||
'play_code' => 'straight',
|
||||
'dimension' => 4,
|
||||
'digit_slot' => null,
|
||||
'bet_mode' => null,
|
||||
'unit_bet_amount' => 200_000,
|
||||
'total_bet_amount' => 200_000,
|
||||
'rebate_rate_snapshot' => 0,
|
||||
'commission_rate_snapshot' => 0,
|
||||
'actual_deduct_amount' => 200_000,
|
||||
'odds_snapshot_json' => null,
|
||||
'rule_snapshot_json' => null,
|
||||
'combination_count' => 1,
|
||||
'estimated_max_payout' => 0,
|
||||
'risk_locked_amount' => 0,
|
||||
'status' => 'success',
|
||||
'fail_reason_code' => null,
|
||||
'fail_reason_text' => null,
|
||||
'win_amount' => 15_000,
|
||||
'jackpot_win_amount' => 0,
|
||||
'settled_at' => null,
|
||||
],
|
||||
);
|
||||
|
||||
TicketItem::query()->firstOrCreate(
|
||||
['ticket_no' => 'TK-HALL-'.$suffix.'-002'],
|
||||
[
|
||||
'order_id' => $o1->id,
|
||||
'player_id' => $player->id,
|
||||
'draw_id' => $draw->id,
|
||||
'original_number' => '5678',
|
||||
'normalized_number' => '5678',
|
||||
'play_code' => 'straight',
|
||||
'dimension' => 4,
|
||||
'digit_slot' => null,
|
||||
'bet_mode' => null,
|
||||
'unit_bet_amount' => 80_000,
|
||||
'total_bet_amount' => 80_000,
|
||||
'rebate_rate_snapshot' => 0,
|
||||
'commission_rate_snapshot' => 0,
|
||||
'actual_deduct_amount' => 80_000,
|
||||
'odds_snapshot_json' => null,
|
||||
'rule_snapshot_json' => null,
|
||||
'combination_count' => 1,
|
||||
'estimated_max_payout' => 0,
|
||||
'risk_locked_amount' => 0,
|
||||
'status' => 'success',
|
||||
'fail_reason_code' => null,
|
||||
'fail_reason_text' => null,
|
||||
'win_amount' => 0,
|
||||
'jackpot_win_amount' => 5_000,
|
||||
'settled_at' => null,
|
||||
],
|
||||
);
|
||||
|
||||
$o2 = TicketOrder::query()->firstOrCreate(
|
||||
['order_no' => 'ORD-HALL-'.$suffix.'-B'],
|
||||
[
|
||||
'player_id' => $player->id,
|
||||
'draw_id' => $draw->id,
|
||||
'currency_code' => strtoupper((string) ($player->default_currency ?? 'NPR')),
|
||||
'total_bet_amount' => 120_000,
|
||||
'total_rebate_amount' => 0,
|
||||
'total_actual_deduct' => 120_000,
|
||||
'total_estimated_payout' => 0,
|
||||
'status' => 'placed',
|
||||
'submit_source' => 'h5',
|
||||
'client_trace_id' => 'hall-fixture-b',
|
||||
],
|
||||
);
|
||||
|
||||
TicketItem::query()->firstOrCreate(
|
||||
['ticket_no' => 'TK-HALL-'.$suffix.'-003'],
|
||||
[
|
||||
'order_id' => $o2->id,
|
||||
'player_id' => $player->id,
|
||||
'draw_id' => $draw->id,
|
||||
'original_number' => '2387',
|
||||
'normalized_number' => '2387',
|
||||
'play_code' => 'straight',
|
||||
'dimension' => 4,
|
||||
'digit_slot' => null,
|
||||
'bet_mode' => null,
|
||||
'unit_bet_amount' => 120_000,
|
||||
'total_bet_amount' => 120_000,
|
||||
'rebate_rate_snapshot' => 0,
|
||||
'commission_rate_snapshot' => 0,
|
||||
'actual_deduct_amount' => 120_000,
|
||||
'odds_snapshot_json' => null,
|
||||
'rule_snapshot_json' => null,
|
||||
'combination_count' => 1,
|
||||
'estimated_max_payout' => 0,
|
||||
'risk_locked_amount' => 0,
|
||||
'status' => 'success',
|
||||
'fail_reason_code' => null,
|
||||
'fail_reason_text' => null,
|
||||
'win_amount' => 0,
|
||||
'jackpot_win_amount' => 0,
|
||||
'settled_at' => null,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
private function seedPendingReviewBatch(Draw $draw): void
|
||||
{
|
||||
$max = (int) DrawResultBatch::query()->where('draw_id', $draw->id)->max('result_version');
|
||||
$next = max(1, $max + 1);
|
||||
|
||||
DrawResultBatch::query()->firstOrCreate(
|
||||
[
|
||||
'draw_id' => $draw->id,
|
||||
'result_version' => $next,
|
||||
],
|
||||
[
|
||||
'source_type' => 'manual',
|
||||
'rng_seed_hash' => null,
|
||||
'raw_seed_encrypted' => null,
|
||||
'status' => DrawResultBatchStatus::PendingReview->value,
|
||||
'created_by' => null,
|
||||
'confirmed_by' => null,
|
||||
'confirmed_at' => null,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
private function seedAbnormalTransfers(Player $player): void
|
||||
{
|
||||
$rows = [
|
||||
['TR-HALL-DEMO-F1', 'failed', 'idem-hall-f1'],
|
||||
['TR-HALL-DEMO-P1', 'processing', 'idem-hall-p1'],
|
||||
['TR-HALL-DEMO-R1', 'pending_reconcile', 'idem-hall-r1'],
|
||||
];
|
||||
|
||||
foreach ($rows as [$no, $status, $idem]) {
|
||||
TransferOrder::query()->firstOrCreate(
|
||||
['transfer_no' => $no],
|
||||
[
|
||||
'player_id' => $player->id,
|
||||
'direction' => 'in',
|
||||
'currency_code' => strtoupper((string) ($player->default_currency ?? 'NPR')),
|
||||
'amount' => 10_000,
|
||||
'idempotent_key' => $idem,
|
||||
'status' => $status,
|
||||
'external_request_payload' => null,
|
||||
'external_response_payload' => null,
|
||||
'external_ref_no' => null,
|
||||
'fail_reason' => $status === 'failed' ? 'demo seed' : null,
|
||||
'finished_at' => $status === 'failed' ? now() : null,
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
165
database/seeders/DashboardSecondaryScenariosSeeder.php
Normal file
165
database/seeders/DashboardSecondaryScenariosSeeder.php
Normal file
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Lottery\DrawResultBatchStatus;
|
||||
use App\Lottery\DrawStatus;
|
||||
use App\Lottery\SettlementBatchStatus;
|
||||
use App\Models\Draw;
|
||||
use App\Models\DrawResultBatch;
|
||||
use App\Models\DrawResultItem;
|
||||
use App\Models\RiskPool;
|
||||
use App\Models\SettlementBatch;
|
||||
use App\Services\Draw\DrawPrizeLayout;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* 【多场景演示】额外写入几条 **非大厅当期** 的期号与数据,便于列表、开奖结果、结算摘要等联调。
|
||||
*
|
||||
* - `…-996`:已结算 + 已发布开奖批次 + 完整 23 条 `draw_result_items` + 一条已完成结算批次 + 少量风险池
|
||||
* - `…-995`:未开始的 pending 期(将来开售)
|
||||
*
|
||||
* ```bash
|
||||
* php artisan db:seed --class="Database\\Seeders\\DashboardSecondaryScenariosSeeder"
|
||||
* ```
|
||||
*/
|
||||
class DashboardSecondaryScenariosSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
if (app()->environment('production')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$now = Carbon::now()->utc();
|
||||
$biz = $now->format('Y-m-d');
|
||||
$ymd = str_replace('-', '', $biz);
|
||||
|
||||
DB::transaction(function () use ($biz, $ymd, $now): void {
|
||||
$this->seedSettledShowcase($biz, $ymd, $now);
|
||||
$this->seedPendingFuture($biz, $ymd, $now);
|
||||
});
|
||||
|
||||
$this->command?->info('DashboardSecondaryScenariosSeeder: 已写入 -996 settled 与 -995 pending 演示期。');
|
||||
}
|
||||
|
||||
private function seedSettledShowcase(string $biz, string $ymd, Carbon $now): void
|
||||
{
|
||||
$drawTime = $now->copy()->subHours(6);
|
||||
|
||||
$draw = Draw::query()->updateOrCreate(
|
||||
['draw_no' => $ymd.'-996'],
|
||||
[
|
||||
'business_date' => $biz,
|
||||
'sequence_no' => 996,
|
||||
'status' => DrawStatus::Settled->value,
|
||||
'start_time' => $drawTime->copy()->subMinutes(30),
|
||||
'close_time' => $drawTime->copy()->subSeconds(30),
|
||||
'draw_time' => $drawTime,
|
||||
'cooling_end_time' => $drawTime->copy()->addMinutes(10),
|
||||
'result_source' => 'rng',
|
||||
'current_result_version' => 1,
|
||||
'settle_version' => 1,
|
||||
'is_reopened' => false,
|
||||
],
|
||||
);
|
||||
|
||||
$batch = DrawResultBatch::query()->updateOrCreate(
|
||||
[
|
||||
'draw_id' => $draw->id,
|
||||
'result_version' => 1,
|
||||
],
|
||||
[
|
||||
'source_type' => 'rng',
|
||||
'rng_seed_hash' => hash('sha256', 'dashboard-secondary-996'),
|
||||
'raw_seed_encrypted' => null,
|
||||
'status' => DrawResultBatchStatus::Published->value,
|
||||
'created_by' => null,
|
||||
'confirmed_by' => null,
|
||||
'confirmed_at' => $drawTime,
|
||||
],
|
||||
);
|
||||
|
||||
DrawResultItem::query()->where('result_batch_id', $batch->id)->delete();
|
||||
|
||||
foreach (DrawPrizeLayout::slots() as $i => $slot) {
|
||||
$n = (($i + 7) * 401) % 10_000;
|
||||
$num = str_pad((string) $n, 4, '0', STR_PAD_LEFT);
|
||||
DrawResultItem::query()->create([
|
||||
'draw_id' => $draw->id,
|
||||
'result_batch_id' => $batch->id,
|
||||
'prize_type' => $slot['prize_type'],
|
||||
'prize_index' => $slot['prize_index'],
|
||||
'number_4d' => $num,
|
||||
'suffix_3d' => substr($num, -3),
|
||||
'suffix_2d' => substr($num, -2),
|
||||
'head_digit' => (int) substr($num, 0, 1),
|
||||
'tail_digit' => (int) substr($num, 3, 1),
|
||||
]);
|
||||
}
|
||||
|
||||
SettlementBatch::query()->updateOrCreate(
|
||||
[
|
||||
'draw_id' => $draw->id,
|
||||
'settle_version' => 1,
|
||||
],
|
||||
[
|
||||
'result_batch_id' => $batch->id,
|
||||
'status' => SettlementBatchStatus::Completed->value,
|
||||
'total_ticket_count' => 12,
|
||||
'total_win_count' => 3,
|
||||
'total_payout_amount' => 450_000,
|
||||
'total_jackpot_payout_amount' => 20_000,
|
||||
'started_at' => $drawTime->copy()->addMinutes(2),
|
||||
'finished_at' => $drawTime->copy()->addMinutes(5),
|
||||
],
|
||||
);
|
||||
|
||||
$pools = [
|
||||
['2468', 1_000_000, 400_000, 0],
|
||||
['1357', 800_000, 200_000, 0],
|
||||
['90X9', 600_000, 540_000, 0],
|
||||
['9X9X', 400_000, 160_000, 0],
|
||||
['12AB', 300_000, 90_000, 0],
|
||||
['8888', 200_000, 200_000, 1],
|
||||
];
|
||||
foreach ($pools as [$num, $cap, $locked, $sold]) {
|
||||
RiskPool::query()->updateOrCreate(
|
||||
['draw_id' => $draw->id, 'normalized_number' => $num],
|
||||
[
|
||||
'total_cap_amount' => $cap,
|
||||
'locked_amount' => $locked,
|
||||
'remaining_amount' => max(0, $cap - $locked),
|
||||
'sold_out_status' => $sold,
|
||||
'version' => 1,
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function seedPendingFuture(string $biz, string $ymd, Carbon $now): void
|
||||
{
|
||||
$start = $now->copy()->addHours(8);
|
||||
$close = $start->copy()->addMinutes(10);
|
||||
$drawT = $close->copy()->addSeconds(30);
|
||||
|
||||
Draw::query()->updateOrCreate(
|
||||
['draw_no' => $ymd.'-995'],
|
||||
[
|
||||
'business_date' => $biz,
|
||||
'sequence_no' => 995,
|
||||
'status' => DrawStatus::Pending->value,
|
||||
'start_time' => $start,
|
||||
'close_time' => $close,
|
||||
'draw_time' => $drawT,
|
||||
'cooling_end_time' => null,
|
||||
'result_source' => null,
|
||||
'current_result_version' => 0,
|
||||
'settle_version' => 0,
|
||||
'is_reopened' => false,
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,9 @@ class DatabaseSeeder extends Seeder
|
||||
AdminRbacAndUserSeeder::class,
|
||||
DevPlayerAndWalletSeeder::class,
|
||||
DrawDemoSeeder::class,
|
||||
// 仪表盘:对齐「大厅 resolve 当期」+ 多场景演示期(-996 settled / -995 pending)
|
||||
DashboardHallFixtureSeeder::class,
|
||||
DashboardSecondaryScenariosSeeder::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ use App\Http\Controllers\Api\V1\Admin\Config\RiskCapVersionIndexController;
|
||||
use App\Http\Controllers\Api\V1\Admin\Config\RiskCapVersionPublishController;
|
||||
use App\Http\Controllers\Api\V1\Admin\Config\RiskCapVersionShowController;
|
||||
use App\Http\Controllers\Api\V1\Admin\Config\RiskCapVersionStoreController;
|
||||
use App\Http\Controllers\Api\V1\Admin\Dashboard\AdminDashboardController;
|
||||
use App\Http\Controllers\Api\V1\Admin\Draw\AdminDrawFinanceSummaryController;
|
||||
use App\Http\Controllers\Api\V1\Admin\Draw\AdminDrawIndexController;
|
||||
use App\Http\Controllers\Api\V1\Admin\Draw\AdminDrawResultBatchesIndexController;
|
||||
@@ -146,6 +147,9 @@ Route::prefix('v1')->group(function (): void {
|
||||
// 名称:后台接口连通性探测(需 Bearer Token;不校验细粒度 RBAC)
|
||||
Route::get('ping', AdminPingController::class)->name('ping');
|
||||
|
||||
/** 首页仪表盘:聚合大厅 + 当期财务/风控/待办计数(细粒度权限在控制器内按块判断) */
|
||||
Route::get('dashboard', AdminDashboardController::class)->name('dashboard');
|
||||
|
||||
/** §8 钱包对账:超管可管、风控查看、财务可管、客服单用户 */
|
||||
Route::middleware('admin.permission:prd.wallet_reconcile.manage|prd.wallet_reconcile.view|prd.wallet_reconcile.view_cs')->group(function (): void {
|
||||
Route::get('wallet/transfer-orders', TransferOrderListController::class)
|
||||
|
||||
73
tests/Feature/AdminDashboardApiTest.php
Normal file
73
tests/Feature/AdminDashboardApiTest.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
use App\Models\AdminUser;
|
||||
use App\Models\Draw;
|
||||
use App\Models\RiskPool;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
test('admin dashboard aggregates hall finance and risk for super admin', function (): void {
|
||||
$draw = Draw::query()->create([
|
||||
'draw_no' => '20260512-010',
|
||||
'business_date' => '2026-05-12',
|
||||
'sequence_no' => 10,
|
||||
'status' => 'open',
|
||||
'start_time' => now()->subHour(),
|
||||
'close_time' => now()->addHour(),
|
||||
'draw_time' => now()->addHours(2),
|
||||
'cooling_end_time' => null,
|
||||
'result_source' => null,
|
||||
'current_result_version' => 0,
|
||||
'settle_version' => 0,
|
||||
'is_reopened' => false,
|
||||
]);
|
||||
|
||||
RiskPool::query()->create([
|
||||
'draw_id' => $draw->id,
|
||||
'normalized_number' => '1234',
|
||||
'total_cap_amount' => 1_000_000,
|
||||
'locked_amount' => 200_000,
|
||||
'remaining_amount' => 800_000,
|
||||
'sold_out_status' => 0,
|
||||
'version' => 1,
|
||||
]);
|
||||
|
||||
RiskPool::query()->create([
|
||||
'draw_id' => $draw->id,
|
||||
'normalized_number' => '9999',
|
||||
'total_cap_amount' => 100,
|
||||
'locked_amount' => 100,
|
||||
'remaining_amount' => 0,
|
||||
'sold_out_status' => 1,
|
||||
'version' => 2,
|
||||
]);
|
||||
|
||||
$admin = AdminUser::query()->create([
|
||||
'username' => 'dash_admin',
|
||||
'name' => 'Dash QA',
|
||||
'email' => null,
|
||||
'password' => Hash::make('secret-strong'),
|
||||
'status' => 0,
|
||||
]);
|
||||
grantSuperAdminRole($admin);
|
||||
$token = $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->getJson('/api/v1/admin/dashboard')
|
||||
->assertOk()
|
||||
->assertJsonPath('data.hall.draw_no', '20260512-010')
|
||||
->assertJsonPath('data.resolved_draw.id', $draw->id)
|
||||
->assertJsonPath('data.capabilities.draw_finance_risk', true)
|
||||
->assertJsonPath('data.capabilities.wallet_transfer_view', true)
|
||||
->assertJsonPath('data.finance.draw_id', $draw->id)
|
||||
->assertJsonPath('data.draw.result_batch_counts.total', 0)
|
||||
->assertJsonPath('data.risk.locked_amount', 200_100)
|
||||
->assertJsonPath('data.risk.cap_amount', 1_000_100)
|
||||
->assertJsonPath('data.risk.sold_out_buckets.d4', 1);
|
||||
});
|
||||
|
||||
test('admin dashboard returns 401 without token', function (): void {
|
||||
$this->getJson('/api/v1/admin/dashboard')->assertStatus(401);
|
||||
});
|
||||
Reference in New Issue
Block a user