182 lines
6.9 KiB
PHP
182 lines
6.9 KiB
PHP
<?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\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,
|
||
'is_enabled' => $row->is_enabled,
|
||
'min_bet_amount' => $row->min_bet_amount,
|
||
'max_bet_amount' => $row->max_bet_amount,
|
||
'display_order' => $row->display_order,
|
||
'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,
|
||
'is_enabled' => (bool) $pt->is_enabled,
|
||
'min_bet_amount' => 100,
|
||
'max_bet_amount' => 500_000_000,
|
||
'display_order' => (int) $pt->sort_order,
|
||
'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'],
|
||
'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),
|
||
'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
|
||
{
|
||
$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(),
|
||
];
|
||
}
|
||
}
|