feat: 添加结算功能,更新 TicketItem 模型以支持最新结算详情,增强 DrawTickService 以自动处理结算,更新 TicketWalletService 以支持派彩入账,扩展 API 路由以管理结算批次和奖池
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Draw;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Models\AdminUser;
|
||||
use App\Models\Draw;
|
||||
use App\Services\Settlement\SettlementOrchestrator;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* POST /api/v1/admin/draws/{draw}/settlement/run — 对 `settling` 期号执行结算(可关自动结算时手工触发)。
|
||||
*/
|
||||
final class DrawSettlementRunController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly SettlementOrchestrator $orchestrator,
|
||||
) {}
|
||||
|
||||
public function __invoke(Request $request, Draw $draw): JsonResponse
|
||||
{
|
||||
$admin = $request->user();
|
||||
if (! $admin instanceof AdminUser) {
|
||||
return ApiResponse::error(
|
||||
trans('admin.unauthenticated', [], $request->lotteryLocale()),
|
||||
ErrorCode::AdminUnauthenticated->value,
|
||||
null,
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
||||
$ran = $this->orchestrator->trySettleDraw($draw);
|
||||
|
||||
$draw->refresh();
|
||||
|
||||
return ApiResponse::success([
|
||||
'ran' => $ran,
|
||||
'draw_no' => $draw->draw_no,
|
||||
'status' => $draw->status,
|
||||
'settle_version' => (int) $draw->settle_version,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Jackpot;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\JackpotContribution;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* GET /api/v1/admin/jackpot/contributions — Jackpot 蓄水流水。
|
||||
*/
|
||||
final class AdminJackpotContributionIndexController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request): JsonResponse
|
||||
{
|
||||
$perPage = min(max((int) $request->integer('per_page', 25), 1), 100);
|
||||
$page = max((int) $request->integer('page', 1), 1);
|
||||
$drawNo = trim((string) $request->query('draw_no', ''));
|
||||
|
||||
$q = JackpotContribution::query()
|
||||
->with(['draw:id,draw_no', 'pool:id,currency_code', 'player:id,username,site_player_id', 'ticketItem:id,ticket_no'])
|
||||
->orderByDesc('id');
|
||||
|
||||
if ($drawNo !== '') {
|
||||
$q->whereHas('draw', fn ($d) => $d->where('draw_no', 'like', '%'.$drawNo.'%'));
|
||||
}
|
||||
|
||||
$paginator = $q->paginate($perPage, ['*'], 'page', $page);
|
||||
|
||||
return ApiResponse::success([
|
||||
'items' => collect($paginator->items())->map(fn (JackpotContribution $r) => [
|
||||
'id' => (int) $r->id,
|
||||
'draw_id' => (int) $r->draw_id,
|
||||
'draw_no' => $r->draw?->draw_no,
|
||||
'jackpot_pool_id' => (int) $r->jackpot_pool_id,
|
||||
'currency_code' => $r->pool?->currency_code,
|
||||
'player_id' => (int) $r->player_id,
|
||||
'player_username' => $r->player?->username,
|
||||
'ticket_item_id' => $r->ticket_item_id !== null ? (int) $r->ticket_item_id : null,
|
||||
'ticket_no' => $r->ticketItem?->ticket_no,
|
||||
'contribution_amount' => (int) $r->contribution_amount,
|
||||
'created_at' => $r->created_at?->toIso8601String(),
|
||||
])->all(),
|
||||
'meta' => [
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'total' => $paginator->total(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Jackpot;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\JackpotPayoutLog;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* GET /api/v1/admin/jackpot/payout-logs — Jackpot 派彩(爆池)记录。
|
||||
*/
|
||||
final class AdminJackpotPayoutLogIndexController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request): JsonResponse
|
||||
{
|
||||
$perPage = min(max((int) $request->integer('per_page', 25), 1), 100);
|
||||
$page = max((int) $request->integer('page', 1), 1);
|
||||
$drawNo = trim((string) $request->query('draw_no', ''));
|
||||
|
||||
$q = JackpotPayoutLog::query()
|
||||
->with(['draw:id,draw_no', 'pool:id,currency_code'])
|
||||
->orderByDesc('id');
|
||||
|
||||
if ($drawNo !== '') {
|
||||
$q->whereHas('draw', fn ($d) => $d->where('draw_no', 'like', '%'.$drawNo.'%'));
|
||||
}
|
||||
|
||||
$paginator = $q->paginate($perPage, ['*'], 'page', $page);
|
||||
|
||||
return ApiResponse::success([
|
||||
'items' => collect($paginator->items())->map(fn (JackpotPayoutLog $r) => [
|
||||
'id' => (int) $r->id,
|
||||
'draw_id' => (int) $r->draw_id,
|
||||
'draw_no' => $r->draw?->draw_no,
|
||||
'jackpot_pool_id' => (int) $r->jackpot_pool_id,
|
||||
'currency_code' => $r->pool?->currency_code,
|
||||
'trigger_type' => $r->trigger_type,
|
||||
'total_payout_amount' => (int) $r->total_payout_amount,
|
||||
'winner_count' => (int) $r->winner_count,
|
||||
'trigger_snapshot_json' => $r->trigger_snapshot_json,
|
||||
'created_at' => $r->created_at?->toIso8601String(),
|
||||
])->all(),
|
||||
'meta' => [
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'total' => $paginator->total(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Jackpot;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\JackpotPool;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
/**
|
||||
* GET /api/v1/admin/jackpot/pools — Jackpot 奖池配置列表。
|
||||
*/
|
||||
final class AdminJackpotPoolIndexController extends Controller
|
||||
{
|
||||
public function __invoke(): JsonResponse
|
||||
{
|
||||
$rows = JackpotPool::query()->orderBy('currency_code')->get();
|
||||
|
||||
return ApiResponse::success([
|
||||
'items' => $rows->map(fn (JackpotPool $p) => $this->row($p))->values()->all(),
|
||||
]);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function row(JackpotPool $p): array
|
||||
{
|
||||
return [
|
||||
'id' => (int) $p->id,
|
||||
'currency_code' => $p->currency_code,
|
||||
'current_amount' => (int) $p->current_amount,
|
||||
'contribution_rate' => (string) $p->contribution_rate,
|
||||
'trigger_threshold' => (int) $p->trigger_threshold,
|
||||
'payout_rate' => (string) $p->payout_rate,
|
||||
'force_trigger_draw_gap' => (int) $p->force_trigger_draw_gap,
|
||||
'min_bet_amount' => (int) $p->min_bet_amount,
|
||||
'status' => (int) $p->status,
|
||||
'last_trigger_draw_id' => $p->last_trigger_draw_id !== null ? (int) $p->last_trigger_draw_id : null,
|
||||
'updated_at' => $p->updated_at?->toIso8601String(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Jackpot;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\JackpotPool;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* PUT /api/v1/admin/jackpot/pools/{pool} — 更新奖池运营参数(蓄水比例、阈值等)。
|
||||
*/
|
||||
final class AdminJackpotPoolUpdateController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request, JackpotPool $pool): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'current_amount' => 'sometimes|integer|min:0',
|
||||
'contribution_rate' => 'sometimes|numeric|min:0|max:1',
|
||||
'trigger_threshold' => 'sometimes|integer|min:0',
|
||||
'payout_rate' => 'sometimes|numeric|min:0|max:1',
|
||||
'force_trigger_draw_gap' => 'sometimes|integer|min:0',
|
||||
'min_bet_amount' => 'sometimes|integer|min:0',
|
||||
'status' => 'sometimes|integer|in:0,1',
|
||||
]);
|
||||
|
||||
$pool->fill($data);
|
||||
$pool->save();
|
||||
|
||||
return ApiResponse::success([
|
||||
'id' => (int) $pool->id,
|
||||
'currency_code' => $pool->currency_code,
|
||||
'current_amount' => (int) $pool->current_amount,
|
||||
'contribution_rate' => (string) $pool->contribution_rate,
|
||||
'trigger_threshold' => (int) $pool->trigger_threshold,
|
||||
'payout_rate' => (string) $pool->payout_rate,
|
||||
'force_trigger_draw_gap' => (int) $pool->force_trigger_draw_gap,
|
||||
'min_bet_amount' => (int) $pool->min_bet_amount,
|
||||
'status' => (int) $pool->status,
|
||||
'last_trigger_draw_id' => $pool->last_trigger_draw_id !== null ? (int) $pool->last_trigger_draw_id : null,
|
||||
'updated_at' => $pool->updated_at?->toIso8601String(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Settlement;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\SettlementBatch;
|
||||
use App\Models\TicketSettlementDetail;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* GET /api/v1/admin/settlement-batches/{batch}/details — 该批次下注单结算明细分页。
|
||||
*/
|
||||
final class AdminSettlementBatchDetailsController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request, SettlementBatch $batch): JsonResponse
|
||||
{
|
||||
$perPage = min(max((int) $request->integer('per_page', 25), 1), 100);
|
||||
$page = max((int) $request->integer('page', 1), 1);
|
||||
|
||||
$paginator = TicketSettlementDetail::query()
|
||||
->where('settlement_batch_id', $batch->id)
|
||||
->with([
|
||||
'ticketItem:id,ticket_no,play_code,player_id',
|
||||
'ticketItem.player:id,username,site_player_id',
|
||||
])
|
||||
->orderBy('id')
|
||||
->paginate($perPage, ['*'], 'page', $page);
|
||||
|
||||
return ApiResponse::success([
|
||||
'batch_id' => (int) $batch->id,
|
||||
'items' => collect($paginator->items())->map(function ($row) {
|
||||
/** @var TicketSettlementDetail $row */
|
||||
$item = $row->ticketItem;
|
||||
$player = $item?->player;
|
||||
|
||||
return [
|
||||
'id' => (int) $row->id,
|
||||
'ticket_item_id' => (int) $row->ticket_item_id,
|
||||
'ticket_no' => $item?->ticket_no,
|
||||
'play_code' => $item?->play_code,
|
||||
'player_id' => $item?->player_id,
|
||||
'player_username' => $player?->username,
|
||||
'site_player_id' => $player?->site_player_id,
|
||||
'matched_prize_tier' => $row->matched_prize_tier,
|
||||
'win_amount' => (int) $row->win_amount,
|
||||
'jackpot_allocation_amount' => (int) $row->jackpot_allocation_amount,
|
||||
'match_detail_json' => $row->match_detail_json,
|
||||
'created_at' => $row->created_at?->toIso8601String(),
|
||||
];
|
||||
})->all(),
|
||||
'meta' => [
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'total' => $paginator->total(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Settlement;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\SettlementBatch;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* GET /api/v1/admin/settlement-batches — 结算批次分页列表。
|
||||
*/
|
||||
final class AdminSettlementBatchIndexController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request): JsonResponse
|
||||
{
|
||||
$perPage = min(max((int) $request->integer('per_page', 25), 1), 100);
|
||||
$page = max((int) $request->integer('page', 1), 1);
|
||||
$drawNo = trim((string) $request->query('draw_no', ''));
|
||||
$status = trim((string) $request->query('status', ''));
|
||||
|
||||
$q = SettlementBatch::query()
|
||||
->with(['draw:id,draw_no'])
|
||||
->orderByDesc('id');
|
||||
|
||||
if ($drawNo !== '') {
|
||||
$q->whereHas('draw', fn ($d) => $d->where('draw_no', 'like', '%'.$drawNo.'%'));
|
||||
}
|
||||
|
||||
if ($status !== '') {
|
||||
$q->where('status', $status);
|
||||
}
|
||||
|
||||
$paginator = $q->paginate($perPage, ['*'], 'page', $page);
|
||||
|
||||
return ApiResponse::success([
|
||||
'items' => collect($paginator->items())->map(fn (SettlementBatch $b) => $this->row($b))->all(),
|
||||
'meta' => [
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'total' => $paginator->total(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function row(SettlementBatch $b): array
|
||||
{
|
||||
return [
|
||||
'id' => (int) $b->id,
|
||||
'draw_id' => (int) $b->draw_id,
|
||||
'draw_no' => $b->draw?->draw_no,
|
||||
'result_batch_id' => (int) $b->result_batch_id,
|
||||
'settle_version' => (int) $b->settle_version,
|
||||
'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,
|
||||
'started_at' => $b->started_at?->toIso8601String(),
|
||||
'finished_at' => $b->finished_at?->toIso8601String(),
|
||||
'created_at' => $b->created_at?->toIso8601String(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Settlement;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\SettlementBatch;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
/**
|
||||
* GET /api/v1/admin/settlement-batches/{batch} — 单批次摘要。
|
||||
*/
|
||||
final class AdminSettlementBatchShowController extends Controller
|
||||
{
|
||||
public function __invoke(SettlementBatch $batch): JsonResponse
|
||||
{
|
||||
$batch->load(['draw:id,draw_no,business_date,status', 'resultBatch:id,result_version,status']);
|
||||
|
||||
return ApiResponse::success([
|
||||
'id' => (int) $batch->id,
|
||||
'draw_id' => (int) $batch->draw_id,
|
||||
'draw_no' => $batch->draw?->draw_no,
|
||||
'draw_status' => $batch->draw?->status,
|
||||
'result_batch_id' => (int) $batch->result_batch_id,
|
||||
'result_batch_version' => $batch->resultBatch?->result_version,
|
||||
'result_batch_status' => $batch->resultBatch?->status,
|
||||
'settle_version' => (int) $batch->settle_version,
|
||||
'status' => $batch->status,
|
||||
'total_ticket_count' => (int) $batch->total_ticket_count,
|
||||
'total_win_count' => (int) $batch->total_win_count,
|
||||
'total_payout_amount' => (int) $batch->total_payout_amount,
|
||||
'total_jackpot_payout_amount' => (int) $batch->total_jackpot_payout_amount,
|
||||
'started_at' => $batch->started_at?->toIso8601String(),
|
||||
'finished_at' => $batch->finished_at?->toIso8601String(),
|
||||
'created_at' => $batch->created_at?->toIso8601String(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Jackpot;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\JackpotPool;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* `GET /api/v1/jackpot/summary` — 当前奖池水位(公开;玩家端开奖区展示)。
|
||||
*/
|
||||
class JackpotSummaryController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request): JsonResponse
|
||||
{
|
||||
$currency = strtoupper(trim((string) $request->query('currency_code', 'NPR')));
|
||||
if (strlen($currency) > 16) {
|
||||
$currency = 'NPR';
|
||||
}
|
||||
|
||||
$pool = JackpotPool::query()
|
||||
->where('currency_code', $currency)
|
||||
->where('status', 1)
|
||||
->first();
|
||||
|
||||
return ApiResponse::success([
|
||||
'currency_code' => $currency,
|
||||
'enabled' => $pool !== null,
|
||||
'current_amount_minor' => $pool !== null ? (int) $pool->current_amount : 0,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Ticket;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Draw;
|
||||
use App\Models\Player;
|
||||
use App\Models\TicketCombination;
|
||||
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/draws/{draw_no}/my-match` — 当期本人号码与已发布开奖 23 格的交集(用于开奖页高亮)。
|
||||
*/
|
||||
class TicketDrawMyMatchController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DrawResultViewService $drawResultView,
|
||||
) {}
|
||||
|
||||
public function __invoke(Request $request, string $draw_no): JsonResponse
|
||||
{
|
||||
/** @var Player $player */
|
||||
$player = $request->attributes->get('lottery_player');
|
||||
$draw_no = trim($draw_no);
|
||||
|
||||
$draw = Draw::query()->where('draw_no', $draw_no)->first();
|
||||
if ($draw === null || ! in_array($draw->status, DrawResultViewService::publishedDrawStatuses(), true)) {
|
||||
return ApiResponse::success([
|
||||
'draw_no' => $draw_no,
|
||||
'hit_numbers_4d' => [],
|
||||
'total_win_minor' => 0,
|
||||
'total_jackpot_win_minor' => 0,
|
||||
'has_bets' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
$payload = $this->drawResultView->summarizeDraw($draw);
|
||||
if ($payload === null) {
|
||||
return ApiResponse::success([
|
||||
'draw_no' => $draw_no,
|
||||
'hit_numbers_4d' => [],
|
||||
'total_win_minor' => 0,
|
||||
'total_jackpot_win_minor' => 0,
|
||||
'has_bets' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
$board = collect($payload['result_items'] ?? [])
|
||||
->pluck('number_4d')
|
||||
->filter()
|
||||
->map(fn ($n) => self::norm4d((string) $n))
|
||||
->unique()
|
||||
->flip();
|
||||
|
||||
$itemIds = TicketItem::query()
|
||||
->where('draw_id', $draw->id)
|
||||
->where('player_id', $player->id)
|
||||
->whereIn('status', ['success', 'settled_win', 'settled_lose'])
|
||||
->pluck('id');
|
||||
|
||||
$hasBets = $itemIds->isNotEmpty();
|
||||
|
||||
$hits = [];
|
||||
if ($hasBets) {
|
||||
$hits = TicketCombination::query()
|
||||
->whereIn('ticket_item_id', $itemIds)
|
||||
->pluck('number_4d')
|
||||
->map(fn ($n) => self::norm4d((string) $n))
|
||||
->filter(fn (string $n) => isset($board[$n]))
|
||||
->unique()
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
$sums = TicketItem::query()
|
||||
->where('draw_id', $draw->id)
|
||||
->where('player_id', $player->id)
|
||||
->whereIn('status', ['settled_win', 'settled_lose'])
|
||||
->selectRaw('coalesce(sum(win_amount),0) as sum_win, coalesce(sum(jackpot_win_amount),0) as sum_jackpot')
|
||||
->first();
|
||||
|
||||
return ApiResponse::success([
|
||||
'draw_no' => $draw_no,
|
||||
'hit_numbers_4d' => $hits,
|
||||
'total_win_minor' => (int) ($sums->sum_win ?? 0),
|
||||
'total_jackpot_win_minor' => (int) ($sums->sum_jackpot ?? 0),
|
||||
'has_bets' => $hasBets,
|
||||
]);
|
||||
}
|
||||
|
||||
private static function norm4d(string $n): string
|
||||
{
|
||||
$n = preg_replace('/\D/', '', $n) ?? '';
|
||||
|
||||
return str_pad(substr($n, -4), 4, '0', STR_PAD_LEFT);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Ticket;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Player;
|
||||
use App\Models\TicketItem;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* `GET /api/v1/ticket/items` — 我的注单(注项列表,支持 `draw_no` 筛选)。
|
||||
*/
|
||||
class TicketItemsIndexController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request): JsonResponse
|
||||
{
|
||||
/** @var Player $player */
|
||||
$player = $request->attributes->get('lottery_player');
|
||||
|
||||
$perPage = max(1, min(50, (int) $request->query('per_page', 20)));
|
||||
$page = max(1, (int) $request->query('page', 1));
|
||||
$drawNo = $request->query('draw_no');
|
||||
|
||||
$query = TicketItem::query()
|
||||
->where('ticket_items.player_id', $player->id)
|
||||
->with([
|
||||
'draw:id,draw_no,business_date',
|
||||
'order:id,order_no,currency_code,created_at',
|
||||
])
|
||||
->orderByDesc('ticket_items.id');
|
||||
|
||||
if (is_string($drawNo) && $drawNo !== '') {
|
||||
$drawNo = trim($drawNo);
|
||||
$query->whereHas('draw', fn ($q) => $q->where('draw_no', $drawNo));
|
||||
}
|
||||
|
||||
$paginator = $query->paginate(perPage: $perPage, page: $page);
|
||||
|
||||
$items = collect($paginator->items())->map(function (TicketItem $row): array {
|
||||
return [
|
||||
'ticket_no' => $row->ticket_no,
|
||||
'order_no' => $row->order?->order_no,
|
||||
'draw_no' => $row->draw?->draw_no,
|
||||
'currency_code' => $row->order?->currency_code,
|
||||
'play_code' => $row->play_code,
|
||||
'original_number' => $row->original_number,
|
||||
'total_bet_amount' => (int) $row->total_bet_amount,
|
||||
'actual_deduct_amount' => (int) $row->actual_deduct_amount,
|
||||
'status' => $row->status,
|
||||
'win_amount' => (int) $row->win_amount,
|
||||
'jackpot_win_amount' => (int) $row->jackpot_win_amount,
|
||||
'placed_at' => $row->order?->created_at?->toIso8601String(),
|
||||
'updated_at' => $row->updated_at?->toIso8601String(),
|
||||
];
|
||||
})->values()->all();
|
||||
|
||||
return ApiResponse::success([
|
||||
'items' => $items,
|
||||
'total' => $paginator->total(),
|
||||
'page' => $paginator->currentPage(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user