feat: 拆分开奖与结算审核流程,新增手动结果录入、重开和派彩审批接口

This commit is contained in:
2026-05-16 18:01:06 +08:00
parent 83046b402d
commit 4f143c7cb1
38 changed files with 1992 additions and 170 deletions

View File

@@ -4,6 +4,8 @@ namespace App\Http\Controllers\Api\V1\Admin\Draw;
use Carbon\Carbon;
use App\Models\Draw;
use App\Models\TicketItem;
use App\Models\TicketOrder;
use Illuminate\Http\Request;
use App\Support\AdminApiList;
use Illuminate\Http\JsonResponse;
@@ -56,6 +58,14 @@ final class AdminDrawIndexController extends Controller
'current_result_version' => (int) $draw->current_result_version,
'settle_version' => (int) $draw->settle_version,
'is_reopened' => (bool) $draw->is_reopened,
'total_bet_minor' => (int) TicketOrder::query()->where('draw_id', $draw->id)->sum('total_actual_deduct'),
'total_payout_minor' => (int) TicketItem::query()->where('draw_id', $draw->id)->sum('win_amount')
+ (int) TicketItem::query()->where('draw_id', $draw->id)->sum('jackpot_win_amount'),
'profit_loss_minor' => (int) TicketOrder::query()->where('draw_id', $draw->id)->sum('total_actual_deduct')
- (
(int) TicketItem::query()->where('draw_id', $draw->id)->sum('win_amount')
+ (int) TicketItem::query()->where('draw_id', $draw->id)->sum('jackpot_win_amount')
),
'updated_at' => $draw->updated_at?->toIso8601String(),
];
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Http\Controllers\Api\V1\Admin\Draw;
use App\Models\Draw;
use App\Lottery\ErrorCode;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Services\Draw\DrawAdminActionService;
final class DrawCancelController extends Controller
{
public function __construct(
private readonly DrawAdminActionService $service,
) {}
public function __invoke(Draw $draw): JsonResponse
{
try {
$cancelled = $this->service->cancelBeforeResult($draw);
} catch (\RuntimeException) {
return ApiResponse::error(trans('api.client_error'), ErrorCode::ClientHttpError->value, null, 409);
}
return ApiResponse::success([
'draw_no' => $cancelled->draw_no,
'status' => $cancelled->status,
]);
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace App\Http\Controllers\Api\V1\Admin\Draw;
use App\Models\Draw;
use App\Lottery\ErrorCode;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Services\Draw\DrawAdminActionService;
final class DrawManualCloseController extends Controller
{
public function __construct(
private readonly DrawAdminActionService $service,
) {}
public function __invoke(Draw $draw): JsonResponse
{
try {
$closed = $this->service->manualClose($draw);
} catch (\RuntimeException) {
return ApiResponse::error(trans('api.client_error'), ErrorCode::ClientHttpError->value, null, 409);
}
return ApiResponse::success([
'draw_no' => $closed->draw_no,
'status' => $closed->status,
'close_time' => $closed->close_time?->toIso8601String(),
]);
}
}

View File

@@ -0,0 +1,57 @@
<?php
namespace App\Http\Controllers\Api\V1\Admin\Draw;
use App\Models\Draw;
use App\Models\AdminUser;
use App\Lottery\ErrorCode;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Services\Draw\DrawManualResultService;
use App\Http\Requests\Admin\DrawManualResultBatchStoreRequest;
final class DrawManualResultBatchStoreController extends Controller
{
public function __construct(
private readonly DrawManualResultService $service,
) {}
public function __invoke(DrawManualResultBatchStoreRequest $request, Draw $draw): JsonResponse
{
$admin = $request->user();
if (! $admin instanceof AdminUser) {
return ApiResponse::error(
trans('admin.unauthenticated', [], $request->lotteryLocale()),
ErrorCode::AdminUnauthenticated->value,
null,
401,
);
}
try {
$batch = $this->service->createPendingBatch($draw, $admin, $request->validated('items'));
} catch (\RuntimeException) {
return ApiResponse::error(
trans('api.client_error', [], $request->lotteryLocale()),
ErrorCode::ClientHttpError->value,
null,
409,
);
}
$draw->refresh();
return ApiResponse::success([
'draw_no' => $draw->draw_no,
'status' => $draw->status,
'batch' => [
'id' => (int) $batch->id,
'result_version' => (int) $batch->result_version,
'source_type' => $batch->source_type,
'status' => $batch->status,
'items_count' => $batch->items()->count(),
],
]);
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Http\Controllers\Api\V1\Admin\Draw;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Services\Draw\DrawPlannerService;
final class DrawPlanGenerateController extends Controller
{
public function __construct(
private readonly DrawPlannerService $planner,
) {}
public function __invoke(): JsonResponse
{
return ApiResponse::success($this->planner->ensureBuffer());
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace App\Http\Controllers\Api\V1\Admin\Draw;
use App\Models\Draw;
use App\Models\AdminUser;
use App\Lottery\ErrorCode;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Services\Draw\DrawReopenService;
use App\Http\Requests\Admin\DrawReopenRequest;
final class DrawReopenController extends Controller
{
public function __construct(
private readonly DrawReopenService $service,
) {}
public function __invoke(DrawReopenRequest $request, Draw $draw): JsonResponse
{
$admin = $request->user();
if (! $admin instanceof AdminUser) {
return ApiResponse::error(
trans('admin.unauthenticated', [], $request->lotteryLocale()),
ErrorCode::AdminUnauthenticated->value,
null,
401,
);
}
if (! $admin->isSuperAdmin()) {
return ApiResponse::error(
trans('admin.permission_denied', [], $request->lotteryLocale()),
ErrorCode::AdminForbidden->value,
['required_any' => [AdminUser::ROLE_SUPER_ADMIN]],
403,
);
}
try {
$reopened = $this->service->reopenCooldownDraw($draw, $admin, $request->validated('reason') ?? null);
} catch (\RuntimeException) {
return ApiResponse::error(
trans('api.client_error', [], $request->lotteryLocale()),
ErrorCode::ClientHttpError->value,
null,
409,
);
}
return ApiResponse::success([
'draw_no' => $reopened->draw_no,
'status' => $reopened->status,
'is_reopened' => (bool) $reopened->is_reopened,
'current_result_version' => (int) $reopened->current_result_version,
'cooling_end_time' => $reopened->cooling_end_time?->toIso8601String(),
]);
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace App\Http\Controllers\Api\V1\Admin\Draw;
use App\Models\Draw;
use App\Lottery\ErrorCode;
use App\Support\ApiResponse;
use App\Lottery\DrawStatus;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Services\Draw\DrawRngRunner;
final class DrawRngRunController extends Controller
{
public function __construct(
private readonly DrawRngRunner $rng,
) {}
public function __invoke(Draw $draw): JsonResponse
{
try {
$batch = DB::transaction(function () use ($draw) {
/** @var Draw $locked */
$locked = Draw::query()->whereKey($draw->id)->lockForUpdate()->firstOrFail();
if ($locked->status !== DrawStatus::Closed->value || $locked->resultBatches()->exists()) {
throw new \RuntimeException('draw_not_runnable');
}
return $this->rng->executeLocked($locked);
});
} catch (\RuntimeException) {
return ApiResponse::error(trans('api.client_error'), ErrorCode::ClientHttpError->value, null, 409);
}
$draw->refresh();
return ApiResponse::success([
'draw_no' => $draw->draw_no,
'status' => $draw->status,
'batch' => [
'id' => (int) $batch->id,
'result_version' => (int) $batch->result_version,
'source_type' => $batch->source_type,
'status' => $batch->status,
'items_count' => $batch->items()->count(),
],
]);
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace App\Http\Controllers\Api\V1\Admin\Settlement;
use App\Models\AdminUser;
use App\Lottery\ErrorCode;
use App\Support\ApiResponse;
use App\Models\SettlementBatch;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\SettlementBatchReviewRequest;
use App\Services\Settlement\SettlementBatchWorkflowService;
final class AdminSettlementBatchApproveController extends Controller
{
public function __construct(private readonly SettlementBatchWorkflowService $service) {}
public function __invoke(SettlementBatchReviewRequest $request, SettlementBatch $batch): JsonResponse
{
$admin = $request->user();
if (! $admin instanceof AdminUser) {
return ApiResponse::error(trans('admin.unauthenticated'), ErrorCode::AdminUnauthenticated->value, null, 401);
}
try {
$updated = $this->service->approve($batch, $admin, $request->validated('remark') ?? null);
} catch (\RuntimeException) {
return ApiResponse::error(trans('api.client_error'), ErrorCode::ClientHttpError->value, null, 409);
}
return ApiResponse::success([
'id' => (int) $updated->id,
'status' => $updated->status,
'review_status' => $updated->review_status,
]);
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Http\Controllers\Api\V1\Admin\Settlement;
use App\Models\SettlementBatch;
use Symfony\Component\HttpFoundation\StreamedResponse;
final class AdminSettlementBatchExportController
{
public function __invoke(SettlementBatch $batch): StreamedResponse
{
$batch->load(['draw:id,draw_no']);
$filename = 'settlement-'.$batch->id.'-'.($batch->draw?->draw_no ?? 'draw').'.csv';
return response()->streamDownload(function () use ($batch): void {
$out = fopen('php://output', 'w');
fputcsv($out, ['ticket_no', 'play_code', 'player_id', 'matched_prize_tier', 'win_amount', 'jackpot_amount', 'match_detail']);
$batch->details()->with('ticketItem')->orderBy('id')->chunk(200, function ($rows) use ($out): void {
foreach ($rows as $row) {
fputcsv($out, [
$row->ticketItem?->ticket_no,
$row->ticketItem?->play_code,
$row->ticketItem?->player_id,
$row->matched_prize_tier,
(int) $row->win_amount,
(int) $row->jackpot_allocation_amount,
json_encode($row->match_detail_json, JSON_UNESCAPED_UNICODE),
]);
}
});
fclose($out);
}, $filename, ['Content-Type' => 'text/csv; charset=UTF-8']);
}
}

View File

@@ -46,6 +46,9 @@ final class AdminSettlementBatchIndexController extends Controller
'result_batch_id' => (int) $b->result_batch_id,
'settle_version' => (int) $b->settle_version,
'status' => $b->status,
'review_status' => $b->review_status,
'reviewed_at' => $b->reviewed_at?->toIso8601String(),
'paid_at' => $b->paid_at?->toIso8601String(),
'total_ticket_count' => (int) $b->total_ticket_count,
'total_win_count' => (int) $b->total_win_count,
'total_payout_amount' => (int) $b->total_payout_amount,

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Http\Controllers\Api\V1\Admin\Settlement;
use App\Lottery\ErrorCode;
use App\Support\ApiResponse;
use App\Models\SettlementBatch;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Services\Settlement\SettlementBatchWorkflowService;
final class AdminSettlementBatchPayoutController extends Controller
{
public function __construct(private readonly SettlementBatchWorkflowService $service) {}
public function __invoke(SettlementBatch $batch): JsonResponse
{
try {
$updated = $this->service->payout($batch);
} catch (\RuntimeException) {
return ApiResponse::error(trans('api.client_error'), ErrorCode::ClientHttpError->value, null, 409);
}
return ApiResponse::success([
'id' => (int) $updated->id,
'status' => $updated->status,
'paid_at' => $updated->paid_at?->toIso8601String(),
]);
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace App\Http\Controllers\Api\V1\Admin\Settlement;
use App\Models\AdminUser;
use App\Lottery\ErrorCode;
use App\Support\ApiResponse;
use App\Models\SettlementBatch;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\SettlementBatchReviewRequest;
use App\Services\Settlement\SettlementBatchWorkflowService;
final class AdminSettlementBatchRejectController extends Controller
{
public function __construct(private readonly SettlementBatchWorkflowService $service) {}
public function __invoke(SettlementBatchReviewRequest $request, SettlementBatch $batch): JsonResponse
{
$admin = $request->user();
if (! $admin instanceof AdminUser) {
return ApiResponse::error(trans('admin.unauthenticated'), ErrorCode::AdminUnauthenticated->value, null, 401);
}
try {
$updated = $this->service->reject($batch, $admin, $request->validated('remark') ?? null);
} catch (\RuntimeException) {
return ApiResponse::error(trans('api.client_error'), ErrorCode::ClientHttpError->value, null, 409);
}
return ApiResponse::success([
'id' => (int) $updated->id,
'status' => $updated->status,
'review_status' => $updated->review_status,
]);
}
}

View File

@@ -26,6 +26,11 @@ final class AdminSettlementBatchShowController extends Controller
'result_batch_status' => $batch->resultBatch?->status,
'settle_version' => (int) $batch->settle_version,
'status' => $batch->status,
'review_status' => $batch->review_status,
'reviewed_by' => $batch->reviewed_by,
'reviewed_at' => $batch->reviewed_at?->toIso8601String(),
'review_remark' => $batch->review_remark,
'paid_at' => $batch->paid_at?->toIso8601String(),
'total_ticket_count' => (int) $batch->total_ticket_count,
'total_win_count' => (int) $batch->total_win_count,
'total_payout_amount' => (int) $batch->total_payout_amount,

View File

@@ -4,7 +4,10 @@ namespace App\Http\Controllers\Api\V1\Ticket;
use App\Models\Player;
use App\Lottery\ErrorCode;
use App\Models\SettlementBatch;
use App\Models\TicketItem;
use App\Models\TicketOrder;
use App\Models\WalletTxn;
use App\Support\ApiResponse;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
@@ -51,6 +54,89 @@ final class TicketItemShowController extends Controller
$drawPayload = $published && $draw !== null ? $this->drawResultView->summarizeDraw($draw) : null;
$detail = $item->latestSettlementDetail;
$settlementBatch = $detail?->batch;
$order = $item->order;
$betTxn = WalletTxn::query()
->where('player_id', $player->id)
->where('biz_type', 'bet_deduct')
->where('biz_no', $order?->order_no)
->orderByDesc('id')
->first();
$payoutTxn = WalletTxn::query()
->where('player_id', $player->id)
->where('biz_type', 'settle_payout')
->where('biz_no', 'like', 'SB%')
->orderByDesc('id')
->first();
$timeline = [];
if ($order?->created_at !== null) {
$timeline[] = [
'code' => 'placed',
'label' => '已下注',
'time' => $order->created_at->toIso8601String(),
];
}
if ($betTxn?->created_at !== null) {
$timeline[] = [
'code' => 'deducted',
'label' => '已扣款',
'time' => $betTxn->created_at->toIso8601String(),
];
}
if ($drawPayload !== null && $draw?->current_result_version !== null) {
$timeline[] = [
'code' => 'draw_published',
'label' => '开奖结果已发布',
'time' => $draw->draw_time?->toIso8601String() ?? $draw->updated_at?->toIso8601String(),
];
}
if ($settlementBatch?->started_at !== null) {
$timeline[] = [
'code' => 'settlement_started',
'label' => '结算开始',
'time' => $settlementBatch->started_at->toIso8601String(),
];
}
if ($item->settled_at !== null) {
$timeline[] = [
'code' => 'settled',
'label' => $item->status === 'settled_win' ? '已派彩' : '已结算',
'time' => $item->settled_at->toIso8601String(),
];
} elseif ($payoutTxn?->created_at !== null) {
$timeline[] = [
'code' => 'settled',
'label' => '已派彩',
'time' => $payoutTxn->created_at->toIso8601String(),
];
}
$matchDetail = $detail?->match_detail_json;
$matchedLines = [];
if (is_array($matchDetail['lines'] ?? null)) {
foreach ($matchDetail['lines'] as $line) {
if (! is_array($line)) {
continue;
}
$matchedLines[] = [
'number_4d' => isset($line['number_4d']) ? (string) $line['number_4d'] : null,
'matched_tier' => isset($line['matched_tier']) ? (string) $line['matched_tier'] : null,
'bet_amount' => isset($line['bet_amount']) ? (int) $line['bet_amount'] : null,
'odds_value' => isset($line['odds_value']) ? (int) $line['odds_value'] : null,
'payout' => isset($line['payout']) ? (int) $line['payout'] : null,
];
}
}
$matchResult = [
'matched' => $detail !== null && ((int) $detail->win_amount > 0 || (int) $detail->jackpot_allocation_amount > 0),
'matched_prize_tier' => $detail?->matched_prize_tier,
'win_amount_minor' => $detail !== null ? (int) $detail->win_amount : 0,
'jackpot_allocation_minor' => $detail !== null ? (int) $detail->jackpot_allocation_amount : 0,
'match_detail' => $matchDetail,
'lines' => $matchedLines,
];
return ApiResponse::success([
'ticket_no' => $item->ticket_no,
@@ -83,6 +169,8 @@ final class TicketItemShowController extends Controller
'win_amount_minor' => (int) $detail->win_amount,
'jackpot_allocation_minor' => (int) $detail->jackpot_allocation_amount,
],
'match_result' => $matchResult,
'timeline' => $timeline,
'published_draw_results' => $drawPayload,
]);
}

View File

@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api\V1\Ticket;
use App\Models\Player;
use App\Models\TicketItem;
use App\Models\WalletTxn;
use App\Support\ApiResponse;
use Illuminate\Http\Request;
use App\Support\PaginationTrait;
@@ -26,6 +27,14 @@ final class TicketItemsIndexController extends Controller
$perPage = $this->perPage($request, 'per_page', 20, 50);
$page = $this->page($request);
$drawNo = $request->query('draw_no');
$statusInput = $request->query('status', []);
$statusValues = is_array($statusInput) ? array_values(array_filter(array_map(
fn ($status) => is_string($status) ? trim($status) : '',
$statusInput,
))) : [];
$number = trim((string) $request->query('number', ''));
$startDate = $this->normalizeDate((string) $request->query('start_date', ''));
$endDate = $this->normalizeDate((string) $request->query('end_date', ''));
$query = TicketItem::query()
->where('ticket_items.player_id', $player->id)
@@ -40,6 +49,26 @@ final class TicketItemsIndexController extends Controller
$query->whereHas('draw', fn ($q) => $q->where('draw_no', $drawNo));
}
if ($statusValues !== []) {
$query->whereIn('ticket_items.status', $statusValues);
}
if ($number !== '') {
$query->where(function ($q) use ($number): void {
$q->where('ticket_items.original_number', 'like', '%'.$number.'%')
->orWhere('ticket_items.normalized_number', 'like', '%'.$number.'%')
->orWhere('ticket_items.ticket_no', 'like', '%'.$number.'%');
});
}
if ($startDate !== null) {
$query->whereHas('order', fn ($q) => $q->whereDate('created_at', '>=', $startDate));
}
if ($endDate !== null) {
$query->whereHas('order', fn ($q) => $q->whereDate('created_at', '<=', $endDate));
}
$paginator = $query->paginate(perPage: $perPage, page: $page);
$items = collect($paginator->items())->map(function (TicketItem $row): array {
@@ -77,4 +106,14 @@ final class TicketItemsIndexController extends Controller
'last_page' => $paginator->lastPage(),
]);
}
private function normalizeDate(string $value): ?string
{
$value = trim($value);
if ($value === '' || ! preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
return null;
}
return $value;
}
}