- 在多个控制器中将权限检查从 hasAdminPermission 更新为 hasPermissionCode,以增强权限管理的灵活性。 - 引入 AdminScopePolicy,优化基于代理节点的权限和数据过滤逻辑,确保管理员能够更精确地控制访问权限。 - 在请求验证中添加 agent_node_id 字段,确保 API 接口支持代理节点的相关操作。 - 更新 AdminUser 模型,新增 hasPermissionCode 方法,以支持更细粒度的权限检查。 - 优化审计日志记录逻辑,确保在处理请求时能够准确记录管理员的操作。
80 lines
3.3 KiB
PHP
80 lines
3.3 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Api\V1\Admin\Draw;
|
||
|
||
use App\Models\Draw;
|
||
use App\Models\TicketItem;
|
||
use App\Models\TicketOrder;
|
||
use App\Models\AdminUser;
|
||
use App\Support\ApiResponse;
|
||
use App\Models\SettlementBatch;
|
||
use Illuminate\Http\JsonResponse;
|
||
use App\Http\Controllers\Controller;
|
||
use App\Support\AdminScopePolicy;
|
||
|
||
/**
|
||
* GET /api/v1/admin/draws/{draw}/finance-summary — 单期投注/派彩汇总(客服/财务视角,PRD §15.4)。
|
||
*
|
||
* 口径:以订单实扣汇总为「当期投注」;以注项中奖+Jackpot 为「当期派彩」;差额为近似毛损益(不含回水等细项)。
|
||
*/
|
||
final class AdminDrawFinanceSummaryController extends Controller
|
||
{
|
||
public function __invoke(\Illuminate\Http\Request $request, Draw $draw): JsonResponse
|
||
{
|
||
$admin = $request->lotteryAdmin();
|
||
abort_if(! $admin instanceof AdminUser, 401);
|
||
$scope = AdminScopePolicy::resolveContext($request, $admin);
|
||
|
||
$drawId = (int) $draw->id;
|
||
|
||
$orders = TicketOrder::query()->where('draw_id', $drawId);
|
||
$items = TicketItem::query()->where('draw_id', $drawId);
|
||
AdminScopePolicy::applyViaPlayer($orders, $scope);
|
||
AdminScopePolicy::applyViaPlayer($items, $scope);
|
||
|
||
$totalBetMinor = (int) (clone $orders)->sum('total_actual_deduct');
|
||
$orderCount = (int) (clone $orders)->count();
|
||
$itemCount = (int) (clone $items)->count();
|
||
|
||
$currencyCode = (string) ((clone $orders)->value('currency_code') ?? '');
|
||
|
||
$totalWinMinor = (int) (clone $items)->sum('win_amount');
|
||
$totalJackpotWinMinor = (int) (clone $items)->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 ApiResponse::success([
|
||
'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,
|
||
]);
|
||
}
|
||
}
|