feat: 添加新的错误码以支持配置版本管理,更新彩票配置以启用手动审核,增强 API 路由以支持玩法和赔率版本化管理
This commit is contained in:
163
app/Services/Config/OddsStreamService.php
Normal file
163
app/Services/Config/OddsStreamService.php
Normal file
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Config;
|
||||
|
||||
use App\Lottery\ConfigVersionStatus;
|
||||
use App\Models\AdminUser;
|
||||
use App\Models\Currency;
|
||||
use App\Models\OddsItem;
|
||||
use App\Models\OddsVersion;
|
||||
use App\Models\PlayType;
|
||||
use App\Services\AuditLogger;
|
||||
use App\Support\OddsStandardScopes;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/** 后台:赔率版本({@see odds_versions} / {@see odds_items}) */
|
||||
final class OddsStreamService
|
||||
{
|
||||
/** @return LengthAwarePaginator<int, OddsVersion> */
|
||||
public function paginate(?string $status, int $perPage): LengthAwarePaginator
|
||||
{
|
||||
$q = OddsVersion::query()->orderByDesc('id');
|
||||
if ($status !== null && $status !== '') {
|
||||
$q->where('status', $status);
|
||||
}
|
||||
|
||||
return $q->paginate($perPage);
|
||||
}
|
||||
|
||||
public function createDraft(AdminUser $admin, ?string $reason, ?int $cloneFromVersionId): OddsVersion
|
||||
{
|
||||
$nextNo = (int) (OddsVersion::query()->max('version_no') ?? 0) + 1;
|
||||
|
||||
return DB::transaction(function () use ($admin, $reason, $cloneFromVersionId, $nextNo): OddsVersion {
|
||||
$draft = OddsVersion::query()->create([
|
||||
'version_no' => $nextNo,
|
||||
'status' => ConfigVersionStatus::Draft->value,
|
||||
'effective_at' => null,
|
||||
'updated_by' => $admin->id,
|
||||
'reason' => $reason,
|
||||
]);
|
||||
|
||||
$source = null;
|
||||
if ($cloneFromVersionId !== null) {
|
||||
$source = OddsVersion::query()->whereKey($cloneFromVersionId)->firstOrFail();
|
||||
} else {
|
||||
$source = OddsVersion::query()
|
||||
->where('status', ConfigVersionStatus::Active->value)
|
||||
->first();
|
||||
}
|
||||
|
||||
if ($source !== null) {
|
||||
foreach ($source->items()->orderBy('currency_code')->orderBy('play_code')->get() as $row) {
|
||||
OddsItem::query()->create([
|
||||
'version_id' => $draft->id,
|
||||
'play_code' => $row->play_code,
|
||||
'prize_scope' => $row->prize_scope,
|
||||
'odds_value' => $row->odds_value,
|
||||
'rebate_rate' => $row->rebate_rate,
|
||||
'commission_rate' => $row->commission_rate,
|
||||
'currency_code' => $row->currency_code,
|
||||
'extra_config_json' => $row->extra_config_json,
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
$currency = Currency::query()->where('is_bettable', true)->where('is_enabled', true)->orderBy('code')->firstOrFail();
|
||||
foreach (PlayType::query()->orderBy('sort_order')->orderBy('play_code')->get() as $pt) {
|
||||
foreach (OddsStandardScopes::PRESET_ODDS_BY_SCOPE as $scope => $oddsValue) {
|
||||
OddsItem::query()->create([
|
||||
'version_id' => $draft->id,
|
||||
'play_code' => $pt->play_code,
|
||||
'prize_scope' => $scope,
|
||||
'odds_value' => $oddsValue,
|
||||
'rebate_rate' => 0,
|
||||
'commission_rate' => 0,
|
||||
'currency_code' => $currency->code,
|
||||
'extra_config_json' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$draft->refresh();
|
||||
OddsStandardScopes::syncMissingForVersion($draft);
|
||||
|
||||
return $draft->fresh(['items']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $items
|
||||
*/
|
||||
public function replaceItems(OddsVersion $draft, array $items, AdminUser $admin): void
|
||||
{
|
||||
DB::transaction(function () use ($draft, $items, $admin): void {
|
||||
OddsItem::query()->where('version_id', $draft->id)->delete();
|
||||
|
||||
foreach ($items as $row) {
|
||||
OddsItem::query()->create([
|
||||
'version_id' => $draft->id,
|
||||
'play_code' => (string) $row['play_code'],
|
||||
'prize_scope' => (string) $row['prize_scope'],
|
||||
'odds_value' => (int) $row['odds_value'],
|
||||
'rebate_rate' => (float) ($row['rebate_rate'] ?? 0),
|
||||
'commission_rate' => (float) ($row['commission_rate'] ?? 0),
|
||||
'currency_code' => strtoupper((string) $row['currency_code']),
|
||||
'extra_config_json' => $row['extra_config_json'] ?? null,
|
||||
]);
|
||||
}
|
||||
|
||||
$draft->forceFill(['updated_by' => $admin->id])->save();
|
||||
});
|
||||
}
|
||||
|
||||
public function publish(OddsVersion $draft, AdminUser $admin, ?Request $request = null): void
|
||||
{
|
||||
$before = $this->snapshotVersion($draft);
|
||||
|
||||
DB::transaction(function () use ($draft, $admin): void {
|
||||
/** @var OddsVersion|null $current */
|
||||
$current = OddsVersion::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: 'odds',
|
||||
actionCode: 'publish',
|
||||
targetType: 'odds_version',
|
||||
targetId: (string) $draft->id,
|
||||
beforeJson: $before,
|
||||
afterJson: $after,
|
||||
);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function snapshotVersion(OddsVersion $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