feat: 添加新的错误码以支持投注功能,更新数据库填充器以增强玩法和赔率配置,扩展 API 路由以支持风险池管理
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Risk;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Draw;
|
||||
use App\Models\RiskPool;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* GET /api/v1/admin/draws/{draw}/risk-pools — 按期号分页查询赔付池事实(售罄筛选、排序)。
|
||||
*
|
||||
* 对齐产品文档:后台查看售罄与风险占用、热门号码监控(按占用排序)。
|
||||
*/
|
||||
final class AdminRiskPoolIndexController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request, Draw $draw): JsonResponse
|
||||
{
|
||||
$perPage = min(max((int) $request->integer('per_page', 25), 1), 100);
|
||||
$soldOutOnly = $request->boolean('sold_out_only');
|
||||
$sort = trim((string) $request->query('sort', 'usage_desc'));
|
||||
|
||||
$q = RiskPool::query()->where('draw_id', $draw->id);
|
||||
|
||||
if ($soldOutOnly) {
|
||||
$q->where('sold_out_status', 1);
|
||||
}
|
||||
|
||||
match ($sort) {
|
||||
'locked_desc' => $q->orderByDesc('locked_amount')->orderBy('normalized_number'),
|
||||
'remaining_asc' => $q->orderBy('remaining_amount')->orderBy('normalized_number'),
|
||||
'number_asc' => $q->orderBy('normalized_number'),
|
||||
default => $q->orderByRaw('(locked_amount * 1.0 / NULLIF(total_cap_amount, 0)) DESC')
|
||||
->orderByDesc('locked_amount')
|
||||
->orderBy('normalized_number'),
|
||||
};
|
||||
|
||||
/** @var LengthAwarePaginator $paginator */
|
||||
$paginator = $q->paginate($perPage);
|
||||
|
||||
return ApiResponse::success([
|
||||
'draw_id' => (int) $draw->id,
|
||||
'draw_no' => $draw->draw_no,
|
||||
'items' => collect($paginator->items())->map(fn (RiskPool $row) => $this->row($row))->all(),
|
||||
'meta' => [
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'total' => $paginator->total(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function row(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,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Risk;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Draw;
|
||||
use App\Models\RiskPoolLockLog;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* GET /api/v1/admin/draws/{draw}/risk-pool-lock-logs — 风险池占用/释放流水(审计与监控)。
|
||||
*/
|
||||
final class AdminRiskPoolLockLogIndexController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request, Draw $draw): JsonResponse
|
||||
{
|
||||
$perPage = min(max((int) $request->integer('per_page', 25), 1), 100);
|
||||
$action = trim((string) $request->query('action_type', ''));
|
||||
$number = trim((string) $request->query('normalized_number', ''));
|
||||
|
||||
$q = RiskPoolLockLog::query()
|
||||
->where('draw_id', $draw->id)
|
||||
->with(['ticketItem:id,ticket_no,play_code,player_id'])
|
||||
->orderByDesc('created_at')
|
||||
->orderByDesc('id');
|
||||
|
||||
if ($action !== '' && in_array($action, ['lock', 'release'], true)) {
|
||||
$q->where('action_type', $action);
|
||||
}
|
||||
|
||||
if ($number !== '' && preg_match('/^[0-9]{4}$/', $number) === 1) {
|
||||
$q->where('normalized_number', $number);
|
||||
}
|
||||
|
||||
/** @var LengthAwarePaginator $paginator */
|
||||
$paginator = $q->paginate($perPage);
|
||||
|
||||
return ApiResponse::success([
|
||||
'draw_id' => (int) $draw->id,
|
||||
'draw_no' => $draw->draw_no,
|
||||
'items' => collect($paginator->items())->map(fn (RiskPoolLockLog $log) => $this->row($log))->all(),
|
||||
'meta' => [
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'total' => $paginator->total(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function row(RiskPoolLockLog $log): array
|
||||
{
|
||||
return [
|
||||
'id' => (int) $log->id,
|
||||
'normalized_number' => $log->normalized_number,
|
||||
'action_type' => $log->action_type,
|
||||
'amount' => (int) $log->amount,
|
||||
'source_reason' => $log->source_reason,
|
||||
'ticket_item_id' => $log->ticket_item_id,
|
||||
'ticket_no' => $log->ticketItem?->ticket_no,
|
||||
'play_code' => $log->ticketItem?->play_code,
|
||||
'player_id' => $log->ticketItem?->player_id,
|
||||
'created_at' => $log->created_at?->toIso8601String(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Risk;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Models\Draw;
|
||||
use App\Models\RiskPool;
|
||||
use App\Models\RiskPoolLockLog;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* GET /api/v1/admin/draws/{draw}/risk-pools/{number_4d} — 单号码风险池详情 + 占用流水。
|
||||
*/
|
||||
final class AdminRiskPoolShowController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request, Draw $draw, string $number_4d): JsonResponse
|
||||
{
|
||||
if (preg_match('/^[0-9]{4}$/', $number_4d) !== 1) {
|
||||
return ApiResponse::error('normalized_number 须为 4 位数字', ErrorCode::ValidationFailed->value, null, 422);
|
||||
}
|
||||
|
||||
$pool = RiskPool::query()
|
||||
->where('draw_id', $draw->id)
|
||||
->where('normalized_number', $number_4d)
|
||||
->first();
|
||||
|
||||
if ($pool === null) {
|
||||
return ApiResponse::error('该期尚无此号码的风险池记录', ErrorCode::NotFound->value, null, 404);
|
||||
}
|
||||
|
||||
$perPage = min(max((int) $request->integer('per_page', 20), 1), 100);
|
||||
|
||||
/** @var LengthAwarePaginator $paginator */
|
||||
$paginator = RiskPoolLockLog::query()
|
||||
->where('draw_id', $draw->id)
|
||||
->where('normalized_number', $number_4d)
|
||||
->with(['ticketItem:id,ticket_no,play_code,player_id'])
|
||||
->orderByDesc('created_at')
|
||||
->orderByDesc('id')
|
||||
->paginate($perPage);
|
||||
|
||||
$cap = (int) $pool->total_cap_amount;
|
||||
$locked = (int) $pool->locked_amount;
|
||||
|
||||
return ApiResponse::success([
|
||||
'draw_id' => (int) $draw->id,
|
||||
'draw_no' => $draw->draw_no,
|
||||
'pool' => [
|
||||
'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,
|
||||
],
|
||||
'logs' => [
|
||||
'items' => collect($paginator->items())->map(fn (RiskPoolLockLog $log) => $this->logRow($log))->all(),
|
||||
'meta' => [
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'total' => $paginator->total(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function logRow(RiskPoolLockLog $log): array
|
||||
{
|
||||
return [
|
||||
'id' => (int) $log->id,
|
||||
'action_type' => $log->action_type,
|
||||
'amount' => (int) $log->amount,
|
||||
'source_reason' => $log->source_reason,
|
||||
'ticket_item_id' => $log->ticket_item_id,
|
||||
'ticket_no' => $log->ticketItem?->ticket_no,
|
||||
'play_code' => $log->ticketItem?->play_code,
|
||||
'player_id' => $log->ticketItem?->player_id,
|
||||
'created_at' => $log->created_at?->toIso8601String(),
|
||||
];
|
||||
}
|
||||
}
|
||||
37
app/Http/Controllers/Api/V1/Ticket/TicketPlaceController.php
Normal file
37
app/Http/Controllers/Api/V1/Ticket/TicketPlaceController.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Ticket;
|
||||
|
||||
use App\Exceptions\TicketOperationException;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Ticket\TicketPlaceRequest;
|
||||
use App\Services\Ticket\TicketPlacementService;
|
||||
use App\Support\ApiResponse;
|
||||
use App\Support\LotteryMessage;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
final class TicketPlaceController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TicketPlacementService $placementService,
|
||||
) {}
|
||||
|
||||
public function __invoke(TicketPlaceRequest $request): JsonResponse
|
||||
{
|
||||
$player = $request->lotteryPlayer();
|
||||
abort_if($player === null, 500, 'lottery_player missing');
|
||||
|
||||
try {
|
||||
$data = $this->placementService->place($player, $request->validated());
|
||||
} catch (TicketOperationException $e) {
|
||||
return ApiResponse::error(
|
||||
LotteryMessage::wallet($request, $e->lotteryCode),
|
||||
$e->lotteryCode,
|
||||
$e->payload,
|
||||
$e->httpStatus,
|
||||
);
|
||||
}
|
||||
|
||||
return ApiResponse::success($data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Ticket;
|
||||
|
||||
use App\Exceptions\TicketOperationException;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Ticket\TicketPreviewRequest;
|
||||
use App\Services\Ticket\TicketPreviewService;
|
||||
use App\Support\ApiResponse;
|
||||
use App\Support\LotteryMessage;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
final class TicketPreviewController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TicketPreviewService $previewService,
|
||||
) {}
|
||||
|
||||
public function __invoke(TicketPreviewRequest $request): JsonResponse
|
||||
{
|
||||
try {
|
||||
$data = $this->previewService->preview($request->validated());
|
||||
} catch (TicketOperationException $e) {
|
||||
return ApiResponse::error(
|
||||
LotteryMessage::wallet($request, $e->lotteryCode),
|
||||
$e->lotteryCode,
|
||||
$e->payload,
|
||||
$e->httpStatus,
|
||||
);
|
||||
}
|
||||
|
||||
return ApiResponse::success($data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user