Files
lotteryLaravel/app/Services/Config/PlayConfigStreamService.php

263 lines
10 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Services\Config;
use App\Models\PlayType;
use App\Models\AdminUser;
use Illuminate\Http\Request;
use App\Services\AuditLogger;
use App\Models\PlayConfigItem;
use App\Models\PlayConfigVersion;
use Illuminate\Support\Facades\DB;
use App\Lottery\ConfigVersionStatus;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
/** 后台:玩法配置版本({@see play_config_versions} / {@see play_config_items} */
final class PlayConfigStreamService
{
/** @return LengthAwarePaginator<int, PlayConfigVersion> */
public function paginate(?string $status, int $perPage, int $page = 1): LengthAwarePaginator
{
$q = PlayConfigVersion::query()->orderByDesc('id');
if ($status !== null && $status !== '') {
$q->where('status', $status);
}
return $q->paginate($perPage, ['*'], 'page', max(1, $page));
}
public function createDraft(AdminUser $admin, ?string $reason, ?int $cloneFromVersionId): PlayConfigVersion
{
$nextNo = (int) (PlayConfigVersion::query()->max('version_no') ?? 0) + 1;
return DB::transaction(function () use ($admin, $reason, $cloneFromVersionId, $nextNo): PlayConfigVersion {
$draft = PlayConfigVersion::query()->create([
'version_no' => $nextNo,
'status' => ConfigVersionStatus::Draft->value,
'effective_at' => null,
'updated_by' => $admin->id,
'reason' => $reason,
]);
$source = null;
if ($cloneFromVersionId !== null) {
$source = PlayConfigVersion::query()->whereKey($cloneFromVersionId)->firstOrFail();
} else {
$source = PlayConfigVersion::query()
->where('status', ConfigVersionStatus::Active->value)
->first();
}
if ($source !== null) {
foreach ($source->items()->orderBy('play_code')->get() as $row) {
PlayConfigItem::query()->create([
'version_id' => $draft->id,
'play_code' => $row->play_code,
'category' => $row->category,
'dimension' => $row->dimension,
'bet_mode' => $row->bet_mode,
'display_name_zh' => $row->display_name_zh,
'display_name_en' => $row->display_name_en,
'display_name_ne' => $row->display_name_ne,
'is_enabled' => $row->is_enabled,
'min_bet_amount' => $row->min_bet_amount,
'max_bet_amount' => $row->max_bet_amount,
'display_order' => $row->display_order,
'supports_multi_number' => $row->supports_multi_number,
'reserved_rule_json' => $row->reserved_rule_json,
'rule_text_zh' => $row->rule_text_zh,
'rule_text_en' => $row->rule_text_en,
'rule_text_ne' => $row->rule_text_ne,
'extra_config_json' => $row->extra_config_json,
]);
}
} else {
foreach (PlayType::query()->orderBy('sort_order')->orderBy('play_code')->get() as $pt) {
PlayConfigItem::query()->create([
'version_id' => $draft->id,
'play_code' => $pt->play_code,
'category' => $pt->category,
'dimension' => $pt->dimension,
'bet_mode' => $pt->bet_mode,
'display_name_zh' => $pt->display_name_zh,
'display_name_en' => $pt->display_name_en,
'display_name_ne' => $pt->display_name_ne,
'is_enabled' => (bool) $pt->is_enabled,
'min_bet_amount' => 100,
'max_bet_amount' => 500_000_000,
'display_order' => (int) $pt->sort_order,
'supports_multi_number' => (bool) $pt->supports_multi_number,
'reserved_rule_json' => $pt->reserved_rule_json,
'rule_text_zh' => null,
'rule_text_en' => null,
'rule_text_ne' => null,
'extra_config_json' => null,
]);
}
}
return $draft->fresh(['items']);
});
}
/**
* @param array<int, array<string, mixed>> $items
*/
public function replaceItems(PlayConfigVersion $draft, array $items, AdminUser $admin): void
{
DB::transaction(function () use ($draft, $items, $admin): void {
PlayConfigItem::query()->where('version_id', $draft->id)->delete();
foreach ($items as $row) {
PlayConfigItem::query()->create([
'version_id' => $draft->id,
'play_code' => (string) $row['play_code'],
'category' => $row['category'] ?? null,
'dimension' => $row['dimension'] ?? null,
'bet_mode' => $row['bet_mode'] ?? null,
'display_name_zh' => $row['display_name_zh'] ?? null,
'display_name_en' => $row['display_name_en'] ?? null,
'display_name_ne' => $row['display_name_ne'] ?? null,
'is_enabled' => (bool) ($row['is_enabled'] ?? true),
'min_bet_amount' => (int) ($row['min_bet_amount'] ?? 0),
'max_bet_amount' => (int) ($row['max_bet_amount'] ?? 0),
'display_order' => (int) ($row['display_order'] ?? 0),
'supports_multi_number' => (bool) ($row['supports_multi_number'] ?? false),
'reserved_rule_json' => $row['reserved_rule_json'] ?? null,
'rule_text_zh' => isset($row['rule_text_zh']) ? (string) $row['rule_text_zh'] : null,
'rule_text_en' => isset($row['rule_text_en']) ? (string) $row['rule_text_en'] : null,
'rule_text_ne' => isset($row['rule_text_ne']) ? (string) $row['rule_text_ne'] : null,
'extra_config_json' => $row['extra_config_json'] ?? null,
]);
}
$draft->forceFill(['updated_by' => $admin->id])->save();
});
}
public function publish(PlayConfigVersion $draft, AdminUser $admin, ?Request $request = null): void
{
$this->validatePublishableDraft($draft);
$before = $this->snapshotVersion($draft);
DB::transaction(function () use ($draft, $admin): void {
/** @var PlayConfigVersion|null $current */
$current = PlayConfigVersion::query()
->where('status', ConfigVersionStatus::Active->value)
->lockForUpdate()
->first();
if ($current !== null) {
$current->forceFill(['status' => ConfigVersionStatus::Archived->value])->save();
}
$draft->forceFill([
'status' => ConfigVersionStatus::Active->value,
'effective_at' => now(),
'updated_by' => $admin->id,
])->save();
});
$after = $this->snapshotVersion($draft->fresh(['items']));
AuditLogger::recordForAdmin(
$admin,
$request,
moduleCode: 'play_config',
actionCode: 'publish',
targetType: 'play_config_version',
targetId: (string) $draft->id,
beforeJson: $before,
afterJson: $after,
);
}
public function deleteVersion(PlayConfigVersion $version, AdminUser $admin, ?Request $request = null): void
{
$before = $this->snapshotVersion($version);
DB::transaction(function () use ($version): void {
$version->delete();
});
AuditLogger::recordForAdmin(
$admin,
$request,
moduleCode: 'play_config',
actionCode: 'delete',
targetType: 'play_config_version',
targetId: (string) $version->id,
beforeJson: $before,
afterJson: null,
);
}
/** @return array<string, mixed> */
private function snapshotVersion(PlayConfigVersion $v): array
{
return [
'id' => $v->id,
'version_no' => $v->version_no,
'status' => $v->status,
'effective_at' => $v->effective_at?->toIso8601String(),
'items_count' => $v->items()->count(),
];
}
private function validatePublishableDraft(PlayConfigVersion $draft): void
{
$items = $draft->items()->orderBy('play_code')->get();
$allowedPlayCodes = array_fill_keys(
PlayType::query()->pluck('play_code')->all(),
true,
);
$errors = [];
$seenPlayCodes = [];
if ($items->isEmpty()) {
$errors['items'][] = '草稿至少需要一条玩法配置';
}
foreach ($items as $index => $row) {
$playCode = (string) $row->play_code;
$minBet = (int) $row->min_bet_amount;
$maxBet = (int) $row->max_bet_amount;
if (! isset($allowedPlayCodes[$playCode])) {
$errors["items.$index.play_code"][] = '玩法不存在';
}
if (isset($seenPlayCodes[$playCode])) {
$errors["items.$index.play_code"][] = '玩法重复';
}
$seenPlayCodes[$playCode] = true;
if ($minBet < 0) {
$errors["items.$index.min_bet_amount"][] = '最小下注额不能小于 0';
}
if ($maxBet < 0) {
$errors["items.$index.max_bet_amount"][] = '最大下注额不能小于 0';
}
if ($maxBet < $minBet) {
$errors["items.$index.max_bet_amount"][] = '最大下注额不能小于最小下注额';
}
if ($row->display_name_zh === null || $row->display_name_zh === '') {
$errors["items.$index.display_name_zh"][] = '显示名称不能为空';
}
if ($row->display_order === null) {
$errors["items.$index.display_order"][] = '排序不能为空';
}
}
if ($errors !== []) {
throw ValidationException::withMessages($errors);
}
}
}