feat: 添加 Laravel Reverb 支持,更新 .env.example 文件以配置 WebSocket,增强彩票调度功能,更新 API 路由以支持期号管理与结果发布
This commit is contained in:
89
app/Services/Draw/DrawPublishService.php
Normal file
89
app/Services/Draw/DrawPublishService.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Draw;
|
||||
|
||||
use App\Lottery\DrawResultBatchStatus;
|
||||
use App\Lottery\DrawStatus;
|
||||
use App\Models\AdminUser;
|
||||
use App\Models\Draw;
|
||||
use App\Models\DrawResultBatch;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* 人工审核通过后发布结果;或 RNG 自动生成路径内联调用同一事务字段更新。
|
||||
*/
|
||||
final class DrawPublishService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly LotteryHallRealtimeBroadcaster $hallRealtime,
|
||||
private readonly DrawHallSnapshotBuilder $snapshot,
|
||||
) {}
|
||||
|
||||
public function publishManualBatch(DrawResultBatch $batch, AdminUser $admin): Draw
|
||||
{
|
||||
$draw = DB::transaction(function () use ($batch, $admin): Draw {
|
||||
/** @var DrawResultBatch $lockedBatch */
|
||||
$lockedBatch = DrawResultBatch::query()->whereKey($batch->id)->lockForUpdate()->firstOrFail();
|
||||
if ($lockedBatch->status !== DrawResultBatchStatus::PendingReview->value) {
|
||||
throw new \RuntimeException('batch_not_pending_review');
|
||||
}
|
||||
|
||||
/** @var Draw $draw */
|
||||
$draw = Draw::query()->whereKey($lockedBatch->draw_id)->lockForUpdate()->firstOrFail();
|
||||
$lockedBatch->forceFill([
|
||||
'status' => DrawResultBatchStatus::Published->value,
|
||||
'confirmed_by' => $admin->id,
|
||||
'confirmed_at' => now(),
|
||||
])->save();
|
||||
|
||||
return $this->applyPublishedToDraw($draw, $lockedBatch);
|
||||
});
|
||||
|
||||
$data = $this->snapshot->build();
|
||||
$this->hallRealtime->notifyResultPublished($data);
|
||||
$this->hallRealtime->notifyStatusChange($data);
|
||||
|
||||
return $draw;
|
||||
}
|
||||
|
||||
/** RNG 自动生成且无需审核时在同一事务调用 */
|
||||
public function markPublishedInTransaction(Draw $draw, DrawResultBatch $batch): Draw
|
||||
{
|
||||
$batch->forceFill([
|
||||
'status' => DrawResultBatchStatus::Published->value,
|
||||
'confirmed_by' => null,
|
||||
'confirmed_at' => now(),
|
||||
])->save();
|
||||
|
||||
$draw = $this->applyPublishedToDraw($draw, $batch);
|
||||
|
||||
DB::afterCommit(function (): void {
|
||||
$data = app(DrawHallSnapshotBuilder::class)->build();
|
||||
app(LotteryHallRealtimeBroadcaster::class)->notifyResultPublished($data);
|
||||
});
|
||||
|
||||
return $draw;
|
||||
}
|
||||
|
||||
private function applyPublishedToDraw(Draw $draw, DrawResultBatch $batch): Draw
|
||||
{
|
||||
$cooldownMinutes = (int) config('lottery.draw.cooldown_minutes', 15);
|
||||
if ($cooldownMinutes > 0) {
|
||||
$draw->forceFill([
|
||||
'status' => DrawStatus::Cooldown->value,
|
||||
'current_result_version' => (int) $batch->result_version,
|
||||
'result_source' => $batch->source_type,
|
||||
'cooling_end_time' => now()->addMinutes($cooldownMinutes),
|
||||
])->save();
|
||||
} else {
|
||||
$draw->forceFill([
|
||||
'status' => DrawStatus::Settling->value,
|
||||
'current_result_version' => (int) $batch->result_version,
|
||||
'result_source' => $batch->source_type,
|
||||
'cooling_end_time' => null,
|
||||
])->save();
|
||||
}
|
||||
|
||||
return $draw->refresh();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user