feat: 添加新的错误码以支持配置版本管理,更新彩票配置以启用手动审核,增强 API 路由以支持玩法和赔率版本化管理
This commit is contained in:
145
app/Services/Config/RiskCapStreamService.php
Normal file
145
app/Services/Config/RiskCapStreamService.php
Normal file
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Config;
|
||||
|
||||
use App\Lottery\ConfigVersionStatus;
|
||||
use App\Models\AdminUser;
|
||||
use App\Models\RiskCapItem;
|
||||
use App\Models\RiskCapVersion;
|
||||
use App\Services\AuditLogger;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/** 后台:风控封顶版本({@see risk_cap_versions} / {@see risk_cap_items}) */
|
||||
final class RiskCapStreamService
|
||||
{
|
||||
/** @return LengthAwarePaginator<int, RiskCapVersion> */
|
||||
public function paginate(?string $status, int $perPage): LengthAwarePaginator
|
||||
{
|
||||
$q = RiskCapVersion::query()->orderByDesc('id');
|
||||
if ($status !== null && $status !== '') {
|
||||
$q->where('status', $status);
|
||||
}
|
||||
|
||||
return $q->paginate($perPage);
|
||||
}
|
||||
|
||||
public function createDraft(AdminUser $admin, ?string $reason, ?int $cloneFromVersionId): RiskCapVersion
|
||||
{
|
||||
$nextNo = (int) (RiskCapVersion::query()->max('version_no') ?? 0) + 1;
|
||||
|
||||
return DB::transaction(function () use ($admin, $reason, $cloneFromVersionId, $nextNo): RiskCapVersion {
|
||||
$draft = RiskCapVersion::query()->create([
|
||||
'version_no' => $nextNo,
|
||||
'status' => ConfigVersionStatus::Draft->value,
|
||||
'effective_at' => null,
|
||||
'updated_by' => $admin->id,
|
||||
'reason' => $reason,
|
||||
]);
|
||||
|
||||
$source = null;
|
||||
if ($cloneFromVersionId !== null) {
|
||||
$source = RiskCapVersion::query()->whereKey($cloneFromVersionId)->firstOrFail();
|
||||
} else {
|
||||
$source = RiskCapVersion::query()
|
||||
->where('status', ConfigVersionStatus::Active->value)
|
||||
->first();
|
||||
}
|
||||
|
||||
if ($source !== null) {
|
||||
foreach ($source->items()->orderBy('normalized_number')->get() as $row) {
|
||||
RiskCapItem::query()->create([
|
||||
'version_id' => $draft->id,
|
||||
'draw_id' => $row->draw_id,
|
||||
'normalized_number' => $row->normalized_number,
|
||||
'cap_amount' => $row->cap_amount,
|
||||
'cap_type' => $row->cap_type,
|
||||
]);
|
||||
}
|
||||
} 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',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return $draft->fresh(['items']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $items
|
||||
*/
|
||||
public function replaceItems(RiskCapVersion $draft, array $items, AdminUser $admin): void
|
||||
{
|
||||
DB::transaction(function () use ($draft, $items, $admin): void {
|
||||
RiskCapItem::query()->where('version_id', $draft->id)->delete();
|
||||
|
||||
foreach ($items as $row) {
|
||||
RiskCapItem::query()->create([
|
||||
'version_id' => $draft->id,
|
||||
'draw_id' => isset($row['draw_id']) ? (int) $row['draw_id'] : null,
|
||||
'normalized_number' => (string) $row['normalized_number'],
|
||||
'cap_amount' => (int) $row['cap_amount'],
|
||||
'cap_type' => (string) $row['cap_type'],
|
||||
]);
|
||||
}
|
||||
|
||||
$draft->forceFill(['updated_by' => $admin->id])->save();
|
||||
});
|
||||
}
|
||||
|
||||
public function publish(RiskCapVersion $draft, AdminUser $admin, ?Request $request = null): void
|
||||
{
|
||||
$before = $this->snapshotVersion($draft);
|
||||
|
||||
DB::transaction(function () use ($draft, $admin): void {
|
||||
/** @var RiskCapVersion|null $current */
|
||||
$current = RiskCapVersion::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: 'risk_cap',
|
||||
actionCode: 'publish',
|
||||
targetType: 'risk_cap_version',
|
||||
targetId: (string) $draft->id,
|
||||
beforeJson: $before,
|
||||
afterJson: $after,
|
||||
);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function snapshotVersion(RiskCapVersion $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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user