- 在多个控制器中引入 agent_node_id,以支持基于代理节点的权限和数据过滤。 - 更新 AdminRole 和 AdminUser 模型,新增角色范围和代理节点相关功能,提升角色管理的灵活性。 - 在请求验证中添加 agent_node_id 字段,确保 API 接口支持代理节点的相关操作。 - 优化 LotterySettings 服务,支持批量写入设置,提升配置管理的效率。 - 更新仪表板和报告服务,增强数据统计功能,确保管理员能够获取更全面的统计信息。
391 lines
14 KiB
PHP
391 lines
14 KiB
PHP
<?php
|
||
|
||
namespace App\Services\Admin;
|
||
|
||
use Carbon\Carbon;
|
||
use App\Models\Draw;
|
||
use App\Models\RiskPool;
|
||
use App\Models\AdminUser;
|
||
use App\Models\TicketItem;
|
||
use App\Models\TicketOrder;
|
||
use App\Models\TransferOrder;
|
||
use App\Models\SettlementBatch;
|
||
use App\Models\DrawResultBatch;
|
||
use App\Lottery\DrawResultBatchStatus;
|
||
use App\Services\Draw\DrawHallSnapshotBuilder;
|
||
use App\Support\AdminDataScope;
|
||
|
||
/**
|
||
* 后台首页仪表盘:聚合大厅快照、当期财务、期号面板、风控摘要、异常转账计数。
|
||
*
|
||
* 权限与原先分散接口一致:开奖查看权负责 draw/finance/risk;钱包对账权负责异常转账总数。
|
||
*/
|
||
final class AdminDashboardSnapshotBuilder
|
||
{
|
||
public function __construct(
|
||
private readonly DrawHallSnapshotBuilder $hallSnapshot,
|
||
private readonly AdminReportQueryService $reportQuery,
|
||
) {}
|
||
|
||
/** @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,
|
||
'today_finance' => null,
|
||
'lifetime_finance' => null,
|
||
'finance' => null,
|
||
'draw' => null,
|
||
'risk' => null,
|
||
'platform_risk' => null,
|
||
'result_batch_queue' => null,
|
||
'abnormal_transfer_total' => null,
|
||
'warnings' => [],
|
||
'capabilities' => [
|
||
'draw_finance_risk' => $canDraw,
|
||
'wallet_transfer_view' => $canWallet,
|
||
],
|
||
];
|
||
|
||
if ($canDraw) {
|
||
$this->fillPlatformOverview($out, $admin);
|
||
}
|
||
|
||
if ($canWallet) {
|
||
$out['abnormal_transfer_total'] = $this->abnormalTransferTotal($admin);
|
||
}
|
||
|
||
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, $admin);
|
||
$out['draw'] = $this->drawPanel($draw);
|
||
$out['risk'] = $this->riskPanel($draw);
|
||
}
|
||
|
||
return $out;
|
||
}
|
||
|
||
/** @param array<string, mixed> $out */
|
||
private function fillPlatformOverview(array &$out, AdminUser $admin): void
|
||
{
|
||
$out['today_finance'] = $this->todayFinanceSummary($admin);
|
||
$out['lifetime_finance'] = $this->reportQuery->platformLifetimeTotals($admin);
|
||
$out['platform_risk'] = $this->platformRiskSummary();
|
||
$out['result_batch_queue'] = $this->resultBatchQueue();
|
||
}
|
||
|
||
private function canDrawFinanceAndRisk(AdminUser $admin): bool
|
||
{
|
||
return $admin->hasAdminPermission('prd.dashboard.view')
|
||
|| $admin->hasAdminPermission('prd.draw_result.manage')
|
||
|| $admin->hasAdminPermission('prd.draw_result.view')
|
||
|| $admin->hasAdminPermission('prd.risk.view')
|
||
|| $admin->hasAdminPermission('prd.risk.manage');
|
||
}
|
||
|
||
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(AdminUser $admin): int
|
||
{
|
||
$query = TransferOrder::query()
|
||
->whereIn('status', ['processing', 'failed', 'pending_reconcile']);
|
||
AdminDataScope::applyEloquentViaPlayer($query, $admin);
|
||
|
||
return (int) $query->count();
|
||
}
|
||
|
||
/**
|
||
* 按业务日汇总「今日」投注/派彩/盈亏(与报表中心 daily-profit 口径一致)。
|
||
*
|
||
* @return array<string, mixed>
|
||
*/
|
||
private function todayFinanceSummary(AdminUser $admin): array
|
||
{
|
||
$today = now()->toDateString();
|
||
$rows = $this->reportQuery->dailyProfitRows($today, $today, $admin);
|
||
$row = $rows[0] ?? [
|
||
'business_date' => $today,
|
||
'total_bet_minor' => 0,
|
||
'total_payout_minor' => 0,
|
||
'approx_house_gross_minor' => 0,
|
||
];
|
||
|
||
$currencyCode = (string) (TicketOrder::query()
|
||
->join('draws', 'draws.id', '=', 'ticket_orders.draw_id')
|
||
->where('draws.business_date', $today)
|
||
->value('ticket_orders.currency_code') ?? '');
|
||
|
||
return [
|
||
'business_date' => (string) $row['business_date'],
|
||
'currency_code' => $currencyCode !== '' ? $currencyCode : null,
|
||
'total_bet_minor' => (int) $row['total_bet_minor'],
|
||
'total_payout_minor' => (int) $row['total_payout_minor'],
|
||
'approx_house_gross_minor' => (int) $row['approx_house_gross_minor'],
|
||
];
|
||
}
|
||
|
||
/** @return array<string, mixed> */
|
||
private function financeSummary(Draw $draw, AdminUser $admin): array
|
||
{
|
||
$drawId = (int) $draw->id;
|
||
|
||
$orderQuery = TicketOrder::query()->where('draw_id', $drawId);
|
||
$itemQuery = TicketItem::query()->where('draw_id', $drawId);
|
||
AdminDataScope::applyEloquentViaPlayer($orderQuery, $admin);
|
||
AdminDataScope::applyEloquentViaPlayer($itemQuery, $admin);
|
||
|
||
$totalBetMinor = (int) $orderQuery->sum('total_actual_deduct');
|
||
$orderCount = (int) $orderQuery->count();
|
||
$itemCount = (int) $itemQuery->count();
|
||
|
||
$currencyCode = (string) ((clone $orderQuery)->value('currency_code') ?? '');
|
||
|
||
$totalWinMinor = (int) $itemQuery->sum('win_amount');
|
||
$totalJackpotWinMinor = (int) (clone $itemQuery)->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{
|
||
* pending_review_total: int,
|
||
* pending_draw_count: int,
|
||
* published_total: int,
|
||
* batch_total: int,
|
||
* first_pending_draw_id: int|null,
|
||
* first_pending_batch_id: int|null
|
||
* }
|
||
*/
|
||
private function resultBatchQueue(): array
|
||
{
|
||
$pendingQuery = DrawResultBatch::query()
|
||
->where('status', DrawResultBatchStatus::PendingReview->value);
|
||
|
||
$pendingTotal = (int) (clone $pendingQuery)->count();
|
||
|
||
$pendingDrawCount = $pendingTotal > 0
|
||
? (int) (clone $pendingQuery)->distinct('draw_id')->count('draw_id')
|
||
: 0;
|
||
|
||
$firstPending = $pendingTotal > 0
|
||
? (clone $pendingQuery)->orderBy('id')->first(['id', 'draw_id'])
|
||
: null;
|
||
|
||
return [
|
||
'pending_review_total' => $pendingTotal,
|
||
'pending_draw_count' => $pendingDrawCount,
|
||
'published_total' => (int) DrawResultBatch::query()
|
||
->where('status', DrawResultBatchStatus::Published->value)
|
||
->count(),
|
||
'batch_total' => (int) DrawResultBatch::query()->count(),
|
||
'first_pending_draw_id' => $firstPending !== null ? (int) $firstPending->draw_id : null,
|
||
'first_pending_batch_id' => $firstPending !== null ? (int) $firstPending->id : null,
|
||
];
|
||
}
|
||
|
||
/** @return array{locked_amount: int, cap_amount: int, usage_percent: float} */
|
||
private function platformRiskSummary(): array
|
||
{
|
||
$sums = RiskPool::query()
|
||
->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);
|
||
|
||
return [
|
||
'locked_amount' => $locked,
|
||
'cap_amount' => $cap,
|
||
'usage_percent' => $cap > 0 ? round(($locked / $cap) * 100, 4) : 0.0,
|
||
];
|
||
}
|
||
|
||
/** @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';
|
||
}
|
||
}
|