Files
lotteryLaravel/app/Http/Controllers/Api/V1/Ticket/TicketItemShowController.php

90 lines
3.4 KiB
PHP

<?php
namespace App\Http\Controllers\Api\V1\Ticket;
use App\Http\Controllers\Controller;
use App\Lottery\ErrorCode;
use App\Models\Player;
use App\Models\TicketItem;
use App\Services\Draw\DrawResultViewService;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
/**
* `GET /api/v1/ticket/items/{ticket_no}` — 注单详情(单注项 + 组合 + 结算摘要)。
*/
class TicketItemShowController extends Controller
{
public function __construct(
private readonly DrawResultViewService $drawResultView,
) {}
public function __invoke(Request $request, string $ticket_no): JsonResponse
{
/** @var Player $player */
$player = $request->attributes->get('lottery_player');
$ticket_no = trim($ticket_no);
$item = TicketItem::query()
->where('ticket_no', $ticket_no)
->where('player_id', $player->id)
->with([
'combinations',
'draw',
'order',
'latestSettlementDetail',
])
->first();
if ($item === null) {
return ApiResponse::error(
trans('api.not_found', [], $request->lotteryLocale()),
ErrorCode::NotFound->value,
null,
404,
);
}
$draw = $item->draw;
$published = $draw !== null && in_array($draw->status, DrawResultViewService::publishedDrawStatuses(), true);
$drawPayload = $published && $draw !== null ? $this->drawResultView->summarizeDraw($draw) : null;
$detail = $item->latestSettlementDetail;
return ApiResponse::success([
'ticket_no' => $item->ticket_no,
'order_no' => $item->order?->order_no,
'draw_no' => $draw?->draw_no,
'currency_code' => $item->order?->currency_code,
'play_code' => $item->play_code,
'dimension' => $item->dimension,
'digit_slot' => $item->digit_slot,
'original_number' => $item->original_number,
'normalized_number' => $item->normalized_number,
'unit_bet_amount' => (int) $item->unit_bet_amount,
'total_bet_amount' => (int) $item->total_bet_amount,
'rebate_rate_snapshot' => (string) $item->rebate_rate_snapshot,
'actual_deduct_amount' => (int) $item->actual_deduct_amount,
'status' => $item->status,
'win_amount' => (int) $item->win_amount,
'jackpot_win_amount' => (int) $item->jackpot_win_amount,
'settled_at' => $item->settled_at?->toIso8601String(),
'placed_at' => $item->order?->created_at?->toIso8601String(),
'odds_snapshot_json' => $item->odds_snapshot_json,
'combinations' => $item->combinations->map(fn ($c) => [
'combination_no' => (int) $c->combination_no,
'number_4d' => (string) $c->number_4d,
'bet_amount' => (int) $c->bet_amount,
'estimated_payout' => (int) $c->estimated_payout,
])->values()->all(),
'settlement' => $detail === null ? null : [
'matched_prize_tier' => $detail->matched_prize_tier,
'win_amount_minor' => (int) $detail->win_amount,
'jackpot_allocation_minor' => (int) $detail->jackpot_allocation_amount,
],
'published_draw_results' => $drawPayload,
]);
}
}