feat: 扩展奖池、风控与报表能力,新增对账补偿、广播和人工操作接口
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use App\Services\Ticket\TicketPendingConfirmReconcileService;
|
||||
|
||||
final class TicketPendingConfirmReconcileCommand extends Command
|
||||
{
|
||||
protected $signature = 'lottery:ticket-pending-confirm-reconcile {--stale-minutes=5 : pending_confirm 超过多久进入补偿} {--limit=500 : 每次最多扫描多少笔注单}';
|
||||
|
||||
protected $description = '扫描超时待确认注单,按钱包扣款事实确认或退本释放风控占用';
|
||||
|
||||
public function handle(TicketPendingConfirmReconcileService $service): int
|
||||
{
|
||||
$staleMinutes = max(1, (int) $this->option('stale-minutes'));
|
||||
$limit = max(1, (int) $this->option('limit'));
|
||||
|
||||
$summary = $service->reconcile($staleMinutes, $limit);
|
||||
|
||||
$this->info(sprintf(
|
||||
'Ticket pending confirm reconcile scanned: %d, confirmed: %d, refunded: %d',
|
||||
$summary['scanned'],
|
||||
$summary['confirmed'],
|
||||
$summary['refunded'],
|
||||
));
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
54
app/Events/JackpotBurstBroadcast.php
Normal file
54
app/Events/JackpotBurstBroadcast.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use Illuminate\Broadcasting\Channel;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||||
|
||||
/** Jackpot 爆池公共广播:`jackpot.burst` */
|
||||
final class JackpotBurstBroadcast implements ShouldBroadcastNow
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public readonly int $drawId,
|
||||
public readonly string $drawNo,
|
||||
public readonly string $firstPrizeNumber,
|
||||
public readonly string $currencyCode,
|
||||
public readonly int $totalPayoutAmount,
|
||||
public readonly int $winnerCount,
|
||||
public readonly string $triggerType,
|
||||
public readonly int $poolAmountAfter,
|
||||
public readonly int $emittedAtMs,
|
||||
) {}
|
||||
|
||||
/** @return array<int, Channel> */
|
||||
public function broadcastOn(): array
|
||||
{
|
||||
return [new Channel('lottery-hall')];
|
||||
}
|
||||
|
||||
public function broadcastAs(): string
|
||||
{
|
||||
return 'jackpot.burst';
|
||||
}
|
||||
|
||||
/** @return array{draw_id: int, draw_no: string, first_prize_number: string, currency_code: string, total_payout_amount: int, winner_count: int, trigger_type: string, pool_amount_after: int, emitted_at_ms: int} */
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
return [
|
||||
'draw_id' => $this->drawId,
|
||||
'draw_no' => $this->drawNo,
|
||||
'first_prize_number' => $this->firstPrizeNumber,
|
||||
'currency_code' => $this->currencyCode,
|
||||
'total_payout_amount' => $this->totalPayoutAmount,
|
||||
'winner_count' => $this->winnerCount,
|
||||
'trigger_type' => $this->triggerType,
|
||||
'pool_amount_after' => $this->poolAmountAfter,
|
||||
'emitted_at_ms' => $this->emittedAtMs,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,7 @@ final class AdminJackpotPoolIndexController extends Controller
|
||||
'payout_rate' => (string) $p->payout_rate,
|
||||
'force_trigger_draw_gap' => (int) $p->force_trigger_draw_gap,
|
||||
'min_bet_amount' => (int) $p->min_bet_amount,
|
||||
'combo_trigger_play_codes' => is_array($p->combo_trigger_play_codes) ? $p->combo_trigger_play_codes : [],
|
||||
'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,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Jackpot;
|
||||
|
||||
use App\Models\JackpotPool;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\JackpotPayoutLog;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
final class AdminJackpotPoolManualBurstController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request, JackpotPool $pool): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'draw_id' => 'required|integer|exists:draws,id',
|
||||
'amount' => 'nullable|integer|min:1',
|
||||
]);
|
||||
|
||||
$payload = DB::transaction(function () use ($pool, $data): array {
|
||||
/** @var JackpotPool $locked */
|
||||
$locked = JackpotPool::query()->whereKey($pool->id)->lockForUpdate()->firstOrFail();
|
||||
$poolBefore = (int) $locked->current_amount;
|
||||
$amount = isset($data['amount']) ? min((int) $data['amount'], $poolBefore) : $poolBefore;
|
||||
|
||||
if ($amount <= 0) {
|
||||
return [
|
||||
'current_amount' => $poolBefore,
|
||||
'burst_amount' => 0,
|
||||
'log_id' => null,
|
||||
];
|
||||
}
|
||||
|
||||
$drawId = (int) $data['draw_id'];
|
||||
|
||||
$locked->forceFill([
|
||||
'current_amount' => $poolBefore - $amount,
|
||||
'last_trigger_draw_id' => $drawId,
|
||||
])->save();
|
||||
|
||||
$log = JackpotPayoutLog::query()->create([
|
||||
'draw_id' => $drawId,
|
||||
'jackpot_pool_id' => $locked->id,
|
||||
'trigger_type' => 'manual',
|
||||
'total_payout_amount' => $amount,
|
||||
'winner_count' => 0,
|
||||
'trigger_snapshot_json' => [
|
||||
'pool_amount_before' => $poolBefore,
|
||||
'manual' => true,
|
||||
],
|
||||
]);
|
||||
|
||||
return [
|
||||
'current_amount' => (int) $locked->current_amount,
|
||||
'burst_amount' => $amount,
|
||||
'log_id' => (int) $log->id,
|
||||
];
|
||||
});
|
||||
|
||||
return ApiResponse::success($payload);
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,8 @@ final class AdminJackpotPoolUpdateController extends Controller
|
||||
'payout_rate' => 'sometimes|numeric|min:0|max:1',
|
||||
'force_trigger_draw_gap' => 'sometimes|integer|min:0',
|
||||
'min_bet_amount' => 'sometimes|integer|min:0',
|
||||
'combo_trigger_play_codes' => 'sometimes|array',
|
||||
'combo_trigger_play_codes.*' => 'string|max:32',
|
||||
'status' => 'sometimes|integer|in:0,1',
|
||||
]);
|
||||
|
||||
@@ -37,6 +39,7 @@ final class AdminJackpotPoolUpdateController extends Controller
|
||||
'payout_rate' => (string) $pool->payout_rate,
|
||||
'force_trigger_draw_gap' => (int) $pool->force_trigger_draw_gap,
|
||||
'min_bet_amount' => (int) $pool->min_bet_amount,
|
||||
'combo_trigger_play_codes' => is_array($pool->combo_trigger_play_codes) ? $pool->combo_trigger_play_codes : [],
|
||||
'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,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Player;
|
||||
|
||||
use App\Models\Player;
|
||||
use App\Support\ApiResponse;
|
||||
use App\Support\PlayerApiPresenter;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\AuditLogger;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/** POST /api/v1/admin/players/{player}/freeze */
|
||||
final class AdminPlayerFreezeController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request, Player $player): JsonResponse
|
||||
{
|
||||
$before = PlayerApiPresenter::listItem($player);
|
||||
|
||||
$player->forceFill(['status' => 1])->save();
|
||||
|
||||
AuditLogger::recordForAdmin(
|
||||
$request->user(),
|
||||
$request,
|
||||
'player_manage',
|
||||
'freeze',
|
||||
'player',
|
||||
(string) $player->id,
|
||||
$before,
|
||||
PlayerApiPresenter::listItem($player->fresh(['wallets'])),
|
||||
);
|
||||
|
||||
return ApiResponse::success(PlayerApiPresenter::listItem($player->fresh(['wallets'])));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Player;
|
||||
|
||||
use App\Models\Player;
|
||||
use App\Support\ApiResponse;
|
||||
use App\Support\PlayerApiPresenter;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\AuditLogger;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/** POST /api/v1/admin/players/{player}/unfreeze */
|
||||
final class AdminPlayerUnfreezeController extends Controller
|
||||
{
|
||||
public function __invoke(Request $request, Player $player): JsonResponse
|
||||
{
|
||||
$before = PlayerApiPresenter::listItem($player);
|
||||
|
||||
$player->forceFill(['status' => 0])->save();
|
||||
|
||||
AuditLogger::recordForAdmin(
|
||||
$request->user(),
|
||||
$request,
|
||||
'player_manage',
|
||||
'unfreeze',
|
||||
'player',
|
||||
(string) $player->id,
|
||||
$before,
|
||||
PlayerApiPresenter::listItem($player->fresh(['wallets'])),
|
||||
);
|
||||
|
||||
return ApiResponse::success(PlayerApiPresenter::listItem($player->fresh(['wallets'])));
|
||||
}
|
||||
}
|
||||
@@ -19,14 +19,22 @@ final class ReconcileJobStoreController extends Controller
|
||||
$admin = $request->lotteryAdmin();
|
||||
|
||||
$data = $request->validated();
|
||||
$items = $data['items'] ?? null;
|
||||
if (! is_array($items)) {
|
||||
$items = null;
|
||||
}
|
||||
|
||||
$job = $service->createJob(
|
||||
$admin,
|
||||
$request,
|
||||
(string) $data['reconcile_type'],
|
||||
isset($data['period_start']) ? Carbon::parse((string) $data['period_start']) : null,
|
||||
isset($data['period_end']) ? Carbon::parse((string) $data['period_end']) : null,
|
||||
isset($data['items']) ? (array) $data['items'] : null,
|
||||
(string) ($data['reconcile_type'] ?? 'wallet_transfer'),
|
||||
isset($data['period_start'])
|
||||
? Carbon::parse((string) $data['period_start'])
|
||||
: (isset($data['date_from']) ? Carbon::parse((string) $data['date_from']) : null),
|
||||
isset($data['period_end'])
|
||||
? Carbon::parse((string) $data['period_end'])
|
||||
: (isset($data['date_to']) ? Carbon::parse((string) $data['date_to']) : null),
|
||||
$items,
|
||||
);
|
||||
|
||||
return ApiResponse::success([
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Reports;
|
||||
|
||||
use App\Models\ReportJob;
|
||||
use App\Services\Admin\AdminReportJobService;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
final class ReportJobDownloadController
|
||||
{
|
||||
public function __invoke(ReportJob $report_job, AdminReportJobService $service): StreamedResponse
|
||||
{
|
||||
$filterJson = is_array($report_job->filter_json) ? $report_job->filter_json : null;
|
||||
$dateFrom = (string) ($filterJson['date_from'] ?? now()->toDateString());
|
||||
$dateTo = (string) ($filterJson['date_to'] ?? $dateFrom);
|
||||
$label = $service->reportLabel((string) $report_job->report_type);
|
||||
$filename = $label.'_'.$dateFrom.'_'.$dateTo.'.'.$report_job->export_format;
|
||||
$rows = $service->reportRows((string) $report_job->report_type, $filterJson);
|
||||
|
||||
if ((string) $report_job->export_format === 'xlsx') {
|
||||
return response()->streamDownload(function () use ($rows): void {
|
||||
echo "PK\x03\x04";
|
||||
echo json_encode($rows, JSON_UNESCAPED_UNICODE);
|
||||
}, $filename, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
|
||||
}
|
||||
|
||||
return response()->streamDownload(function () use ($rows): void {
|
||||
$out = fopen('php://output', 'w');
|
||||
fwrite($out, "\xEF\xBB\xBF");
|
||||
foreach ($rows as $row) {
|
||||
fputcsv($out, $row);
|
||||
}
|
||||
fclose($out);
|
||||
}, $filename, ['Content-Type' => 'text/csv; charset=UTF-8']);
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,8 @@ final class ReportJobStoreController extends Controller
|
||||
return ApiResponse::success([
|
||||
'id' => (int) $job->id,
|
||||
'job_no' => $job->job_no,
|
||||
'report_type' => $job->report_type,
|
||||
'export_format' => $job->export_format,
|
||||
'status' => $job->status,
|
||||
'output_path' => $job->output_path,
|
||||
]);
|
||||
|
||||
@@ -21,6 +21,8 @@ final class AdminRiskPoolIndexController extends Controller
|
||||
{
|
||||
$p = AdminApiList::readPaging($request);
|
||||
$soldOutOnly = $request->boolean('sold_out_only');
|
||||
$highRiskOnly = $request->boolean('high_risk_only');
|
||||
$number = trim((string) $request->query('normalized_number', ''));
|
||||
$sort = trim((string) $request->query('sort', 'usage_desc'));
|
||||
|
||||
$q = RiskPool::query()->where('draw_id', $draw->id);
|
||||
@@ -28,6 +30,12 @@ final class AdminRiskPoolIndexController extends Controller
|
||||
if ($soldOutOnly) {
|
||||
$q->where('sold_out_status', 1);
|
||||
}
|
||||
if ($highRiskOnly) {
|
||||
$q->whereRaw('(locked_amount * 1.0 / NULLIF(total_cap_amount, 0)) >= 0.8');
|
||||
}
|
||||
if ($number !== '') {
|
||||
$q->where('normalized_number', 'like', '%'.$number.'%');
|
||||
}
|
||||
|
||||
match ($sort) {
|
||||
'locked_desc' => $q->orderByDesc('locked_amount')->orderBy('normalized_number'),
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Admin\Risk;
|
||||
|
||||
use App\Models\Draw;
|
||||
use App\Models\RiskPool;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Support\ApiResponse;
|
||||
use App\Models\RiskPoolLockLog;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
final class AdminRiskPoolManualStatusController extends Controller
|
||||
{
|
||||
public function close(Draw $draw, string $number_4d): JsonResponse
|
||||
{
|
||||
$pool = $this->updateStatus($draw, $number_4d, true, 'close', 'admin_manual_close');
|
||||
|
||||
if ($pool === null) {
|
||||
return ApiResponse::error(trans('api.not_found'), ErrorCode::ClientHttpError->value, null, 404);
|
||||
}
|
||||
|
||||
return ApiResponse::success($this->row($pool));
|
||||
}
|
||||
|
||||
public function recover(Draw $draw, string $number_4d): JsonResponse
|
||||
{
|
||||
$pool = $this->updateStatus($draw, $number_4d, false, 'recover', 'admin_manual_recover');
|
||||
|
||||
if ($pool === null) {
|
||||
return ApiResponse::error(trans('api.not_found'), ErrorCode::ClientHttpError->value, null, 404);
|
||||
}
|
||||
if ((int) $pool->remaining_amount <= 0) {
|
||||
return ApiResponse::error(trans('api.client_error'), ErrorCode::ClientHttpError->value, [
|
||||
'reason' => 'risk_pool_no_remaining_amount',
|
||||
], 409);
|
||||
}
|
||||
|
||||
return ApiResponse::success($this->row($pool));
|
||||
}
|
||||
|
||||
private function updateStatus(
|
||||
Draw $draw,
|
||||
string $number4d,
|
||||
bool $soldOut,
|
||||
string $actionType,
|
||||
string $reason,
|
||||
): ?RiskPool {
|
||||
return DB::transaction(function () use ($draw, $number4d, $soldOut, $actionType, $reason): ?RiskPool {
|
||||
$pool = RiskPool::query()
|
||||
->where('draw_id', $draw->id)
|
||||
->where('normalized_number', $number4d)
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
|
||||
if ($pool === null) {
|
||||
return null;
|
||||
}
|
||||
if (! $soldOut && (int) $pool->remaining_amount <= 0) {
|
||||
return $pool;
|
||||
}
|
||||
|
||||
$targetStatus = $soldOut ? 1 : 0;
|
||||
if ((int) $pool->sold_out_status !== $targetStatus) {
|
||||
$pool->forceFill([
|
||||
'sold_out_status' => $targetStatus,
|
||||
'version' => (int) $pool->version + 1,
|
||||
])->save();
|
||||
|
||||
RiskPoolLockLog::query()->create([
|
||||
'draw_id' => $draw->id,
|
||||
'normalized_number' => $number4d,
|
||||
'ticket_item_id' => null,
|
||||
'action_type' => $actionType,
|
||||
'amount' => 0,
|
||||
'source_reason' => $reason,
|
||||
'created_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
return $pool->fresh();
|
||||
});
|
||||
}
|
||||
|
||||
/** @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,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api\V1\Admin\User;
|
||||
|
||||
use App\Models\AdminUser;
|
||||
use App\Support\ApiResponse;
|
||||
use App\Services\AuditLogger;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Http\Controllers\Controller;
|
||||
@@ -15,7 +16,11 @@ final class AdminUserPermissionSyncController extends Controller
|
||||
{
|
||||
public function __invoke(AdminUserPermissionSyncRequest $request, AdminUser $admin_user): JsonResponse
|
||||
{
|
||||
$slugs = array_values(array_unique($request->validated('permissions')));
|
||||
$input = $request->validated();
|
||||
$slugs = array_values(array_unique(array_values(array_filter(
|
||||
(array) ($input['permissions'] ?? $input['permission_slugs'] ?? []),
|
||||
static fn ($v) => is_string($v) && $v !== '',
|
||||
))));
|
||||
$siteId = AdminUser::defaultAdminSiteId();
|
||||
|
||||
$codes = [];
|
||||
@@ -51,6 +56,19 @@ final class AdminUserPermissionSyncController extends Controller
|
||||
|
||||
$admin_user->load('roles');
|
||||
|
||||
AuditLogger::recordForAdmin(
|
||||
$request->lotteryAdmin(),
|
||||
$request,
|
||||
'admin_users',
|
||||
'sync_permissions',
|
||||
'admin_user',
|
||||
(string) $admin_user->id,
|
||||
null,
|
||||
[
|
||||
'permission_slugs' => $slugs,
|
||||
],
|
||||
);
|
||||
|
||||
return ApiResponse::success([
|
||||
'id' => (int) $admin_user->id,
|
||||
'username' => $admin_user->username,
|
||||
|
||||
@@ -2,37 +2,23 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Jackpot;
|
||||
|
||||
use App\Models\JackpotPool;
|
||||
use App\Support\ApiResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use App\Support\CurrencyFormatter;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\Jackpot\JackpotSummaryService;
|
||||
|
||||
/**
|
||||
* `GET /api/v1/jackpot/summary` — 当前奖池水位(公开;玩家端开奖区展示)。
|
||||
*/
|
||||
final class JackpotSummaryController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly JackpotSummaryService $summary,
|
||||
) {}
|
||||
|
||||
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();
|
||||
|
||||
$amountMinor = $pool !== null ? (int) $pool->current_amount : 0;
|
||||
|
||||
return ApiResponse::success([
|
||||
'currency_code' => $currency,
|
||||
'enabled' => $pool !== null,
|
||||
'current_amount_minor' => $amountMinor,
|
||||
'current_amount_formatted' => CurrencyFormatter::fromMinor($amountMinor),
|
||||
]);
|
||||
return ApiResponse::success($this->summary->summary((string) $request->query('currency_code', 'NPR')));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,8 +22,10 @@ final class AdminUserPermissionSyncRequest extends FormRequest
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'permissions' => ['required', 'array'],
|
||||
'permissions' => ['sometimes', 'array'],
|
||||
'permissions.*' => ['string', 'max:128'],
|
||||
'permission_slugs' => ['sometimes', 'array'],
|
||||
'permission_slugs.*' => ['string', 'max:128'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,9 +22,17 @@ final class ReconcileJobStoreRequest extends FormRequest
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'date_from' => ['required', 'date_format:Y-m-d'],
|
||||
'date_to' => ['required', 'date_format:Y-m-d', 'after_or_equal:date_from'],
|
||||
'date_from' => ['sometimes', 'date_format:Y-m-d'],
|
||||
'date_to' => ['sometimes', 'date_format:Y-m-d', 'after_or_equal:date_from'],
|
||||
'player_id' => ['sometimes', 'nullable', 'integer', 'min:1'],
|
||||
'reconcile_type' => ['sometimes', 'string', 'max:64'],
|
||||
'period_start' => ['sometimes', 'date'],
|
||||
'period_end' => ['sometimes', 'date'],
|
||||
'items' => ['sometimes', 'array'],
|
||||
'items.*.side_a_ref' => ['sometimes', 'nullable', 'string', 'max:128'],
|
||||
'items.*.side_b_ref' => ['sometimes', 'nullable', 'string', 'max:128'],
|
||||
'items.*.difference_amount' => ['sometimes', 'integer'],
|
||||
'items.*.status' => ['sometimes', 'string', 'max:32'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
/**
|
||||
* 报表任务创建请求。
|
||||
@@ -22,10 +23,34 @@ final class ReportJobStoreRequest extends FormRequest
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'report_type' => ['required', 'string', 'max:64'],
|
||||
'report_type' => ['required', 'string', Rule::in(self::reportTypes())],
|
||||
'export_format' => ['sometimes', 'string', Rule::in(['csv', 'xlsx'])],
|
||||
'parameters' => ['sometimes', 'array'],
|
||||
'parameters.date_from' => ['sometimes', 'nullable', 'date_format:Y-m-d'],
|
||||
'parameters.date_to' => ['sometimes', 'nullable', 'date_format:Y-m-d'],
|
||||
'parameters.date_to' => ['sometimes', 'nullable', 'date_format:Y-m-d', 'after_or_equal:parameters.date_from'],
|
||||
'filter_json' => ['sometimes', 'array'],
|
||||
'filter_json.date_from' => ['sometimes', 'nullable', 'date_format:Y-m-d'],
|
||||
'filter_json.date_to' => ['sometimes', 'nullable', 'date_format:Y-m-d', 'after_or_equal:filter_json.date_from'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public static function reportTypes(): array
|
||||
{
|
||||
return [
|
||||
'draw_profit_summary',
|
||||
'daily_profit_summary',
|
||||
'player_win_loss',
|
||||
'wallet_transfer_report',
|
||||
'hot_number_risk_report',
|
||||
'play_dimension_report',
|
||||
'sold_out_number_report',
|
||||
'rebate_commission_report',
|
||||
'audit_operation_report',
|
||||
'wallet_txns_daily',
|
||||
'transfer_orders_daily',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ final class JackpotPool extends Model
|
||||
'payout_rate',
|
||||
'force_trigger_draw_gap',
|
||||
'min_bet_amount',
|
||||
'combo_trigger_play_codes',
|
||||
'status',
|
||||
'last_trigger_draw_id',
|
||||
];
|
||||
@@ -30,6 +31,7 @@ final class JackpotPool extends Model
|
||||
'payout_rate' => 'decimal:4',
|
||||
'force_trigger_draw_gap' => 'integer',
|
||||
'min_bet_amount' => 'integer',
|
||||
'combo_trigger_play_codes' => 'array',
|
||||
'status' => 'integer',
|
||||
'last_trigger_draw_id' => 'integer',
|
||||
];
|
||||
|
||||
@@ -21,6 +21,9 @@ final class AdminReportJobService
|
||||
{
|
||||
return DB::transaction(function () use ($admin, $request, $reportType, $exportFormat, $filterJson): ReportJob {
|
||||
$jobNo = 'RPT'.now()->format('YmdHis').strtoupper(Str::random(4));
|
||||
$params = $this->extractReportParameters($request, $filterJson);
|
||||
$dateFrom = (string) ($params['date_from'] ?? now()->toDateString());
|
||||
$dateTo = (string) ($params['date_to'] ?? $dateFrom);
|
||||
|
||||
$job = ReportJob::query()->create([
|
||||
'job_no' => $jobNo,
|
||||
@@ -29,7 +32,7 @@ final class AdminReportJobService
|
||||
'export_format' => $exportFormat,
|
||||
'filter_json' => $filterJson,
|
||||
'status' => 'completed',
|
||||
'output_path' => 'reports/'.$jobNo.'.'.$exportFormat,
|
||||
'output_path' => 'reports/'.$this->reportLabel($reportType).'_'.$dateFrom.'_'.$dateTo.'.'.$exportFormat,
|
||||
'error_message' => null,
|
||||
'finished_at' => now(),
|
||||
]);
|
||||
@@ -52,4 +55,62 @@ final class AdminReportJobService
|
||||
return $job;
|
||||
});
|
||||
}
|
||||
|
||||
public function reportLabel(string $reportType): string
|
||||
{
|
||||
return match ($reportType) {
|
||||
'draw_profit_summary' => '期号盈亏',
|
||||
'daily_profit_summary' => '每日盈亏汇总',
|
||||
'player_win_loss' => '玩家输赢报表',
|
||||
'wallet_transfer_report', 'wallet_txns_daily', 'transfer_orders_daily' => '玩家转入转出报表',
|
||||
'hot_number_risk_report' => '热门号码风险报表',
|
||||
'play_dimension_report' => '玩法维度报表',
|
||||
'sold_out_number_report' => '售罄号码报表',
|
||||
'rebate_commission_report' => '佣金回水报表',
|
||||
'audit_operation_report' => '后台操作审计报表',
|
||||
default => $reportType,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array<int, string|int|float|null>>
|
||||
*/
|
||||
public function reportRows(string $reportType, ?array $filterJson): array
|
||||
{
|
||||
$dateFrom = (string) ($filterJson['date_from'] ?? now()->toDateString());
|
||||
$dateTo = (string) ($filterJson['date_to'] ?? $dateFrom);
|
||||
|
||||
return match ($reportType) {
|
||||
'daily_profit_summary' => [
|
||||
['日期', '下注', '派彩', '盈亏'],
|
||||
[$dateFrom, 1000, 600, 400],
|
||||
[$dateTo, 1200, 500, 700],
|
||||
],
|
||||
'audit_operation_report' => [
|
||||
['模块', '操作', '操作者', '时间', 'IP'],
|
||||
['report_jobs', 'enqueue', 'admin', now()->toIso8601String(), '127.0.0.1'],
|
||||
],
|
||||
default => [
|
||||
['报表类型', '开始日期', '结束日期'],
|
||||
[$this->reportLabel($reportType), $dateFrom, $dateTo],
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function extractReportParameters(Request $request, ?array $filterJson): array
|
||||
{
|
||||
$parameters = $request->input('parameters');
|
||||
if (is_array($parameters)) {
|
||||
return $parameters;
|
||||
}
|
||||
|
||||
if (is_array($filterJson)) {
|
||||
return $filterJson;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,12 +12,17 @@ use App\Services\AuditLogger;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Support\OddsStandardScopes;
|
||||
use App\Lottery\ConfigVersionStatus;
|
||||
use App\Services\Draw\LotteryHallRealtimeBroadcaster;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
|
||||
/** 后台:赔率版本({@see odds_versions} / {@see odds_items}) */
|
||||
final class OddsStreamService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly LotteryHallRealtimeBroadcaster $hallRealtime,
|
||||
) {}
|
||||
|
||||
/** @return LengthAwarePaginator<int, OddsVersion> */
|
||||
public function paginate(?string $status, int $perPage, int $page = 1): LengthAwarePaginator
|
||||
{
|
||||
@@ -138,6 +143,11 @@ final class OddsStreamService
|
||||
});
|
||||
|
||||
$after = $this->snapshotVersion($draft->fresh(['items']));
|
||||
$this->hallRealtime->notifyOddsUpdate(
|
||||
(int) $draft->id,
|
||||
'v'.(string) $draft->version_no,
|
||||
['version_no' => (int) $draft->version_no],
|
||||
);
|
||||
|
||||
AuditLogger::recordForAdmin(
|
||||
$admin,
|
||||
|
||||
@@ -10,12 +10,17 @@ use App\Models\PlayConfigItem;
|
||||
use App\Models\PlayConfigVersion;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Lottery\ConfigVersionStatus;
|
||||
use App\Services\Draw\LotteryHallRealtimeBroadcaster;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
|
||||
/** 后台:玩法配置版本({@see play_config_versions} / {@see play_config_items}) */
|
||||
final class PlayConfigStreamService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly LotteryHallRealtimeBroadcaster $hallRealtime,
|
||||
) {}
|
||||
|
||||
/** @return LengthAwarePaginator<int, PlayConfigVersion> */
|
||||
public function paginate(?string $status, int $perPage, int $page = 1): LengthAwarePaginator
|
||||
{
|
||||
@@ -140,6 +145,12 @@ final class PlayConfigStreamService
|
||||
{
|
||||
$this->validatePublishableDraft($draft);
|
||||
$before = $this->snapshotVersion($draft);
|
||||
$currentItems = PlayConfigItem::query()
|
||||
->whereIn('version_id', PlayConfigVersion::query()
|
||||
->select('id')
|
||||
->where('status', ConfigVersionStatus::Active->value))
|
||||
->get()
|
||||
->keyBy('play_code');
|
||||
|
||||
DB::transaction(function () use ($draft, $admin): void {
|
||||
/** @var PlayConfigVersion|null $current */
|
||||
@@ -160,6 +171,7 @@ final class PlayConfigStreamService
|
||||
});
|
||||
|
||||
$after = $this->snapshotVersion($draft->fresh(['items']));
|
||||
$this->broadcastToggleDiffs($currentItems, $draft->items()->get());
|
||||
|
||||
AuditLogger::recordForAdmin(
|
||||
$admin,
|
||||
@@ -173,6 +185,26 @@ final class PlayConfigStreamService
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Support\Collection<string, PlayConfigItem> $currentItems
|
||||
* @param \Illuminate\Support\Collection<int, PlayConfigItem> $nextItems
|
||||
*/
|
||||
private function broadcastToggleDiffs(\Illuminate\Support\Collection $currentItems, \Illuminate\Support\Collection $nextItems): void
|
||||
{
|
||||
foreach ($nextItems as $next) {
|
||||
$current = $currentItems->get($next->play_code);
|
||||
if ($current === null || (bool) $current->is_enabled === (bool) $next->is_enabled) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->hallRealtime->notifyPlayToggle(
|
||||
(string) $next->play_code,
|
||||
(bool) $next->is_enabled,
|
||||
'play config version published',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteVersion(PlayConfigVersion $version, AdminUser $admin, ?Request $request = null): void
|
||||
{
|
||||
$before = $this->snapshotVersion($version);
|
||||
|
||||
@@ -59,15 +59,13 @@ final class RiskCapStreamService
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
foreach (['0000', '1234', '9999'] as $num) {
|
||||
RiskCapItem::query()->create([
|
||||
'version_id' => $draft->id,
|
||||
'draw_id' => null,
|
||||
'normalized_number' => $num,
|
||||
'cap_amount' => 50_000_000_000,
|
||||
'cap_type' => 'per_number',
|
||||
]);
|
||||
}
|
||||
RiskCapItem::query()->create([
|
||||
'version_id' => $draft->id,
|
||||
'draw_id' => null,
|
||||
'normalized_number' => '0000',
|
||||
'cap_amount' => 50_000_000_000,
|
||||
'cap_type' => 'default',
|
||||
]);
|
||||
}
|
||||
|
||||
return $draft->fresh(['items']);
|
||||
@@ -179,7 +177,10 @@ final class RiskCapStreamService
|
||||
$normalizedNumber = (string) $row->normalized_number;
|
||||
$capAmount = (int) $row->cap_amount;
|
||||
$drawId = $row->draw_id === null ? '__null__' : (string) $row->draw_id;
|
||||
$key = $drawId.'|'.$normalizedNumber;
|
||||
$capType = (string) $row->cap_type;
|
||||
$key = $capType === 'default'
|
||||
? 'default|'.$drawId
|
||||
: $drawId.'|'.$normalizedNumber;
|
||||
|
||||
if (! preg_match('/^[0-9]{4}$/', $normalizedNumber)) {
|
||||
$errors["items.$index.normalized_number"][] = '号码必须是 4 位数字';
|
||||
@@ -189,6 +190,10 @@ final class RiskCapStreamService
|
||||
$errors["items.$index.cap_amount"][] = '封顶金额必须大于 0';
|
||||
}
|
||||
|
||||
if ($capType === 'default' && $row->draw_id !== null) {
|
||||
$errors["items.$index.cap_type"][] = '默认封顶不能绑定具体期号';
|
||||
}
|
||||
|
||||
if (isset($seenKeys[$key])) {
|
||||
$errors["items.$index"][] = '同一期号与号码存在重复封顶配置';
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use App\Lottery\DrawStatus;
|
||||
use App\Models\DrawResultItem;
|
||||
use App\Models\DrawResultBatch;
|
||||
use App\Lottery\DrawResultBatchStatus;
|
||||
use App\Services\Jackpot\JackpotSummaryService;
|
||||
|
||||
/**
|
||||
* `GET draw/current` 与大厅 WS 快照共用数据结构。
|
||||
@@ -17,6 +18,10 @@ use App\Lottery\DrawResultBatchStatus;
|
||||
*/
|
||||
final class DrawHallSnapshotBuilder
|
||||
{
|
||||
public function __construct(
|
||||
private readonly JackpotSummaryService $jackpotSummary,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Tick 未及时跑时,DB 仍为 `open` 但已到封盘时刻;对外快照与界面应对齐真实可下注态(见 DrawTickService::openToClosingOrClosed)。
|
||||
*
|
||||
@@ -143,6 +148,7 @@ final class DrawHallSnapshotBuilder
|
||||
'seconds_to_draw' => $secsToDraw,
|
||||
'cooling_end_time' => $target->cooling_end_time?->toIso8601String(),
|
||||
'seconds_remaining_in_cooldown' => $coolingRemain,
|
||||
'jackpot' => $this->jackpotSummary->summary('NPR'),
|
||||
];
|
||||
|
||||
$riskAlerts = RiskPool::query()
|
||||
|
||||
@@ -8,6 +8,7 @@ use App\Models\DrawResultItem;
|
||||
use App\Models\DrawResultBatch;
|
||||
use Illuminate\Support\Collection;
|
||||
use App\Lottery\DrawResultBatchStatus;
|
||||
use App\Services\Jackpot\JackpotSummaryService;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
|
||||
/**
|
||||
@@ -15,6 +16,10 @@ use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
*/
|
||||
final class DrawResultViewService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly JackpotSummaryService $jackpotSummary,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 与 `docs/01-产品文档` GET /api/v1/results 示例键名对齐(1st/2nd/3rd/starter/consolation)。
|
||||
*
|
||||
@@ -99,6 +104,7 @@ final class DrawResultViewService
|
||||
'draw_time_iso' => $draw->draw_time?->toIso8601String(),
|
||||
'result_version' => $version,
|
||||
'result_source' => $draw->result_source,
|
||||
'jackpot' => $this->jackpotSummary->summary('NPR'),
|
||||
'results' => $numbers,
|
||||
'result_items' => $items->map(fn (DrawResultItem $r) => [
|
||||
'prize_type' => $r->prize_type,
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Events\OddsUpdateBroadcast;
|
||||
use App\Events\PlayToggleBroadcast;
|
||||
use App\Events\RiskSoldOutBroadcast;
|
||||
use App\Events\RiskWarningBroadcast;
|
||||
use App\Events\JackpotBurstBroadcast;
|
||||
use App\Events\DrawCountdownBroadcast;
|
||||
use App\Events\DrawStatusChangeBroadcast;
|
||||
use App\Events\DrawResultPublishedBroadcast;
|
||||
@@ -13,7 +14,7 @@ use App\Events\DrawResultPublishedBroadcast;
|
||||
/**
|
||||
* 对齐界面文档 §2.1:大厅公共频道广播(`lottery-hall`)。
|
||||
* 包含:draw.countdown、draw.status_change、result.published、
|
||||
* risk.sold_out、risk.warning、play.toggle、odds.update
|
||||
* risk.sold_out、risk.warning、play.toggle、odds.update、jackpot.burst
|
||||
*/
|
||||
final class LotteryHallRealtimeBroadcaster
|
||||
{
|
||||
@@ -132,6 +133,34 @@ final class LotteryHallRealtimeBroadcaster
|
||||
));
|
||||
}
|
||||
|
||||
/** `jackpot.burst` —— Jackpot 爆池动画与浏览器通知 */
|
||||
public function notifyJackpotBurst(
|
||||
int $drawId,
|
||||
string $drawNo,
|
||||
string $firstPrizeNumber,
|
||||
string $currencyCode,
|
||||
int $totalPayoutAmount,
|
||||
int $winnerCount,
|
||||
string $triggerType,
|
||||
int $poolAmountAfter,
|
||||
): void {
|
||||
if (! $this->driverSupportsRealtime()) {
|
||||
return;
|
||||
}
|
||||
|
||||
broadcast(new JackpotBurstBroadcast(
|
||||
$drawId,
|
||||
$drawNo,
|
||||
$firstPrizeNumber,
|
||||
strtoupper($currencyCode),
|
||||
$totalPayoutAmount,
|
||||
$winnerCount,
|
||||
$triggerType,
|
||||
$poolAmountAfter,
|
||||
(int) floor(microtime(true) * 1000),
|
||||
));
|
||||
}
|
||||
|
||||
private function driverSupportsRealtime(): bool
|
||||
{
|
||||
$default = config('broadcasting.default');
|
||||
|
||||
@@ -30,11 +30,12 @@ final class JackpotBurstAllocator
|
||||
|
||||
$thresholdOk = (int) $pool->current_amount >= (int) $pool->trigger_threshold;
|
||||
$gapOk = $this->gapTriggerMet($pool);
|
||||
if (! $thresholdOk && ! $gapOk) {
|
||||
$comboOk = $this->comboTriggerMet($pool, $winners);
|
||||
if (! $thresholdOk && ! $gapOk && ! $comboOk) {
|
||||
return ['allocations' => [], 'pool_payout' => 0, 'trigger' => null];
|
||||
}
|
||||
|
||||
$trigger = $thresholdOk ? 'threshold' : 'forced_gap';
|
||||
$trigger = $thresholdOk ? 'threshold' : ($gapOk ? 'forced_gap' : 'play_combo');
|
||||
|
||||
$poolBefore = (int) $pool->current_amount;
|
||||
$poolPayout = (int) floor($poolBefore * (float) $pool->payout_rate);
|
||||
@@ -81,6 +82,8 @@ final class JackpotBurstAllocator
|
||||
'trigger_snapshot_json' => [
|
||||
'threshold_ok' => $thresholdOk,
|
||||
'gap_ok' => $gapOk,
|
||||
'combo_ok' => $comboOk,
|
||||
'combo_trigger_play_codes' => $this->comboTriggerPlayCodes($pool),
|
||||
'pool_amount_before' => $poolBefore,
|
||||
'payout_rate' => (string) $pool->payout_rate,
|
||||
],
|
||||
@@ -104,4 +107,35 @@ final class JackpotBurstAllocator
|
||||
|
||||
return $count >= $gap;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, array{item: TicketItem, matched_tier: ?string, gross_win: int}> $winners
|
||||
*/
|
||||
private function comboTriggerMet(JackpotPool $pool, Collection $winners): bool
|
||||
{
|
||||
$codes = $this->comboTriggerPlayCodes($pool);
|
||||
if ($codes === []) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $winners->contains(
|
||||
fn (array $r): bool => in_array((string) $r['item']->play_code, $codes, true),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function comboTriggerPlayCodes(JackpotPool $pool): array
|
||||
{
|
||||
$raw = $pool->combo_trigger_play_codes;
|
||||
if (! is_array($raw)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_values(array_filter(
|
||||
array_map(fn ($v): string => strtolower(trim((string) $v)), $raw),
|
||||
fn (string $v): bool => $v !== '',
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
48
app/Services/Jackpot/JackpotSummaryService.php
Normal file
48
app/Services/Jackpot/JackpotSummaryService.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Jackpot;
|
||||
|
||||
use App\Models\Draw;
|
||||
use App\Models\JackpotPool;
|
||||
use App\Support\CurrencyFormatter;
|
||||
use App\Lottery\DrawStatus;
|
||||
|
||||
final class JackpotSummaryService
|
||||
{
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function summary(string $currencyCode): array
|
||||
{
|
||||
$currency = strtoupper(trim($currencyCode));
|
||||
if ($currency === '' || strlen($currency) > 16) {
|
||||
$currency = 'NPR';
|
||||
}
|
||||
|
||||
$pool = JackpotPool::query()
|
||||
->where('currency_code', $currency)
|
||||
->where('status', 1)
|
||||
->first();
|
||||
|
||||
$amountMinor = $pool !== null ? (int) $pool->current_amount : 0;
|
||||
|
||||
return [
|
||||
'currency_code' => $currency,
|
||||
'enabled' => $pool !== null,
|
||||
'current_amount_minor' => $amountMinor,
|
||||
'current_amount_formatted' => CurrencyFormatter::fromMinor($amountMinor),
|
||||
'draws_since_last_burst' => $pool !== null ? $this->drawsSinceLastBurst($pool) : null,
|
||||
'last_trigger_draw_id' => $pool?->last_trigger_draw_id !== null ? (int) $pool->last_trigger_draw_id : null,
|
||||
];
|
||||
}
|
||||
|
||||
private function drawsSinceLastBurst(JackpotPool $pool): int
|
||||
{
|
||||
$lastId = (int) ($pool->last_trigger_draw_id ?? 0);
|
||||
|
||||
return (int) Draw::query()
|
||||
->where('status', DrawStatus::Settled->value)
|
||||
->when($lastId > 0, fn ($q) => $q->where('id', '>', $lastId))
|
||||
->count();
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ use Illuminate\Support\Facades\DB;
|
||||
use App\Lottery\DrawResultBatchStatus;
|
||||
use App\Lottery\SettlementBatchStatus;
|
||||
use App\Models\TicketSettlementDetail;
|
||||
use App\Services\Draw\LotteryHallRealtimeBroadcaster;
|
||||
use App\Services\Ticket\RiskPoolService;
|
||||
use App\Services\Jackpot\JackpotBurstAllocator;
|
||||
|
||||
@@ -28,6 +29,7 @@ final class SettlementOrchestrator
|
||||
private readonly SettlementPayoutAdjuster $payoutAdjuster,
|
||||
private readonly JackpotBurstAllocator $jackpotBurst,
|
||||
private readonly RiskPoolService $riskPool,
|
||||
private readonly LotteryHallRealtimeBroadcaster $hallRealtime,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -129,6 +131,8 @@ final class SettlementOrchestrator
|
||||
|
||||
$allocations = [];
|
||||
$totalJackpotPayout = 0;
|
||||
$jackpotTrigger = null;
|
||||
$jackpotPoolAfter = null;
|
||||
if ($pool !== null) {
|
||||
$burstInput = collect($prepared)->map(fn (array $p): array => [
|
||||
'item' => $p['item'],
|
||||
@@ -138,6 +142,8 @@ final class SettlementOrchestrator
|
||||
$burstOut = $this->jackpotBurst->allocate($locked, $pool, $burstInput);
|
||||
$allocations = $burstOut['allocations'];
|
||||
$totalJackpotPayout = (int) $burstOut['pool_payout'];
|
||||
$jackpotTrigger = $burstOut['trigger'];
|
||||
$jackpotPoolAfter = (int) $pool->fresh()->current_amount;
|
||||
}
|
||||
|
||||
$ticketCount = 0;
|
||||
@@ -197,6 +203,19 @@ final class SettlementOrchestrator
|
||||
'settle_version' => $nextSettleVersion,
|
||||
])->save();
|
||||
|
||||
if ($pool !== null && $totalJackpotPayout > 0 && is_string($jackpotTrigger)) {
|
||||
$this->hallRealtime->notifyJackpotBurst(
|
||||
(int) $locked->id,
|
||||
(string) $locked->draw_no,
|
||||
$board->firstPrizeNumber4d(),
|
||||
$currency,
|
||||
$totalJackpotPayout,
|
||||
count($allocations),
|
||||
$jackpotTrigger,
|
||||
(int) $jackpotPoolAfter,
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -144,6 +144,17 @@ final class PlayCatalogResolver
|
||||
return (int) $generic->cap_amount;
|
||||
}
|
||||
|
||||
$default = RiskCapItem::query()
|
||||
->where('version_id', $riskVersion->id)
|
||||
->whereNull('draw_id')
|
||||
->where('cap_type', 'default')
|
||||
->orderByDesc('id')
|
||||
->first();
|
||||
|
||||
if ($default !== null) {
|
||||
return (int) $default->cap_amount;
|
||||
}
|
||||
|
||||
return 50_000_000_000;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,10 @@ final class RiskPoolService
|
||||
$rows = [];
|
||||
foreach ($locks as $lock) {
|
||||
$pool = $this->firstOrMakePool($drawId, $lock['number_4d']);
|
||||
if ((int) $pool->sold_out_status === 1) {
|
||||
throw new TicketOperationException('risk_sold_out', ErrorCode::RiskPoolSoldOut->value);
|
||||
}
|
||||
|
||||
$remaining = (int) $pool->remaining_amount;
|
||||
if ($remaining < (int) $lock['amount']) {
|
||||
throw new TicketOperationException('risk_sold_out', ErrorCode::RiskPoolSoldOut->value);
|
||||
@@ -82,7 +86,7 @@ final class RiskPoolService
|
||||
}
|
||||
|
||||
$amount = (int) $lock['amount'];
|
||||
if ((int) $pool->remaining_amount < $amount) {
|
||||
if ((int) $pool->sold_out_status === 1 || (int) $pool->remaining_amount < $amount) {
|
||||
throw new TicketOperationException('risk_sold_out', ErrorCode::RiskPoolSoldOut->value);
|
||||
}
|
||||
|
||||
@@ -229,6 +233,10 @@ LUA;
|
||||
->lockForUpdate()
|
||||
->firstOrFail();
|
||||
|
||||
if ((int) $pool->sold_out_status === 1) {
|
||||
throw new TicketOperationException('risk_sold_out', ErrorCode::RiskPoolSoldOut->value);
|
||||
}
|
||||
|
||||
$pool->forceFill([
|
||||
'locked_amount' => (int) $pool->locked_amount + $amount,
|
||||
'remaining_amount' => (int) $pool->remaining_amount - $amount,
|
||||
@@ -295,16 +303,16 @@ LUA;
|
||||
|
||||
private function firstOrMakePool(int $drawId, string $number4d): RiskPool
|
||||
{
|
||||
return RiskPool::query()->firstOrCreate(
|
||||
['draw_id' => $drawId, 'normalized_number' => $number4d],
|
||||
[
|
||||
'total_cap_amount' => $this->catalogResolver->resolveCapAmount($drawId, $number4d),
|
||||
'locked_amount' => 0,
|
||||
'remaining_amount' => $this->catalogResolver->resolveCapAmount($drawId, $number4d),
|
||||
'sold_out_status' => 0,
|
||||
'version' => 0,
|
||||
],
|
||||
);
|
||||
$pool = RiskPool::query()
|
||||
->where('draw_id', $drawId)
|
||||
->where('normalized_number', $number4d)
|
||||
->first();
|
||||
|
||||
if ($pool !== null) {
|
||||
return $pool;
|
||||
}
|
||||
|
||||
return $this->createPool($drawId, $number4d);
|
||||
}
|
||||
|
||||
private function createPool(int $drawId, string $number4d): RiskPool
|
||||
|
||||
112
app/Services/Ticket/TicketPendingConfirmReconcileService.php
Normal file
112
app/Services/Ticket/TicketPendingConfirmReconcileService.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ticket;
|
||||
|
||||
use App\Models\WalletTxn;
|
||||
use App\Models\TicketItem;
|
||||
use App\Models\TicketOrder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
final class TicketPendingConfirmReconcileService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RiskPoolService $riskPool,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array{scanned:int, confirmed:int, refunded:int}
|
||||
*/
|
||||
public function reconcile(int $staleMinutes, int $limit): array
|
||||
{
|
||||
$cutoff = now()->subMinutes($staleMinutes);
|
||||
$orders = TicketOrder::query()
|
||||
->where('status', 'pending_confirm')
|
||||
->where('updated_at', '<=', $cutoff)
|
||||
->orderBy('id')
|
||||
->limit($limit)
|
||||
->get();
|
||||
|
||||
$summary = ['scanned' => 0, 'confirmed' => 0, 'refunded' => 0];
|
||||
|
||||
foreach ($orders as $order) {
|
||||
$result = DB::transaction(function () use ($order): string {
|
||||
$lockedOrder = TicketOrder::query()
|
||||
->whereKey($order->id)
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
|
||||
if ($lockedOrder === null || $lockedOrder->status !== 'pending_confirm') {
|
||||
return 'skipped';
|
||||
}
|
||||
|
||||
$hasPostedDeduct = WalletTxn::query()
|
||||
->where('biz_type', 'bet_deduct')
|
||||
->where('biz_no', $lockedOrder->order_no)
|
||||
->where('status', 'posted')
|
||||
->exists();
|
||||
|
||||
if ($hasPostedDeduct) {
|
||||
TicketItem::query()
|
||||
->where('order_id', $lockedOrder->id)
|
||||
->where('status', 'pending_confirm')
|
||||
->update([
|
||||
'status' => 'success',
|
||||
'fail_reason_code' => null,
|
||||
'fail_reason_text' => null,
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
$lockedOrder->forceFill(['status' => 'placed'])->save();
|
||||
|
||||
return 'confirmed';
|
||||
}
|
||||
|
||||
$items = TicketItem::query()
|
||||
->where('order_id', $lockedOrder->id)
|
||||
->where('status', 'pending_confirm')
|
||||
->with('combinations')
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
|
||||
foreach ($items as $item) {
|
||||
$locks = [];
|
||||
foreach ($item->combinations as $combo) {
|
||||
$locks[] = [
|
||||
'number_4d' => (string) $combo->number_4d,
|
||||
'amount' => (int) $combo->estimated_payout,
|
||||
];
|
||||
}
|
||||
|
||||
if ($locks !== []) {
|
||||
$this->riskPool->release((int) $lockedOrder->draw_id, $item, $locks);
|
||||
}
|
||||
|
||||
$item->forceFill([
|
||||
'status' => 'refunded',
|
||||
'fail_reason_code' => 'pending_confirm_timeout',
|
||||
'fail_reason_text' => 'pending_confirm_timeout_refund',
|
||||
'risk_locked_amount' => 0,
|
||||
])->save();
|
||||
}
|
||||
|
||||
$lockedOrder->forceFill(['status' => 'refunded'])->save();
|
||||
|
||||
return 'refunded';
|
||||
});
|
||||
|
||||
if ($result === 'skipped') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$summary['scanned']++;
|
||||
if ($result === 'confirmed') {
|
||||
$summary['confirmed']++;
|
||||
}
|
||||
if ($result === 'refunded') {
|
||||
$summary['refunded']++;
|
||||
}
|
||||
}
|
||||
|
||||
return $summary;
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ use App\Lottery\ErrorCode;
|
||||
use App\Models\TicketItem;
|
||||
use App\Lottery\DrawStatus;
|
||||
use App\Models\TicketOrder;
|
||||
use App\Models\PlayerWallet;
|
||||
use App\Models\TicketCombination;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Exceptions\TicketOperationException;
|
||||
@@ -114,6 +115,16 @@ final class TicketPlacementService
|
||||
);
|
||||
}
|
||||
|
||||
$walletBalance = (int) (PlayerWallet::query()
|
||||
->where('player_id', $player->id)
|
||||
->where('wallet_type', 'lottery')
|
||||
->where('currency_code', $currencyCode)
|
||||
->lockForUpdate()
|
||||
->value('balance') ?? 0);
|
||||
if ($walletBalance < $totalActualDeduct) {
|
||||
throw new TicketOperationException('bet_insufficient_balance', ErrorCode::BetInsufficientBalance->value);
|
||||
}
|
||||
|
||||
$order = TicketOrder::query()->create([
|
||||
'order_no' => $this->newOrderNo(),
|
||||
'player_id' => $player->id,
|
||||
@@ -123,14 +134,13 @@ final class TicketPlacementService
|
||||
'total_rebate_amount' => $totalRebate,
|
||||
'total_actual_deduct' => $totalActualDeduct,
|
||||
'total_estimated_payout' => $totalEstimatedPayout,
|
||||
'status' => 'placed',
|
||||
'status' => 'pending',
|
||||
'submit_source' => 'h5',
|
||||
'client_trace_id' => $payload['client_trace_id'] ?? null,
|
||||
]);
|
||||
|
||||
$successfulItems = [];
|
||||
$failedItems = [];
|
||||
$successfulEvaluatedLines = [];
|
||||
$successTotalBet = 0;
|
||||
$successTotalRebate = 0;
|
||||
$successTotalActualDeduct = 0;
|
||||
@@ -204,11 +214,10 @@ final class TicketPlacementService
|
||||
$item->forceFill([
|
||||
'actual_deduct_amount' => (int) $evaluated['actual_deduct_amount'],
|
||||
'risk_locked_amount' => $lockedAmount,
|
||||
'status' => 'success',
|
||||
'status' => 'pending_confirm',
|
||||
])->save();
|
||||
|
||||
$successfulItems[] = $item;
|
||||
$successfulEvaluatedLines[] = ['item' => $item, 'evaluated' => $evaluated];
|
||||
$successTotalBet += (int) $evaluated['total_bet_amount'];
|
||||
$successTotalRebate += $rebateAmount;
|
||||
$successTotalActualDeduct += (int) $evaluated['actual_deduct_amount'];
|
||||
@@ -224,37 +233,76 @@ final class TicketPlacementService
|
||||
'total_rebate_amount' => $successTotalRebate,
|
||||
'total_actual_deduct' => $successTotalActualDeduct,
|
||||
'total_estimated_payout' => $successTotalEstimatedPayout,
|
||||
'status' => $failedItems === [] ? 'placed' : 'partial_failed',
|
||||
'status' => $failedItems === [] ? 'pending_confirm' : 'partial_pending_confirm',
|
||||
])->save();
|
||||
|
||||
try {
|
||||
$balanceAfter = $this->ticketWalletService->deduct($player, $currencyCode, $successTotalActualDeduct, $order);
|
||||
|
||||
foreach ($successfulItems as $item) {
|
||||
$this->jackpotContribution->recordFromPlacedTicketItem($item, $draw, $currencyCode);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
foreach ($successfulEvaluatedLines as $row) {
|
||||
$locks = array_map(fn (array $combo): array => [
|
||||
'number_4d' => $combo['number_4d'],
|
||||
'amount' => $combo['estimated_payout'],
|
||||
], $row['evaluated']['combinations']);
|
||||
$this->riskPoolService->release((int) $draw->id, $row['item'], $locks);
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return [
|
||||
'order' => $order,
|
||||
'balance_after' => $balanceAfter,
|
||||
'draw_id' => (int) $draw->id,
|
||||
'currency_code' => $currencyCode,
|
||||
'successful_item_ids' => array_map(
|
||||
fn (TicketItem $item): int => (int) $item->id,
|
||||
$successfulItems,
|
||||
),
|
||||
'has_failed_items' => $failedItems !== [],
|
||||
'success_total_actual_deduct' => $successTotalActualDeduct,
|
||||
];
|
||||
});
|
||||
|
||||
$order = $placement['order'];
|
||||
$balanceAfter = $placement['balance_after'];
|
||||
$order = TicketOrder::query()->whereKey($placement['order']->id)->firstOrFail();
|
||||
$draw = Draw::query()->whereKey((int) $placement['draw_id'])->firstOrFail();
|
||||
|
||||
$draw = Draw::query()->whereKey($order->draw_id)->firstOrFail();
|
||||
try {
|
||||
$balanceAfter = $this->ticketWalletService->deduct($player, (string) $placement['currency_code'], (int) $placement['success_total_actual_deduct'], $order);
|
||||
|
||||
DB::transaction(function () use ($order, $draw, $placement): void {
|
||||
$successfulItems = TicketItem::query()
|
||||
->whereIn('id', $placement['successful_item_ids'])
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
|
||||
foreach ($successfulItems as $item) {
|
||||
$item->forceFill(['status' => 'success'])->save();
|
||||
$this->jackpotContribution->recordFromPlacedTicketItem($item, $draw, (string) $placement['currency_code']);
|
||||
}
|
||||
|
||||
$order->forceFill([
|
||||
'status' => $placement['has_failed_items'] ? 'partial_failed' : 'placed',
|
||||
])->save();
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
DB::transaction(function () use ($order): void {
|
||||
$items = TicketItem::query()
|
||||
->where('order_id', $order->id)
|
||||
->where('status', 'pending_confirm')
|
||||
->with('combinations')
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
|
||||
foreach ($items as $item) {
|
||||
$locks = [];
|
||||
foreach ($item->combinations as $combo) {
|
||||
$locks[] = [
|
||||
'number_4d' => (string) $combo->number_4d,
|
||||
'amount' => (int) $combo->estimated_payout,
|
||||
];
|
||||
}
|
||||
$this->riskPoolService->release((int) $order->draw_id, $item, $locks);
|
||||
$item->forceFill([
|
||||
'status' => 'refunded',
|
||||
'fail_reason_code' => (string) ErrorCode::BetInsufficientBalance->value,
|
||||
'fail_reason_text' => 'wallet_deduct_failed_refund',
|
||||
'risk_locked_amount' => 0,
|
||||
])->save();
|
||||
}
|
||||
|
||||
$order->forceFill(['status' => 'refunded'])->save();
|
||||
});
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$order = TicketOrder::query()->whereKey($order->id)->firstOrFail();
|
||||
|
||||
return [
|
||||
'order_no' => $order->order_no,
|
||||
|
||||
Reference in New Issue
Block a user